source:
buchla-68k/libcio/fgets.c@
6262b5c
Last change on this file since 6262b5c was 6262b5c, checked in by , 7 years ago | |
---|---|
|
|
File size: 1.1 KB |
Line | |
---|---|
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 "all.h" |
9 | |
10 | #define EATCHAR '\n' /* character to be "eaten" on input */ |
11 | |
12 | int16_t agetc(FILE *ptr) |
13 | { |
14 | register int16_t c; |
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 | |
34 | int8_t *gets(int8_t *line) |
35 | { |
36 | register int8_t *cp; |
37 | register int16_t i; |
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 | |
52 | int8_t *fgets(int8_t *s, int16_t n, FILE *fp) |
53 | { |
54 | register int16_t c; |
55 | register int8_t *cp; |
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 | |
75 |
Note:
See TracBrowser
for help on using the repository browser.