source: buchla-68k/orig/IOLIB/FGETS.C@ 0170798

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
RevLine 
[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
13int
14agetc(ptr)
15register FILE *ptr;
16{
17 register int c;
18
19top:
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
37char *
38gets(line)
39char *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
57char *
58fgets(s, n, fp)
59char *s;
60int n;
61FILE *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.