进程启动后修改命令行参数

进程启动后修改命令行参数

 1  #include "environment.h"
 2 
 3  extern char** environ;
 4  void my_initproctitle(char* argv[], char** last);
 5  void my_setproctitle(char* argv[], char** last, char* title);
 6  
 7  int main(int argc, char* argv[])
 8  {
 9     SetAppTitleInit(argc,argv);
10     char s_title[] = "ymc1234";
11      
12     SetAppTitle(s_title);
13     while(1); 
14     return 0;
15  }
16   
17  void my_initproctitle(char* argv[], char** last)
18  {
19      int i = 0;
20      char* p_tmp = NULL;
21      size_t i_size = 0;
22   
23      for(i = 0; environ[i]; i++){
24          i_size += strlen(environ[i]) + 1;
25      }
26       
27      p_tmp = malloc(i_size);
28      if(p_tmp == NULL){
29          return ;
30      }
31   
32      *last = argv[0];
33      for(i = 0; argv[i]; i++){
34          *last += strlen(argv[i]) + 1;
35      }
36   
37      for(i = 0; environ[i]; i++){
38          i_size = strlen(environ[i]) + 1;
39          *last += i_size;
40   
41          strncpy(p_tmp, environ[i], i_size);
42          environ[i] = p_tmp;
43          p_tmp += i_size;
44      }
45   
46      (*last)--;
47   
48      return ;
49   
50  }
51   
52  void my_setproctitle(char* argv[], char** last, char* title)
53  {
54      char* p_tmp = NULL;
55 
56      p_tmp = argv[0];
57       memset(p_tmp, 0, *last - p_tmp); 
58      strncpy(p_tmp, title, *last - p_tmp);
59      return ;
60  }
61  
62  void SetAppTitleInit(int argc, char* argv[])
63  {
64      g_argc = argc;
65      g_argv = argv;
66  }
67  
68   void SetAppTitle(char* title)
69   {
70       char* p_last = NULL;
71       my_initproctitle(g_argv, &p_last);
72       my_setproctitle(g_argv, &p_last, title);
73   }

猜你喜欢

转载自www.cnblogs.com/andy2015/p/11984631.html
今日推荐