Blame stdio-common/xbug.c

Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
typedef struct _Buffer {
Packit Service 82fcde
  char *buff;
Packit Service 82fcde
  int  room, used;
Packit Service 82fcde
} Buffer;
Packit Service 82fcde
Packit Service 82fcde
void InitBuffer (Buffer *b);
Packit Service 82fcde
void AppendToBuffer (Buffer *b, const char *str, int len);
Packit Service 82fcde
void ReadFile (Buffer *buffer, FILE *input);
Packit Service 82fcde
Packit Service 82fcde
#define INIT_BUFFER_SIZE 10000
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
InitBuffer (Buffer *b)
Packit Service 82fcde
{
Packit Service 82fcde
  b->room = INIT_BUFFER_SIZE;
Packit Service 82fcde
  b->used = 0;
Packit Service 82fcde
  b->buff = (char *)malloc(INIT_BUFFER_SIZE*sizeof(char));
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
AppendToBuffer (Buffer *b, const char *str, int len)
Packit Service 82fcde
{
Packit Service 82fcde
  while (b->used + len > b->room) {
Packit Service 82fcde
    b->buff = (char *)realloc(b->buff, 2*b->room*(sizeof(char)));
Packit Service 82fcde
    b->room *= 2;
Packit Service 82fcde
  }
Packit Service 82fcde
  strncpy(b->buff + b->used, str, len);
Packit Service 82fcde
  b->used += len;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
ReadFile (Buffer *buffer, FILE *input)
Packit Service 82fcde
{
Packit Service 82fcde
  char       buf[BUFSIZ + 1];
Packit Service 82fcde
  int        bytes;
Packit Service 82fcde
Packit Service 82fcde
  buffer->used = 0;
Packit Service 82fcde
  while (!feof(input) && (bytes = fread(buf, 1, BUFSIZ, input)) > 0) {
Packit Service 82fcde
    AppendToBuffer(buffer, buf, bytes);
Packit Service 82fcde
  }
Packit Service 82fcde
  AppendToBuffer(buffer, "", 1);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
main(int argc, char *argv[])
Packit Service 82fcde
{
Packit Service 82fcde
  char filename[] = "xbug.c";
Packit Service 82fcde
  FILE *input;
Packit Service 82fcde
  Buffer buffer;
Packit Service 82fcde
Packit Service 82fcde
  InitBuffer(&buffer);
Packit Service 82fcde
Packit Service 82fcde
  if (!freopen (filename, "r", stdin))
Packit Service 82fcde
    fprintf(stderr, "cannot open file\n");
Packit Service 82fcde
Packit Service 82fcde
  unsetenv ("LD_LIBRARY_PATH");
Packit Service 82fcde
Packit Service 82fcde
  if (!(input = popen("/bin/cat", "r")))
Packit Service 82fcde
    fprintf(stderr, "cannot run \n");
Packit Service 82fcde
Packit Service 82fcde
  ReadFile(&buffer, input);
Packit Service 82fcde
  pclose(input);
Packit Service 82fcde
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}