home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / utilslib / utils_old / c / file < prev    next >
Encoding:
Text File  |  1994-02-22  |  3.3 KB  |  147 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "sys/file.h"
  4.  
  5. int __file_debug = (0!=0);
  6. char __file_lastfname[256] = { '\0' };
  7. FILE *__file_lastfile = NULL;
  8.  
  9. int fileno(FILE *f) {
  10.   if (f == stdin) return(0);
  11.   if (f == stdout) return(1);
  12.   if (f == stderr) return(2);
  13.   return((int)f);
  14. }
  15.  
  16. static int fdtofile(int fd) {
  17.   if (fd == 0) return(stdin);
  18.   if (fd == 1) return(stdout);
  19.   if (fd == 2) return(stderr);
  20.   return((FILE *)fd);
  21. }
  22.  
  23. int unlink(char *s) {
  24.   return(remove(s));
  25. }
  26.  
  27. int fdopen(int fd, char *mode) {
  28.   if (fd != (int)__file_lastfile) return(-1);  /* Can't remember this one */
  29.   fclose((FILE *)fd);
  30.   fd = (int)fopen(__file_lastfname, mode);
  31.   if (fd == 0) return(-1);
  32.   return((int)fd);
  33. }
  34.  
  35. int creat(char *fname, int mode) {
  36. FILE *f;
  37.   if (__file_debug) {
  38.     fprintf(stderr, "open: creat(\"%s\", %d)\n", fname, mode);
  39.   }
  40.   f = fopen(fname, "w");
  41.   if (f == NULL) return(-1);
  42.   strcpy(__file_lastfname, fname);
  43.   __file_lastfile = f;
  44.   return((int)f);
  45. }
  46.  
  47. long lseek(int fd, long where, int whence) {
  48. int rc;
  49.   rc =  fseek(fdtofile(fd), (long)where, whence);
  50.   if (rc != 0) return(-1L);
  51.   return(ftell((FILE *)fd));
  52. }
  53.  
  54. int write(int fd, char *buf, unsigned int bytes) {
  55. int rc;
  56.   rc = fwrite(buf, bytes, 1, fdtofile(fd));
  57.   if (rc != 1) return(-1);
  58.   return(bytes);
  59. }
  60.  
  61. int read(int fd, char *buf, int bytes) {
  62. int rc;
  63.   rc = fread(buf, 1, (size_t)bytes, fdtofile(fd));
  64.   if (rc == EOF) return(-1);
  65.   return(rc);
  66. }
  67.  
  68.  
  69. int open(char *fname, int mode, ...) {
  70.   char *fmode;
  71.   FILE *f;
  72.  
  73.   if (__file_debug) {
  74.     fprintf(stderr, "open: open(\"%s\", 0x%2x -> ", fname, mode);
  75.     if ((mode & O_RAW) != 0) fprintf(stderr, " O_RAW");
  76.     if ((mode & O_NEW) != 0) fprintf(stderr, " O_NEW");
  77.     if ((mode & O_CREAT) != 0) fprintf(stderr, " O_CREAT");
  78.     if ((mode & O_RDWR) != 0) fprintf(stderr, " O_RDWR");
  79.     if ((mode & O_RDONLY) != 0) fprintf(stderr, " O_RDONLY");
  80.     fprintf(stderr, ")\n");
  81.   }
  82.   if ((mode & O_CREAT) != 0) {
  83.     if (__file_debug) {
  84.       fprintf(stderr, "open: fclose(fopen(\"%s\", \"ab+\"))\n", fname);
  85.     }
  86.     fclose(fopen(fname, "ab+"));
  87.   }
  88.   if ((mode & O_RDONLY) != 0) {
  89.     if ((mode & O_RAW) != 0) {
  90.       fmode = "rb";
  91.     } else {
  92.       fmode = "r";
  93.     }
  94.   } else {
  95.     if ((mode & O_RDWR) != 0) {
  96.       if ((mode & O_NEW) != 0) {
  97.         if ((mode & O_RAW) != 0) {
  98.           fmode = "wb+";
  99.         } else {
  100.           fmode = "w+";
  101.         }
  102.       } else {
  103.         if ((mode & O_RAW) != 0) {
  104.           fmode = "rb+";
  105.         } else {
  106.           fmode = "r+";
  107.         }
  108.       }
  109.     } else {
  110.       if ((mode & O_RAW) != 0) {
  111.         fmode = "wb";
  112.       } else {
  113.         fmode = "w";
  114.       }
  115.     }
  116.   }
  117.   f = fopen(fname, fmode);
  118.   if (__file_debug) {
  119.     fprintf(stderr, "open: fopen(\"%s\",\"%s\") -> %8lx\n",
  120.                      fname, fmode, (long)f);
  121.   }
  122.   if (f == NULL) return(-1);
  123.   strcpy(__file_lastfname, fname);
  124.   __file_lastfile = f;
  125.   return((int)f);
  126. }
  127.  
  128. int close(int fd) {
  129.   if (fd == (int)__file_lastfile) { /* Should extend this to remember all the others... */
  130.     __file_lastfname[0] = '\0';
  131.     __file_lastfile = NULL;
  132.   }
  133.   if (fclose(fdtofile(fd)) == EOF) return(-1);
  134.   return(0);
  135. }
  136.  
  137. extern int chmod(char *f, int mode) {
  138.   return(0); /* This actually has some meaning on
  139.                 the archie, so could be emulated */
  140. }
  141.  
  142. extern int chown(char *f, int owner, int group) {
  143.   return(0);
  144. }
  145.  
  146.  
  147.