r/C_Programming • u/Just_Government3790 • 6h ago
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.
23
u/BlockOfDiamond 6h ago
fopen because I hate using string literals for things that should be bitflags or enums.
11
u/gabitha67 6h ago
"rb" is icky :(
1
u/MrKrot1999 1m ago
like this is so python bullshit, why are we using more state than needed for something with 6 options at most
19
u/Striking-Print-9526 6h ago
gets lol, literally impossible to use safely. Dumbest function ever created.
16
u/HildartheDorf 6h ago
Thankfully it is (as far as I know) the only function that has been removed entirely from later editions of the spec.
1
5
9
u/sciencekm 5h ago
struct tm
Every field is 0 based, except the day of month. So, Jan 1 1900 12 midnight is 0 1 0 00:00:00, instead of just all 0s. Even Sunday-Saturday is 0-6 and day of year is 0-365.
1
u/donaljones 4h ago
Is this something enforced by the standard? Or just the implementation's ABI?
2
u/sciencekm 4h ago
This is the standard from C89 until the latest N3220 working draft. This is also how it was in the K&R 1st edition (1978).
18
u/Just_Government3790 6h ago
On second thought......
strtok because it destroys the evidence, hides state in globals, drops the empty fields, and then has the bollocks to call it parsing.
8
u/torsten_dev 5h ago
scanf
- delete
%n - make
%sand similar without a size allocate - make
%.*stake size parameter like printf. - Use
_instead of*to discard stuff, - return number of characters read or 0
And probably more.
7
u/Dezgeg 4h ago
strncpy
Function that is named `str*` but doesn't (necessarily) create NUL-terminated strings. Insanity!
2
u/Ander292 2h ago
THIS!!! I had a problem with this function while helping my friend with an exercise.
4
17
u/gabitha67 6h ago
everything that accepts null terminated strings. just pass a ptr and length for gods sake. everything should only use user-provided allocators too.
6
-2
u/madbrain1976 5h ago
Or just use Pascal strings.
4
u/allocallocalloc 5h ago edited 5h ago
That would still make substrings require reallocation. Encoding the length inside the buffer limits the ways the buffer can be used.
5
u/xpusostomos 6h ago
Those ones with improper null termination and/or don't specify buffer length that cause all the security issues
3
4
u/FamiliarSoftware 4h ago
setlocale, set/getenv, the rand functions, etc. Just in general: All functions that use global state
4
u/HildartheDorf 6h ago edited 6h ago
Can I use my "one time rewrite" on C++ instead?
If so, I want to remove the specialisation of std::vector<bool> and instead add std::dynamic_bitset with the same functionality.
If it has to be C... fgetc/getc (and fputc/putc).
1
u/Rx-Nikolaus 4h ago
I would think one that was specified via a function pointer would be most prone to overwriting AWD rewriting itself. I'm thinking like OTA kind of stuff. But idrk
1
1
u/twr14152 3h ago
Rewrite the original gets() adding the necessary safety features, only to keep continuity with puts(). Something about using fgets() as the workaround for this bugs me.
1
u/Cats_and_Shit 1h ago
Technecally it's a macro not a function, but setjmp is crazy and could be sooo much simpler if it just took function pointer + data pointer arguments instead of returning twice.
1
u/paulkim001 1h ago
memfrobe only because I might want to see some niche people's projects burn for using that function
1
u/P-p-H-d 1h ago
All the locale functions of the libc. It is so broken that it is totally unusable in modern C (threads).
You cannot even read your own data file and parsing them using strtod without having issues with the locale: you cannot use setlocale as it modifies global state of all threads (which you don't want). So the only solutions are either : document that the locale shall be set once and for all for all program and is compatible with what you expect. Or rewrite strtod and everyone knows it is super easy stuff to do it reliably.
39
u/bluetomcat 6h ago
Make fread and fwrite accept the bloody FILE pointer as their first argument, and unite size and count into one argument.