home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 198_01 / fileio.c < prev    next >
C/C++ Source or Header  |  1990-01-23  |  7KB  |  328 lines

  1. /*
  2.  * The routines in this file read and write ASCII files from the disk. All of
  3.  * the knowledge about files are here.
  4.  */
  5.  
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. #if    USG | BSD | (MSDOS & MSC)
  11. #include    <sys/types.h>
  12. #endif
  13.  
  14. #if    USG | BSD | (MSDOS & (MSC | TURBO))
  15. #include    <sys/stat.h>
  16. #endif
  17.  
  18. #if    C86
  19. #include <stat.h>
  20. #endif
  21.  
  22. #if    VMS
  23. #include <stat.h>
  24. #include <fab.h>
  25. #endif
  26.  
  27. #ifndef    O_RDONLY
  28. #define O_RDONLY    0
  29. #endif
  30.  
  31. /* Make getc and putc macroes if they are missing */
  32.  
  33. #ifndef getc
  34. #define    getc(f)    fgetc(f)
  35. #endif
  36.  
  37. #ifndef putc
  38. #define    putc(c,f)    fputc(c,f)
  39. #endif
  40.  
  41. FILE    *ffp;        /* File pointer, all functions. */
  42. int eofflag;        /* end-of-file flag */
  43.  
  44. /*
  45.  * Open a file for reading.
  46.  */
  47. ffropen(fn, getstat)
  48. char    *fn;
  49. {
  50. #if    VMS | USG | BSD
  51.     int    ffd;        /* File descriptor */
  52.     struct stat s;
  53.  
  54.     ffd = open(fn, O_RDONLY);
  55.     if (getstat && fstat(ffd, &s) == 0) {
  56.         curbp->b_fmode = s.st_mode;
  57. #if    VMS
  58.         curbp->b_fab_rfm = s.st_fab_rfm;
  59.         curbp->b_fab_rat = s.st_fab_rat;
  60. #endif
  61.     }
  62.     if (ffd < 0 || (ffp=fdopen(ffd, "r")) == NULL)
  63. #else
  64. #if MSDOS
  65.     if ((ffp=fopen(fn, ((curbp->b_mode&MDBINARY)? "rb": "r"))) == NULL)
  66. #else
  67.     if ((ffp=fopen(fn, "r")) == NULL)
  68. #endif
  69. #endif
  70.                 return (FIOFNF);
  71.     eofflag = FALSE;
  72.         return (FIOSUC);
  73. }
  74.  
  75. #if    VMS
  76. /* names for record attributes and record formats */
  77. int rms_maxrat = 3;
  78. char *rms_rat[] = {"ftn", "cr", "prn", "blk"};
  79. int rms_maxrfm = 6;
  80. char *rms_rfm[] = /* {"udf",  "fix",  "var", "vfc", "stm", "stmlf", "stmcr"} */
  81.              {"stmlf","stmlf","var", "vfc", "stm", "stmlf", "stmcr"};
  82. #endif
  83.  
  84. /*
  85.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  86.  * (cannot create).
  87.  */
  88. ffwopen(fn)
  89. char    *fn;
  90. {
  91. #if    VMS | USG | BSD
  92.     int    ffd;        /* File descriptor */
  93.  
  94. #if     VMS
  95.     int i, n;
  96.     char ratstr[80], rfmstr[80];
  97.  
  98.     n = curbp->b_fab_rfm;
  99.     if (n < 0 || n > rms_maxrfm) n = 2;
  100.     sprintf(rfmstr, "rfm=%s", rms_rfm[ n ]);
  101.  
  102.     n = 0;
  103.     strcpy(ratstr, "rat=");
  104.     for(i = 0; i <= rms_maxrat; i++)
  105.         if (curbp->b_fab_rat & (1 << i))    {
  106.             if (n++ > 0) strcat(ratstr, ",");
  107.             strcat(ratstr, rms_rat[i]);
  108.         }
  109.     if (n == 0) strcat(ratstr, rms_rat[1]);
  110.  
  111.     ffd = creat(fn, curbp->b_fmode, rfmstr, ratstr);
  112. #else
  113.     ffd = creat(fn, curbp->b_fmode);
  114. #endif
  115.     if (ffd < 0 || (ffp=fdopen(ffd, "w")) == NULL) {
  116. #else
  117. #if MSDOS
  118.     if ((ffp=fopen(fn, ((curbp->b_mode&MDBINARY)? "wb": "w"))) == NULL) {
  119. #else
  120.     if ((ffp=fopen(fn, "w")) == NULL) {
  121. #endif
  122. #endif
  123.                 mlwrite("Cannot open file for writing");
  124.                 return (FIOERR);
  125.         }
  126.         return (FIOSUC);
  127. }
  128.  
  129. /*
  130.  * Close a file. Should look at the status in all systems.
  131.  */
  132. ffclose()
  133. {
  134.     /* free this since we do not need it anymore */
  135.     if (fline) {
  136.         free(fline);
  137.         fline = NULL;
  138.     }
  139.  
  140. #if    MSDOS & CTRLZ
  141.     putc(26, ffp);        /* add a ^Z at the end of the file */
  142. #endif
  143.     
  144. #if     V7 | USG | BSD | (MSDOS & (LATTICE | MSC | TURBO | C86))
  145.         if (fclose(ffp) != FALSE) {
  146.                 mlwrite("Error closing file");
  147.                 return(FIOERR);
  148.         }
  149.         return(FIOSUC);
  150. #else
  151.         fclose(ffp);
  152.         return (FIOSUC);
  153. #endif
  154. }
  155.  
  156. /*
  157.  * Write a line to the already opened file. The "buf" points to the buffer,
  158.  * and the "nbuf" is its length, less the free newline. Return the status.
  159.  * Check only at the newline.
  160.  */
  161. ffputline(buf, nbuf)
  162. char    buf[];
  163. {
  164.         register int    i;
  165. #if    CRYPT
  166.     char c;        /* character to translate */
  167.  
  168.     if (cryptflag) {
  169.             for (i = 0; i < nbuf; ++i) {
  170.             c = buf[i] & 0xff;
  171.             crypt(&c, 1);
  172.             putc(c, ffp);
  173.         }
  174.     } else
  175.             for (i = 0; i < nbuf; ++i)
  176.                     putc(buf[i]&0xFF, ffp);
  177. #else
  178.         for (i = 0; i < nbuf; ++i)
  179.                 putc(buf[i]&0xFF, ffp);
  180. #endif
  181.  
  182. #if    ST520
  183.         putc('\r', ffp);
  184. #endif        
  185.         putc('\n', ffp);
  186.  
  187.         if (ferror(ffp)) {
  188.                 mlwrite("Write I/O error");
  189.                 return (FIOERR);
  190.         }
  191.  
  192.         return (FIOSUC);
  193. }
  194.  
  195. /*
  196.  * Read a line from a file, and store the bytes in the supplied buffer. The
  197.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  198.  * at the end of the file that don't have a newline present. Check for I/O
  199.  * errors too. Return status.
  200.  */
  201. ffgetline()
  202.  
  203. {
  204.         register int c;        /* current character read */
  205.         register int i;        /* current index into fline */
  206.     register char *tmpline;    /* temp storage for expanding line */
  207.  
  208.     frlen = 0;
  209.  
  210.     /* if we are at the end...return it */
  211.     if (eofflag)
  212.         return(FIOEOF);
  213.  
  214.     /* dump fline if it ended up too big */
  215.     if (flen > 2 * NSTRING && fline != NULL) {
  216.         free(fline);
  217.         fline = NULL;
  218.     }
  219.  
  220.     /* if we don't have an fline, allocate one */
  221.     if (fline == NULL)
  222.         if ((fline = malloc(flen = NSTRING)) == NULL)
  223.             return(FIOMEM);
  224.  
  225.     /* read the line in */
  226.         i = 0;
  227.         while ((c = getc(ffp)) != EOF && c != '\n') {
  228.                 fline[i++] = c;
  229.         /* if it's longer, get more room */
  230.                 if (i >= flen) {
  231.                     if ((tmpline = malloc(flen+NSTRING)) == NULL)
  232.                         return(FIOMEM);
  233.             for(i=0; i<flen; i++) tmpline[i] = fline[i];
  234.             i = flen;
  235.                     flen += NSTRING;
  236.                     free(fline);
  237.                     fline = tmpline;
  238.                 }
  239.         }
  240.  
  241. #if    ST520
  242.     if(fline[i-1] == '\r')
  243.         i--;
  244. #endif
  245.  
  246.     /* test for any errors that may have occured */
  247.         if (c == EOF) {
  248.                 if (ferror(ffp)) {
  249.                         mlwrite("File read error");
  250.                         return(FIOERR);
  251.                 }
  252.  
  253.                 if (i != 0)
  254.             eofflag = TRUE;
  255.         else
  256.             return(FIOEOF);
  257.         }
  258.  
  259.     /* terminate and decrypt the string */
  260.     frlen = i;
  261.         fline[i] = 0;
  262.  
  263. #if    CRYPT
  264.     if (cryptflag)
  265.         crypt(fline, frlen);
  266. #endif
  267.         return(FIOSUC);
  268. }
  269.  
  270. #if    AZTEC & MSDOS
  271. #undef    fgetc
  272. /*    a1getc:        Get an ascii char from the file input stream
  273.             but DO NOT strip the high bit
  274. */
  275.  
  276. int a1getc(fp)
  277.  
  278. FILE *fp;
  279.  
  280. {
  281.     int c;        /* translated character */
  282.  
  283.     c = getc(fp);    /* get the character */
  284.  
  285.     /* if its a <LF> char, throw it out  */
  286.     while (c == 10)
  287.         c = getc(fp);
  288.  
  289.     /* if its a <RETURN> char, change it to a LF */
  290.     if (c == '\r')
  291.         c = '\n';
  292.  
  293.     /* if its a ^Z, its an EOF */
  294.     if (c == 26)
  295.         c = EOF;
  296.  
  297.     return(c);
  298. }
  299. #endif
  300.  
  301. #if    ABACKUP
  302.  
  303. /*
  304.  * Test if a file is newer than its auto-saved version
  305.  */
  306.  
  307. ffisnewer(fn)
  308. char    *fn;
  309. {
  310. #if    VMS | USG | BSD | (MSDOS & (MSC | TURBO | C86))
  311.     char asvname[NFILEN + 4];
  312. #if    C86
  313.     struct fbuf asvstat, fnstat;
  314. #else
  315.     struct stat asvstat, fnstat;
  316. #endif
  317.  
  318.     makeasvname(asvname, fn);
  319.     if (stat(asvname, &asvstat) != 0) return(TRUE);
  320.     if (stat(fn, &fnstat) != 0) return(FALSE);
  321.     if (asvstat.st_ctime > fnstat.st_ctime) return(FALSE);
  322. #endif
  323.     return(TRUE);
  324. }
  325.  
  326. #endif
  327.  
  328.