-
Notifications
You must be signed in to change notification settings - Fork 0
/
q.c
63 lines (48 loc) · 1.15 KB
/
q.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
58
59
60
61
62
63
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *get_dyna_line()
{
const size_t buffer_length = 42;
const size_t alloc_increment = 42;
char buffer[buffer_length];
char *result = NULL;
size_t characters_got = 0;
size_t result_read = 0;
size_t result_avail = 0;
while (fgets(buffer, buffer_length, stdin)) // пока
{
characters_got = strlen(buffer);
result_read += characters_got;
if (result_read >= result_avail)
{
result_avail += alloc_increment;
result = realloc(result, result_avail);
strcat(result, buffer);
}
if (buffer[characters_got - 1] == '\n')
break;
}
if (ferror(stdin))
{
if (result)
free(result);
return NULL;
}
return result;
}
int main()
{
char* dyna_buffer;
puts("Input a line:");
if((dyna_buffer = get_dyna_line()) == NULL && ferror(stdin))
fputs("Input error\n", stderr), exit(1);
if(dyna_buffer)
{
printf("Your input: %s", dyna_buffer);
free(dyna_buffer);
}
else
puts("EOF");
exit(0);
}