Repository: Lincheng1993/apue Branch: master Commit: 9b3925f53247 Files: 48 Total size: 26.3 KB Directory structure: gitextract_bal5dal_/ ├── APUE/ │ ├── 1_10.c │ ├── 1_3.c │ ├── 1_4.c │ ├── 1_5.c │ ├── 1_6.c │ ├── 1_7.c │ ├── 1_8.c │ ├── 1_9.c │ ├── 2_16.c │ ├── 2_17.c │ ├── 3_1.c │ ├── 3_11.c │ ├── 3_12_1.c │ ├── 3_12_2.c │ ├── 3_2.c │ ├── 3_5.c │ ├── 4_12.c │ ├── 4_16.c │ ├── 4_21.c │ ├── 4_22.c │ ├── 4_23.c │ ├── 4_24.c │ ├── 4_25.c │ ├── 4_3.c │ ├── 4_8.c │ ├── 4_9.c │ ├── 5_11.c │ ├── 5_12.c │ ├── 5_13.c │ ├── 5_15.c │ ├── 5_4.c │ ├── 5_5.c │ ├── 6_11.c │ ├── 6_2.c │ ├── 7_1.c │ ├── 7_13.c │ ├── 7_16.c │ ├── 7_3.c │ ├── 7_4.c │ ├── 8_1.c │ ├── 8_12.c │ ├── 8_3.c │ ├── 8_5.c │ ├── 8_6.c │ ├── 8_8.c │ └── FuncPointer.c ├── Makefile └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: APUE/1_10.c ================================================ #include "apue.h" #include static void sig_int(int); int main(void){ char buf[MAXLINE]; pid_t pid; int status; if (signal(SIGINT,sig_int) == SIG_ERR){ err_sys("signal error"); } printf("%% "); while (fgets(buf, MAXLINE, stdin) != NULL){ if (buf[strlen(buf)-1] == '\n'){ buf[strlen(buf)-1] = 0; } if ((pid = fork()) < 0){ err_sys("fork error"); } else if (pid == 0){ execlp(buf, buf, (char*)0); err_ret("couldn't execute : %s", buf); exit(127); } if ((pid = waitpid(pid, &status, 0)) < 0){ err_sys("waitpid error"); } printf("%% "); } exit(0); } void sig_int(int signo){ printf("interrupt \n%% "); } ================================================ FILE: APUE/1_3.c ================================================ #include "apue.h" #include int main(int argc, char *argv[]){ DIR *dp; struct dirent *dirp; if (argc != 2){ err_quit("usage : ls directory_name"); } if ((dp = opendir(argv[1])) == NULL){ err_sys("Can't open %s", argv[1]); } while ((dirp = readdir(dp)) != NULL){ printf("%s\n", dirp->d_name); } closedir(dp); exit(0); } ================================================ FILE: APUE/1_4.c ================================================ #include "apue.h" #define BUFFSIZE 4096 int main(void){ int n = 0; char buf[BUFFSIZE]; while((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0){ if (write(STDOUT_FILENO, buf, n) != n){ err_sys("write error"); } } if (n < 0){ err_sys("read error"); } exit(0); } ================================================ FILE: APUE/1_5.c ================================================ #include "apue.h" int main(void){ int c; while ((c = getc(stdin)) != EOF){ if (putc(c, stdout) == EOF){ err_sys("output error"); } } if (ferror(stdin)){ err_sys("input error"); } exit(0); } ================================================ FILE: APUE/1_6.c ================================================ #include "apue.h" int main(void){ printf("hello world from process ID %ld\n", (long)getpid()); exit(0); } ================================================ FILE: APUE/1_7.c ================================================ #include "apue.h" #include int main(void){ char buf[MAXLINE]; pid_t pid; int status; printf("%% "); while (fgets(buf, MAXLINE, stdin) != NULL){ if (buf[strlen(buf)-1] == '\n'){ buf[strlen(buf)-1] = 0; } if ((pid = fork()) < 0){ err_sys("fork error"); } else if (pid == 0){ execlp(buf, buf, (char*)0); err_ret("couldn't execute : %s", buf); exit(127); } if ((pid = waitpid(pid, &status, 0)) < 0){ err_sys("waitpid error"); } printf("%% "); } exit(0); } ================================================ FILE: APUE/1_8.c ================================================ #include "apue.h" #include int main(int argc, char* argv[]){ fprintf(stderr, "EACCES : %s\n", strerror(EACCES)); errno = ENOENT; perror(argv[0]); exit(0); } ================================================ FILE: APUE/1_9.c ================================================ #include "apue.h" int main(void){ printf("uid = %d, gid = %d\n", getuid(), getgid()); exit(0); } ================================================ FILE: APUE/2_16.c ================================================ #include "apue.h" #include #ifdef PATH_MAX static long pathmax = PATH_MAX; #else static long pathmax = 0; #endif static long posix_version = 0; static long xsi_version = 0; #define PATH_MAX_GUESS 1024 char* path_alloc(size_t *sizep){ char *ptr; size_t size; if (posix_version == 0){ posix_version = sysconf(_SC_VERSION); } if (xsi_version == 0){ xsi_version = sysconf(_SC_XOPEN_VERSION); } if (pathmax == 0){ errno = 0; if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0){ if (errno == 0){ pathmax = PATH_MAX_GUESS; } else{ err_sys("pathconf error for _PC_PATH_MAX"); } } else{ pathmax++; } } if ((posix_version < 200112L) && (xsi_version < 4)){ size = pathmax + 1; } else{ size = pathmax; } if ((ptr = malloc(size)) == NULL){ err_sys("malloc error for pathname"); } if (sizep != NULL){ *sizep = size; } return(ptr); } ================================================ FILE: APUE/2_17.c ================================================ #include "apue.h" #include #include #ifdef OPEN_MAX static long openmax = OPEN_MAX; #else static long openmax = 0; #endif #define OPEN_MAX_GUESS 256 long open_max(void){ if (openmax == 0){ errno = 0; if ((openmax = sysconf(_SC_OPEN_MAX)) < 0){ if (errno == 0){ openmax = OPEN_MAX_GUESS; } else{ err_sys("sysconf error for _SC_OPEN_MAX"); } } } return(openmax); } ================================================ FILE: APUE/3_1.c ================================================ #include "apue.h" int main(void){ if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1){ printf("cannot seek\n"); } else{ printf("seek ok\n"); } exit(0); } ================================================ FILE: APUE/3_11.c ================================================ #include "apue.h" #include int main(int argc, char* argv[]){ int val; if (argc != 2){ err_quit("usage : main "); } if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0){ err_sys("fcntl error for fd %d", atoi(argv[1])); } switch (val & O_ACCMODE){ case O_RDONLY: printf("read only"); break; case O_WRONLY: printf("write only"); break; case O_RDWR: printf("read write"); break; default: err_dump("unknown access mode"); } if (val & O_APPEND){ printf(", append"); } if (val & O_NONBLOCK){ printf(", nonblocking"); } if (val & O_SYNC){ printf(", synchronous writes"); } # if !defined(_POSIX_C_SOURCE) && defined(O_FSYNC) && (O_FSYNC != O_SYNC) if (val & O_FSYNC){ printf(", synchronous writes"); } #endif putchar('\n'); exit(0); } ================================================ FILE: APUE/3_12_1.c ================================================ #include "apue.h" #include void set_fl(int fd, int flags){ int val; if ((val = fcntl(fd, F_GETFL, 0)) < 0){ err_sys("fcntl F_GETFL error"); } val |= flags; if (fcntl(fd, F_SETFL, val) < 0){ err_sys("fcntl F_SETFL error"); } } ================================================ FILE: APUE/3_12_2.c ================================================ #include "apue.h" #include void set_fl(int fd, int flags){ int val; if ((val = fcntl(fd, F_GETFL, 0)) < 0){ err_sys("fcntl F_GETFL error"); } val &= ~flags; if (fcntl(fd, F_SETFL, val) < 0){ err_sys("fcntl F_SETFL error"); } } ================================================ FILE: APUE/3_2.c ================================================ #include "apue.h" #include char buf1[] = "abcdefghij"; char buf2[] = "ABCDEFGHIJ"; int main(void){ int fd; if ((fd = creat("file.hole", FILE_MODE)) < 0){ err_sys("creat error"); } if (write(fd, buf1, 10) != 10){ err_sys("buf1 write error"); } if (lseek(fd, 16834, SEEK_SET) == -1){ err_sys("lseek error"); } if (write(fd, buf2, 10) != 10){ err_sys("buf2 write error"); } exit(0); } ================================================ FILE: APUE/3_5.c ================================================ #include "apue.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){ err_sys("write error"); } } if (n < 0){ err_sys("read error"); } exit(0); } ================================================ FILE: APUE/4_12.c ================================================ #include "apue.h" int main(void){ struct stat statbuf; if (stat("foo", &statbuf) < 0){ err_sys("stat error for foo"); } if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0){ err_sys("chmod error for foo"); } if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0){ err_sys("chmod error for bar"); } exit(0); } ================================================ FILE: APUE/4_16.c ================================================ #include "apue.h" #include int main(void){ if (open("tempfile", O_RDWR) < 0){ err_sys("open error"); } if (unlink("tempfile") < 0){ err_sys("unlink error"); } printf("file unlinked\n"); sleep(15); printf("done\n"); exit(0); } ================================================ FILE: APUE/4_21.c ================================================ #include "apue.h" #include int main(int argc, char* argv[]){ int i, fd; struct stat statbuf; struct timespec times[2]; for (i = 1; i < argc; i++){ if (stat(argv[1], &statbuf) < 0){ err_ret("%s : stat error", argv[1]); continue; } if ((fd = open(argv[1], O_RDWR | O_TRUNC)) < 0){ err_ret("%s : open error", argv[1]); continue; } times[0] = statbuf.st_atim; times[1] = statbuf.st_mtim; if (funtime(fd, times) < 0){ err_ret("%s : funtime error", argv[1]); } exit(0); } } ================================================ FILE: APUE/4_22.c ================================================ #include "apue.h" #include #include #include "2_16.c" typedef int Myfunc(const char *, const struct stat *, int); static Myfunc myfunc; static int myftw(char *, Myfunc *); static int dopath(Myfunc *); static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot; int main(int argc, char *argv[]){ int ret; if (argc != 2){ err_quit("usage : ftw "); } ret = myftw(argv[1], myfunc); ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock; if (ntot == 0){ ntot = 1; } printf("regular files = %7ld, %5.2f %%\n", nreg, nreg + 100.0 / ntot); printf("directories = %7ld, %5.2f %%\n", ndir, ndir + 100.0 / ntot); printf("block special = %7ld, %5.2f %%\n", nblk, nblk + 100.0 / ntot); printf("char special = %7ld, %5.2f %%\n", nchr, nchr + 100.0 / ntot); printf("FIFOs = %7ld, %5.2f %%\n", nfifo, nfifo + 100.0 / ntot); printf("symbolic links = %7ld, %5.2f %%\n", nslink, nslink + 100.0 / ntot); printf("sockets = %7ld, %5.2f %%\n", nsock, nsock + 100.0 / ntot); exit(ret); } #define FTW_F 1 #define FTW_D 2 #define FTW_DNR 3 #define FTW_NS 4 static char *fullpath; static size_t pathlen; static int myftw(char *pathname, Myfunc *func){ fullpath = path_alloc(&pathlen); if (pathlen <= strlen(pathname)){ pathlen = strlen(pathname) * 2; if ((fullpath = realloc(fullpath, pathlen)) == NULL){ err_sys("realloc failed"); } } strcpy(fullpath, pathname); return(dopath(func)); } static int dopath(Myfunc func){ struct stat statbuf; struct dirent *dirp; DIR *dp; int ret, n; if (lstat(fullpath, &statbuf) < 0){ return(func(fullpath, &statbuf, FTW_NS)); } if (S_ISDIR(statbuf.st_mode) == 0){ return(func(fullpath, &statbuf, FTW_F)); } if ((ret = func(fullpath, &statbuf, FTW_D)) != 0){ return(ret); } n = strlen(fullpath); if ((n + NAME_MAX + 2) > pathlen){ pathlen *= 2; if ((fullpath = realloc(fullpath, pathlen)) == NULL){ err_sys("realloc failed"); } } fullpath[n++] = '/'; fullpath[n] = 0; if ((dp = opendir(fullpath)) == NULL){ return(func(fullpath, &statbuf, FTW_DNR)); } while ((dirp = readdir(dp)) != NULL){ if (strcmp(dirp->d_name, ".") == 0 | strcmp(dirp->d_name, "..") == 0){ continue; } strcpy(&fullpath[n], dirp->d_name); if ((ret = dopath(func)) != 0){ break; } } fullpath[n-1] = 0; if (closedir(dp) < 0){ err_ret("can't close directory %s", fullpath); } return(ret); } static int myfunc(const char *pathname, const struct stat *statptr, int type){ switch (type){ case FTW_F: switch (statptr->st_mode & S_IFMT){ case S_IFREG: nreg++; break; case S_IFBLK: nblk++; break; case S_IFCHR: nchr++; break; case S_IFIFO: nfifo++; break; case S_IFLNK: nslink++; break; case S_IFSOCK: nsock++; break; case S_IFDIR: err_dump("for S_IFDIR for %s", pathname); } break; case FTW_D: ndir++; break; case FTW_DNR: err_ret("can't read directory %s", pathname); break; case FTW_NS: err_ret("stat error for %s", pathname); break; default: err_dump("unknown type %d for pathname %s", type, pathname); } return(0); } ================================================ FILE: APUE/4_23.c ================================================ #include "apue.h" int main(void){ if (chdir("/tmp") < 0){ err_sys("chdir failed"); } printf("chdir to /tmp succeeded\n"); exit(0); } ================================================ FILE: APUE/4_24.c ================================================ #include "apue.h" int main(void){ char *ptr; size_t size; if (chdir("/usr/spool/uucppublic") < 0){ err_sys("chdir failed"); } ptr = path_alloc(&size); if (getcwd(ptr, size) == NULL){ err_sys("getcwd failed"); } printf("%s\n", ptr); exit(0); } ================================================ FILE: APUE/4_25.c ================================================ #include "apue.h" #include int main(int argc, char *argv[]){ int i; struct stat buf; for (i = 1; i < argc; i++){ printf("%s : ", argv[i]); if (stat(argv[i], &buf) < 0){ err_ret("stat error"); continue; } printf("dev = %d/%d\n", MAJOR(buf.st_dev), MINOR(buf.st_dev)); if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)){ printf("(%s) rdev %d/%d\n", (S_ISCHR(buf.st_mode) ? "character" : "block", MAJOR(buf.st_rdev), MINOR(buf.st_rdev))); } printf("\n"); } exit(0); } ================================================ FILE: APUE/4_3.c ================================================ #include "apue.h" int main(int argc, char* argv[]){ int i; struct stat buf; char* ptr; for (i = 1; i < argc; i++){ printf("%s : ", argv[i]); if (lstat(argv[i], &buf) < 0){ err_ret("lstat error"); continue; } if (S_ISREG(buf.st_mode)){ ptr = "regular"; } else if (S_ISDIR(buf.st_mode)){ ptr = "directory"; } else if (S_ISCHR(buf.st_mode)){ ptr = "character special"; } else if (S_ISBLK(buf.st_mode)){ ptr = "block special"; } else if (S_ISFIFO(buf.st_mode)){ ptr = "fifo"; } else if (S_ISLNK(buf.st_mode)){ ptr = "symbolic link"; } else if (S_ISSOCK(buf.st_mode)){ ptr = "socket"; } else { ptr = "** unknown mode **"; } printf("%s\n", ptr); } exit(0); } ================================================ FILE: APUE/4_8.c ================================================ #include "apue.h" #include int main(int argc, char* argv[]){ if (argc != 2){ err_quit("usage : main "); } if (access(argv[1], R_OK) < 0){ err_ret("access error for %s", argv[1]); } else{ printf("read access OK\n"); } if (open(argv[1], O_RDONLY) < 0){ err_ret("open error for %s", argv[1]); } else{ printf("open for reading OK\n"); } exit(0); } ================================================ FILE: APUE/4_9.c ================================================ #include "apue.h" #include #define RWRWRW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) int main(void){ umask(0); if (creat("foo", RWRWRW) < 0){ err_sys("creat error for foo"); } umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (creat("bar", RWRWRW) < 0){ err_sys("creat error for bar"); } exit(0); } ================================================ FILE: APUE/5_11.c ================================================ #include "apue.h" void pr_stdio(const char *, FILE *); int is_unbuffered(FILE *); int is_linebuffered(FILE *); int buffer_size(FILE *); int main(void){ FILE *fp; fputs("enter any character\n", stdout); if (getchar() == EOF){ err_sys("getchar error"); } fputs("one line to standard error\n", stderr); pr_stdio("stdin", stdin); pr_stdio("stdout", stdout); pr_stdio("stderr", stderr); if ((fp = fopen("/etc/passwd", "r")) == NULL){ err_sys("fopen error"); } if (getc(fp) == EOF){ err_sys("getc error"); } pr_stdio("/etc/passwd", fp); exit(0); } void pr_stdio(const char *name, FILE *fp){ printf("stream = %s, ", name); if (is_unbuffered(fp)){ printf("unbuffered"); } else if (is_linebuffered(fp)){ printf("line buffered"); } else{ printf("full buffered"); } printf(", buffer size = %d\n", buffer_size(fp)); } int is_unbuffered(FILE *fp){ return(fp->_flags & _IONBF); } int is_linebuffered(FILE *fp){ return(fp->_flags & _IOLBF); } int buffer_size(FILE *fp){ return(fp->_IO_buf_end - fp->_IO_buf_base); } ================================================ FILE: APUE/5_12.c ================================================ #include "apue.h" int main(void){ char name[L_tmpnam], line[MAXLINE]; FILE *fp; printf("%s\n", tmpnam(NULL)); tmpnam(name); printf("%s\n", name); if ((fp = tmpfile()) == NULL){ err_sys("tmpfile error"); } fputs("one line of output\n", fp); rewind(fp); if (fgets(line, sizeof(line), fp) == NULL){ err_sys("fgets error"); } fputs(line, stdout); exit(0); } ================================================ FILE: APUE/5_13.c ================================================ #include "apue.h" void make_tmp(char *template); int main(void){ char good_template[] = "/tmp/dirXXXXXX"; char *bad_template = "/tmp/dirXXXXXX"; printf("trying to create first temp file...\n"); make_tmp(good_template); printf("trying to create second temp file...\n"); make_tmp(bad_template); exit(0); } void make_tmp(char *template){ int fd; struct stat statbuf; if ((fd = mkstemp(template)) < 0){ err_sys("mkstemp error"); } printf("temp name = %s\n", template); close(fd); if (stat(template, &statbuf) < 0){ if (errno == ENOENT){ printf("file doesn't exist\n"); } else{ err_sys("stat error"); } } else{ printf("file exist\n"); unlink(template); } } ================================================ FILE: APUE/5_15.c ================================================ #include "apue.h" #define BSZ 48 int main(void){ FILE *fp; char buf[BSZ]; memset(buf, 'a', BSZ-2); buf[BSZ-2] = '\0'; buf[BSZ-1] = 'X'; if ((fp = fmemopen(buf, BSZ, "w+")) == NULL){ err_sys("fmemopen error"); } printf("initial buffer contents : %s\n", buf); fprintf(fp, "hello world"); printf("before flush : %s\n", buf); fflush(fp); printf("after flush : %s\n", buf); printf("length of string in buffer = %ld\n", (long)strlen(buf)); memset(buf, 'b', BSZ-2); buf[BSZ-2] = '\0'; buf[BSZ-1] = 'X'; fprintf(fp, "hello world"); fseek(fp, 0, SEEK_SET); printf("after fseek : %s\n", buf); printf("length of string in buffer = %ld\n", (long)strlen(buf)); memset(buf, 'c', BSZ-2); buf[BSZ-2] = '\0'; buf[BSZ-1] = 'X'; fprintf(fp, "hello world"); fclose(fp); printf("after fclose : %s\n", buf); printf("length of string in buffer = %ld\n", (long)strlen(buf)); return(0); } ================================================ FILE: APUE/5_4.c ================================================ #include "apue.h" int main(void){ int c; while ((c = getc(stdin)) != EOF){ if (putc(c, stdout) == EOF){ err_sys("output error"); } } if (ferror(stdin)){ err_sys("input error"); } exit(0); } ================================================ FILE: APUE/5_5.c ================================================ #include "apue.h" int main(void){ char buf[MAXLINE]; while (fgets(buf, MAXLINE, stdin) != NULL){ if (fputs(buf, stdout) == EOF){ err_sys("output error"); } } if (ferror(stdin)){ err_sys("input error"); } exit(0); } ================================================ FILE: APUE/6_11.c ================================================ #include #include #include int main(void){ time_t t; struct tm *tmp; char buf1[16]; char buf2[64]; time(&t); tmp = localtime(&t); if (strftime(buf1, 16, "time and date : %r, %a %b %d, %Y", tmp) == 0){ printf("buffer length 16 is too small\n"); } else{ printf("%s\n", buf1); } if (strftime(buf2, 64, "time and date : %r, %a %b %d, %Y", tmp) == 0){ printf("buffer length 64 is too small\n"); } else{ printf("%s\n", buf2); } exit(0); } ================================================ FILE: APUE/6_2.c ================================================ #include #include #include struct passwd *getpwnam(const char *name){ struct passwd *ptr; setpwent(); while ((ptr = getpwent) != NULL){ if (strcmp(name, ptr->pw_name) == 0){ break; } } endpwent(); return(ptr); } ================================================ FILE: APUE/7_1.c ================================================ #include int main(void){ printf("hello world\n"); } ================================================ FILE: APUE/7_13.c ================================================ #include "apue.h" #include static void f1(int, int, int, int); static void f2(void); static jmp_buf jmpbuffer; static int globval; int main(void){ int autoval; register int regival; volatile int volaval; static int statval; globval = 1; autoval = 2; regival = 3; volaval = 4; statval = 5; if (setjmp(jmpbuffer) != 0){ printf("after longjmp : \n"); printf("globval = %d, autoval = %d, regival = %d, volaval = %d, statval = %d\n", globval, autoval, regival, volaval, statval); exit(0); } globval = 95; autoval = 96; regival = 97; volaval = 98; statval = 99; f1(autoval, regival, volaval, statval); exit(0); } static void f1(int i, int j, int k, int l){ printf("in f1() : \n"); printf("globval = %d, autoval = %d, regival = %d, volaval = %d, statval = %d\n", globval, i, j, k, l); f2(); } static void f2(){ longjmp(jmpbuffer, 1); } ================================================ FILE: APUE/7_16.c ================================================ #include "apue.h" #include #define doit(name) pr_limits(#name, name) static void pr_limits(char *, int); int main(void){ #ifdef RLIMIT_AS doit(RLIMIT_AS); #endif doit(RLIMIT_CORE); doit(RLIMIT_CPU); doit(RLIMIT_DATA); doit(RLIMIT_FSIZE); #ifdef RLIMIT_MEMLOCK doit(RLIMIT_MEMLOCK); #endif #ifdef RLIMIT_MSGQUEUE doit(RLIMIT_MSGQUEUE); #endif #ifdef RLIMIT_NICE doit(RLIMIT_NICE); #endif doit(RLIMIT_NOFILE); #ifdef RLIMIT_NPROC doit(RLIMIT_NPROC); #endif #ifdef RLIMIT_NPTS doit(RLIMIT_NPTS); #endif #ifdef RLIMIT_RSS doit(RLIMIT_RSS); #endif #ifdef RLIMIT_SBSIZE doit(RLIMIT_SBSIZE); #endif #ifdef RLIMIT_SIGPENDING doit(RLIMIT_SIGPENDING); #endif doit(RLIMIT_STACK); #ifdef RLIMIT_SWAP doit(RLIMIT_SWAP); #endif #ifdef RLIMIT_VMEM doit(RLIMIT_VMEM); #endif exit(0); } static void pr_limits(char *name, int resource){ struct rlimit limit; unsigned long long lim; if (getrlimit(resource, &limit) < 0){ err_sys("getrlimit error"); } printf("%-14s ", name); if (limit.rlim_cur == RLIM_INFINITY){ printf("(infinity) "); } else{ lim = limit.rlim_cur; printf("%10lld ", lim); } if (limit.rlim_max == RLIM_INFINITY){ printf("(infinity) "); } else{ lim = limit.rlim_max; printf("%10lld ", lim); } putchar((int)'\n'); } ================================================ FILE: APUE/7_3.c ================================================ #include "apue.h" static void my_exit1(void); static void my_exit2(void); int main(void){ if (atexit(my_exit2) != 0){ err_sys("can't register my_exit2"); } if (atexit(my_exit1) != 0){ err_sys("can't register my_exit1"); } if (atexit(my_exit1) != 0){ err_sys("can't register my_exit1"); } printf("main is done\n"); return(0); } static void my_exit1(void){ printf("first exit handler\n"); } static void my_exit2(void){ printf("second exit handler\n"); } ================================================ FILE: APUE/7_4.c ================================================ #include "apue.h" int main(int argc, char *argv[]){ int i; for (i = 0; i < argc; i++){ printf("argv[%d] : %s\n", i, argv[i]); } exit(0); } ================================================ FILE: APUE/8_1.c ================================================ #include "apue.h" int globvar = 6; char buf[] = "a write to stdout\n"; int main(void){ int var; pid_t pid; var = 88; if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf) - 1){ err_sys("write error"); } printf("before fork : \n"); if ((pid = fork()) < 0){ err_sys("fork error"); } else if (pid == 0){ globvar++; var++; } else{ sleep(2); } printf("pid = %ld, glob = %d, var = %d\n", (long)getpid(), globvar, var); exit(0); } ================================================ FILE: APUE/8_12.c ================================================ #include "apue.h" static void charactatime(char *); int main(void){ pid_t pid; if ((pid = fork()) < 0){ err_sys("fork error"); } else if (pid == 0){ charactatime("output from child\n"); } else{ charactatime("output from parent\n"); } exit(0); } static void charactatime(char *str){ char* ptr; int c; setbuf(stdout, NULL); for (ptr = str; (c = *ptr++) != 0;){ putc(c, stdout); } } ================================================ FILE: APUE/8_3.c ================================================ #include "apue.h" int globvar = 6; int main(void){ int var; pid_t pid; var = 88; printf("before fork : \n"); if ((pid = vfork()) < 0){ err_sys("vfork error"); } else if (pid == 0){ globvar++; var++; _exit(0); } printf("pid = %ld, glob = %d, var = %d\n", (long)getpid(), globvar, var); exit(0); } ================================================ FILE: APUE/8_5.c ================================================ #include "apue.h" #include void pr_exit(int status){ if (WIFEXITED(status)){ printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)){ printf("abnormal termination, signal number = %d%s\n", WTERMSIG(status), #ifdef WCOREDUMP WCOREDUMP(status) ? " (core file generated )" : ""); #else ""); #endif } else if (WIFSTOPPED(status)){ printf("child stopped, signal number = %d\n", WSTOPSIG(status)); } } ================================================ FILE: APUE/8_6.c ================================================ #include "apue.h" #include void pr_exit(int status); int main(void){ pid_t pid; int status; if ((pid = fork()) < 0){ err_sys("fork error"); } else if(pid == 0){ exit(7); } if (wait(&status) != pid){ err_sys("wait error"); } pr_exit(status); if ((pid = fork()) < 0){ err_sys("fork error"); } else if(pid == 0){ abort(); } if (wait(&status) != pid){ err_sys("wait error"); } pr_exit(status); if ((pid = fork()) < 0){ err_sys("fork error"); } else if(pid == 0){ status /= 0; } if (wait(&status) != pid){ err_sys("wait error"); } pr_exit(status); exit(0); } void pr_exit(int status){ if (WIFEXITED(status)){ printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)){ printf("abnormal termination, signal number = %d%s\n", WTERMSIG(status), #ifdef WCOREDUMP WCOREDUMP(status) ? " (core file generated )" : ""); #else ""); #endif } else if (WIFSTOPPED(status)){ printf("child stopped, signal number = %d\n", WSTOPSIG(status)); } } ================================================ FILE: APUE/8_8.c ================================================ #include "apue.h" #include int main(void){ pid_t pid; if ((pid = fork()) < 0){ err_sys("fork error"); } else if (pid == 0){ if ((pid = fork()) < 0){ err_sys("fork error"); } else if (pid > 0){ exit(0); } sleep(2); printf("second child, parent pid = %ld\n", (long)getppid()); exit(0); } if (waitpid(pid, NULL, 0) != pid){ err_sys("waitpid error"); } exit(0); } ================================================ FILE: APUE/FuncPointer.c ================================================ #include typedef int Myfunc(int a, int b); Myfunc myfunc; int myftw(int , int , Myfunc *); int main(void){ int x = 3; int y = 4; int ret = myftw(&x, &y, &myfunc); printf("%d\n", ret); return 0; } int myfunc(int a, int b){ return (a < b); } int myftw(int a, int b, Myfunc *func){ return func(a, b); } ================================================ FILE: Makefile ================================================ # Find out all the include and source files for SRC_FILE := $(wildcard *.c) OBJ_FILE := $(subst .c,.o,$(SRC_FILE)) TARGET := main CFLAGS := -std=c99 .PHONY: all all: $(TARGET) $(TARGET): $(OBJ_FILE) $(CC) -o $@ $^ %.o: %.c $(CC) -o $@ -c $< $(CFLAGS) clean: rm $(OBJ_FILE) $(TARGET) -f ================================================ FILE: README.md ================================================ # apue Advanced Programming in the UNIX