r/voidlinux • u/epiclolzjamz • Jul 08 '21
Void linux inspired me to do something
Hello void linux community! I wrote a small program for enabling/disabling services in runit because I was lazy and didn't want to keep using the ln command. As such, that means void inspired me to write my very first C program. Please help me improve upon it / roast me on my garbage code here.
17
u/oreo639 Jul 08 '21 edited Jul 09 '21
Making small, useful utilities is a great way to learn a language, including C.
Some comments I have are that:
You should probably stick with either tabs or spaces for indentation for the duration of that file (and preferably the entire codebase), mixing them only creates inconsistency.
In your makefile, usually you would put
$(CC)instead of a specific compiler so people can compile it with any compiler (or cross compile it).Not really an issue but just something to keep in mind. In C
func()is expanded tofunc(...)(which means it accepts any number of inputs). This is different from C++ where it expands tofunc(void)(which means it accepts 0 inputs). When writing in C, it is considered best practice to explicitly writefunc(void)for functions that accept 0 inputs.Another non issue that's good to keep in mind is that you don't need to manually put
\0at the end of a string. A string is defined as an array of characters ending in a\0so just writing"/var/service"means that there is already a NULL terminator at the end.
If I made any mistakes feel free to correct me.
2
5
u/kyoto711 Jul 08 '21
Point 3 blew my mind, didn't know that at all. I should probably review some basics.
4
3
u/Ok-Tip-6972 Jul 09 '21
Cool! I'll share some comments too:
You should print errors to
stderr, so changeprintf("something")tofprintf(stderr, "something"). Some people even print help tostderr.There's no need to specify the length of this array, it will automagically guess it when you specify
dir[].You use hard coded lengths. This is simpler to code but less space efficient. This isn't that much of a problem here, but it could lead to issues in a bigger project.
You shouldn't return 0 on invalid option.
Your code ain't that bad. Keep it up!
1
u/epiclolzjamz Jul 09 '21
I'm unsure as to what point #3 is. My best guess is the
char var[256]I put to initialize the service and sv variables. And if that is, how would I go about fixing that?2
u/Ok-Tip-6972 Jul 09 '21 edited Jul 09 '21
As I said, this is more complicated to fix.
Your
svarray has space for 256 characters (including'\0'). Let's say that the user wants to enable theadbservice. Thesvwould then store"ln -s /etc/sv/adb /var/service". That's 31 characters (including'\0'). There are 224 characters wasted. There is even a possibility that the request will exceed the 256 characters. This will not cause a memory leak because you use snprintf correctly, but it will still fail.I am not going to teach you how dynamic allocation works. It is a complicated topic. There are plenty or resources available online. I will show you a modified version of your program that uses
mallocto dynamically allocate memory needed. The modified version is here: https://github.com/meator/roonit/commit/9860f3b17d9dc857fe49c0fa1574d78fd0399ccc.The modified version does not change the
servicearray. You could try to change this array too if you want. (I have also reverted a commit that breaks your program.)1
u/epiclolzjamz Jul 09 '21
I'll implement these after I read up on
mallocand pointers. I'm aware that they serve an important part of the C language somewhere on a stackoverflow answer. So currently, I'm reading "The C programming language" second edition. Thanks for the example! (Also I'll revert that commit that breaks things)
3
u/Cosmo-de-Bris Jul 09 '21
For the first try on C this is quite good.
I'd like to add some recommendations:
instead of
if ( argc < 2 || argc > 3) { //If this OR that
help();
}go with this
// check argument count and maybe print help
if ( argc != 2 ) {
help(argc);
exit(1);
} else {
/* program code */
exit(0);
}of course you can move the exit code to the help function. Also I'd encourage you either switch the number of arguments in the help function or use different names. (enable is a better name if the function in enabling something. It could be usage_enable() for the enable keyword.)
It is a good effort though and you chose something interesting for your first project. Keep on it.
2
1
u/backtickbot Jul 09 '21
2
4
Jul 08 '21 edited Jul 08 '21
[removed] — view removed comment
7
u/epiclolzjamz Jul 08 '21
I noticed that I forgot an int cmd in the disable part of the code. Fixed it a little bit ago. Thanks!
24
u/eftepede Jul 08 '21
This code is fun. Your comments are even funnier!
Fingers crossed, good luck with your learning, very good idea to start with something actually useful instead of rewriting helloworlds or 'bob, alice and peter lives in different cities' from books.