popenとpclose

#include < stdio.h >
#include < signal.h >
#include < errno.h >

#define R (0)
#define W (1)

static struct pid {
    struct pid *next;
    FILE *fp;
    pid_t pid;
} *pidlist;

FILE *popen_err(char *command,char *option)
{
    struct pid *cur;
    int pipe_c2p_e[2];
    int pid;
    FILE *fp;
    
    if ((*option != 'r') || option[1]){
        fprintf(stderr,"popen_err():option error:return(NULL)\n");
        return (NULL);
    }
    if ((cur = (struct pid *)malloc(sizeof(struct pid))) == NULL){
        fprintf(stderr,"popen_err():malloc error:return(NULL)\n");
        return (NULL);
    }
    if(piep(pipe_c2p_e)<0){
        perror("pipe");
        return(NULL);
    }
    if((pid=fork())<0){
        perror("fork");
        close(pipe_c2p_e[R]);
        close(pipe_c2p_e[W]);
        return(NULL);
    }
    if(pid==0){
        close(pipe_c2p_e[R]);
        dup2(pipe_c2p_e[W],2);
        close(pipe_c2p_e[W]);
        execlp("sh","sh","-c",command,NULL);
        _exit(127);
    }
    close(pipe_c2p_e[W]);
    fp=fdopen(pipe_c2p_e[R],option);
    cur->fp = fp;
    cur->pid = pid;
    cur->next = pidlist;
    pidlist = cur;
    
    return(fp);
}

int pclose_err(FILE *fp)
{
    register struct pid *cur, *last;
#if BSD /* for */
    int omask;
#else /* SYSV */
    sigset_t set,omask;
#endif
    
    int pstat;
    pid_t pid;
    extern int errno;
    
    fclose(fp);
    for(last = NULL, cur = pidlist; cur; last = cur, cur = cur->next){
        if (cur->fp == fp){
            break;
        }
    }
    if (cur == NULL){
        return (-1);
    }
#if BSD /* for BSD */
    omask = sigblock(sigmask(SIGINT|sigmask(SIGQUIT)|sigmask(SIGHUP));
                     do {
                         pid = waitpid(cur->pid, (int *) &pstat, 0);
                     } while (pid == -1 && errno == EINTER);
                     (void)sigsetmask(omask);
}
#else /* SYSV */
                     sigemptyset(&set);
                     sigaddset(&set,SIGINT);
                     sigaddset(&set,SIGQUIT);
                     sigaddset(&set,SIGHUP);
                     sigprocmask(SIG_SETMASK,&set,&omask);
                     do {
                         pid = waitpid(cur->pid, (int *)&pstat, 0);
                     } while (pid == -1 && errno == EINTR);
                     sigprocmask(SIG_SETMASK, &omask,NULL);
#endif
                     if (last == NULL){
                         pidlist = cur->next;
                     }
                     else {
                         last->next = cur->next;
                     }
                     free(cur);

                     return (pid == -1 ? -1 : pstat);
                     }

execlp

#include < stdio.h >
#include < signal.h >

#define R (0)
#define W (1)

int popen2(char *command,int *fd_r, int *fd_w)
{
    int pipe_c2p[2],pipe_p2c[2];
    int pid;
    
    if(pipe(pipe_c2p)<0){
        perror("popen2");
        return(-1);
    }
    if(pipe(pipe_p2c)<0){
        perror("popen2");
        close(pipe_c2p[R]);
        close(pipe_c2p[W]);
        return(-1);
    }
    if((pid=fork())<0){
        perror("popen2");
        close(pipe_c2p[R]);
        close(pipe_c2p[W]);
        close(pipe_p2c[R]);
        close(pipe_p2c[W]);
        return(-1);
    }
    if(pid==0){
        close(pipe_p2c[W]);
        close(pipe_c2p[R]);
        dup2(pipe_p2c[R],0);
        dup2(pipe_c2p[W],1);
        close(pipe_p2c[R]);
        close(pipe_c2p[W]);
        if(execlp("sh","sh","-c",command,NULL)<0){
            perror("popen2");
            close(pipe_p2c[R]);
            close(pipe_c2p[W]);
            exit(1);
        }
    }
    close(pipe_p2c[R]);
    close(pipe_c2p[W]);
    *fd_w=pipe_p2c[W];
    *fd_r=pipe_c2p[W];
    return(pid);
}

popen()

#include < stdio.h >
#include < string.h >

void main()
{
    char str[512],*ptr;
    FILE *fp;
    if((fp=popen("ls -1","r"))==NULL){
        fprintf(stderr,"error!!!\n");
        exit(-1);
    }
    while(1){
        fgets(str,512,fp);
        if(feof(fp)){
            break;
        }
        ptr=strchr(str,'\n');
        if(ptr!=NULL){
            *ptr='\0';
        }
        printf("%s\n",str);
    }
    pclose(fp);
}

プログラムから他のプログラムを実行

System()は、実行したいコマンドを引数で渡せばそのまま実行してくれます。厳密には、shというシェルに渡して実行してもらいます。

#include < stdio.h >
#include < string.h >

void main()
{
    char filename[80],str[512],*ptr;
    FILE *fp;
    sprintf(filename,"/tmp/ls%d.tmp",getpid());
    sprintf(str,"ls -1 > %s",filename);
    system(str);
    if((fp=fopen(filename,"r"))==NULL){
        fprintf(stderr,"error!!!\n");
        exit(-1);
    }
    while(1){
        fgets(str,512,fp);
        if(feof(fp)){
            break;
        }
        ptr=strchr(str,'\n');
        if(ptr!=NULL){
            *ptr='\0';
        }
        printf("%s\n",str);
    }
    fclose(fp);
    sprintf(str,"rm -f %s",filename);
    system(str);
}

読出しサンプル

#include < stdio.h >
#include < math.h >
#include < fcnt1.h >
#include < math.h >
#include < sys/types.h >
#include < unistd.h >
#include < sys/mman.h >
typedef struct {
    char str[512];
    long lval;
    double dval;
}SSS;
#define NUMBER (10000)
void main()
{
    int fd;
    long psize,size;
    SSS *ptr;
    long i;
    if((fd=open("MapFile",O_RDWR))== -1){
        perror("open");
        exit(-1);
    }
#ifdef BSD
    psize=getpagesize();
#else
    psize=sysconf(_SC_PAGE_SIZE);
#endif
    size=(NUMBER*sizeof(SSS)/psize+1)*psize;
    /* map */
    ptr=(SSS *)mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    /* test */
    while(1){
        for(i=0;i
	

freopen

#include < stdio.h >
#include < stdlib.h >
#include < string.h >

void main(int argc, char *argv[])
{
    FILE *fp;
    fp=freopen("/tmp/debug.log","w",stdout);
    printf("Content-type:text/html\n\n");
    printf("\n");
    printf("\n");
    printf(" \
           CONTENT=\"text/html; charset=x-euc\">\n");
    printf("test\n");
    printf("\n");
    printf("\n");
    printf("

test

\n"); printf("\n"); printf("\n"); }

ディレクトリ内容の読み出し

#include < stdio.h >
#include < dirent.h >

void main(int argc,char *argv[])
{
    DIR *dir;
    struct dirent *dp;
    char path[512];
    
    if(argc<=1){
        strcpy(path,".");
    }
    else {
        strcpy(path,argv[1]);
    }
    if((dir=opendir(path))==NULL){
        perror("opendir");
        exit(-1);
    }
    for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
        printf("%s\n",dp->d_name);
    }
    closedir(dir);
}

可変引数の用途

#include < stdio.h >
#include < varargs.h >

int debug_print(va_alist)
va_dcl
{
    va_list args;
    char *fmt;
    char buf[256];
    
    va_start(args);
    fmt=va_arg(args,char *);
    vsprintf(buf,fmt,args);
    va_end(args);
    fprintf(stderr,"DEBUG[ %s \n",buf);
    return(0);
}

グローバルデータを使う方法

unsigned char Map[ 100 ][ 100 ];

void main()
{
    InitMap();
    LoadMapData();
    SaveMapData();
}

int InitMap()
{
    int x, y;
    
    for(y=0;y<100;y++){
        for(x=0;x<100;x++){
            Map[x][y]=(unsigned char)0;
        }
    }
    return(0);
}

int LoadMapData()
{
    int x,y;
    
    for(y=0;y<100;y++){
        for(x=0;x<100;x++){
            Map[x][y]=getc();
            if(feof(stdin)){
                return(-1);
            }
        }
    }
    return(0);
}

int SaveMapData()
{
    int x,y;
    
    for(y=0;y<100;y++){
        for(x=0;x<100;x++){
            putc(Map[x][y]);
        }
    }
    return(0);
}

関数の実装

#include < stdio.h >

short GetShort();

void main()
{
    short num;
    
    while(1){
        /* short standard input, insert to num */
        num=GetShort();
        /* display num binary */
        ShortBinPrint(num);
    }
}

short GetShort()
{
    char buf[80];
    short num;
    
    fgets(buf, sizeof(buf)-1,stdin);
    num=(short)atoi(buf);
    
    return(0);
}

int ShortBinPrint(num)
short num;
{
    char buf[20];
    
    ShortToBinString(num,buf);
    
    printf("%\n",buf);
    
    return(0);
}

int ShortToBingString(num,buf)
short num;
char *ptr;

ptr=buf;
for(i=15;i>=0;i--){
    *ptr=(char)(((num>>i)&0x01)+'0');
    ptr++;
}
*ptr='\0';

return 0;
}