Fri Jun 7 16:48:13 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / stdio-common / xbug.c
blobec648f556669c5055bbe80c14fa97e867e882f26
1 #include <stdio.h>
3 typedef struct _Buffer {
4 char *buff;
5 int room, used;
6 } Buffer;
8 #define INIT_BUFFER_SIZE 10000
10 void InitBuffer(b)
11 Buffer *b;
13 b->room = INIT_BUFFER_SIZE;
14 b->used = 0;
15 b->buff = (char *)malloc(INIT_BUFFER_SIZE*sizeof(char));
18 void AppendToBuffer(b, str, len)
19 register Buffer *b;
20 char *str;
21 register int len;
23 while (b->used + len > b->room) {
24 b->buff = (char *)realloc(b->buff, 2*b->room*(sizeof(char)));
25 b->room *= 2;
27 strncpy(b->buff + b->used, str, len);
28 b->used += len;
31 void ReadFile(buffer, input)
32 register Buffer *buffer;
33 FILE *input;
35 char buf[BUFSIZ + 1];
36 register int bytes;
38 buffer->used = 0;
39 while (!feof(input) && (bytes = fread(buf, 1, BUFSIZ, input)) > 0) {
40 AppendToBuffer(buffer, buf, bytes);
42 AppendToBuffer(buffer, "", 1);
45 main()
47 char * filename = "xbug.c";
48 FILE *input;
49 Buffer buffer;
51 InitBuffer(&buffer);
53 if (!freopen (filename, "r", stdin))
54 fprintf(stderr, "cannot open file\n");
56 if (!(input = popen("/bin/cat", "r")))
57 fprintf(stderr, "cannot run \n");
59 ReadFile(&buffer, input);
60 pclose(input);
62 return 0;