How does passing argument to main work
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);
}
=========================================
|