[lkml]   [2003]   [Jan]   [15]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: Changing argv[0] under Linux.

Well, I just can't give this up! Here is a procedure plus a test
program that reallocates and copies environment strings so the
process title can be altered. The procedure returns the amount
of space available for the new name so that strncpy() can be
used to prevent damage.

If you don't like me pretending that main() gets the environment
after args[], you can access environ directly anyway with the
same result. Have fun!

If you don't have enough string-space to make a large enough name,
just increase the size of your environment.

Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
Why is the government concerned about the lunatic fringe? Think about it.

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

extern char **environ;

size_t init_settitle(char *argv[], char *env[])
size_t i, len;
char *limit;
char *var;
i = 0;
while(env[i] != NULL)
len = strlen(env[i]) + 1; /* Room for the '\0' */
var = (char *) malloc(len);
memcpy(var, env[i], len); /* Copy the '\0' also */
limit = env[i]; /* Start of last string */
env[i++] = var; /* New environment ptr */
while(*limit) /* End of last string */
return (limit - argv[0]); /* Space we can use */

int main(int c, char *argv[], char *env[])
size_t len, i;
len = init_settitle(argv, env);
i = 0;
while(environ[i] != NULL)
printf("Length allowed = %u\n", len);
strncpy(argv[0], "Antidesestablishmentarianism", len);
i = 0;
while(environ[i] != NULL)
return 0;

 \ /
  Last update: 2005-03-22 13:32    [W:0.053 / U:11.108 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site