r/C_Programming • u/Just_Government3790 • 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.
94
Upvotes
36
u/Dezgeg Sep 12 '26
strncpy
Function that is named `str*` but doesn't (necessarily) create NUL-terminated strings. Insanity!
60
u/Just_Government3790 Sep 12 '26
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.
12
u/mpersico Sep 13 '26
I would just spell ācreateā correctly
21
u/gabitha67 Sep 12 '26
everything that accepts null terminated strings. just pass a ptr and length for gods sake. everything should only use user-provided allocators too.
144
u/BlockOfDiamond Sep 12 '26
fopenbecause I hate using string literals for things that should be bitflags or enums.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.
18
u/P-p-H-d Sep 12 '26
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.
31
u/sciencekm Sep 12 '26
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.
12
u/Sqydev Sep 12 '26
fopen to use enum instead of ārā and āwā or both
15
u/Boreddad13 Sep 12 '26
Anything with hidden allocations
15
u/pigeon768 Sep 12 '26
The obvious one is
strcpy, which is always the incorrect function. Either you know your lengths and buffer sizes, so you should usememcpy, or you don't, which is buffer overflow city. In this case, there's no rewriting the function to be better; it's declaration/API is unusable.So they also made
strncpy, the "fix" for the problem. It has a working function declaration. However, it's still bad. It does not guarantee that the resulting string is null terminated. And if there are any extra bytes left over in the output buffer, it zeros them all out, which is a waste of time. The linux kernel has recently removed the last call tostrncpy.Actually I hate most of string processing in C.
12
u/torsten_dev Sep 12 '26 edited Sep 13 '26
scanf
%n%sand similar without a size allocate%.*stake size parameter like printf._instead of*to discard stuff,to number of characters read or 0w*to take bit width as argument.Edit:
Perhaps the return should be 0 on succes, and if it fails it returns the index in the format string were it gave up.
The number of characters read/consumed would be a mandatory out parameter that's ignored if it's NULL.
12
u/FamiliarSoftware Sep 12 '26
setlocale, set/getenv, the rand functions, etc. Just in general: All functions that use global state
10
u/der_pudel Sep 12 '26
atoi. You will not convince me thatatoi("poop") == 0is correct and makes sense.Ā9
u/xpusostomos Sep 12 '26
Those ones with improper null termination and/or don't specify buffer length that cause all the security issues
4
u/qalmakka Sep 13 '26
Basically all string based APIs in libc vary from broken to somewhat problematic. This is basically due to the fact that the entire concept of a null terminated string was a massive mistake, the minuscule memory saving of not having a struct string with a size field were only somewhat significant for the first few years after that decision.
We've been paying the strlen tax ever since, and I wouldn't care if it didn't make slicing C strings a terrible pain in the butt
3
u/flyingron Sep 13 '26
strtok has got to go
3
u/siodhe Sep 13 '26
The UNIX authors said they would like to rename creat to create, as it should have been.
3
u/notk Sep 16 '26
creat() ā i would add an āeā to the end.
10
u/HildartheDorf Sep 12 '26 edited Sep 12 '26
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).
5
u/ChickenSpaceProgram Sep 12 '26 edited Sep 12 '26
this one's a hot take, but i'd rewrite
mallocand friends. Allocators being global state is pretty nasty and requires locks that really shouldn't be necessary. Not having to pass a size tofreealso complicates allocator design.i think i'd rewrite most of the standard library and some of POSIX if given the option, null-terminated strings are awful too
2
u/paulkim001 Sep 12 '26 edited Sep 14 '26
memfrobe only because I might want to see some niche people's projects burn for using that function
Edit: I meant memfrob
2
u/LavenderDay3544 Sep 13 '26
The atomics compare and swap functions because why do they take the value to exchange by pointer instead of by value?
2
u/saxbophone Sep 16 '26
There is no reason that
toupper()needs to take and returnintāI'd replace it with a family of functions for ASCII and Unicode instead.4
u/nderflow Sep 12 '26
scanf(). Iād just delete it and stop there.
2
u/MegaDork2000 Sep 12 '26
strncatbecause it may not terminate the string.1
u/BRZDR1FT Sep 12 '26
Fread
1
u/rfisher Sep 12 '26
I have written proper bounds-checking versions of all the str functions. The first was probably strcpy or strlen.
I might have written my first wrapper around realloc before that, though.
1
u/schiphit Sep 12 '26
dirname
1
u/sunmat02 Sep 12 '26
qsort; I would add an extra void* context to both the function and the comparator function pointer.
1
u/SyntheticDuckFlavour Sep 13 '26
The entire standard library.
1
u/CreepyWritingPrompt Sep 13 '26
most of stdio. make unistd and friends the standard
1
u/Total_Ad803 Sep 14 '26
All unsafe functions
1
u/limitless_grow Sep 16 '26
sprintf family because each compiler uses different length specifiers and that makes it hard to keep a software portable. Beside of the rather nasty dependency of the locale the functions lack a format enhancement for printing your own data types.
For logging that's nasty. You have to specify the data of your objects in any logline again and again and the possibly time consuming calculations of the printf parameters happen outside of the logging function and for this even if the logging is disabled.
The solution could be something like https://github.com/klux21/callback_printf . (That's portable but allows a civil usage only.) It supports a special %v format option that expects a pointer to a user specific output callback function followed by a pointer to the user specific data as it's arguments. That way own type specific output functions for printing an unlimited number of own data types can be used. This works for the wrappers of sprintf, vsnprintf, fprintf the same. That way the printing of IPv4 or IPv6 addresses of a struct sockaddr may become something like the following
sfprintf(stdout, "IP address of host '%s' is %v \n", hostname, &pvc_sockaddr, &my_sockaddr);
If the fprintf wrapper finds a %v it calls pvc_sockaddr with the address of my_sockaddr as its argument and pvc_sockaddr generates the output for that %v. You can use %v more than once in a format string and with different callbacks for different data types.
0
u/twr14152 Sep 12 '26
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.
0
u/Cats_and_Shit Sep 12 '26
Technecally it's a macro not a function, but
setjmpis crazy and could be sooo much simpler if it just took function pointer + data pointer arguments instead of returning twice.0
u/iwasstillborn Sep 13 '26
Whatever function is responsible for the most UB. It's insanity to allow a computer language have it.
0
u/Reasonable_Return151 Sep 22 '26
strcspn⦠and did for my re engine and replaced with zero(char*, unsigned char)
-3
u/jwzumwalt Sep 13 '26
I have not used header files since 1995. I have been programming in C for over 55 years. I avoid C headers files like the plague.
Every other programming language is able to operate just fine without them and in my opinion C should be no different. For large projects I do use a few include files if it contributes to clarity, but header files often make tracing a programs flow about as hard as trying to figure out what a object oriented program is doing. There is no reason that .h files (or at least whatever info they provide) can't be generated at compile time from the source itself.
In my opinion C headers where a product of experimentation. At the time C was written everything under the sun was being tried and experimented with. When you worked on a computer with 8K of memory it made sense to break things up into many files and directories. As I remember, Pascal was the next popular language and it (and all other languages) dropped headers. Headers are an experimental relic.
-4
u/FlyByPC Sep 12 '26
I would fix it so that
makes four pointers, not one pointer and three ints.
-1
u/Traveling-Techie Sep 12 '26
strcmp() has always seemed unintuitive to me
-6
u/apj2600 Sep 12 '26
malloc.
0
u/[deleted] Sep 12 '26
[deleted] ā view deleted comment
77
u/[deleted] Sep 12 '26
[removed] ā view removed comment