June 26th, 2003, 04:16 PM
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
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
struct stat stbuf;
time_t old_time = 0;
progname = argv;
if (argc < 2)
error ("Usage: %s filename [cmd]", progname);
if ((fd = open(argv, 0)) == -1)
error ("can 't open %s", argv);
while (stbuf.st_mtime != old_time)
old_time = stbuf.st_mtime;
if (argc == 2)
execlp("cat", "cat", argv, (char *) 0);
error ("can 't execute cat %s", argv);
error ("can 't execute %s", argv);
June 26th, 2003, 04:27 PM
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 is always the program name itself.
June 26th, 2003, 04:48 PM
How to run the program
Please show me how to run this program on a command line.
./waitfile program-name second-argument
1) What should second-argument contains ?
June 26th, 2003, 04:52 PM
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:
int main(int argc, char *argv)
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.
You have to give it a filename or else it will crash. See 4).
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 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 is the first parameter, which here is prog1 .
argv 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 through argv[n].
BTW, execlp and execvp will replace the current program with the program being called, so they never return from the call.
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?
June 26th, 2003, 05:02 PM
Re: How to run the program
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.