r/cprogramming • u/PowerCreepy4109 • Oct 20 '26
I am learning C programming by making this project a unix shell(ForkOff). Looking for a quick code review!
Hello there,
I am learning C and system programming by building this shell named ForkOff.
Its currently handles command parsing, executing binaries with fork() + execv(), wait state, and basic buildins like cd echo pwd exit type
Repo: https://github.com/Anchit3034/ForkOff
I'd love a feedback on my memory management, pointer handling, and amy bad habits i should fix early on. Tips on adding piping next are also welcome!
Thanks!
19
Upvotes
3
u/Rich-Engineer2670 Oct 20 '26 edited Oct 20 '26
I'll have to give it a deeper look but at first glance a few things....
foo > remotehost:bar or foo | remotehost:bar or remotehost:command
If you take this concept to its end, and do a bit more support work, you can do things like:
command1 | (host1:command2, host2:command2, host3:command2) | command3
which means:
That lets you fire off scripts which fire off child workers on multiple machines, who do your bidding and feed the results back into your chain. In other OSes, this would have been solved by "inter-machine pipes" where they just existed in the namespace and if you wrote to /machine/object, your bytes just ended up there, but then we need either a user-space daemon or kernel code.
2
u/sciencekm Oct 20 '26
The following is strange. No matter what the result of the call to signal() is, you immediately go to the "while" loop. There is no need for the "if" logic.
1
u/ConceptMinimum Oct 24 '26
I realy like your project. I like the fact, that it's unfinished, and in progres, but it's a lot of work already done. It could be usefull to define how advanced result you want to achieve. Is it small shell with 15 hardcoded commands? extendable with libs or modules? or thing like SH/BASH with pipes, substitutions, io redirections, autocompletion etc.?
You have to remember one thing. 90% of working with code - is reading it and troubleshooting it.
- i'd change goto labels to smth more readable.
I mean 'goto l1' inside some function in some if tells nothing, to person reads only this part of code, and don't remember whole code. If there will be 'goto __exit__main__' or 'goto __main_loop_begin__' helps a lot, also could replace comments like '/* main loop begin */'
- Also things, that will be problematic to debug are: at the beggining you have '#define SIZE' and 'char buffer[]512]' than in some function and 'char cwd[128]' in some moment you'll met situation, where those buffers wouldn't be enough, or use 128 index on 512sized table. And you'll finish with debugger trying to reach that segfault or loosing characters and then increase digits inside every function that could be affected (imagine you're handling 35k lines of code). Beter practice is either better naming like MAX_COMMAND_LENGTH, MAX_ECHO_LENGTH etc..... Or stdint.h (described below)
- Avoid hardcoding things like ' if(count >= 65536)' (especially size is defined earlier, and it's name just SIZE, of what?) use things like UINT16_MAX from stdint.h, (there are propper values for every int type) That will help you with choosing right type for indexes, eg. 'char buf[UINT8_MAX]; uint8_t index=0; while(index < UINT8_MAX)....' with such practises you'll be more safe from getting out of tables and indexes.
- Also I think, that now is the right moment to add .h file with function headers and even move all the functions except 'main' into another .c file
1
u/Tiny_Rent_5936 Oct 20 '26
I like the code a lot. I'm not an expert in C, but I could read well trough the code. Adhering to what the previous user said, instead of build everything inside a single file, just divide the shell into different files, one for each tool you want to implement. Apart from that, good code, you're going the right way