0

Can anyone explain me what argc and argv does in this code and why those variables are parameters in the main function? We had in lectures example that showed those both variables so I'm using them without knowing what they do.

main (argc, argv)
    char *argv[];
    {
        int fd;
        extern int errno;
        if(argc < 2){
            fprintf(stderr, "No file\n");
            exit(1);
        }

        if((fd = creat(argv[1], 0777)) < 0){
            fprintf(stderr, "Cannot creat file %s\n", argv[1]);
            exit(1);
        }       


    switch (fork()) {
        case -1:
            fprintf(stderr, "Fork error\n");
            exit(1);
        case 0:
            close(1);
            dup(fd);
            close(fd);
            execl("/bin/pwd", "pwd", NULL);
            perror("Exec");
            break;
        default:
            close(fd);

    }


    exit(0);
    }
  • 6
    Oh god, old K&R-style function declarations, implicit `int`... Whoever is giving this as lecture material in 2016 should be permanently banned from teaching. – EOF Mar 31 '16 at 19:47
  • They are optional command line parameters passed on with the program. argc gives the amount of parameters, argv holds pointers to the actual strings. – Unimportant Mar 31 '16 at 19:47
  • That, my friend,looks like some pretty damn old C code, say early 80's. I sincerely hope this doesn't come from actual courseware... – fvu Mar 31 '16 at 19:48
  • There is plenty of reference to [google](https://www.google.co.uk/?gws_rd=ssl#q=main+c+argc+argv+tutorial) – Weather Vane Mar 31 '16 at 19:49
  • Note that the declaration of `errno` is antiquated too — these days, you should only ever `#include `. That should only be used as a scare story — this is what C used to be like once upon a very long time ago (a quarter of a century ago)! – Jonathan Leffler Mar 31 '16 at 19:59
  • Note that [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/) covers the arguments too. – Jonathan Leffler Mar 31 '16 at 20:05
  • The code insists on being given at least one argument. It will try to zap (truncate) the file named, or will create it as an empty publicly readable, writable and executable file unless you have `umask` set, and then provides it as the standard output to the `/bin/pwd` program, which will write the name of the current working directory to standard output, if it can deduce what it is. Effectively: running `./program file` is equivalent to `/bin/pwd > file` except that the permissions on the created file include execute permissions which the shell doesn't. – Jonathan Leffler Mar 31 '16 at 20:12

1 Answers1

0

argc is an int giving the program the number of arguments passed to the program by the operating system. argv is an array of null-terminated strings containing the actual arguments passed to the program.

The exact parameters are operating system dependent, but argv[0] is often the executable name including the path, while argv[argc] is always a NULL pointer. (Thanks to @WeatherVane for pointing this out) All versions of the C standard require argv[argc] to be a NULL pointer, and all prior precedent for hosted systems required it too ('freestanding' — aka embedded — systems run under different rules, even now).

Not all programs require command-line arguments, but they are an integral part of just about any operating system.

The C standard requires the following definition if both parameters are used:

int main(int argc, char *argv[])
...

I doubt K&R C is used in production code anymore.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
owacoder
  • 4,815
  • 20
  • 47
  • `argv[0]` is usually the executable's file name. `argv[argc]` is usually an end marker `NULL` pointer. – Weather Vane Mar 31 '16 at 19:52
  • 1
    @WeatherVane: It's not "usually a `NULL` pointer", for hosted implementations it *has to be*: C11 draft standard n1570 *5.1.2.2.1 Program startup 2 If they are declared, the parameters to the main function shall obey the following constraints: — The value of argc shall be nonnegative. — argv[argc] shall be a null pointer.* – EOF Mar 31 '16 at 19:57
  • 1
    @EOF I was being cautious because of the use of the `exec` family of functions, which I don't think require any executable name as `argv[0]`. But I wasn't wrong, thank you. – Weather Vane Mar 31 '16 at 20:05
  • @WeatherVane: I'm not trying to be a nuisance, sorry. The `exec()`-family doesn't actually let you specify `argc`, the OS (or dynamic loader) has to count the number of `char *`s passed to `exec()`. The citation I gave continues *[...]— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment.[...]*, but as best I can tell the *program name* is pretty arbitrary and doesn't have to be the name of the binary (see `busybox`). – EOF Mar 31 '16 at 20:13
  • @EOF that would be a good caution if you were parsing `argv` to end on `NULL`, when the executable name wasn't available. – Weather Vane Mar 31 '16 at 20:20