473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Processing command line arguments

In K&R (2nd edition) the authors published the following code for
dealing with command line arguments (p. 117):

while (--argc 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}

But this solution raised some criticism:

"The sample program increments argv[0]. Although argv is
modifiable, and argv[0][0] is modifiable (if argv[0] is
not a null pointer), argv[0] is not modifiable. (At least,
not always.)"

(Peter Seebacher, http://www.plethora.net/~seebs/c/knr.html)

"p.117 (§5.10): In the find example, the program increments
argv[0]. This is not specifically forbidden, but not
specifically allowed either."

(Source:
Errata for The C Programming Language, Second Edition
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html)

Now my question is: would the following (slight modification of the
original code) be a reasonable (or at least correct) solution?:

while (--argc 0 && (*++argv)[0] == '-') {
char *arg = *argv;
while (c = *++arg)
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
}
G.

--

E-mail: info<at>simple-line<Punkt>de
Apr 21 '07 #1
5 2285

"Gregor H." <nomail@invalidwrote in message
news:ma********************************@4ax.com...
In K&R (2nd edition) the authors published the following code for
dealing with command line arguments (p. 117):

while (--argc 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}

But this solution raised some criticism:

"The sample program increments argv[0]. Although argv is
modifiable, and argv[0][0] is modifiable (if argv[0] is
not a null pointer), argv[0] is not modifiable. (At least,
not always.)"

(Peter Seebacher, http://www.plethora.net/~seebs/c/knr.html)

"p.117 (§5.10): In the find example, the program increments
argv[0]. This is not specifically forbidden, but not
specifically allowed either."

(Source:
Errata for The C Programming Language, Second Edition
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html)

Now my question is: would the following (slight modification of the
original code) be a reasonable (or at least correct) solution?:

while (--argc 0 && (*++argv)[0] == '-') {
This line is virtually unreadable. I know K and R used the same construct.
Things have moved on.
>
char *arg = *argv;
while (c = *++arg)
However this is the way round. argv contains a list of strings. Nothing
conceptually difficult about that, but you can make it look difficult
because strigns are not atomic objects in C. It is a bad idea to modify the
strings in place, though I think they are writeable for historical reasons,
and it is a bad idea and may even be illegal to modify the pointers in the
argv array.
However if you take copies then you can do what you want with the copy,
naturally.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Apr 21 '07 #2
Gregor H. wrote:
In K&R (2nd edition) the authors published the following code for
dealing with command line arguments (p. 117):

while (--argc 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}

But this solution raised some criticism:

"The sample program increments argv[0]. Although argv is
modifiable, and argv[0][0] is modifiable (if argv[0] is
not a null pointer), argv[0] is not modifiable. (At least,
not always.)"

(Peter Seebacher, http://www.plethora.net/~seebs/c/knr.html)

"p.117 (§5.10): In the find example, the program increments
argv[0]. This is not specifically forbidden, but not
specifically allowed either."

(Source:
Errata for The C Programming Language, Second Edition
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html)
The criticisms are incorrect. The standard is poorly worded (and for
that reason, the issue has come up multiple times), but both the
intent and the literal wording are that there are no prohibitions
against modifying argv[0].

There are arguably stylistic problems, though, so you could use
something like

if (argc != 0)
{
char **argptr;
for (argptr = argv + 1; *argptr != NULL; argptr++)
{
char *arg = *argptr;
if (arg[0] != '-')
break;
/* ... */
}
}

which not only avoids the unclarity of modifying argv[0], but is also
easier to comprehend.

Apr 21 '07 #3
On Apr 21, 10:52 am, "Malcolm McLean" <regniz...@btinternet.com>
wrote:
"Gregor H." <nomail@invalidwrote in message
(Why did you start a new thread to reply to this question?)
news:ma********************************@4ax.com...
In K&R (2nd edition) the authors published the following code for
dealing with command line arguments (p. 117):
while (--argc 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
But this solution raised some criticism:
"The sample program incrementsargv[0]. Althoughargvis
modifiable, andargv[0][0] is modifiable (ifargv[0] is
not a null pointer),argv[0] is not modifiable. (At least,
not always.)"
(Peter Seebacher,http://www.plethora.net/~seebs/c/knr.html)
"p.117 (§5.10): In the find example, the program increments
argv[0]. This is not specifically forbidden, but not
specifically allowed either."
(Source:
Errata for The C Programming Language, Second Edition
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html)
Now my question is: would the following (slight modification of the
original code) be a reasonable (or at least correct) solution?:
while (--argc 0 && (*++argv)[0] == '-') {

This line is virtually unreadable. I know K and R used the same construct.
Things have moved on.
Say what? It's terse, clear, and correct - what more can you ask for
in a line of code?
char *arg = *argv;
while (c = *++arg)

However this is the way round.argvcontains a list of strings. Nothing
conceptually difficult about that, but you can make it look difficult
because strigns are not atomic objects in C. It is a bad idea to modify the
strings in place, though I think they are writeable for historical reasons,
and it is a bad idea and may even be illegal to modify the pointers in theargvarray.
However if you take copies then you can do what you want with the copy,
naturally.
--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

Apr 21 '07 #4
On 21 Apr 2007 11:43:00 -0700, Fr************@googlemail.com wrote:
>>>
while (--argc 0 && (*++argv)[0] == '-') { ... }
This line is virtually unreadable. I know K and R used the same construct.
Things have moved on.
Say what? It's terse, clear, and correct - what more can you ask for
in a line of code?
Agree with you. But maybe he would prefer the variant

while (--argc 0 && **++argv == '-') { ... }

This way we do not mix pointer notation with index notation. Maybe
this is a "better" solution. Actually, I use _this variant_ in my
improved code. So *my* version actually is:

while (--argc 0 && **++argv == '-') {
char *arg = *argv;
while (c = *++arg)
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
...
break;
}
}
G.

--

E-mail: info<at>simple-line<Punkt>de
Apr 21 '07 #5
On Apr 21, 11:07 pm, Gregor H. <nomail@invalidwrote:
On 21 Apr 2007 11:43:00 -0700, Francine.Ne...@googlemail.com wrote:
>while (--argc 0 && (*++argv)[0] == '-') { ... }
This line is virtually unreadable. I know K and R used the same construct.
Things have moved on.
Say what? It's terse, clear, and correct - what more can you ask for
in a line of code?

Agree with you. But maybe he would prefer the variant

while (--argc 0 && **++argv == '-') { ... }

This way we do not mix pointer notation with index notation. Maybe
this is a "better" solution.
Good point - you save a few characters doing it this way too :)
>Actually, I use _this variant_ in my
improved code. So *my* version actually is:

while (--argc 0 && **++argv == '-') {
char *arg = *argv;
while (c = *++arg)
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
...
break;
}

}

G.

--

E-mail: info<at>simple-line<Punkt>de

Apr 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hari | last post by:
can i have command line arguments in VS.NET applicatio? if yes how? Can i have some code snippets of the above functionality? I know we can acjieve this in console application form command...
3
by: Dubya | last post by:
Hello Everyone, I have this little app, that I would like to configure two modes of operation by using command line arguments. If the app is started with no arguments, the Main UserForm shows...
23
by: Daniel Rudy | last post by:
Hello, I'm trying to learn how command line arguments are handled in C. The following code segment that I wrote as a test program compiles, but when I try to run it, it core dumps. This is...
1
by: amirmira | last post by:
I would like to set command line arguments to a service at install time. I need to do this because I need to get information from different registry locations depending on my command line argument....
3
by: Dubya | last post by:
Hello Everyone, I have this little app, that I would like to configure two modes of operation by using command line arguments. If the app is started with no arguments, the Main UserForm shows...
1
by: Rune Jacobsen | last post by:
Hi, I've been trying to figure this one out, but my experience just doesn't have what it takes... :| I am writing an application that reads an XML file and displays the contents in various...
40
by: raphfrk | last post by:
I have a program which reads in 3 filenames from the command line prog filename1 filename2 filename3 However, it doesn't work when one of the filenames has spaces in it (due to a directory...
2
by: Milan | last post by:
Hi, Please guide me how to set command line argument and how to retrive command line argument. Senario: vb.net application should be able to execute from command prompt by passing login and...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.