Print argv and environ with fork .

The comdination of 8_17 and 8_16

The source code of 8_17.c

cat -n 8_17.c
     1  #include "apue.h"
     2
     3  int main(int argc, char * argv[])
     4  {
     5          int i;
     6          char **envp;
     7          extern char **environ;
     8
     9          for(i = 0; i < argc; ++i){
    10                  printf("argc[%d]:%s\n",i,argv[i]);
    11          }
    12
    13          for(envp = environ; *envp != NULL; ++envp){
    14                  printf("%s\n",*envp);
    15          }
    16          exit(0);
    17  }


The correction of 8_16.c

<bldc:/home/tingbinz/apue.3e/SBSCODE/8>R*_*G:vim 8_16.c
#include "apue.h"
#include <sys/wait.h>


char *env_init[] = {"USER=unknown","PATH = /tmp",NULL };
int main()
{
        pid_t pid;
        if ((pid = fork()) < 0){
                err_sys("fork error");
        }
        else if (pid == 0){
                if (execle("/home/tingbinz/bin/echoall","echo","ls",(char *)0,env_init) < 0)
                        err_sys("execle errpr");
        }


        if (waitpid(pid,NULL,0) < 0)
                err_sys("waitpid error");


        if ((pid = fork()) < 0)
                err_sys("fork error");
        else if (pid == 0){
                if(execlp("echoall","/tmp","8_16.c",(char *)0) < 0)
                        err_sys("fork error");


        }
        return 0;


}
~
~
~
~
"8_16.c" 28L, 539C written

The result:

<bldc:/home/tingbinz/apue.3e/SBSCODE/8>R*_*G:./execelp

argc[0]:echo
argc[1]:ls
USER=unknown
PATH = /tmp
<bldc:/home/tingbinz/apue.3e/SBSCODE/8>R*_*G:argc[0]:/tmp
argc[1]:8_16.c
_=./execelp
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章