home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-4 / BDSLIB.ARK / FGETS.C < prev    next >
Text File  |  1983-07-15  |  2KB  |  83 lines

  1. /*
  2.  * fgets
  3.  * This function is slightly different from the one of the same name
  4.  * in the BDS C Standard library. Here as in the Version 7 U**X Library
  5.  * the size parameter specifies the maximum size of the string to help
  6.  * prevent buffer overflow. The maximum size for buffers on tty devices
  7.  * is limited to MAXLINE because of the limitations of the CP/M.
  8.  * Last Edit 7/1/83
  9.  */
  10.  
  11. char *
  12. fgets(s,size,stream)
  13. char *s;
  14. int  *size;
  15. FILE *stream;
  16. {
  17.     int count, c;
  18.     char *cptr;
  19.     count = size;
  20.     if (isatty(stream)) {
  21.         if (count>MAXLINE)
  22.             count = MAXLINE;
  23.     }
  24.     cptr = s;
  25.     if ((c = fgetc(stream)) == CPMEOF || c == EOF)
  26.         return NULL;
  27.     do {
  28.         if ((*cptr++ = c) == '\n') {
  29.           if (cptr>s+1 && *(cptr-2) == '\r')
  30.             *(--cptr - 1) = '\n';
  31.           break;
  32.         }
  33.     }
  34.     while (count-- && (c=fgetc(stream)) != EOF && c != CPMEOF) ;
  35.     if (c == CPMEOF)
  36.         ungetc(c,stream);    /* push back control-Z */
  37.     *cptr = '\0';
  38.     return s;
  39. }
  40. /*
  41.  * fputs
  42.  * This is essentially the same as the function in the BDS C Standard
  43.  * Library. It conforms exactly to the function of the same name in
  44.  * the Version 7 U**X Standard C Library.
  45.  * Last Edit 7/1/83
  46.  */
  47.  
  48. void
  49. fputs(s,stream)
  50. char *s;
  51. FILE *stream;
  52. {
  53.     char c;
  54.     while (c = *s++) {
  55.         if (c == '\n')
  56.             fputc('\r',stream);
  57.         fputc(c,stream);
  58.     }
  59.     return ;
  60. }
  61.  
  62. /*
  63.  * puts
  64.  * This writes the specified string on standard output terminated
  65.  * with a newline character. It is slightly different from the
  66.  * function in the BDS C Standard Library for the sake of compatibility
  67.  * Last Edit 7/1/83
  68.  */
  69.  
  70. void
  71. puts(s)
  72. char *s;
  73. {
  74.     char c;
  75.     while (c = *s++) {
  76.         if (c == '\n')
  77.             fputc('\r',stdout);
  78.         fputc(c,stdout);
  79.     }
  80.     fputc('\n',stdout);
  81.     return ;
  82. }
  83.