6 typedef struct _Buffer
{
11 void InitBuffer (Buffer
*b
);
12 void AppendToBuffer (Buffer
*b
, const char *str
, int len
);
13 void ReadFile (Buffer
*buffer
, FILE *input
);
15 #define INIT_BUFFER_SIZE 10000
18 InitBuffer (Buffer
*b
)
20 b
->room
= INIT_BUFFER_SIZE
;
22 b
->buff
= (char *)malloc(INIT_BUFFER_SIZE
*sizeof(char));
26 AppendToBuffer (Buffer
*b
, const char *str
, int len
)
28 while (b
->used
+ len
> b
->room
) {
29 b
->buff
= (char *)realloc(b
->buff
, 2*b
->room
*(sizeof(char)));
32 strncpy(b
->buff
+ b
->used
, str
, len
);
37 ReadFile (Buffer
*buffer
, FILE *input
)
43 while (!feof(input
) && (bytes
= fread(buf
, 1, BUFSIZ
, input
)) > 0) {
44 AppendToBuffer(buffer
, buf
, bytes
);
46 AppendToBuffer(buffer
, "", 1);
50 main(int argc
, char *argv
[])
52 char filename
[] = "xbug.c";
58 if (!freopen (filename
, "r", stdin
))
59 fprintf(stderr
, "cannot open file\n");
61 unsetenv ("LD_LIBRARY_PATH");
63 if (!(input
= popen("/bin/cat", "r")))
64 fprintf(stderr
, "cannot run \n");
66 ReadFile(&buffer
, input
);