r/C_Programming • • Sep 12 '26

Question What C function would you delete and rewrite first, and why?

For me it's fnmatch - mostly because it's 40 year's old and smell's funny.

96 Upvotes

238 comments sorted by

View all comments

Show parent comment

5

u/musbur Sep 12 '26

"and there's no portable way to do it." -- which is exactly why fwrite() can't do it.

18

u/a4qbfb Sep 12 '26

fwrite() is part of the implementation, it's allowed to do non-portable things

1

u/musbur Sep 13 '26

yeah but a file handle is pretty much a file handle anywhere while (recursive) directory creation is conceptually something totally different.

1

u/a4qbfb Sep 13 '26

so?

1

u/musbur Sep 13 '26

C just doesn't go that deep into OS abstraction. It's a choice to keep the language simple. It also doesn't have associate arrays, linked lists, a network stack, multithreading, or an http server.

4

u/a4qbfb Sep 13 '26

C does have multithreading; none of the other features you list are OS abstractions. An implementation is absolutely free to automatically create parent directories on fopen() if it wants to, as long as it documents it.

1

u/musbur Sep 13 '26

Opening and writing to a file descriptor has nothing to do with file system structure. There's no reason to wrap unrelated concepts into a single function. Regarding multithreading, which standard introduced that? Can't find it in K&R.

5

u/a4qbfb Sep 13 '26

K&R is 48 years old and predates the ISO standard by 12 years. Multithreading was added in C11, 15 years ago. The C standard does not know about directories, so an implementation is free to do whatever it wants with them.

PS: the standard does not know about file descriptors either, only I/O streams.

0

u/flatfinger Sep 14 '26

People were writing multi-tasking code in C even before the publication of C89. A multi-tasking OS would need to have a small number of assembly-language functions, but for the most part all that was needed was to have a compiler refrain from ordering any loads or stores across volatile accesses--something commercial compilers like MSVC used to do by default, Keil can be configured to do, and gcc can't do--except at -O0, without the use of gcc-specific syntax (which ARM was designed to support).

1

u/flatfinger Sep 14 '26

Many system's file handles can't hold characters that were pushed back with ungetc.

2

u/flatfinger Sep 14 '26

The usefulness of stdio.h would have been enhanced if, instead of limiting itself to things every system can do, it provided means of querying whether various features are known to be available, known to be unavailable, or would have their availability determined by things outside the control of the implementation. If, for example, a system allows file systems to be configured to have or not have implicit-directory-creation semantics, an implementation of fopen() may auto-create directories when used with a file system that is configured in that fashion, but not when used with one that isn't.

Console I/O in the C standard is absolute rubbish because of the Standard's failure to recognize features that 99% of implementations would support.

2

u/musbur Sep 14 '26

So basically an implementation is free to either offer some feature like directory creation or not? Then why not just use that implementation's system library to create directories?

2

u/flatfinger Sep 14 '26

Code which tests whether a feature is supported would be usable interchangeably on all systems that support it, while working around unsupported features when practical. For example, a "more"-style utility could advance a page with spacebar on systems that support raw console I/O, or on every newline on systems that don't.