-
Notifications
You must be signed in to change notification settings - Fork 0
/
readin.c
45 lines (39 loc) · 1.24 KB
/
readin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*----------------------------------------------------------------------
* ReadIn - read a file into a list of lines.
* Copyright c. 1988, Gary Oberbrunner.
* This program may be freely redistributed providing that this source code
* and the accompanying copyright notice are preserved intact.
*----------------------------------------------------------------------
*/
#include <string.h>
#include <stdio.h>
#include "std.h"
#include "colm.h"
#include "lists.h"
#define MAX_LEN 20000 /* maximum string length */
extern int debug;
static char SCCSId[] = "@(#)readin.c 1.1 6/3/88 (MASSCOMP) 22:44:47";
int ReadIn(const char *fname, NODE *list)
{
char Instr[MAX_LEN];
FILE *infile = NULL;
int TotalLines = 0; /* total # of lines read */
if (!strcmp(fname, "-"))
infile = stdin;
else if ((infile = fopen(fname, "r")) == NULL) {
fprintf(stderr,"Can't open file %s\n.", fname);
exit(9);
}
while (fgets(Instr, MAX_LEN, infile)) {
if (*Instr == '\0') {
fprintf(stderr,
"Error: Null found in supposedly line-oriented data.\n");
exit(2);
}
TotalLines++;
Instr[strlen(Instr)-1] = '\0'; /* strip newline */
append(Instr, list); /* add it to the list */
assert(list->prev != NULL);
}
return TotalLines;
}