0

I want to write a simple C program using terminal in Linux. I don't know how to check if no option was provided during program executing:

./program.a

Here's my script:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
 int opt;

           while ((opt = getopt (argc, argv, "il:")) != -1)
               switch (opt) 
                {
                case 'i':
                   printf("This is option i");           
                break;
                case 'l':
                   printf("This is option l");
                break;
                default:
                   fprintf(stderr,"Usage: %s [-i] opt [-l] opt\n",argv[0]); 
                }

if (argc == -1) {
printf("Without option");
}
}

So the output with:

./program.a

Should be:

"Without option"

I tried to do it with "if" and set argc to -1, 0 or NULL, but it doesn't work. I know that in bash I can use sth like that: if [ $# -eq 0] or if [-z "${p}" ], to check if no option was provided, but in C I have no idea how to check that...

I have a second question too: is it possible to somehow combine bash functions with C code in one script/program?

Thanks for any hints. B

BloodyMary
  • 173
  • 13
  • 1
    `if (argc == 1)`. Maybe forget about `getopt` for now and just play around with various ways of running the program and the corresponding values in `argc` / `argv`. – melpomene Nov 17 '18 at 22:12
  • @melpomene Thanks for your reply! So it was so close... However, I don't understand why it's "1". Getopts works with "!= -1", so I understood that if it's not "-1" then there is an option, so when it's "-1" no option is provided. – BloodyMary Nov 17 '18 at 22:30
  • 1
    Your usage message doesn't tie in with the `getopt()` option string `il:`. In classical (non-Linux) command lines, you specify options and option arguments before non-option "operands". The `-i` option doesn't take an argument; the `-l` option does. Nominally, your usage message should be `Usage: %s [-i] [-l opt] arg ...\n"` or something similar. It isn't entirely clear what you expect since you don't capture `optarg` when `-l` is caught. After the `getopt()` loop, you should check `optind` against `argc`. I can't think how `argc == -1` would be true. Don't forget to exit after the usage. – Jonathan Leffler Nov 17 '18 at 22:40
  • @JonathanLeffler Thanks for that additional comment! I know that it doesn't work properly right now. I'm just starting with this script. Firstly I wanted to have a special text when no option is provided, and as you can see, I stuck with it. Now, with help of Stack users I know how to correct my mistake. I will also take into consideration your additional comments. Huge thanks for you! – BloodyMary Nov 17 '18 at 22:49
  • Note that the `getopt()` provided with Linux does interesting things with argument lists unless you set the environment variable `POSIXLY_CORRECT` to a value such as `1`. In particular, it will look for options after operands, up to the end of the argument list (or the `--` argument which indicates the end of the options), and permutes the arguments so that the non-option arguments are all at the end. Personally, I don't like that behaviour, but I must be in a minority. See also the GNU [`getopt()`](https://www.gnu.org/software/libc/manual/html_node/Getopt.html#Getopt) documentation. – Jonathan Leffler Nov 17 '18 at 22:52

1 Answers1

1

I think you need check argc number if you don't pass any argument(option) for starting, your argc will 1, otherwise 1 + count(options).

SG92
  • 76
  • 9
  • Thanks for an answer! @melpomene wrote the same thing in the comment. I wrongly understood getopt then... I thought that if getopts works with "!= -1", so when it's not "-1" then there is an option, and by that I thought that when it's "-1" then no option is provided. – BloodyMary Nov 17 '18 at 22:33
  • @BloodyMary its great. Thank you too. – SG92 Nov 17 '18 at 22:36