LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-26-2003, 04:11 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
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);
}
=========================================
 
Old 06-26-2003, 04:30 PM   #2
Palin
Member
 
Registered: Feb 2003
Location: A Meatlocker, well feels like one
Distribution: Gentoo
Posts: 292

Rep: Reputation: 30
here is how those two arguments act when passed to main.
the first is the number of arguments passed to the command line
the second is an array of strings containing the arguments. It looks like this program expects atleast a filename.
 
Old 06-26-2003, 04:44 PM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
How would you run the program ? Please specify it

Is it as shown below ?
ex: ./waitfile program1 second-argument

1) What is the second-argument ?
2) What should the second-argument contain ?
 
Old 06-26-2003, 05:04 PM   #4
Palin
Member
 
Registered: Feb 2003
Location: A Meatlocker, well feels like one
Distribution: Gentoo
Posts: 292

Rep: Reputation: 30
I don't believe this program requires a second argument.
./waitfile <filename>
 
Old 06-27-2003, 11:12 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
<off-topic>

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

{
[...]
That's an old book, ins't it?

Last edited by Hko; 06-27-2003 at 11:24 AM.
 
Old 06-27-2003, 01:08 PM   #6
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
The name of the book is

The name of the book is
The UNIX Programming Environment
by Brian W. Kernighan and Rob Pike
published in 1984

the program is on page 221
========================

If that is the old code, then what is its new counterpart ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
segfault when passing array[] as argument froboozle Programming 11 06-28-2005 03:06 PM
passing function pointer as argument worldmagic Programming 7 08-04-2004 03:33 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
-ne argument expected WHY doesnt this work VisionZ Linux - Newbie 8 03-27-2004 12:39 AM
PHP Script argument passing error... lokee Linux - Software 5 04-24-2003 09:42 AM


All times are GMT -5. The time now is 01:31 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration