LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-10-2003, 07:53 PM   #1
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Rep: Reputation: 30
argv[] help wanted. (In C)


I'm trying to get some agrv working but it just wont work. (Because don't know how to use argv i guess.)

/*
Local user lazy man's setup
*/

#include <stdio.h>
#include <string.h>


main(int argc, char **argv) {

if(argc < 2) { help(); }
if(strcmp(*argv, " -b") == 0) {printf("You a stinker\n\n\n");}
return 0;
}

help() {

printf("Options are:\n");
printf("1:\t Add syncookies support (Assuming it's compiled)\n");
printf("2:\t Close certain ports (-h ports for list)\n");
printf("3:\t ENd.\n");
return 0;

}


I'm sure it would be done easily with anothere language but how will that improve my C skills?
Program isn't complete, but I would like to get argv working before I tinker with the rest.
 
Old 11-10-2003, 08:22 PM   #2
hazza
Member
 
Registered: Nov 2003
Location: Australia
Distribution: Mandrake, SUSE, Fedora
Posts: 122

Rep: Reputation: 15
That would be because "*argv" points to the command line used the run the program your trying to make. It should be "argv[1]".
 
Old 11-10-2003, 08:24 PM   #3
LogicG8
Member
 
Registered: Jun 2003
Location: Long Island, NY
Distribution: Gentoo Unstable (what a misnomer)
Posts: 380

Rep: Reputation: 30
try using argv[1]

you are using argv[0] which is the command you ran.

Code:
/*
Local user lazy man's setup
*/

#include <stdio.h>
#include <string.h>


int main(int argc, char **argv) {
	
	if(argc < 2) {
		help();
	}
	
	if(strcmp(argv[1], "-b") == 0) {
		printf("You a stinker\n\n\n");
	}
	
	return 0;
}

int help() {
	printf("Options are:\n");
	printf("1:\t Add syncookies support (Assuming it's compiled)\n");
	printf("2:\t Close certain ports (-h ports for list)\n");
	printf("3:\t End.\n");
	return 0;
}
And for me, next time will you post code
with the formatting tags like this:

[ code ]
/* code goes here */
[ /code ]

with out the spaces
 
Old 11-10-2003, 08:26 PM   #4
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
char **argv is an array of strings (which are arays of chars).
when ur doing strcmp(*argv,...), what part of the arguments passed at cmd-line are u trying to compare?

for instance:
./pgm_name one two three four

would have
argc = 5
argv[0] = pgm_name
argv[1] = one
argv[2] = two
argv[3] = three
argv[4] = four


so if you want to compare the third argument you pass with "-b", you would have to do a

strcmp (argv[3], "-b")
 
Old 11-10-2003, 09:34 PM   #5
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
1: Will do with the [code]..

2: I get this error with the *argv[1]
File.c:17: warning: passing arg 1 of `strcmp' makes pointer from integer without a cast

3: Doing strcmp(argv[1], "-b") compiles no error, but segfaults once ran.

vexer@ominous:~$ gcc SlackEdit.c -o SlackEdit
vexer@ominous:~$ ./SlackEdit
Options are:
1: Add syncookies support (Assuming it's compiled)
2: Close certain ports (-h ports for list)
3: ENd.
Segmentation fault
 
Old 11-10-2003, 09:38 PM   #6
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
Update.

doing ./file without options segfaults but added with the -b option, it works no segfault/error
 
Old 11-10-2003, 09:47 PM   #7
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
Lol, after endless amount of tinkering, the segfault has been fixed.

gdb was pointing it's error to strcmp. But I thought, why would strcmp be called if I put no arguments in there? Wouldn't it just run help() and exit? The return 0 would send it back to main() then it would die at strcmp for some reason, so I removed the return 0; in help() and added it after the help() called in main().
 
Old 11-11-2003, 01:20 AM   #8
hazza
Member
 
Registered: Nov 2003
Location: Australia
Distribution: Mandrake, SUSE, Fedora
Posts: 122

Rep: Reputation: 15
Put "return 0;" after "help();" in the same if block. Otherwise the section of code that assumes there is an argument will still run.

Last edited by hazza; 11-11-2003 at 01:47 AM.
 
Old 11-11-2003, 05:53 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by vexer
doing ./file without options segfaults but added with the -b option, it works no segfault/error
argv[1] points to the first argument, so if you run it without an argument, argv[1] points nowhere. Hence segfault. You should check if there actually is an argument before trying to read it, eg:
Code:
if (argc > 1) {
	if(strcmp(argv[1], "-b") == 0) {
		printf("You a stinker\n\n\n");
	}
}
 
Old 11-17-2003, 05:09 AM   #10
LOUDSilence
Member
 
Registered: Oct 2003
Location: Sydney
Distribution: Red Hat 8
Posts: 77

Rep: Reputation: 15
write a routine to parse your comandline arguments


for (count = 1 ; count<=argc ; count ++)
{
...
if(strcmp(argv[count],"WHATEVER")==0) {
DOWHATEVER
}

}
 
  


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
argv[1]++ operation and sort sachitha Programming 2 10-14-2005 11:16 AM
Question about argv[] ghoughto Programming 6 10-25-2004 09:38 AM
argc argv linuxanswer Programming 8 10-25-2003 07:54 PM
Help with argv pilot1 Programming 7 08-23-2003 02:20 PM
'delete[] argv': is this correct?? Disruptor Programming 4 07-10-2003 11:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:44 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration