2.12.90-13
[glibc.git] / stdio-common / xbug.c
bloba04bfc4fd031901847f9c46bf0a319bd8e22226f
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
6 typedef struct _Buffer {
7 char *buff;
8 int room, used;
9 } Buffer;
11 #if __STDC__ - 0
12 void InitBuffer (Buffer *b);
13 void AppendToBuffer (register Buffer *b, const char *str, register int len);
14 void ReadFile (register Buffer *buffer, FILE *input);
15 #endif
17 #define INIT_BUFFER_SIZE 10000
19 void InitBuffer(b)
20 Buffer *b;
22 b->room = INIT_BUFFER_SIZE;
23 b->used = 0;
24 b->buff = (char *)malloc(INIT_BUFFER_SIZE*sizeof(char));
27 void AppendToBuffer(b, str, len)
28 register Buffer *b;
29 const char *str;
30 register int len;
32 while (b->used + len > b->room) {
33 b->buff = (char *)realloc(b->buff, 2*b->room*(sizeof(char)));
34 b->room *= 2;
36 strncpy(b->buff + b->used, str, len);
37 b->used += len;
40 void ReadFile(buffer, input)
41 register Buffer *buffer;
42 FILE *input;
44 char buf[BUFSIZ + 1];
45 register int bytes;
47 buffer->used = 0;
48 while (!feof(input) && (bytes = fread(buf, 1, BUFSIZ, input)) > 0) {
49 AppendToBuffer(buffer, buf, bytes);
51 AppendToBuffer(buffer, "", 1);
54 int
55 main(int argc, char *argv[])
57 char filename[] = "xbug.c";
58 FILE *input;
59 Buffer buffer;
61 InitBuffer(&buffer);
63 if (!freopen (filename, "r", stdin))
64 fprintf(stderr, "cannot open file\n");
66 unsetenv ("LD_LIBRARY_PATH");
68 if (!(input = popen("/bin/cat", "r")))
69 fprintf(stderr, "cannot run \n");
71 ReadFile(&buffer, input);
72 pclose(input);
74 return 0;