[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Re: Safe to change argv?

Why Tea

3/26/2011 12:51:00 AM

On Saturday, March 26, 2011 11:06:56 AM UTC+11, Barry Schwarz wrote:
> On Fri, 25 Mar 2011 16:51:21 -0700 (PDT), Why Tea <ytl...@gmail.com>
> wrote:
>
> >When a program receives its command line arguments,
> >e.g. -abc some_value, is it safe to change "-abc"
> >to "--abc"? In general, how safe is it to extend an
> >argv to less than MAX_ARG_STRLEN?
>
> Paragraph 5.1.2.2.1-2 guarantees that you can change the value of the
> 5 bytes in the string "-abc". It does not say anything about the
> memory that follows the string. It is entirely possible for the "6th
> byte" to be part of the next command line argument. It is also
> possible for the bytes that follow the string to hold system specific
> data needed when main returns to your host environment.
>
> All in all it sounds like a really bad idea (tm).
>
> --
> Remove del for email

I knew I'd be bombarded with the "bad idea" comments. Thanks
for not being too harsh. It's one of those situations when
you have a large amount of code out there and you can't change
some part of the code without affecting all the documentation
that goes with it. For example, this is an old command that
doesn't quite follow the standard convention:

# old_cmd [-a time] [-b time] [-cd some_str] [-efg]

A normal way would be:

# new_cmd [-a time] [-b time] [--cd some_str] [--efg]

Command old_cmd was written with a home brewed getopt. Without
changing the old_cmd syntax, is there a way to change the code
to internally mimic new_cmd in order to make use of getopt_long?
3 Answers

Keith Thompson

3/26/2011 2:17:00 AM

0

Why Tea <ytlim1@gmail.com> writes:
> On Saturday, March 26, 2011 11:06:56 AM UTC+11, Barry Schwarz wrote:
>> On Fri, 25 Mar 2011 16:51:21 -0700 (PDT), Why Tea <ytl...@gmail.com>
>> wrote:
>>
>> >When a program receives its command line arguments,
>> >e.g. -abc some_value, is it safe to change "-abc"
>> >to "--abc"? In general, how safe is it to extend an
>> >argv to less than MAX_ARG_STRLEN?
>>
>> Paragraph 5.1.2.2.1-2 guarantees that you can change the value of the
>> 5 bytes in the string "-abc". It does not say anything about the
>> memory that follows the string. It is entirely possible for the "6th
>> byte" to be part of the next command line argument. It is also
>> possible for the bytes that follow the string to hold system specific
>> data needed when main returns to your host environment.
>>
>> All in all it sounds like a really bad idea (tm).
>
> I knew I'd be bombarded with the "bad idea" comments. Thanks
> for not being too harsh. It's one of those situations when
> you have a large amount of code out there and you can't change
> some part of the code without affecting all the documentation
> that goes with it. For example, this is an old command that
> doesn't quite follow the standard convention:
>
> # old_cmd [-a time] [-b time] [-cd some_str] [-efg]
>
> A normal way would be:
>
> # new_cmd [-a time] [-b time] [--cd some_str] [--efg]
>
> Command old_cmd was written with a home brewed getopt. Without
> changing the old_cmd syntax, is there a way to change the code
> to internally mimic new_cmd in order to make use of getopt_long?

getopt_long() takes the values of argc and argv as arguments. There's
no need to modify the values of argc and argv (the parameters objects
declared in main(); just create new argc and argv values and pass them
to getopt_long().

Allocating and initializing the new strings is left as an exercise. 8-)}

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Jorgen Grahn

3/26/2011 6:37:00 AM

0

On Sat, 2011-03-26, Why Tea wrote:
> On Saturday, March 26, 2011 11:06:56 AM UTC+11, Barry Schwarz wrote:
>> On Fri, 25 Mar 2011 16:51:21 -0700 (PDT), Why Tea <ytl...@gmail.com>
>> wrote:
>>
>> >When a program receives its command line arguments,
>> >e.g. -abc some_value, is it safe to change "-abc"
>> >to "--abc"? In general, how safe is it to extend an
>> >argv to less than MAX_ARG_STRLEN?
>>
>> Paragraph 5.1.2.2.1-2 guarantees that you can change the value of the
>> 5 bytes in the string "-abc". It does not say anything about the
>> memory that follows the string. It is entirely possible for the "6th
>> byte" to be part of the next command line argument. It is also
>> possible for the bytes that follow the string to hold system specific
>> data needed when main returns to your host environment.
>>
>> All in all it sounds like a really bad idea (tm).
....
>
> I knew I'd be bombarded with the "bad idea" comments. Thanks
> for not being too harsh. It's one of those situations when
> you have a large amount of code out there and you can't change
> some part of the code without affecting all the documentation
> that goes with it. For example, this is an old command that
> doesn't quite follow the standard convention:
>
> # old_cmd [-a time] [-b time] [-cd some_str] [-efg]
>
> A normal way would be:
>
> # new_cmd [-a time] [-b time] [--cd some_str] [--efg]
>
> Command old_cmd was written with a home brewed getopt. Without
> changing the old_cmd syntax, is there a way to change the code
> to internally mimic new_cmd in order to make use of getopt_long?

Note that you probably *will* change the syntax in other ways; if you
use getopt_long() you'll get standard Unix features like '--'
terminating the parser (ls -- -dir-), grouping of single-character
flags (ls -lf) and (unless you disable it) the ability to mix flags
and normal arguments (ls dir -l).

If you cannot get the users to adapt to subtle changes (maybe they
have scripts and stuff), keep the existing code.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Seebs

3/26/2011 8:38:00 AM

0

On 2011-03-26, Why Tea <ytlim1@gmail.com> wrote:
> Command old_cmd was written with a home brewed getopt. Without
> changing the old_cmd syntax, is there a way to change the code
> to internally mimic new_cmd in order to make use of getopt_long?

Make your own new argv array.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.