home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 387b.lha / dice_v2.02 / lib / stdio / fopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-03  |  2.6 KB  |  141 lines

  1.  
  2. /*
  3.  *  FOPEN.C        fopen freopen fdopen
  4.  *
  5.  *  (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  6.  *
  7.  *  modes:    a[b][+]     append [binary] [+ignored]
  8.  *        r[b][+]     read [binary] [update-allow writes]
  9.  *        w[b][+]     write [binary] [uupdate-allow reads]
  10.  *
  11.  *        a always appends- write only
  12.  *        r file must exist '+' means can write too.
  13.  *        w file always create/truncate
  14.  *        b binary (ignored)
  15.  *
  16.  *        F INTERNAL, user should never specify.    Used by fdopen.
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23.  
  24. FILE *
  25. fopen(name, modes)
  26. const char *name;
  27. const char *modes;
  28. {
  29.     FILE *fi = malloc(sizeof(FILE));
  30.  
  31.     if (fi) {
  32.     _slow_bzero(fi, sizeof(FILE));
  33.     if (freopen(name, modes, fi) == NULL) {
  34.         free(fi);
  35.         fi = NULL;
  36.     }
  37.     }
  38.     return(fi);
  39. }
  40.  
  41. FILE *
  42. fdopen(fd, modes)
  43. int fd;
  44. const char *modes;
  45. {
  46.     char buf[16];
  47.  
  48.     if (strlen(modes) < 14) {
  49.     buf[0] = 'F';
  50.     strcpy(buf + 1, modes);
  51.     return(fopen((char *)fd, buf);
  52.     }
  53.     return(NULL);
  54. }
  55.  
  56. FILE *
  57. freopen(name, modes, fi)
  58. const char *name;
  59. const char *modes;
  60. FILE *fi;
  61. {
  62.     short fdmode = 0;
  63.     short fimode = 0;
  64.     short fdOpen = 0;
  65.  
  66.     if (fi == stdin || fi == stdout || fi == stderr)
  67.     fimode |= __SIF_NOFREE;
  68.  
  69.     {
  70.     char c;
  71.     while (c = *modes) {
  72.         if (c == 'r') {
  73.         fdmode |= O_RDONLY;
  74.         fimode |= __SIF_READ;
  75.         }
  76.         if (c == 'w') {
  77.         fdmode |= O_WRONLY | O_CREAT | O_TRUNC;
  78.         fimode |= __SIF_WRITE;
  79.         }
  80.         if (c == '+') {
  81.         fdmode |= O_RDWR;
  82.         fdmode &= ~(O_RDONLY|O_WRONLY);
  83.         fimode |= __SIF_READ | __SIF_WRITE;
  84.         }
  85.         if (c == 'b') {
  86.         fdmode |= O_BINARY;
  87.         fimode |= __SIF_BINARY;
  88.         }
  89.         if (c == 'a') {
  90.         fdmode |= O_CREAT | O_APPEND;
  91.         fimode |= __SIF_WRITE | __SIF_APPEND;
  92.         }
  93.         if (c == 'F')       /*  INTERNAL    */
  94.         fdOpen = 1;
  95.         if (c == 'C')       /*  INTERNAL    */
  96.         fimode |= __SIF_REMOVE;
  97.         ++modes;
  98.     }
  99.     }
  100.     if (fi) {
  101.     if (fi->sd_Flags & __SIF_OPEN)
  102.         __fclose(fi);
  103.     _slow_bzero(fi, sizeof(FILE));
  104.     if (fdOpen)
  105.         fi->sd_Fd = (int)name;
  106.     else
  107.         fi->sd_Fd = open(name, fdmode, 0666);
  108.  
  109.     if (fi->sd_Fd >= 0) {
  110.         fi->sd_UC = -1;
  111.  
  112.         if ((fimode & __SIF_NOFREE) == 0) {
  113.         fi->sd_Next = _Iod;
  114.         fi->sd_Prev = &_Iod;
  115.         if (fi->sd_Next)
  116.             fi->sd_Next->sd_Prev = &fi->sd_Next;
  117.         _Iod = fi;
  118.         }
  119.         {
  120.         long pos = lseek(fi->sd_Fd, 0, 1);
  121.  
  122.         if (pos >= 0) {
  123.             fimode |= __SIF_FILE;
  124.             fi->sd_Offset = pos;
  125.         }
  126.         }
  127.  
  128.         fi->sd_BufSiz = BUFSIZ;
  129.         fi->sd_Flags = __SIF_OPEN | fimode;
  130.         fi->sd_RLeft = -1;
  131.         fi->sd_WLeft = -1;
  132.         if (fimode & __SIF_REMOVE)
  133.         fi->sd_Name = name;    /*  name assumed to be malloc'd */
  134.     } else {
  135.         return(NULL);
  136.     }
  137.     }
  138.     return(fi);
  139. }
  140.  
  141.