home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume20 / rc / part04 / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-22  |  772 b   |  33 lines

  1. /* open.c: to insulate <fcntl.h> from the rest of rc. */
  2.  
  3. #include <fcntl.h>
  4. #include "lex.h"
  5. #include "open.h"
  6.  
  7. /* prototype for open() follows. comment out if necessary */
  8.  
  9. /*extern int open(const char *, int,...);*/
  10. extern void rc_error(const char *);
  11.  
  12. /*
  13.    Opens a file with the necessary flags. Assumes the following
  14.    declaration for enum redirtype:
  15.  
  16.     enum redirtype {
  17.         FROM, CREATE, APPEND, HEREDOC, HERESTRING
  18.     };
  19. */
  20.  
  21. static const int mode_masks[] = {
  22.     /* read */    O_RDONLY,
  23.     /* create */    O_TRUNC | O_CREAT | O_WRONLY,
  24.     /* append */    O_APPEND | O_CREAT | O_WRONLY
  25. };
  26.  
  27. int rc_open(const char *name, enum redirtype m) {
  28.     if ((unsigned int) m >= (sizeof(mode_masks)/sizeof(int)))
  29.         rc_error("bad mode passed to rc_open");
  30.  
  31.     return open(name, mode_masks[m], 0644);
  32. }
  33.