6 typedef struct _Buffer
{
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
);
17 #define INIT_BUFFER_SIZE 10000
22 b
->room
= INIT_BUFFER_SIZE
;
24 b
->buff
= (char *)malloc(INIT_BUFFER_SIZE
*sizeof(char));
27 void AppendToBuffer(b
, str
, len
)
32 while (b
->used
+ len
> b
->room
) {
33 b
->buff
= (char *)realloc(b
->buff
, 2*b
->room
*(sizeof(char)));
36 strncpy(b
->buff
+ b
->used
, str
, len
);
40 void ReadFile(buffer
, input
)
41 register Buffer
*buffer
;
48 while (!feof(input
) && (bytes
= fread(buf
, 1, BUFSIZ
, input
)) > 0) {
49 AppendToBuffer(buffer
, buf
, bytes
);
51 AppendToBuffer(buffer
, "", 1);
55 main(int argc
, char *argv
[])
57 char filename
[] = "xbug.c";
63 if (!freopen (filename
, "r", stdin
))
64 fprintf(stderr
, "cannot open file\n");
66 if (!(input
= popen("/bin/cat", "r")))
67 fprintf(stderr
, "cannot run \n");
69 ReadFile(&buffer
, input
);