[ create a new paste ] login | about

Link: http://codepad.org/hMstBppC    [ raw code | output | fork ]

C, pasted on Oct 23:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFSIZE 3 /* >= 2 */
char *getline(FILE *fp)
{
  static char inbuff[BUFFSIZE];
  char *outbuff_malloc, *tmpbuff;
  char *p;
  int fEOL;
  if ((outbuff_malloc = malloc(1)) == NULL) {
    return NULL;
  }
  *outbuff_malloc = '\0';
  fEOL = 0;
  do {
    if (fgets(inbuff, BUFFSIZE, fp) == NULL)
      break;
    for (p = inbuff; *p != '\0'; p++)
      ;
    if (*(p - 1) == '\n') {
      *(p - 1) = '\0';
      fEOL = 1;
    }
    if ((tmpbuff = realloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1)) ==NULL) {
      free(outbuff_malloc);
      return NULL;
    }
    strcat(tmpbuff, inbuff);
    outbuff_malloc = tmpbuff;
  } while (!fEOL);
  if (strlen(outbuff_malloc) == 0) {
    free(outbuff_malloc);
    return NULL;
  }
  return outbuff_malloc;
}
int main() {
  char *p, *q, *r, *t;
  printf("input 1:");
  p = getline(stdin);
  printf("input 2:");
  q = getline(stdin);
  printf("input 3:");
  r = getline(stdin);

  if (strlen(p) > strlen(q)) { t = p; p = q; q = t; }
  if (strlen(q) > strlen(r)) { t = q; q = r; r = t; }
  if (strlen(p) > strlen(q)) { t = p; p = q; q = t; }

  printf("1: %s, 2: %s, 3: %s\n", p, q, r);
  free(p);
  free(q);
  free(r);
  return 0;
}
/* end */


Output:
1
Segmentation fault


Create a new paste based on this one


Comments: