Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8.  
  9. #include "filler.h"
  10.  
  11. #define BUF_SIZE 64
  12.  
  13. typedef struct stream_s
  14. {
  15. char *str;
  16. unsigned int size;
  17. unsigned int limit;
  18. } stream_t;
  19.  
  20. void fatal(char *msg)
  21. {
  22. int len;
  23.  
  24. if (msg != NULL) {
  25. len = strlen(msg);
  26. write(2, msg, len);
  27. }
  28. exit(1);
  29. }
  30.  
  31. input_t *input()
  32. {
  33. input_t *all;
  34. int count = 0;
  35. int res;
  36. char buffer[BUF_SIZE];
  37. int flag = true;
  38. int i = 0;
  39.  
  40. usleep(50000);
  41.  
  42. FILE *logger = fopen("filler_new.log", "a");
  43. fprintf(logger,"Input\n");
  44. //dprintf(fd, "starting reading\n");
  45. all = (input_t*) malloc (sizeof(input_t));
  46. all->array = NULL;
  47. do
  48. {
  49. memset(buffer, 0, BUF_SIZE+1);
  50. res = read(0, buffer, BUF_SIZE);
  51. if(res == 0)
  52. break;
  53. fprintf(logger,"Buffer: %s\n", buffer);
  54. count += res;
  55. all->array = (char *) realloc (all->array, (count+1));
  56. if (flag)
  57. {
  58. strcpy(all->array, buffer);
  59. flag = false;
  60. }
  61. else
  62. {
  63. strcat(all->array, buffer);
  64. }
  65. }
  66. while (res == BUF_SIZE || count == 2);
  67. fprintf(logger,"count = %d\n", count);
  68. fclose(logger);
  69. return all;
  70. }
  71.  
  72. int set_nonblocking(int fd)
  73. {
  74. int flags;
  75.  
  76. flags = fcntl(fd, F_GETFL, 0);
  77. return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  78. }
  79.  
  80. void string_init(char *ptr, char *buffer)
  81. {
  82. }
  83.  
  84. char *string_append(char *ptr, char *buffer)
  85. {
  86. if (ptr == NULL) {
  87. ptr = malloc(BUF_SIZE * sizeof(char));
  88. } else {
  89.  
  90. }
  91. }
  92.  
  93. int read_input(char *request)
  94. {
  95. char buffer[BUF_SIZE];
  96. int res;
  97.  
  98. FILE *logger = fopen("filler_new.log", "a");
  99.  
  100. set_nonblocking(0); //FIXME move out
  101. while(true)
  102. {
  103. res = read(0, buffer, BUF_SIZE - 1);
  104. if (res < 0) {
  105. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  106. fprintf(logger, "GOT errno = EAGAIN#\n");
  107. break;
  108. } else {
  109. fatal("Error while reading(0)");
  110. }
  111. }
  112. buffer[BUF_SIZE - 1] = '\0';
  113. fprintf(logger,"Res: %d Buffer: %s\n", res, buffer);
  114. }
  115. }%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement