unix 环境高级编程(去头文件ourhdr.h后代码)

1-1.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>

int main(int argc,char *argv[])
{
    int temp=1;
    DIR *dp;
    struct dirent *dirp;
       if(argc <2)
       {
           printf("a argumen(the directory name) is required");
           exit(1);
       }
    while(temp<argc)
    {
        if((dp=    opendir(argv[temp]))==NULL)     // DIR *opendir(const char* name);
        {
            printf("can't open %s/n",argv[temp]);
            exit(1);
        }
        while((dirp=readdir(dp))!=NULL)     // struct dirent *readdir(DIR *dirp);
            printf("%s/t",dirp->d_name);
        printf("/n");
        closedir(dp);                 // int closedir(DIR *dir);
        temp++;
    }    
}

-------------------------------------------------------------------------------
1-2.c

#include <unistd.h>        // STDIN_FILENO STDOUT_FILENO 定义在 unistd.h头文件中
#include <stdio.h>        // 它们典型值为0和1
#include <stdlib.h>
#define BUFFSIZE 8192
int main(void)
{
    int n;
    char buf[BUFFSIZE];
    while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0)    // ssize_t read(int fildes, void *buf, size_t nbyte);
        if(write(STDOUT_FILENO,buf,n)!=n)    // The read() function shall attempt to read nbyte bytes from the file associated with the open file
        {    printf("write error/n");    //     descriptor, fildes, into the buffer pointed to by buf.
            exit(1);            // ssize_t write(int fildes, const void *buf, size_t nbyte);
        }                    //The write() function shall attempt to write nbyte bytes from the buffer pointed to by buf to the
                                   //    file associated with the open file descriptor, fildes.
    if(n<0)                        //    f write() is interrupted by a signal after it successfully writes some data,
                                   //    it shall return the number of bytes written.
    {
        printf("read error");
        exit(1);
    }
    exit(0);
}

--------------------------------------------------------------------------------
1-3.c

# include <stdio.h>
# include <stdlib.h>
int main()
{
    int c;
    while((c=getc(stdin))!=EOF)
        if(putc(c,stdout)==EOF)
        {
            printf("output error");
            exit(1);
        }
    if(ferror(stdin))
    {
        printf("input error");
        exit(1);
    }
    exit(0);
}

-----------------------------------------------------------------------------------
1-4.c

# include <sys/types.h>        // pid_t getpid(void); getpid() returns the process ID of the current process.
# include <unistd.h>        // pid_t getppid(void); getppid() returns the process ID of the parent of the current process.
# include <stdio.h>       

int main(void)
{
    printf("The pid of currenct process is %d,ppid is %d/n",getpid(),getppid());
}

---------------------------------------------------------------------------------
1-5.c

#define MAXLINE 100
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(void)
{
    char    buf[MAXLINE];     // define a buffer
    pid_t    pid;       
    int     status;        //
    printf("%% ");    // print prompt (printf requires %% to print %)
    while(fgets(buf,MAXLINE,stdin)!=NULL) // read bytes from stream into the array pointed by buf
    {
        buf[strlen(buf)-1]=0;    // replace newline with null
    }
    if((pid=fork())<0)        // pid_t fork(void); fork creats a new process by duplicating the calling process.
    {                //     The new process referred to as the child.   
        printf("fork error/n");
        exit(1);
    }
    else if(pid == 0)    // child
    {
        execlp(buf,buf,(char*) 0);    //  int execlp(const char *file, const char *arg0, ... /*, (char *)0 */);
        if(errno)            //  errno included by errno.h, errno shall changed if execlp failed
        {
            printf("couldn't execute %s/n",buf);
            exit(127);
        }
    }                    //  pid_t waitpid(pid_t pid, int *stat_loc, int options);   
     if(pid=waitpid(pid,&status,0)<0)    // parent
     {
         printf("waitpid error/n");
         printf("%%  ");
     }
     exit(0);

}
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值