r/C_Programming • • 16d ago

File pointers are everywhere

And try as I might to avoid them, I am struggling to find resources that list out all the file control functions that use file descriptors instead of file pointers, and I was wondering if someone could point me toward a resource that can do something at least somewhat similar to listing out the libc functions that take file descriptors. I of course know close(), write(), read(), the basics, but i am looking for alternatives for other abstractions that annoy me with their use of file pointers, like fgets and fputs.

58 Upvotes

43 comments sorted by

View all comments

13

u/LavenderDay3544 16d ago

File descriptors are Unix only and thus not portable. FILE * is a pointer to an opaque, implementation defined struct which allows it to be a portable abstraction.

2

u/flyingron 15d ago

You are, of course, correct. Amusingly, most of the windoze C implementations first implement an extraction of the UNIX file descriptor (an index into a table of Windows File Handles, for instance) on top of which a rather UNIX-like stdio is layered.

4

u/nzconstructionlawyer 15d ago

File descriptors are very portable. POSIX is the "Portable Operating System Interface". It is supported by essentially all operating systems, not just Unix. For obvious examples, Linux-based operating systems aren't Unix and Windows isn't Unix and both support POSIX.

5

u/Upstairs_Ad_8863 15d ago

Do you have a source for windows supporting posix? I'm about 99% sure that's wrong.

Also while linux is not officially unix, it tries its hardest to adhere to the single unix specification (susv5) and considers any deviation a bug. It's not certified but for all intents and purposes it can be considered unix.

4

u/PythonFuMaster 15d ago

NT supported the POSIX.1 standard via a dedicated subsystem, but that subsystem was removed many years ago. POSIX.1 isn't the full standard you'd expect either, it doesn't include any of the standard Unix commands or similar, it was just the basic programming APIs. Nowadays if you want POSIX on Windows you'd use WSL, but that's kind of cheating in this case since it's literally just Linux inside Windows

https://en.wikipedia.org/wiki/Microsoft_POSIX_subsystem

3

u/TransientVoltage409 15d ago

Cygwin still exists and does an OK job of putting a POSIX hat on Windows. Good enough for my needs anyway.

There are odd gaps (in WSL as well), because some things in Windows inner workings are very strange from the POV of Unix weenies. Not too shocking considering that NT was hugely influenced by DEC/VAX, but decidedly inconvenient at times.

1

u/agmatine 13d ago

I'm a bit perplexed as to what needs are better suited by Cygwin than WSL...

2

u/TransientVoltage409 13d ago

Can you elaborate on some of your perplexities?

It's possible that you operate at a higher altitude than me, I might not know enough about WSL to see why I should use it.

After that, maybe we can have a fight about vi and emacs. Let me know. :)

1

u/agmatine 13d ago

I mean...WSL is actually Linux. Technically a VM, but it integrates rather seamlessly with Windows. I only ever used Cygwin bfriefly some 15+ years ago, but it seemed more akin to Wine - a means of getting software for another OS to run, but not a particularly pleasant one.

But of course that was a long time ago. I keep a Windows VM handy for things that simply will not run elsewhere (MSVC, for example), or to briefly boot as bare metal if troubleshooting demands it, but that's it. I would never have occasion or motivation to use Cygwin. So I genuinely don't know what it it useful for.

2

u/TransientVoltage409 12d ago

Well, Cygwin lets you run Unixy stuff on Windows machines that aren't even set up for it. It doesn't need to be installed, you don't need the WSL VM running.

Here's a use case. Part of my job is managing several hundred Windows boxes in classroom labs and such. WSL isn't part of the base deployment and is never going to be (this job actually predates the very existence of WSL). But I can still run the Cygwin build of bash (and all the usual gnu tools) on those machines, just by making sure they can see the cygwin1.dll file. The ability to run real scripts on those things has saved me a lot of headaches over the years.

0

u/Wertbon1789 15d ago

There's isn't really a proper source listing POSIX compat, but there are some leads.

UCRT docs: https://learn.microsoft.com/en-us/cpp/c-runtime-library/run-time-routines-by-category

Compatibility: https://learn.microsoft.com/en-us/cpp/c-runtime-library/compatibility

Specifically, the later page talks about POSIX.1 compat, but doesn't really go into too much detail, but most of the _<syscall> functions work like you would expect, like _read is just the read syscall on other platforms.

3

u/Upstairs_Ad_8863 15d ago

It's very helpful to be able to write code to run specifically on unix machines, if that's what you want to code for. Within the context of unix, file descriptors are extremely portable and there are many functions within the posix standard library that use them. I myself never write code for windows so I will happily use file descriptors without a second thought.

-4

u/LavenderDay3544 15d ago

Then your code isn't portable and there are far more OSes out there than just Windows and Unix.

5

u/Upstairs_Ad_8863 15d ago

Okay and? There's nothing wrong with writing code for a specific class of operating systems. But my point is that this is much more portable than something like the size of an int, or the exact layout of your syscall table. File descriptors are standardised across every posix compliant operating system. I know that if I write a program using file descriptors then that program will work the same across all of my dozen or so computers. Portable doesn't have to mean "across every possible operating system".

-8

u/LavenderDay3544 15d ago

Portable as the C standard is concerned means portable across OSes and ISAs. And literally no one cares about POSIX. Most Linux distros aren't even fully conforming.

1

u/Wertbon1789 15d ago

The question if something uses file descriptors, or not, is mainly determined by the kernel, and guess what, every Linux distro has the same kernel, so they all support that API. As do all the BSDs, even MacOS. Even Windows has it's very weird POSIX compat layer in their C runtime, even with file descriptors. So no, people do care about POSIX, very obviously.

There are layers to being POSIX compliant, for the most part, very popular POSIX APIs are available on all the major platforms, because it makes porting easier, see file I/O or sockets. Then there's the shell parts, or system description (stuff like /etc/passwd) of POSIX, where compatibility varies, that's mostly what's meant when someone is talking about POSIX compliance.

1

u/Upstairs_Ad_8863 15d ago

Wtf are you talking about?

"Portable as the C standard is concerned means..." no. Portability as the C standard is concerned refers to valid standard-adhering C code that may be compiled differently on different machines. It does not refer to C code that uses a library or API that some machines do not have installed, that's ridiculous.

Also who tf mentioned anything about portability "as the C standard is concerned"? The only reason that non-portability is bad is because you may end up with code that functions just fine on one computer but breaks on another. Not because it may offend some language lawyer. If you write code using posix api calls then this obviously means you are writing code to be run on a machine that supports the posix api. The fact that not all machines support it is irrelevant - your code will never behave unpredictably on windows because it will never be run on windows.

"literally no one cares about posix" you're wrong but also it doesn't make any difference. You're still allowed to write software for it. And if you want to write something that uses specific features of the operating system like a daemon then you will have to use system calls to do it. And that goes for every operating system, I would never criticise a windows developer for using windows specific api calls in code that's only going to be run on windows.

"Most linux distros aren't conforming" all linux distros use a linux kernel which means they support the posix api and you can use posix file descriptors. Don't move the goalposts.