source:
buchla-68k/libcio/fgets.c@
6aa430b
| Last change on this file since 6aa430b was b28a12e, checked in by , 8 years ago | |
|---|---|
|
|
| File size: 1.1 KB | |
| Rev | Line | |
|---|---|---|
| [f40a309] | 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 | ||
| [b28a12e] | 8 | #include "ram.h" |
| [f40a309] | 9 | |
| 10 | #define EATCHAR '\n' /* character to be "eaten" on input */ | |
| 11 | ||
| [7258c6a] | 12 | int16_t agetc(FILE *ptr) |
| [f40a309] | 13 | { |
| [7258c6a] | 14 | register int16_t c; |
| [f40a309] | 15 | |
| 16 | top: | |
| 17 | if ((c = getc(ptr)) NE EOF) { | |
| 18 | ||
| 19 | switch (c &= 0x7F) { | |
| 20 | ||
| 21 | case 0x1a: /* {x}DOS EOF */ | |
| 22 | --ptr->_bp; | |
| 23 | return(EOF); | |
| 24 | ||
| 25 | case EATCHAR: /* CR or LF */ | |
| 26 | case 0: /* NUL */ | |
| 27 | goto top; | |
| 28 | } | |
| 29 | } | |
| 30 | ||
| 31 | return(c); | |
| 32 | } | |
| 33 | ||
| [7258c6a] | 34 | int8_t *gets(int8_t *line) |
| [f40a309] | 35 | { |
| [7258c6a] | 36 | register int8_t *cp; |
| 37 | register int16_t i; | |
| [f40a309] | 38 | |
| 39 | cp = line; | |
| 40 | ||
| 41 | while ((i = getchar()) NE EOF AND i NE '\n') | |
| 42 | *cp++ = i; | |
| 43 | ||
| 44 | *cp = 0; /* terminate the line */ | |
| 45 | ||
| 46 | if ((i EQ EOF) AND (cp EQ line)) | |
| 47 | return(NULL); | |
| 48 | ||
| 49 | return(line); | |
| 50 | } | |
| 51 | ||
| [7258c6a] | 52 | int8_t *fgets(int8_t *s, int16_t n, FILE *fp) |
| [f40a309] | 53 | { |
| [7258c6a] | 54 | register int16_t c; |
| 55 | register int8_t *cp; | |
| [f40a309] | 56 | |
| 57 | cp = s; | |
| 58 | ||
| 59 | while (--n > 0 AND (c = agetc(fp)) NE EOF) { | |
| 60 | ||
| 61 | *cp++ = c; | |
| 62 | ||
| 63 | if (c EQ '\n') | |
| 64 | break; | |
| 65 | } | |
| 66 | ||
| 67 | *cp = 0; | |
| 68 | ||
| 69 | if (c EQ EOF AND cp EQ s) | |
| 70 | return(NULL); | |
| 71 | ||
| 72 | return(s); | |
| 73 | } | |
| 74 | ||
| [6262b5c] | 75 |
Note:
See TracBrowser
for help on using the repository browser.
![(please configure the [header_logo] section in trac.ini)](http://bob.lopatic.de/chrome/site/logo.png)