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