[ create a new paste ] login | about

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

C, pasted on May 21:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int mystrcmp(char const *p, char const *q) {
  int rc;
  while (!(rc = *p++ - *q++) && (*p || *q))
    ;
  return rc;
}

#define BUFFSIZE 3 /* >= 2 */
char *mygetline(FILE *fp) {
  static char inbuff[BUFFSIZE];
  char *outbuff_malloc, *tmpbuff;
  char *p, *r;
  int fEOL;

  if ((outbuff_malloc = malloc(1)) == NULL) {
    return NULL;
  }
  *outbuff_malloc = '\0';
  fEOL = 0;
  do {
    r = fgets(inbuff, BUFFSIZE, fp);
    if (r == NULL)
      break;
    for (p = inbuff; *p != '\0'; p++)
      ;
    if (*(p - 1) == '\n')
      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) {
    for (p = outbuff_malloc; *p != '\0'; p++)
      ;
    if (*(p - 1) == '\n')
      *(p - 1) = '\0';
    return outbuff_malloc;
  }
  free(outbuff_malloc);
  return NULL;
}

int main() {
  char *a;
  char *b;
  int c;
  printf("input str1: ");
  a = mygetline(stdin);
  printf("input str2: ");
  b = mygetline(stdin);
  if (a && b) {
    if ((c = mystrcmp(a, b)) < 0)
      printf("str1 < str2\n");
    else if (c == 0)
      printf("str1 == str2\n");
    else /* if (c > 0) */
      printf("str1 > str2\n");
  }
  free(a);
  free(b);
  return 0;
}
/* end */


Output:
1
input str1: input str2: 


Create a new paste based on this one


Comments: