linux网络编程(3)

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

static int num = 0;
static char namebuf[20];
static char prefix[] = "/tmp/tmp";

char gentemp()
{
    int length,pid;
    //获取进程的标识符
    pid = getpid();
    
    strcpy(namebuf,prefix);
    length = strlen(namebuf);
    /*在文件名中增加pid部分*/
    itoa(pid,&namebuf[length]);
    
    strcat(namebuf,".");
    length=strlen(namebuf);
    do{
        /*增加后缀*/
        itoa(num++,&namebuf[length]);
    }while(access(namebuf,0) != -1);
    return namebuf;
} 

itoa(int i,char *string)
{
    int j,power;
    j = i;
    for(power = 1;j > 0;j = j / 10)
    {
        power *= 10;
    }
    for(;power > 0;power = power / 10)
    {
        *string++ = i / power + '0';
        i = i % power;
    }
    *string = '\0';

}

#include<stdio.h>

extern char** environ;
main()
{
    char** env=environ;
    while(*env)
    {
        printf("%s\n",*env++);
    }
    return 0;
}

#include<unistd.h>
main()
{
    char *argv[]={"showenv",NULL},
        *envp[]={"foo=foo","bar=bar",NULL};
    execve("showenv",argv,envp);
    perror("execev failed.");
    return 0;
}

 

posted @ 2015-11-25 17:34  剑风云  阅读(118)  评论(0编辑  收藏  举报