![]()
![]()
#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;
}
![]()