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.

95 Upvotes

238 comments sorted by

View all comments

89

u/bluetomcat Sep 12 '26

Make fread and fwrite accept the bloody FILE pointer as their first argument, and unite size and count into one argument.

42

u/UnalignedAxis111 Sep 12 '26

Also add a flag to force it to create non existent parent directories, because that's massively annoying and there's no portable way to do it.

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/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.

10

u/ComradeGibbon Sep 12 '26

C doesn't have a slice type and that's utterly lame.

5

u/runningOverA Sep 12 '26 edited Sep 12 '26

you need length terminated strings for slice types to work, which C doesn't have. userspace task right now.

3

u/mort96 Sep 12 '26

Uh.

  1. where did anyone say that this is a kernel space issue
  2. kernel APIs are a part of the problem: if you use slices as strings (either on a language level or as a convention in C) you have to either keep around info about whether a slice is 0 terminated, or do a copy into a null terminated buffer every time you want to pass a string to kernel space

7

u/Disastrous-Team-6431 Sep 12 '26

"right now" for 30 years.

→ More replies (12)

1

u/flatfinger Sep 14 '26

Depending upon what one is doing, it may be more efficient to keep track of a slice using a starting address and length, or using starting and ending addresses. If C had a slice type, whoever implemented it would need to guess which approach would be more efficient, and then programmers who knew which approach would be more efficient for the task at hand would have to guess whether the implementation was going to use that approach, or whether they should manually implement slices using it.

→ More replies (1)

4

u/musbur Sep 13 '26

I'm also annoyed by size and count and which comes first, but I think it's meant for reading fixed-size records and making sure that you don't have to deal with partial records.

3

u/BlockOfDiamond Sep 13 '26

Why was size and count separate anyway?

3

u/bluetomcat Sep 13 '26

Because they designed the API to be record-oriented, not byte-oriented. You are mostly expected to be reading an array of structs, and the return value indicates the number of structs completely read.

For reading arbitrary binary data, this is a bit unintuitive.

2

u/BlockOfDiamond Sep 13 '26

For struct arrays, the caller could just multiply the size by the count manually. The only advantage I could think of is avoiding overflows on systems with a narrow size_t.

1

u/LB-- Sep 14 '26

Not just that. Where multiplication might overflow, the read or write could still succeed, so the implementation could "do the right thing" without making you manually write careful code for it. Pretty obscure scenario nowadays though.

2

u/BlockOfDiamond Sep 14 '26

Yeah, that is what I meant.

→ More replies (2)

1

u/flatfinger Sep 14 '26

Some file systems support a "read up to N records of up to B bytes" function, whose behavior may be different from a request to read 1 record of up to N*B bytes, or N*B one-byte records. While the Standard would not require that implementations allow C programs to read any files that C programs didn't create, such an ability is often useful nonetheless, even if it exposes some environment-specific quirks.

4

u/TheChief275 Sep 12 '26

fputc family as well

2

u/flatfinger Sep 14 '26

Some execution environments may process a 'read up to N records of size S' function in a manner that will always read some number of complete records without reading a partial record at the end. What I view as unfortunate is the failure of the Standard to provide any indication as to whether fread() calls will be processed in that fashion.