platform.texi: Move LAM after LAHF64_SAHF64
[glibc.git] / stdio-common / xbug.c
blob06f74aff005927f35df392da8a8fdb4c27c57407
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 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
17 void
18 InitBuffer (Buffer *b)
20 b->room = INIT_BUFFER_SIZE;
21 b->used = 0;
22 b->buff = (char *)malloc(INIT_BUFFER_SIZE*sizeof(char));
25 void
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)));
30 b->room *= 2;
32 strncpy(b->buff + b->used, str, len);
33 b->used += len;
36 void
37 ReadFile (Buffer *buffer, FILE *input)
39 char buf[BUFSIZ + 1];
40 int bytes;
42 buffer->used = 0;
43 while (!feof(input) && (bytes = fread(buf, 1, BUFSIZ, input)) > 0) {
44 AppendToBuffer(buffer, buf, bytes);
46 AppendToBuffer(buffer, "", 1);
49 int
50 main(int argc, char *argv[])
52 char filename[] = "xbug.c";
53 FILE *input;
54 Buffer buffer;
56 InitBuffer(&buffer);
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);
67 pclose(input);
69 return 0;