-
Notifications
You must be signed in to change notification settings - Fork 0
/
507.c
57 lines (48 loc) · 1.03 KB
/
507.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
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t MAXLEN = 10000;
size_t MAXLINES = 10000000;
/* readlines: read input lines */
int readlines(char **lineptr)
{
int len, nlines;
nlines = 0;
char *line = malloc(MAXLEN);
while ((len = getline(&line, &MAXLEN, stdin)) > 0)
{
if (nlines >= MAXLINES)
return -1;
else
{
*(line + len - 1) = '\0';
char *line2 = malloc(len + 1);
strcpy(line2, line);
*lineptr++ = line2;
nlines++;
}
}
return nlines;
}
/* writelines: write output lines */
void writelines(char **lineptr, int nlines)
{
while (nlines-- > 0)
printf("%s\n", *lineptr++);
}
int main()
{
int nlines;
char **lineptr = malloc(MAXLINES);
/* number of input lines read */
if ((nlines = readlines(lineptr)) >= 0)
{
writelines(lineptr, nlines);
return 0;
}
else
{
printf("error: input too big to sort\n");
return 1;
}
}