Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <fcntl.h>
- #include <errno.h>
- #include "filler.h"
- #define BUF_SIZE 64
- typedef struct stream_s
- {
- char *str;
- unsigned int size;
- unsigned int limit;
- } stream_t;
- void fatal(char *msg)
- {
- int len;
- if (msg != NULL) {
- len = strlen(msg);
- write(2, msg, len);
- }
- exit(1);
- }
- input_t *input()
- {
- input_t *all;
- int count = 0;
- int res;
- char buffer[BUF_SIZE];
- int flag = true;
- int i = 0;
- usleep(50000);
- FILE *logger = fopen("filler_new.log", "a");
- fprintf(logger,"Input\n");
- //dprintf(fd, "starting reading\n");
- all = (input_t*) malloc (sizeof(input_t));
- all->array = NULL;
- do
- {
- memset(buffer, 0, BUF_SIZE+1);
- res = read(0, buffer, BUF_SIZE);
- if(res == 0)
- break;
- fprintf(logger,"Buffer: %s\n", buffer);
- count += res;
- all->array = (char *) realloc (all->array, (count+1));
- if (flag)
- {
- strcpy(all->array, buffer);
- flag = false;
- }
- else
- {
- strcat(all->array, buffer);
- }
- }
- while (res == BUF_SIZE || count == 2);
- fprintf(logger,"count = %d\n", count);
- fclose(logger);
- return all;
- }
- int set_nonblocking(int fd)
- {
- int flags;
- flags = fcntl(fd, F_GETFL, 0);
- return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
- }
- void string_init(char *ptr, char *buffer)
- {
- }
- char *string_append(char *ptr, char *buffer)
- {
- if (ptr == NULL) {
- ptr = malloc(BUF_SIZE * sizeof(char));
- } else {
- }
- }
- int read_input(char *request)
- {
- char buffer[BUF_SIZE];
- int res;
- FILE *logger = fopen("filler_new.log", "a");
- set_nonblocking(0); //FIXME move out
- while(true)
- {
- res = read(0, buffer, BUF_SIZE - 1);
- if (res < 0) {
- if (errno == EAGAIN || errno == EWOULDBLOCK) {
- fprintf(logger, "GOT errno = EAGAIN#\n");
- break;
- } else {
- fatal("Error while reading(0)");
- }
- }
- buffer[BUF_SIZE - 1] = '\0';
- fprintf(logger,"Res: %d Buffer: %s\n", res, buffer);
- }
- }%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement