Q: Can I realloc the Argv[] space somehow? (Linux)

Q: Can I realloc the Argv[] space somehow? (Linux)

Post by Ren Hoe » Fri, 17 Mar 2000 04:00:00



Hi there,

I got a question, I have a program where I want to show some status
when you do a 'ps'. I have made the sample source below and it
works, however you don't know if you got enough room.. it varies
with what the user typed on the commandline.
Is there a way to make it bigger or bend the pointer, without
being smitten by a SEGV?

Many thanks,


Example:

cc -Wall ps_test.c ; a.out 123222222222d fdgsfdggsfdgf qwqwqwqw

26369 ttyp0    S      0:00  |   \_ -bash
28862 ttyp0    S      0:00  |      \_ABCDEFGHIJKLMNOPQRST...<SNIP>
28864 ttyp0    R      0:00  |               \_ ps -fax

-----------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h> /* system */

void main(int Argc, char *Argv[])
{
int total_len=0;
int t;

/*   Comment this back in and get a SEGV
void *p=Argv;
realloc(p, 30);
*/

for(t=0; t<Argc; t++) total_len=total_len+strlen(Argv[t])+1;
for(t=0; t<total_len+25; t++) Argv[0][t]='A'+t;

system("ps -fax | grep -v httpd | grep -v agetty");

Quote:}

-----------------------------------------------------------------------------------------------------
 
 
 

Q: Can I realloc the Argv[] space somehow? (Linux)

Post by Fletcher Glen » Sat, 18 Mar 2000 04:00:00


You cannot use realloc (which contains an implicit free())
on argv, because argv is not part of malloc()ed memory.
Even if you did change the contents of argv, there's no
guarantee that it will show up in the ps output.  Some
UNIX variants copy argv for the process list.

--
                Fletcher Glenn


> Hi there,

> I got a question, I have a program where I want to show some status
> when you do a 'ps'. I have made the sample source below and it
> works, however you don't know if you got enough room.. it varies
> with what the user typed on the commandline.
> Is there a way to make it bigger or bend the pointer, without
> being smitten by a SEGV?

> Many thanks,


> Example:

> cc -Wall ps_test.c ; a.out 123222222222d fdgsfdggsfdgf qwqwqwqw

> 26369 ttyp0    S      0:00  |   \_ -bash
> 28862 ttyp0    S      0:00  |      \_ABCDEFGHIJKLMNOPQRST...<SNIP>
> 28864 ttyp0    R      0:00  |               \_ ps -fax

> -----------------------------------------------------------------------------------------------------
> #include <stdio.h>
> #include <stdlib.h> /* system */

> void main(int Argc, char *Argv[])
> {
> int total_len=0;
> int t;

> /*   Comment this back in and get a SEGV
> void *p=Argv;
> realloc(p, 30);
> */

> for(t=0; t<Argc; t++) total_len=total_len+strlen(Argv[t])+1;
> for(t=0; t<total_len+25; t++) Argv[0][t]='A'+t;

> system("ps -fax | grep -v httpd | grep -v agetty");
> }

> -----------------------------------------------------------------------------------------------------