From b9d884addfe5f9da3ab6936ae9d53cf42a8503be Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Thu, 18 Oct 2018 12:34:24 +0800
Subject: [PATCH] Add charpter1 file I/O example source code

---
 ch1/file_io.c        |   44 +++++++++++
 ch1/syserr.c         |   23 +++++
 ch1/access.c         |   27 ++++++
 ch1/dir.c            |   78 +++++++++++++++++++
 ch1/stat.c           |   15 +++
 ch1/redirect_stdio.c |   28 +++++++
 ch1/stdout.c         |   14 +++
 7 files changed, 229 insertions(+), 0 deletions(-)

diff --git a/ch1/access.c b/ch1/access.c
new file mode 100644
index 0000000..46c2b0d
--- /dev/null
+++ b/ch1/access.c
@@ -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;
+}
diff --git a/ch1/dir.c b/ch1/dir.c
new file mode 100644
index 0000000..e62a56e
--- /dev/null
+++ b/ch1/dir.c
@@ -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);
+    }
+
+}
diff --git a/ch1/file_io.c b/ch1/file_io.c
new file mode 100644
index 0000000..cc1f391
--- /dev/null
+++ b/ch1/file_io.c
@@ -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;
+}
diff --git a/ch1/redirect_stdio.c b/ch1/redirect_stdio.c
new file mode 100644
index 0000000..52270f0
--- /dev/null
+++ b/ch1/redirect_stdio.c
@@ -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);
+}
+
diff --git a/ch1/stat.c b/ch1/stat.c
new file mode 100644
index 0000000..bf3ba9d
--- /dev/null
+++ b/ch1/stat.c
@@ -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;
+}
+
diff --git a/ch1/stdout.c b/ch1/stdout.c
new file mode 100644
index 0000000..bc11354
--- /dev/null
+++ b/ch1/stdout.c
@@ -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;
+}
diff --git a/ch1/syserr.c b/ch1/syserr.c
new file mode 100644
index 0000000..75a34ac
--- /dev/null
+++ b/ch1/syserr.c
@@ -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);
+}

--
Gitblit v1.9.1