home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / CCDL122.ZIP / CLIBS / IO / FOPEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-27  |  1.9 KB  |  103 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <libp.h>
  6.  
  7. extern FILE *_pstreams[_NFILE_];
  8. extern char *_filenames[_NFILE_];
  9. extern int maxfiles;
  10.  
  11. FILE *_basefopen(const char *name, const char *mode,FILE *stream)
  12. {
  13.     int flags = 0,append = 0, update = 0, id = 0,i;
  14.     FILE *file;
  15.     char *buf,*fname;
  16.     if (maxfiles == _NFILE_)
  17.         return 0;
  18.     for (i=0; i < strlen(mode); i++) {
  19.         switch (mode[i]) {
  20.             case 'r':
  21.                 flags |= _F_READ;
  22.                 break;
  23.             case 'w':
  24.                 flags |= _F_WRIT;
  25.                 break;
  26.             case 'a':
  27.                 flags |= _F_WRIT;
  28.                 append = 1;
  29.                 break;
  30.             case '+':
  31.                 update = 1;
  32.                 break;
  33.             case 'b':
  34.                 flags |= _F_BIN;
  35.                 break;
  36.             default:
  37.                 return 0;
  38.         }
  39.     }
  40.     fname = malloc(strlen(name)+1);
  41.     if (!fname) 
  42.         return 0;
  43.     strcpy(fname,name);
  44.     if (stream)
  45.         file = stream;
  46.     else
  47.         if ((file = malloc(sizeof(FILE))) == 0) {
  48.             free(fname);
  49.             return 0;
  50.         }
  51.     file->flags = 0;
  52.     buf = malloc(BUFSIZ);
  53.     if (!buf) {
  54.         free(fname);
  55.         free(file);
  56.         return 0;
  57.     }
  58.     switch (flags & (_F_READ | _F_WRIT)) {
  59.         case 0:
  60.             goto nofile;
  61.         case _F_READ:
  62.             if (update)
  63.                 flags |= _F_WRIT;
  64.              id = _ll_open(name,    _ll_flags(flags));
  65.             break;
  66.         case _F_WRIT:
  67.             if (update)
  68.                 flags |= _F_READ;
  69.             id = _ll_creat(name,_ll_flags(flags));
  70.             break;
  71.         case _F_READ | _F_WRIT:
  72.             return 0;
  73.     }
  74.     if (id == 0)
  75.         goto nofile;
  76.     file->token = FILTOK;
  77.     file->level = 0;
  78.     file->fd = (char) id;
  79.     file->flags |= flags;
  80.     file->istemp = 0;
  81.     file->hold = 0;
  82.     if (flags & _F_BIN)
  83.         setvbuf(file,buf,_IOFBF,BUFSIZ);
  84.     else
  85.         setvbuf(file,buf,_IOLBF,BUFSIZ);
  86.     flags |= _F_BUF;
  87.     if (append) {
  88.         if (fseek(file,0,SEEK_END)) {
  89. nofile:
  90.             free(fname);
  91.             free(file->buffer);
  92.             free(file);
  93.             return 0;
  94.         }
  95.     }
  96.     _filenames[maxfiles] = fname;
  97.     _pstreams[maxfiles++] = file;
  98.     return file;
  99. }
  100. FILE *fopen(const char *name, const char *mode)
  101. {
  102.     return _basefopen(name,mode,0);
  103. }