r/voidlinux • • 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.

75 Upvotes

19 comments sorted by

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.

7

u/epiclolzjamz Jul 08 '21

Thanks for the support!

4

u/[deleted] Jul 09 '21

I've seen code from professional programmers with supposedly years of experience that was about on the same level. I wish I was exaggerating but I'm not. I'd by lying if I would say it was "good", but it's really not all that bad for a first attempt.

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:

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

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

  3. Not really an issue but just something to keep in mind. In C func() is expanded to func(...) (which means it accepts any number of inputs). This is different from C++ where it expands to func(void) (which means it accepts 0 inputs). When writing in C, it is considered best practice to explicitly write func(void) for functions that accept 0 inputs.

  4. Another non issue that's good to keep in mind is that you don't need to manually put \0 at the end of a string. A string is defined as an array of characters ending in a \0 so 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

u/epiclolzjamz Jul 09 '21

Went ahead and tried to implement these fixes!

5

u/kyoto711 Jul 08 '21

Point 3 blew my mind, didn't know that at all. I should probably review some basics.

4

u/[deleted] Jul 09 '21

Is there a formatter for C like black for python or rustfmt?

3

u/Ok-Tip-6972 Jul 09 '21

Cool! I'll share some comments too:

  1. You should print errors to stderr, so change printf("something") to fprintf(stderr, "something"). Some people even print help to stderr.

  2. There's no need to specify the length of this array, it will automagically guess it when you specify dir[].

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

  4. 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 sv array has space for 256 characters (including '\0'). Let's say that the user wants to enable the adb service. The sv would 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 malloc to dynamically allocate memory needed. The modified version is here: https://github.com/meator/roonit/commit/9860f3b17d9dc857fe49c0fa1574d78fd0399ccc.

The modified version does not change the service array. 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 malloc and 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

u/epiclolzjamz Jul 09 '21

Tried implementing this fix too!

1

u/backtickbot Jul 09 '21

Fixed formatting.

Hello, Cosmo-de-Bris: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

4

u/[deleted] 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!