6 typedef struct _Buffer
{
11 void InitBuffer (Buffer
*b
);
12 void AppendToBuffer (register Buffer
*b
, const char *str
, register int len
);
13 void ReadFile (register Buffer
*buffer
, FILE *input
);
15 #define INIT_BUFFER_SIZE 10000
20 b
->room
= INIT_BUFFER_SIZE
;
22 b
->buff
= (char *)malloc(INIT_BUFFER_SIZE
*sizeof(char));
25 void AppendToBuffer(b
, str
, len
)
30 while (b
->used
+ len
> b
->room
) {
31 b
->buff
= (char *)realloc(b
->buff
, 2*b
->room
*(sizeof(char)));
34 strncpy(b
->buff
+ b
->used
, str
, len
);
38 void ReadFile(buffer
, input
)
39 register Buffer
*buffer
;
46 while (!feof(input
) && (bytes
= fread(buf
, 1, BUFSIZ
, input
)) > 0) {
47 AppendToBuffer(buffer
, buf
, bytes
);
49 AppendToBuffer(buffer
, "", 1);
53 main(int argc
, char *argv
[])
55 char filename
[] = "xbug.c";
61 if (!freopen (filename
, "r", stdin
))
62 fprintf(stderr
, "cannot open file\n");
64 unsetenv ("LD_LIBRARY_PATH");
66 if (!(input
= popen("/bin/cat", "r")))
67 fprintf(stderr
, "cannot run \n");
69 ReadFile(&buffer
, input
);