You are not logged in.

#1 2014-03-31 11:52:30

sm4tik
Member
From: Finland, Jyväskylä
Registered: 2006-11-05
Posts: 248
Website

[SOLVED] New with C, adding --size option and passing it forward

I've no experience in C (nor any other language for that matter), but after using i3lock for a while I wanted to make some changes to it and so far it's been a nice leaning experience with cairo. However, now I'm trying to add an option to resize the unlock indicator, but can't figure out how to pass the -s XX to unlock_indicator.c. Here are the relevant (and simplified) parts I've got so far:
In i3lock.c

..
int size = 100;
..
{"size", required_argument, NULL, 's'},
..
char *optstring = "hvnbdc:p:usi:teI:"; // s added
..
case 's':
    size = atoi(optarg);
    break;

and in unlock_indicator.c

..
extern int size;

The default value for size (100) get's passed ok and the unlock indicator is sized as expected (100px * 100px in this case), but as soon as I try to add, say -s 50 to the command line, I have a seg fault. I've tried half the internet in the place of atoi(optarg); but just can't get it right.
Any help would be appreciated, thanks!

Last edited by sm4tik (2014-03-31 17:49:44)

Offline

#2 2014-03-31 13:02:31

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [SOLVED] New with C, adding --size option and passing it forward

First of all, please post a link to the source code that you are working on. Also, please post your modified version of the file.

sm4tik wrote:

as soon as I try to add, say -s 50 to the command line, I have a seg fault. I've tried half the internet in the place of atoi(optarg); but just can't get it right.

Are you certain that the call to "atoi" is what's causing the segmentation fault? Segmentation faults are great because they tell you exactly where the problem is occuring.

If the problem isn't obvious after I look at the source code, then I'll help you get some debugging information (Hint: Compile with "-g", run it with "gdb", and use the "bt" command... big_smile)

Offline

#3 2014-03-31 14:17:42

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] New with C, adding --size option and passing it forward

This probably is not what is causing your code to segfault, but I had a similar case today in which I had to convert a string to an integer. Searching around the web came up with numerous results claiming either that atoi is deprecated and/or unsafe. Apparantly, you should use strtol instead.

Just a heads up wink


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#4 2014-03-31 15:10:41

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] New with C, adding --size option and passing it forward

Or sscanf like the other cases in i3lock's getopt handling.

Offline

#5 2014-03-31 15:24:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: [SOLVED] New with C, adding --size option and passing it forward

I suspect optarg is NULL.  So at the very least, add:

        if (optarg) size = atoi(optarg);

atoi will segfault if passed a null pointer.  So will sscanf - so while that may be a good idea for other reasons, it would still require the conditional check.

Unia, can you point me to any of the info on atoi being unsafe/deprecated?  This would be news to me.  (Not that I doubt it - I'm not up on such news, so I'd just want to learn about it).

EDIT: of course optarg is null, there is no colon after your added 's'.  I don't use getopt - but isn't the colon required to indicate the option takes an additional argument (optarg)?

Last edited by Trilby (2014-03-31 15:28:01)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2014-03-31 15:36:38

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [SOLVED] New with C, adding --size option and passing it forward

Trilby wrote:

of course optarg is null, there is no colon after your added 's'.  I don't use getopt - but isn't the colon required to indicate the option takes an additional argument (optarg)?

I think you win!

man getopt_long wrote:

If such a character is followed by a colon, the option requires an argument

Offline

#7 2014-03-31 16:05:05

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] New with C, adding --size option and passing it forward

Trilby wrote:

Unia, can you point me to any of the info on atoi being unsafe/deprecated?  This would be news to me.  (Not that I doubt it - I'm not up on such news, so I'd just want to learn about it).

I'm no expert either, I just came across some links which mentioned this:
http://blog.ultranurd.net/2004/07/13/atoi-strtol/
http://stackoverflow.com/questions/2892 … -functions
http://stackoverflow.com/questions/1488 … ing-to-int

Granted, I did not do any further research on the topic as strtol suits me fine.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#8 2014-03-31 17:25:59

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: [SOLVED] New with C, adding --size option and passing it forward

Eh ... seems apples and oranges.  I'm still happy with atoi.  Either function will segfault if passed a null string.  In either case you need to check the string variable.  With atoi the limitation is that you can't interpret a result of 0.  Strtol allows you to interpret a return value of 0, but only with several more lines of code.  Atoi could do this too:

char *str = "something here";
int n;
if ( !(n = atoi(str)) && str[0] != '0' ) { /* no number present at start of str */ }

Actually this seems a much easier way to check a 0 return value than strtol's character usage count.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2014-03-31 17:48:23

sm4tik
Member
From: Finland, Jyväskylä
Registered: 2006-11-05
Posts: 248
Website

Re: [SOLVED] New with C, adding --size option and passing it forward

Thanks guys for all your answers. I'll post the whole source if I still need more help (wouldn't be surprised) and keep on researching different ways to get the job done. As I said, I'd probably tried most ways without success, but..

Trilby wrote:

EDIT: of course optarg is null, there is no colon after your added 's'.  I don't use getopt - but isn't the colon required to indicate the option takes an additional argument (optarg)?

this was the true reason! I was sure it was something trivial and actually wondered earlier what the colons were for, but didn't pay much attention to them! (Should've been more logical and checked the colons against the rest of the opts that require an argument). I'll add some sanity checks with the argument and hopefully finally get a failsafe result now that I have it working atlast.

Thanks again for the fast responses, learned something new from each post. Case closed for now.

EDIT: just to keep in line with the rest of the code, I'll use sscanf for now. My language isn't as "fluent" anyway, so let's just say it feels safe to follow the given example just a few lines back wink

Last edited by sm4tik (2014-03-31 18:05:22)

Offline

#10 2014-03-31 18:17:32

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: [SOLVED] New with C, adding --size option and passing it forward

With sscanf (or any of the scanf family) don't forget the & operators on the target variables.  I'm pretty comfortable with C, but I screw this up on a regular basis and only realize my error after a good bit of frustration and confusion.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#11 2014-03-31 18:31:05

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] New with C, adding --size option and passing it forward

Trilby wrote:

only realize my error after a good bit of frustration and confusion.

Sadly, I can relate to that too good tongue


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

Board footer

Powered by FluxBB