Saturday, August 16, 2014

Unix Prog: File I/O(2)

1. Open file with "Write Only"

fileio.c:
 #include<unistd.h>  
 #include<fcntl.h>  
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<errno.h>  
   
 int main()  
 {  
  char buf[4096];  
  int fd;  
  int read_size;  
  int write_size;  
   
  fd = open("test.txt", O_WRONLY);  
  printf("file descriptor: %d\n", fd);  
  if(fd < 0) printf("Error!\n");  
  errno = 0;  
  read_size = read(fd, buf, 12);  
  printf("read size: %d errno: %d\n", read_size, errno);  
  errno = 0;  
  write_size = write(fd, buf, 12);  
  printf("write size: %d errno: %d\n", write_size, errno);  
   
  close(fd);  
 }  

shell:
1) When test.txt is not existed yet, io.out will open the non-existing test.txt, and get the -1 as file descriptor, indicating open failed.
2) create test.txt
3) io.out open the test.txt, return the file descriptor 3, indicating open file successfully. But since it is opened with "O_WRONLY", so read failed, write succeeded.
If it is opened with "O_RDONLY", then only read operation is allowed
If it is opened with "O_RDWR", then both read and write operation are allowed.

 ubuntu@ip-172-31-23-227:~$ ./io.out  
 file descriptor: -1  
 Error!  
 read size: -1 errno: 9  
 write size: -1 errno: 9  
 ubuntu@ip-172-31-23-227:~$ echo "Hello world!" > test.txt  
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 file descriptor: 3  
 read size: -1 errno: 9  
 write size: 12 errno: 0  

errno: 9
 ubuntu@ip-172-31-23-227:~$ less /usr/include/asm-generic/errno-base.h  
 ......  
 #define EBADF      9   /* Bad file number */  
 ......  

2. create a file with creat
fileio.c
create a file named "test.txt" and write some information inside, then read some information out.
 #include<unistd.h>  
 #include<fcntl.h>  
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<errno.h>  
   
 int main()  
 {  
  char buf[4096];  
  int fd;  
  int read_size;  
  int write_size;  
   
  fd = creat("test.txt", S_IRWXU);  
  printf("file descriptor: %d\n", fd);  
  if(fd < 0) printf("Error!\n");  
   
  errno = 0;  
  write_size = write(fd, buf, 12);  
  printf("write size: %d errno: %d\n", write_size, errno);  
   
  errno = 0;  
  read_size = read(fd, buf, 12);  
  printf("read size: %d errno: %d\n", read_size, errno);  
   
  close(fd);  
 }  

shell:
It successfully create the file and write information inside, but file created by "creat" api is only for "write only", so read operation failed.
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 file descriptor: 3  
 write size: 12 errno: 0  
 read size: -1 errno: 9     

creat api's second argument specify the permission bit:
S_IRWXU: file owner has the READ WRITE EXECUTE permission
S_IRUSER(S_IREAD): file owner has the READ permission
S_IWUSER(S_IWRITE): file owner has the WRITE permission
S_IXUSER(S_IEXEC): file owner has the EXECUTE permission
S_IRWXG: group owner has the READ WRITE EXECUTE permission
S_IRGRP: group owner has the READ permission
S_IWGRP: group owner has the WRITE permission
S_IXGRP: group owner has the EXECUTE permission
S_IRWXO: others have the READ WRITE EXECUTE permission
S_IROTH: others have the READ permission
S_IWOTH: others have the WRITE permission
S_IXOTH: others have the EXECUTE permission

they are defined at stat.h and fcntl.h:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/x86_64-linux-gnu/bits/stat.h  
 ......  
 #define __S_IREAD    0400  /* Read by owner. */  
 #define __S_IWRITE   0200  /* Write by owner. */  
 #define __S_IEXEC    0100  /* Execute by owner. */  
 ......  
 ubuntu@ip-172-31-23-227:~$ less /usr/include/fcntl.h  
 ......  
 # include <bits/stat.h>  
 ......  
 # define S_IRUSR    __S_IREAD    /* Read by owner. */  
 # define S_IWUSR    __S_IWRITE   /* Write by owner. */  
 # define S_IXUSR    __S_IEXEC    /* Execute by owner. */  
 /* Read, write, and execute by owner. */  
 # define S_IRWXU    (__S_IREAD|__S_IWRITE|__S_IEXEC)  
   
 # define S_IRGRP    (S_IRUSR >> 3) /* Read by group. */  
 # define S_IWGRP    (S_IWUSR >> 3) /* Write by group. */  
 # define S_IXGRP    (S_IXUSR >> 3) /* Execute by group. */  
 /* Read, write, and execute by group. */  
 # define S_IRWXG    (S_IRWXU >> 3)  
   
 # define S_IROTH    (S_IRGRP >> 3) /* Read by others. */  
 # define S_IWOTH    (S_IWGRP >> 3) /* Write by others. */  
 # define S_IXOTH    (S_IXGRP >> 3) /* Execute by others. */  
 /* Read, write, and execute by others. */  
 # define S_IRWXO    (S_IRWXG >> 3)  
 ......  

No comments:

Post a Comment