Unix Programming - Changing argv

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2006 > Changing argv





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Changing argv
skylazart@gmail.com

2006-05-24, 1:16 pm

Hi All,

How Can I change the argv?

I know its works:
memset (argv[0], 'A', strlen (argv[0]));

But, When I need a new arg bigger than argv[0] size?

I have to create a new array?

Any ideia?

Thankz in advance

ed

2006-05-24, 1:16 pm

On 24 May 2006 08:22:27 -0700
"skylazart@gmail.com" <skylazart@gmail.com> wrote:

> How Can I change the argv?
>
> I know its works:
> memset (argv[0], 'A', strlen (argv[0]));
>
> But, When I need a new arg bigger than argv[0] size?
>
> I have to create a new array?


I would advise that you do not alter the contents of argv, as you have
already figured out, you should copy it if you make changes.

If you want to alter the size of the memory that you allocate use:

ptr = realloc( my_argv[0], newsize );

--
Regards, Ed :: http://www.gnunix.net
proud c++ person
:%s/Open Source/Free Software/g :: Free DNS available

Bjorn Reese

2006-05-25, 7:18 am

skylazart@gmail.com wrote:

> How Can I change the argv?
>
> I know its works:
> memset (argv[0], 'A', strlen (argv[0]));
>
> But, When I need a new arg bigger than argv[0] size?
>
> I have to create a new array?


The only portable way I can think of is to create a new array and
then use re-execute the program with this array. Something like this
(disclaimer: the code below may contain defects).

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static char *faker = "faker";

int main(int argc, char *argv[])
{
int i;
char **array;

if (strcmp(argv[0], faker) != 0) {
/* Build new array */
array = calloc(argc + 1, sizeof(*array));
if (array) {
array[0] = faker;
for (i = 1; i < argc; ++i) {
array[i] = argv[i];
}
execvp(argv[0], array);
}
}

/* Do your normal stuff here */
sleep(60);
return 0;
}

--
mail1dotstofanetdotdk
davids@webmaster.com

2006-05-25, 7:18 am

What is your actual problem? Why do you think you need to change argv?

DS

skylazart@gmail.com

2006-05-29, 5:32 pm

I have a multiprocess system. Each one connect into a specific host.

I would like to identify these processes using ps.

Its only for GNU Linux OS.

skylazart@gmail.com

2006-05-29, 5:32 pm

I have found that proftpd change argvs. To identify what user is
connected on these process.
Im trying to understand its implementation.

Barry Margolin

2006-05-29, 5:32 pm

In article <1148561544.672773.60660@j55g2000cwa.googlegroups.com>,
"skylazart@gmail.com" <skylazart@gmail.com> wrote:

> I have found that proftpd change argvs. To identify what user is
> connected on these process.
> Im trying to understand its implementation.


Is the source code available?

Another application that changes its argv is sendmail, and I know its
source code is available.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
davids@webmaster.com

2006-05-29, 5:32 pm

Aha! Google for 'setproctitle'.

DS

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com