linux programming

4593 ワード

APUE上の一例、実装lsの機能
#include<sys/types.h>
#include <dirent.h>
#include <stdio.h>
int main(int argc,char *argv[])
{

        DIR *dp;
        struct dirent *dirp;
        if(argc!=2){
                printf("not enough argument
"); return 0; } if((dp=(opendir(argv[1])))==NULL) printf("not a directory
"); while((dirp=readdir(dp))!=NULL) printf("%s
",dirp->d_name); closedir(dp); return 0; }

 
 
 
#include<unistd.h>
#include<stdio.h>
#define BUFFSIZE 4096
int main(void)
{
        int n;
        char buf[BUFFSIZE];
        while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0 )
                if(write(STDOUT_FILENO,buf,n)!=n)
                        printf("write error
"); if(n<0) printf("read error
"); exit(0); }

このセグメントプログラム完了機能は、標準入力から入力された文字をbufに格納し、bufの内容を標準出力に出力して表示する
The file descriptor for standard input is 0 (zero); the POSIX  definition is stdIN_FILENO; the corresponding  variable is FILE* stdin.
stdIN_FILENOはintタイプ
 
 
 
#include<unistd.h>
#include<stdio.h>

int main(void)
{
        printf("hello world from process ID %d
",getpid()); exit(0); } ~

getpid() returns the process ID of the calling process. (This is often used by routines that generate unique temporary filenames.)getppid() returns the process ID of the parent of the calling process.
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>
#define MAXLINE 4096
int main()
{

        char buf[MAXLINE];
        pid_t pid;
        int status;

        printf("%% ");
        while(fgets(buf,MAXLINE,stdin) !=NULL){
                if(buf[strlen(buf)-1]=='
') buf[strlen(buf)-1]=0; /* replace newline with null */ if((pid= fork())<0){ printf("fork error
"); }else if(pid==0){ /*child process */ execlp(buf,buf,(char *)0); exit(127); } /*parent*/ if((pid=waitpid(pid,&status,0))<0) printf("waitpid error
"); printf("%% "); } exit(0); }

コードを書く時、注釈記号*と/の間に1つのスペースをあけて、ずっと間違いが見つからないで、結果は長い間やっと探し当てました
char *fgets(char *s, int size, FILE *stream)

fgets() reads in at most one less than size characters fromstream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq\0aq) is stored after the last character in the buffer.
fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the following points:
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.
int execlp(const char *file, const char *arg, ...);
The exec() family of functions replaces the current process image with a new process image.
The const char*arg and subsequent ellipses(省略記号)in the execl()、execlp()、and execle()functions can be thought of as arg 0,arg 1,..., argn.Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program.The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
_POSIX_C_SOURCE
              Defining this macro causes header files to expose definitions as
              follows:
              The value 1 exposes definitions conforming to POSIX.1-1990 and ISO C
                 (1990).
  _POSIX_SOURCE
              Defining this obsolete macro with any value is equivalent to defining
              _POSIX_C_SOURCE with the value 1.