Home | History | Annotate | Download | only in lib
      1 /*
      2  * fopendev.c
      3  */
      4 
      5 #include <stdio.h>
      6 #include <unistd.h>
      7 #include <fcntl.h>
      8 
      9 FILE *fopendev(const struct dev_info * dev, const char *mode)
     10 {
     11     int flags = O_RDONLY;
     12     int plus = 0;
     13     int fd;
     14 
     15     while (*mode) {
     16 	switch (*mode) {
     17 	case 'r':
     18 	    flags = O_RDONLY;
     19 	    break;
     20 	case 'w':
     21 	    flags = O_WRONLY | O_CREAT | O_TRUNC;
     22 	    break;
     23 	case 'a':
     24 	    flags = O_WRONLY | O_CREAT | O_APPEND;
     25 	    break;
     26 	case '+':
     27 	    plus = 1;
     28 	    break;
     29 	}
     30 	mode++;
     31     }
     32 
     33     if (plus) {
     34 	flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
     35     }
     36 
     37     fd = opendev(file, flags);
     38 
     39     if (fd < 0)
     40 	return NULL;
     41     else
     42 	return fdopen(fd, mode);
     43 }
     44