?login_element?

Subversion Repositories NedoOS

Rev

Rev 630 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.         STDLIB1.C -- for BDS C v1.6 --  1/86
  3.         Copyright (c) 1982, 1986 by BD Software, Inc.
  4.  
  5.         The files STDLIB1.C, STDLIB2.C and STDLIB3.C contain the source
  6.         listings for all functions present in the DEFF.CRL library object
  7.         file. (Note: DEFF2.CRL contains the .CSM-coded portions of the
  8.         library.)
  9.  
  10.         STDLIB1.C contains the new K&R standard buffered file I/O functions:
  11.  
  12.         fopen   fclose  fflush
  13.         fgetc   fputc   getw    putw    ungetc
  14.         fread   fwrite  fgets   fputs
  15.         feof    ferror  clearerr
  16. */
  17.  
  18. #include <stdio.h>
  19.  
  20.  
  21. /*
  22.         fopen(filename, mode)
  23.  
  24.         The "mode" parameter may be:
  25.         "r" or "rb"     read, read binary
  26.         "w" or "wb"     write, write binary
  27.         "a" or "ab"     append, append binary
  28.         (If no "b" is appended, text mode assumed by default)
  29. */
  30.  
  31. char *fopen(name,mode)
  32. char *mode, *name;
  33. {
  34.     int i;
  35.     FILE *fp;
  36.  
  37.     if ((fp = alloc(sizeof(*fp))) == NULL)
  38.         return NULL;
  39.     fp->_nextp = fp->_buff;
  40.     fp->_nleft = (NSECTS * SECSIZ);
  41.     fp->_flags = _WRITE;
  42.  
  43.     switch(*mode){
  44.         case 'r':
  45.                 if ((fp->_fd = open(name, 0)) == ERROR)
  46.                         goto error;
  47.                 fp->_nleft = 0;
  48.                 fp->_flags = _READ;
  49.                 break;
  50.         case 'a':
  51.                 if ((fp->_fd = open(name, 2)) == ERROR)
  52.                         goto create;
  53.                 if (!cfsize(fp->_fd))   /* if empty file, just like 'w' */
  54.                         break;
  55.                 if (seek(fp->_fd, -1, 2) == ERROR ||
  56.                     read(fp->_fd, fp->_buff, 1) < 1)
  57.                 {
  58.                         close(fp->_fd);
  59.                         goto error;
  60.                 }
  61.                 if (mode[1] != 'b')
  62.                    for (i=0; i<SECSIZ; i++)
  63.                         if (fp->_buff[i] == CPMEOF)
  64.                         {
  65.                                 seek(fp->_fd, -1, 1);
  66.                                 fp->_nextp += i;
  67.                                 fp->_nleft -= i;
  68.                                 break;
  69.                         }
  70.                 break;
  71.         case 'w':;
  72.    create:      if ((fp->_fd = creat(name)) == ERROR)
  73.                         goto error;
  74.                 break;
  75.         default:
  76.                 goto error;                     /* illegal mode */
  77.     }
  78.  
  79.     if (mode[1] != 'b')                 /* text mode by default */
  80.         fp->_flags |= _TEXT;
  81.     return fp;
  82.  
  83.  error:
  84.     free(fp);
  85.     return NULL;
  86. }
  87.  
  88.  
  89. int fclose(fp)
  90. FILE *fp;
  91. {
  92.         if (fp <= 4)
  93.                 return OK;
  94.         if (fp->_flags & _WRITE)
  95.         {
  96.                 if (fp->_flags & _TEXT)
  97.                         fputc(CPMEOF, fp);
  98.                 if (fflush(fp) == ERROR)
  99.                         return ERROR;
  100.         }
  101.         free(fp);
  102.         return close(fp->_fd);
  103. }
  104.  
  105.  
  106. int fflush(fp)
  107. FILE *fp;
  108. {
  109.         int i; char *p;
  110.  
  111.         if (fp <= 4)
  112.                 return OK;
  113.         if (!(fp->_flags & _WRITE))
  114.                 return ERROR;
  115.  
  116.         if (fp->_nleft == (NSECTS * SECSIZ))
  117.                 return OK;
  118.  
  119.         i = NSECTS - (fp->_nleft / SECSIZ);
  120.         if (write(fp->_fd, fp->_buff, i) != i)
  121.         {
  122.                 fp->_flags |= _ERR;
  123.                 return ERROR;
  124.         }
  125.         i = (i-1) * SECSIZ;
  126.         if (fp->_nleft % SECSIZ) {
  127.                 movmem(fp->_buff + i, fp->_buff, SECSIZ);
  128.                 fp->_nleft += i;
  129.                 fp->_nextp -= i;
  130.                 return seek(fp->_fd, -1, 1);
  131.          }
  132.  
  133.         fp->_nleft = (NSECTS * SECSIZ);
  134.         fp->_nextp = fp->_buff;
  135.         return OK;
  136. }
  137.  
  138. int fgetc(fp)
  139. FILE *fp;
  140. {
  141.         int nsecs;
  142.         char c;
  143.  
  144.         switch(fp)
  145.         {
  146.                 case stdin:     return getchar();
  147.                 case stdrdr:    return bdos(3);
  148.         }
  149.  
  150. top:    if (!fp->_nleft--)              /* if buffer empty, fill it up first */
  151.         {
  152.                 if ((fp->_flags & _EOF) ||
  153.                    (nsecs = read(fp->_fd, fp->_buff, NSECTS)) <= 0)
  154.                 {
  155.                   eof:  fp->_nleft = 0;
  156.                         fp->_flags |= _EOF;
  157.                         return EOF;
  158.                 }
  159.                 fp->_nleft = nsecs * SECSIZ - 1;
  160.                 fp->_nextp = fp->_buff;
  161.         }
  162.         c = *fp -> _nextp++;
  163.         if (fp->_flags & _TEXT)
  164.                 if (c == CPMEOF)
  165.                         goto eof;
  166.                 else if (c == '\r')
  167.                         goto top;       /* Ignore CR's in text files */
  168.         return c;
  169. }
  170.  
  171.  
  172. int fputc(c,fp)
  173. char c;
  174. FILE *fp;
  175. {
  176.         switch (fp)
  177.          {
  178.                 case stdout: return putchar(c); /* std output */
  179.                 case stdlst: return bdos(5,c);  /* list dev.  */
  180.                 case stdpun: return bdos(4,c);  /* to punch   */
  181.                 case stderr: if (c == '\n')
  182.                                 bdos(2,'\r');
  183.                              return bdos(2,c);
  184.                 case stdin: return ERROR;
  185.          }
  186.  
  187.         if (!(fp->_flags & _TEXT))      /* If binary mode, just write it */
  188.                 return _putc(c, fp);
  189.  
  190.         if (c == '\r')          /* Else must be Text mode: */
  191.                 return c;               /*Ignore CR's */
  192.  
  193.         if (c != '\n')                  /* If not newline, just write it */
  194.                 return _putc(c,fp);
  195.  
  196.         if (_putc('\r',fp) == ERROR)    /* Write CR-LF combination */
  197.                 return ERROR;  
  198.         return _putc('\n',fp); 
  199. }
  200.  
  201. _putc(c,fp)
  202. FILE *fp;
  203. {
  204.         if (fp->_flags & _ERR)
  205.                 return ERROR;
  206.  
  207.         if (!fp->_nleft--)              /*   if buffer full, flush it   */
  208.          {
  209.                 if ((write(fp->_fd, fp->_buff, NSECTS)) != NSECTS)
  210.                 {
  211.                         fp->_flags |= _ERR;
  212.                         return ERROR;
  213.                 }
  214.                 fp->_nleft = (NSECTS * SECSIZ - 1);
  215.                 fp->_nextp = fp->_buff;
  216.          }
  217.  
  218.         return *fp->_nextp++ = c;
  219. }
  220.        
  221.  
  222. int ungetc(c, fp)
  223. FILE *fp;
  224. char c;
  225. {
  226.         if (fp == stdin) return ungetch(c);
  227.         if ((fp < 7) || fp -> _nleft == (NSECTS * SECSIZ))
  228.                 return ERROR;
  229.         *--fp -> _nextp = c;
  230.         fp -> _nleft++;
  231.         return OK;
  232. }
  233.  
  234. int getw(fp)
  235. FILE *fp;
  236. {
  237.         int a,b;       
  238.         if (((a = fgetc(fp)) >= 0) && ((b = fgetc(fp)) >=0))
  239.                         return (b << 8) + a;
  240.         return ERROR;
  241. }
  242.  
  243. int putw(w,fp)
  244. unsigned w;
  245. FILE *fp;
  246. {
  247.         if ((fputc(w & 0xff, fp) >=0 ) && (fputc(w / 256,fp) >= 0))
  248.                                 return w;
  249.         return ERROR;
  250. }
  251.  
  252. int fread(buf, size, count, fp)
  253. char *buf;
  254. unsigned size, count;
  255. FILE *fp;
  256. {
  257.         int n_read, n_togo, cnt, i;
  258.  
  259.         n_togo = size * count;
  260.         n_read = 0;
  261.         if (fp->_flags & _EOF)
  262.                 return NULL;
  263.  
  264.         while (n_togo)
  265.         {
  266.                 cnt = (n_togo <= fp->_nleft) ? n_togo : fp->_nleft;
  267.                 movmem(fp->_nextp, buf, cnt);
  268.                 fp->_nextp += cnt;
  269.                 buf += cnt;
  270.                 fp->_nleft -= cnt;
  271.                 n_togo -= cnt;
  272.                 n_read += cnt;
  273.                 if (n_togo)
  274.                 {
  275.                         if ((cnt = read(fp->_fd, fp->_buff, NSECTS)) <=0)
  276.                         {
  277.                                 fp->_flags |= _EOF;
  278.                                 goto text_test;
  279.                         }
  280.                         fp->_nleft = cnt * SECSIZ;
  281.                         fp->_nextp = fp->_buff;
  282.                 }
  283.         }
  284.  text_test:
  285.         if (fp->_flags & _TEXT)
  286.         {
  287.                 i = min(n_read, SECSIZ);
  288.                 while (i--)
  289.                         if (*(buf-i) == CPMEOF)            
  290.                         {
  291.                                 fp->_flags |= _EOF;
  292.                                 return (n_read - i);
  293.                         }
  294.         }
  295.         return (n_read/size);
  296. }
  297.  
  298. int fwrite(buf, size, count, fp)
  299. char *buf;
  300. unsigned size, count;
  301. FILE *fp;
  302. {
  303.         int n_done, n_togo, cnt;
  304.  
  305.         n_togo = size * count;
  306.         n_done = 0;
  307.  
  308.         if (fp->_flags & _ERR)
  309.                 return NULL;
  310.  
  311.         while (n_togo)
  312.         {
  313.                 cnt = (n_togo <= fp->_nleft) ? n_togo : fp->_nleft;
  314.                 movmem(buf, fp->_nextp, cnt);
  315.                 fp->_nextp += cnt;
  316.                 buf += cnt;
  317.                 fp->_nleft -= cnt;
  318.                 n_togo -= cnt;
  319.                 n_done += cnt;
  320.                 if (n_togo)
  321.                 {
  322.                         if ((cnt = write(fp->_fd, fp->_buff, NSECTS)) <= 0)
  323.                         {
  324.                                 fp->_flags |= _ERR;
  325.                                 return ERROR;
  326.                         }
  327.                         fp->_nleft = (NSECTS * SECSIZ);
  328.                         fp->_nextp = fp->_buff;
  329.                 }
  330.         }
  331.         return (n_done/size);
  332. }
  333.  
  334.  
  335. /* Get a line of text from a buffered input file: */
  336.  
  337. char *fgets(s,n,fp)
  338. char *s;
  339. int n;
  340. FILE *fp;
  341. {
  342.         int c;
  343.         char *cs;
  344.        
  345.         cs = s;
  346.         while (--n > 0)
  347.         {
  348.                 if ((c = fgetc(fp)) == EOF)
  349.                 {
  350.                         *cs = '\0';
  351.                         return (cs == s) ? NULL : s;
  352.                 }
  353.                 if ((*cs++ = c) == '\n') break;
  354.         }
  355.         *cs = '\0';
  356.         return s;
  357. }
  358.  
  359.  
  360. /* Write a line of text out to a buffered file: */
  361.  
  362. fputs(s,fp)
  363. char *s;
  364. FILE *fp;
  365. {
  366.         char c;
  367.         while (c = *s++) {
  368.                 if (fputc(c,fp) == ERROR)
  369.                         return ERROR;
  370.         }
  371.         return OK;
  372. }
  373.  
  374. VOID clearerr(fp)
  375. FILE *fp;
  376. {
  377.         fp->_flags &= (~_ERR);
  378. }
  379.  
  380. int feof(fp)
  381. FILE *fp;
  382. {
  383.         return fp->_flags & _EOF;
  384. }
  385.  
  386. int ferror(fp)
  387. FILE *fp;
  388. {
  389.         return fp->_flags & _ERR;
  390. }
  391.