source: buchla-68k/libcio/fgets.c@ 0580615

Last change on this file since 0580615 was 0580615, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Point of no return.

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[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
8#include "stdio.h"
9#include "stddefs.h"
10
11#define EATCHAR '\n' /* character to be "eaten" on input */
12
[0580615]13int agetc(FILE *ptr)
[f40a309]14{
15 register int c;
16
17top:
18 if ((c = getc(ptr)) NE EOF) {
19
20 switch (c &= 0x7F) {
21
22 case 0x1a: /* {x}DOS EOF */
23 --ptr->_bp;
24 return(EOF);
25
26 case EATCHAR: /* CR or LF */
27 case 0: /* NUL */
28 goto top;
29 }
30 }
31
32 return(c);
33}
34
[0580615]35char *gets(char *line)
[f40a309]36{
37 register char *cp;
38 register int i;
39
40 cp = line;
41
42 while ((i = getchar()) NE EOF AND i NE '\n')
43 *cp++ = i;
44
45 *cp = 0; /* terminate the line */
46
47 if ((i EQ EOF) AND (cp EQ line))
48 return(NULL);
49
50 return(line);
51}
52
[0580615]53char *fgets(char *s, int n, FILE *fp)
[f40a309]54{
[73dd55f]55 register int c;
[f40a309]56 register char *cp;
57
58 cp = s;
59
60 while (--n > 0 AND (c = agetc(fp)) NE EOF) {
61
62 *cp++ = c;
63
64 if (c EQ '\n')
65 break;
66 }
67
68 *cp = 0;
69
70 if (c EQ EOF AND cp EQ s)
71 return(NULL);
72
73 return(s);
74}
75
Note: See TracBrowser for help on using the repository browser.