#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    11

    How to use argument within main(argc, argv)


    I got this code from a book. It said "The implementation of waitfile uses fstat to extract the time when the file was last changed".

    1) The program has two argument in main as in
    main(argc, argv).
    Do I included these value at the command line when I
    run the program as in ./waitfile prog1 a

    2) How do I get this program to run ?

    3) How does main(argc, argv) work ?

    4) root:~# ./waitfile
    ./waitfile: 3: Unknown error 3221225157


    =======================================
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>

    char *progname;

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

    {
    int fd;
    struct stat stbuf;
    time_t old_time = 0;

    progname = argv[0];
    if (argc < 2)
    error ("Usage: %s filename [cmd]", progname);
    if ((fd = open(argv[1], 0)) == -1)
    error ("can 't open %s", argv[1]);
    fstat(fd, &stbuf);
    while (stbuf.st_mtime != old_time)
    {
    old_time = stbuf.st_mtime;
    sleep(10);
    fstat(fd, &stbuf);
    }

    if (argc == 2)
    {
    execlp("cat", "cat", argv[1], (char *) 0);
    error ("can 't execute cat %s", argv[1]);
    }
    else
    {
    execvp(argv[2], &argv[2]);
    error ("can 't execute %s", argv[2]);
    }
    exit(0);
    }
    =========================================
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    basically, argc is an integer that contains the number of arguments passed to the program and argv is an array of the arguments that was passed.

    argv[0] is always the program name itself.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    11

    How to run the program


    Please show me how to run this program on a command line.
    Is it

    ./waitfile program-name second-argument

    1) What should second-argument contains ?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    First, your function header is in the oldy-moldy K&R style which does not conform to ANSI C. gcc still compiles it, but you should use the standard instead of an anachronism:

    Change:
    main(argc, argv)
    int argc;
    char *argv[];
    {

    to:
    int main(int argc, char *argv[])
    {


    1) The program has two argument in main as in
    main(argc, argv).
    Do I included these value at the command line when I
    run the program as in ./waitfile prog1 a
    Those two parameters are the command line.
    argv is an array of strings which contain the command and every one of the parameters given on the command line. argc is the number of strings in argv. You use them to read in the parameters.


    2) How do I get this program to run ?
    You have to give it a filename or else it will crash. See 4).


    3) How does main(argc, argv) work ?
    The book must describe it somewhere, because this has been a C standard for just about as long as there's been C.

    As I said, argv is an array of strings which contain the command and every one of the parameters given on the command line and arcg is the number of strings in argv. parameters are delimited by white space. So using your example of ./waitfile prog1 a :
    argc is the number of parameters plus the command; here it is 3.
    argv[0] is the command that was entered. It would be waitfile (I'm not sure if the directory path (./) is also part of the string).
    argv[1] is the first parameter, which here is prog1 .
    argv[2] is the second parameter, which here is a .

    In the code, we see that the first parameter is used as a filename in the fopen() call. We also see that the first parameter is required because if argc is less than 2 then all that was entered in was the command and no parameters. Later, we see that if argc is equal to 2, then only one parameter was entered, so we do not use the command, else a second parameter was entered so we do use a command.

    The thing to remember is that the value of argc will always be one more than the number of parameters entered and that parameters 1 through n will be in argv[1] through argv[n].

    BTW, execlp and execvp will replace the current program with the program being called, so they never return from the call.


    4) root:~# ./waitfile
    ./waitfile: 3: Unknown error 3221225157
    You didn't give the program any filename, so it exited with an error message. BTW, I have no idea what the code is for the error() function and I could not find it in the man pages. What's up with that function?
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222

    Re: How to run the program


    Originally posted by linh
    Please show me how to run this program on a command line.
    Is it

    ./waitfile program-name second-argument

    1) What should second-argument contains ?
    Whatever bash command you want it to be, by the looks of it. It defaults to cat, but you could do rm, mv, cp, less, or whatever you want to. Though I do not understand how that is supposed to relate to the filename in the first parameter.

    If the second argument contains white space, then you will need to put quotes around it to keep the shell from interpreting it; eg:
    ./waitfile foo.bar "mv *.* .."

    I'm a bit hazy on execvp(). The first parameter is the name of the file associated with the command and the second is the argv array that you are passing to that command. You should do a web search on execvp() to a better explanation and examples.

IMN logo majestic logo threadwatch logo seochat tools logo