Last change
on this file since 0170798 was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 7 years ago |
Imported original source code.
|
-
Property mode
set to
100755
|
File size:
1.2 KB
|
Rev | Line | |
---|
[3ae31e9] | 1 | /*
|
---|
| 2 | =============================================================================
|
---|
| 3 | fgets.c -- get a string from an ASCII stream file
|
---|
| 4 | Version 2 -- 1987-07-09 -- D.N. Lynx Crowe
|
---|
| 5 | =============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include "stdio.h"
|
---|
| 9 | #include "stddefs.h"
|
---|
| 10 |
|
---|
| 11 | #define EATCHAR '\n' /* character to be "eaten" on input */
|
---|
| 12 |
|
---|
| 13 | int
|
---|
| 14 | agetc(ptr)
|
---|
| 15 | register FILE *ptr;
|
---|
| 16 | {
|
---|
| 17 | register int c;
|
---|
| 18 |
|
---|
| 19 | top:
|
---|
| 20 | if ((c = getc(ptr)) NE EOF) {
|
---|
| 21 |
|
---|
| 22 | switch (c &= 0x7F) {
|
---|
| 23 |
|
---|
| 24 | case 0x1a: /* {x}DOS EOF */
|
---|
| 25 | --ptr->_bp;
|
---|
| 26 | return(EOF);
|
---|
| 27 |
|
---|
| 28 | case EATCHAR: /* CR or LF */
|
---|
| 29 | case 0: /* NUL */
|
---|
| 30 | goto top;
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return(c);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | char *
|
---|
| 38 | gets(line)
|
---|
| 39 | char *line;
|
---|
| 40 | {
|
---|
| 41 | register char *cp;
|
---|
| 42 | register int i;
|
---|
| 43 |
|
---|
| 44 | cp = line;
|
---|
| 45 |
|
---|
| 46 | while ((i = getchar()) NE EOF AND i NE '\n')
|
---|
| 47 | *cp++ = i;
|
---|
| 48 |
|
---|
| 49 | *cp = 0; /* terminate the line */
|
---|
| 50 |
|
---|
| 51 | if ((i EQ EOF) AND (cp EQ line))
|
---|
| 52 | return(NULL);
|
---|
| 53 |
|
---|
| 54 | return(line);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | char *
|
---|
| 58 | fgets(s, n, fp)
|
---|
| 59 | char *s;
|
---|
| 60 | int n;
|
---|
| 61 | FILE *fp;
|
---|
| 62 | {
|
---|
| 63 | register c;
|
---|
| 64 | register char *cp;
|
---|
| 65 |
|
---|
| 66 | cp = s;
|
---|
| 67 |
|
---|
| 68 | while (--n > 0 AND (c = agetc(fp)) NE EOF) {
|
---|
| 69 |
|
---|
| 70 | *cp++ = c;
|
---|
| 71 |
|
---|
| 72 | if (c EQ '\n')
|
---|
| 73 | break;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | *cp = 0;
|
---|
| 77 |
|
---|
| 78 | if (c EQ EOF AND cp EQ s)
|
---|
| 79 | return(NULL);
|
---|
| 80 |
|
---|
| 81 | return(s);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.