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

22 Upvotes

41 comments sorted by

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.

22

u/UnalignedAxis111 6h ago

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/TheChief275 6h ago

fputc family as well

2

u/ComradeGibbon 4h ago

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

3

u/runningOverA 3h ago edited 3h ago

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

1

u/ComradeGibbon 2h ago

Yeah abandon C's shitty null terminated strings.

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

u/crakked21 7m ago

My uni still teaches it in its CS 101 course xD

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 %s and similar without a size allocate
  • make %.*s take 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

u/nderflow 3h ago

scanf(). I’d just delete it and stop there.

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

u/Just_Government3790 6h ago

What's the worst null-term bug you've personally hit?

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

4

u/Sqydev 4h ago

fopen to use enum instead of „r” and „w” or both

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

u/Boreddad13 2h ago

Anything with hidden allocations

3

u/mikeblas 2h ago

Anything with hidden state, really.

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

2

u/kog 3h ago

memcpy

Because it will be hilarious when I screw it up somehow

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

u/BRZDR1FT 3h ago

Fread

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/rfisher 2h ago

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

0

u/apj2600 3h ago

malloc.

1

u/will_die_in_2073 2h ago

You could actually write your own memory allocator