473,548 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

modifying the strings pointed to by argv

The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?
What can be the maximum length of such a string that is copied ?

Mar 14 '07 #1
56 4413
su************* *@yahoo.com, India said:
The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?
Good question. Why indeed? It's a stupid stupid stupid idea, if ever
there was one.
What can be the maximum length of such a string that is copied ?
0, if you have lots of sense. If you have only a little sense, then find
out how long the existing string is, and don't exceed that value. If
you have no sense whatsoever, pay no attention to either of the above
suggestions.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 14 '07 #2
On 14 Mar, 13:19, Richard Heathfield <r...@see.sig.i nvalidwrote:
subramanian10.. .@yahoo.com, India said:
The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?

Good question. Why indeed? It's a stupid stupid stupid idea, if ever
there was one.
Well, possibly, but there's no particularly good reason I can see for
the arguments passed to your function to be read-only. It could
potentially be useful to space-trim or similar the input arguments and
certainly there's at least one (admittedly sub-optimal) unix[1] hack
involving modifying the argv[] values so that program arguments do not
appear in the ps process listing.

Can you clear up why you think this functionality is unwise ?
[1] Granted, slightly off-topic here, but then again there's a good
few things in the C standard that are that way to avoid breaking how
existng code works, so I'd like to apply for a temporary on-topic
licence for this discussion.


Mar 14 '07 #3
On Mar 14, 9:09*pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?
What can be the maximum length of such a string that is copied ?
Well. There is still something to be clarify.

The standard just says "argc and argv and the strings pointed to by
the argv array shall be modifiable by the program." But the argv array
_itself_ is _not_ required to be modifiable.

The following buggy code only works well when argc >=2 and
strlen(argv[1]) >=1:

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

int main(int argc, char *argv[])
{
char buf[2] = {0};
strcpy(argv[1], buf);
return 0;
}

I guess making that modifiable may be considered for exec*() functions
or for the recursion of main(). And the maximum length maybe found in
POSIX.

Mar 14 '07 #4
On Mar 14, 6:41*pm, "Cong Wang" <xiyou.wangc... @gmail.comwrote :
On Mar 14, 9:09*pm, "subramanian10. ..@yahoo.com, India"

<subramanian10. ..@yahoo.comwro te:
The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?
What can be the maximum length of such a string that is copied ?

Well. There is still something to be clarify.

The standard just says "argc and argv and the strings pointed to by
the argv array shall be modifiable by the program." But the argv array
_itself_ is _not_ required to be modifiable.

The following buggy code only works well when argc >=2 and
strlen(argv[1]) >=1:

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

int main(int argc, char *argv[])
{
* * * * char buf[2] = {0};
* * * * strcpy(argv[1], buf);
* * * * return 0;

}

I guess making that modifiable may be considered for exec*() functions
or for the recursion of main(). And the maximum length maybe found in
POSIX.
No. in VC++ 2005 Express Edition, it allows us to copy, say, "test
message" onto argv[0] itself. Prior to modification argv[0] happened
to be the execautable name. Anyway, I just asked this question for
learning purpose.

Mar 14 '07 #5
On Mar 14, 9:51*pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
On Mar 14, 6:41*pm, "Cong Wang" <xiyou.wangc... @gmail.comwrote :
On Mar 14, 9:09*pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?
What can be the maximum length of such a string that is copied ?
Well. There is still something to be clarify.
The standard just says "argc and argv and the strings pointed to by
the argv array shall be modifiable by the program." But the argvarray
_itself_ is _not_ required to be modifiable.
The following buggy code only works well when argc >=2 and
strlen(argv[1]) >=1:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
* * * * char buf[2] = {0};
* * * * strcpy(argv[1], buf);
* * * * return 0;
}
I guess making that modifiable may be considered for exec*() functions
or for the recursion of main(). And the maximum length maybe found in
POSIX.

No. in VC++ 2005 Express Edition, it allows us to copy, say, "test
message" onto argv[0] itself. Prior to modification argv[0] happened
to be the execautable name. Anyway, I just asked this question for
learning purpose.
No for what?

Copying "test message" onto argv[0] is _not_ modifying argv[0] itself,
only changing the contents of the array which argv[0] points to.
What's more, copying to argv[0] is UB.

Mar 14 '07 #6
On Mar 14, 7:13*pm, "Cong Wang" <xiyou.wangc... @gmail.comwrote :
On Mar 14, 9:51*pm, "subramanian10. ..@yahoo.com, India"

<subramanian10. ..@yahoo.comwro te:
On Mar 14, 6:41*pm, "Cong Wang" <xiyou.wangc... @gmail.comwrote :
On Mar 14, 9:09*pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?
What can be the maximum length of such a string that is copied ?
Well. There is still something to be clarify.
The standard just says "argc and argv and the strings pointed to by
the argv array shall be modifiable by the program." But the argv array
_itself_ is _not_ required to be modifiable.
The following buggy code only works well when argc >=2 and
strlen(argv[1]) >=1:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
* * * * char buf[2] = {0};
* * * * strcpy(argv[1], buf);
* * * * return 0;
}
I guess making that modifiable may be considered for exec*() functions
or for the recursion of main(). And the maximum length maybe found in
POSIX.
No. in VC++ 2005 Express Edition, it allows us to copy, say, "test
message" onto argv[0] itself. Prior to modification argv[0] happened
to be the execautable name. Anyway, I just asked this question for
learning purpose.

No for what?

Copying "test message" onto argv[0] is _not_ modifying argv[0] itself,
only changing the contents of the array which argv[0] points to.
What's more, copying to argv[0] is UB.- Hide quoted text -

- Show quoted text -
I am sorry. Entire post of mine in this thread is wrong.

Mar 14 '07 #7
On Mar 14, 1:41 pm, "Cong Wang" <xiyou.wangc... @gmail.comwrote :
On Mar 14, 9:09 pm, "subramanian10. ..@yahoo.com, India"

<subramanian10. ..@yahoo.comwro te:
The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?
What can be the maximum length of such a string that is copied ?

Well. There is still something to be clarify.

The standard just says "argc and argv and the strings pointed to by
the argv array shall be modifiable by the program." But the argv array
_itself_ is _not_ required to be modifiable.
This came up on comp.std.c again recently. The intent is that the argv
array itself /is/ required to be modifiable, and the actual wording of
the standard can be argued to require the same.

Mar 14 '07 #8
On Mar 14, 6:19 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
subramanian10.. .@yahoo.com, India said:
The standard allows that we can copy strings onto arg[0], arg[1], etc.
Why is it allowed ?

Good question. Why indeed? It's a stupid stupid stupid idea, if ever
there was one.
As with many aspects of Standard C it was to support common pre-
existing practice. In UNIXes of the time the ps command could be used
to view the current argv data. Programs made use of this facility
either to hide the parameters they were invoked with (for example, if
a password was passed as a command line option) or to make application
status information available through ps (sendmail did this if I
remember correctly). I don't know if modern UNIXes and similar still
use this.

Mar 14 '07 #9
Cong Wang wrote:
>
.... snip ...
>
The standard just says "argc and argv and the strings pointed to
by the argv array shall be modifiable by the program." But the
argv array _itself_ is _not_ required to be modifiable.
^^^
I fail to understand why this char-set cannot include the normal
coding for 'f' and 'i', thus causing abnormal displays.
>
The following buggy code only works well when argc >=2 and
strlen(argv[1]) >=1:

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

int main(int argc, char *argv[])
{
char buf[2] = {0};
strcpy(argv[1], buf);
return 0;
}

I guess making that modifiable may be considered for exec*()
functions or for the recursion of main(). And the maximum length
maybe found in POSIX.
There are _no_ restrictions against modifying argv and argc. After
all, those are values local to main. The only restrictions are
against modifying *argv (forbidden) and *argv[i] (for i <= argc),
which is not allowed to extend the length of the string argv[i].
So this has nothing to do with any possible recursive call to main.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 14 '07 #10

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

Similar topics

22
2176
by: Joe Smith | last post by:
It is nothing short of embarrassing to feel the need to ask for help on this. I can't see how I would make the main control for this. What I want is a for loop and a test condition. And while I know, from things I pondered 2 decades ago, that a fella can write code without a goto, I'm stuck. /* sieve1.c */ #define whatever 20...
0
7512
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.