APUE Learning Example Source Code
guowenxue
2018-10-18 b9d884addfe5f9da3ab6936ae9d53cf42a8503be
Add charpter1 file I/O example source code
7 files added
229 ■■■■■ changed files
ch1/access.c 27 ●●●●● patch | view | raw | blame | history
ch1/dir.c 78 ●●●●● patch | view | raw | blame | history
ch1/file_io.c 44 ●●●●● patch | view | raw | blame | history
ch1/redirect_stdio.c 28 ●●●●● patch | view | raw | blame | history
ch1/stat.c 15 ●●●●● patch | view | raw | blame | history
ch1/stdout.c 14 ●●●●● patch | view | raw | blame | history
ch1/syserr.c 23 ●●●●● patch | view | raw | blame | history
ch1/access.c
New file
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <unistd.h>
#define TEST_FILE "access.c"
int main(void)
{
    if( access(TEST_FILE, F_OK)!=0 )
    {
        printf("File %s not exist!\n", TEST_FILE);
        return 0;
    }
    printf("File %s exist!\n", TEST_FILE);
    if(access(TEST_FILE, R_OK)==0)
        printf("READ OK\n");
    if(access(TEST_FILE, W_OK)==0)
        printf("WRITE OK\n");
    if(access(TEST_FILE, X_OK)==0)
        printf("EXEC OK\n");
    return 0;
}
ch1/dir.c
New file
@@ -0,0 +1,78 @@
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#define TEST_DIR "dir"
int main(int argc, char **argv)
{
    int               rv;
    int               fd1;
    int               fd2;
    DIR              *dirp;
    struct dirent    *direntp;
    if( mkdir(TEST_DIR, 0755)<0 )
    {
        printf("create directory '%s' failure: %s\n", TEST_DIR, strerror(errno));
        return -1;
    }
    if( chdir(TEST_DIR)<0 )
    {
        printf("Change directory to '%s' failure: %s\n", TEST_DIR, strerror(errno));
        rv = -2;
        goto cleanup;
    }
    if( (fd1=creat("file1.txt", 0644)) < 0 )
    {
        printf("Create file1.txt failure: %s\n", strerror(errno));
        rv = -3;
        goto cleanup;
    }
    if( (fd2=creat("file2.txt", 0644)) < 0 )
    {
        printf("Create file2.txt failure: %s\n", strerror(errno));
        rv = -4;
        goto cleanup;
    }
    if( chdir("../")<0 )
    {
        printf("Change directory to '%s' failure: %s\n", TEST_DIR, strerror(errno));
        rv = -5;
        goto cleanup;
    }
    if((dirp=opendir(TEST_DIR)) == NULL)
    {
        rv = -6;
        printf("opendir %s error: %s\n", TEST_DIR, strerror(errno));
        goto cleanup;
    }
    while((direntp = readdir(dirp)) != NULL)
    {
        printf("Find file: %s\n", direntp->d_name);
    }
    closedir(dirp);
cleanup:
    if(fd1 >= 0)
    {
        close(fd1);
    }
    if(fd2 >= 0)
    {
        close(fd2);
    }
}
ch1/file_io.c
New file
@@ -0,0 +1,44 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUFSIZE 1024
#define MSG_STR "Hello World\n"
int main(int argc, char *argv[])
{
    int            fd = -1;
    int            rv = -1;
    char           buf[BUFSIZE];
    fd=open("test.txt", O_RDWR|O_CREAT|O_TRUNC, 0666);
    if(fd < 0)
    {
        perror("Open/Create file test.txt failure");
        return 0;
    }
    printf("Open file returned file descriptor [%d]\n", fd);
    if( (rv=write(fd, MSG_STR, strlen(MSG_STR))) < 0 )
    {
        printf("Write %d bytes into file failure: %s\n", rv, strerror(errno));
        goto cleanup;
    }
    //memset(buf, 0, sizeof(buf));
    if( (rv=read(fd, buf, sizeof(buf))) < 0 )
    {
        printf("Read data from file failure: %s\n", strerror(errno));
        goto cleanup;
    }
    printf("Read %d bytes data from file: %s\n", rv, buf);
cleanup:
    close(fd);
    return 0;
}
ch1/redirect_stdio.c
New file
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv)
{
    int                 fd = -1;
    fd = open("std.txt", O_RDWR|O_CREAT|O_TRUNC, 0666);
    if(fd < 0)
    {
        printf("Open file failure: %s\n", strerror(errno));
        return ;
    }
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    printf("fd=%d\n", fd);
    close(fd);
}
ch1/stat.c
New file
@@ -0,0 +1,15 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main (int argc, char **argv)
{
    struct   stat   stbuf;
    stat("stat.c", &stbuf);
    printf("File Mode: %o Real Size: %luB, Space Size: %luB\n", stbuf.st_mode, stbuf.st_size, stbuf.st_blksize);
    return 0;
}
ch1/stdout.c
New file
@@ -0,0 +1,14 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define MSG_STR "Hello World\n"
int main(int main, char *argv[])
{
    printf("%s", MSG_STR);
    fputs(MSG_STR, stdout);
    write(STDOUT_FILENO, MSG_STR, strlen(MSG_STR));
    return 0;
}
ch1/syserr.c
New file
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
  char   *file_name = "/test.txt";
  int    fd=-1;
  fd=open(file_name, O_RDONLY, 066);
  if( fd < 0 )
  {
      perror("Open file failure");
      printf("Open file %s failure: %s\n", file_name, strerror(errno));
      return 0;
  }
  close(fd);
}