]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/xbug.c
* Makefile (subdirs): Replace stdio with stdio-common and $(stdio).
[thirdparty/glibc.git] / stdio-common / xbug.c
1 #include <stdio.h>
2
3 typedef struct _Buffer {
4 char *buff;
5 int room, used;
6 } Buffer;
7
8 #define INIT_BUFFER_SIZE 10000
9
10 void InitBuffer(b)
11 Buffer *b;
12 {
13 b->room = INIT_BUFFER_SIZE;
14 b->used = 0;
15 b->buff = (char *)malloc(INIT_BUFFER_SIZE*sizeof(char));
16 }
17
18 void AppendToBuffer(b, str, len)
19 register Buffer *b;
20 char *str;
21 register int len;
22 {
23 while (b->used + len > b->room) {
24 b->buff = (char *)realloc(b->buff, 2*b->room*(sizeof(char)));
25 b->room *= 2;
26 }
27 strncpy(b->buff + b->used, str, len);
28 b->used += len;
29 }
30
31 void ReadFile(buffer, input)
32 register Buffer *buffer;
33 FILE *input;
34 {
35 char buf[BUFSIZ + 1];
36 register int bytes;
37
38 buffer->used = 0;
39 while (!feof(input) && (bytes = fread(buf, 1, BUFSIZ, input)) > 0) {
40 AppendToBuffer(buffer, buf, bytes);
41 }
42 AppendToBuffer(buffer, "", 1);
43 }
44
45 main()
46 {
47 char * filename = "xbug.c";
48 FILE *input;
49 Buffer buffer;
50
51 InitBuffer(&buffer);
52
53 if (!freopen (filename, "r", stdin))
54 fprintf(stderr, "cannot open file\n");
55
56 if (!(input = popen("/bin/cat", "r")))
57 fprintf(stderr, "cannot run \n");
58
59 ReadFile(&buffer, input);
60 pclose(input);
61
62 return 0;
63 }