日期:2014-05-16 浏览次数:20743 次
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
int mysystem(char* cmdstring, char* buf, int len)
{
int fd[2];
pid_t pid;
int n, count;
memset(buf, 0, len);
if (pipe(fd) < 0)
return -1;
if ((pid = fork()) < 0)
return -1;
else if (pid > 0)
{
close(fd[1]);
count = 0;
while ((n = read(fd[0], buf + count, len)) > 0 && count > len)
count += n;
close(fd[0]);
if (waitpid(pid, NULL, 0) > 0)
return -1;
}
else
{
close(fd[0]);
if (fd[1] != STDOUT_FILENO)
{
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
{
return -1;
}
close(fd[1]);
}
if (execl("/bin/sh", "sh", "-c", cmdstring, (char*)0) == -1)
return -1;
}
return 0;
}
int main()
{
char buf[10];
mysystem("gcc -o add add.c",buf,11);
printf("=%s=",buf);
return 0;
}
int main(void)
{
FILE *stream;
char buf[1024];
memset(buf,'\0',sizeof(buf));
stream = popen("gcc -o mail mail.c","r");
fread(buf,sizeof(char),sizeof(buf),stream);
fclose(stream);
printf("qqqqq:%s:sss\n",buf);
return 0;
}