fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define BUFFSIZE 3 /* >= 2 */
  6. char *mygetline(FILE *fp) {
  7. static char inbuff[BUFFSIZE];
  8. char *outbuff_malloc, *tmpbuff;
  9. char *p, *r;
  10. int fEOL;
  11.  
  12. if ((outbuff_malloc = malloc(1)) == NULL) {
  13. return NULL;
  14. }
  15. *outbuff_malloc = '\0';
  16. fEOL = 0;
  17. do {
  18. r = fgets(inbuff, BUFFSIZE, fp);
  19. if (r == NULL)
  20. break;
  21. for (p = inbuff; *p != '\0'; p++)
  22. ;
  23. if (*(p - 1) == '\n')
  24. fEOL = 1;
  25. if ((tmpbuff = realloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1)) == NULL) {
  26. free(outbuff_malloc);
  27. return NULL;
  28. }
  29. strcat(tmpbuff, inbuff);
  30. outbuff_malloc = tmpbuff;
  31. } while (!fEOL);
  32. if (strlen(outbuff_malloc) > 0) {
  33. char c;
  34. for (p = outbuff_malloc; *p != '\0'; p++)
  35. ;
  36. while ((c = *(--p)) == '\n' || c == '\r')
  37. *p = '\0';
  38. return outbuff_malloc;
  39. }
  40. free(outbuff_malloc);
  41. return NULL;
  42. }
  43.  
  44. int main() {
  45. char *p;
  46. while ((p = mygetline(stdin)) != 0) {
  47. printf("%s\n", p);
  48. free(p);
  49. }
  50. return 0;
  51. }
  52. /* end */
  53.  
Success #stdin #stdout 0.01s 1808KB
stdin
Standard input is empty
stdout
Standard output is empty