[ create a new paste ] login | about

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

C, pasted on Jul 8:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* #define DEBUG */
#if defined(DEBUG)
#include "xmalloc.h"
#else
#define xmalloc(x, y) malloc(x)
#define xfree(x, y) free(x)
#define xrealloc(x, y, z) realloc(x, y)
#define xmallocdump()
#endif
#define ID_GETLINE  1001

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

  if ((outbuff_malloc = xmalloc(1, ID_GETLINE)) == 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 = xrealloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1, ID_GETLINE)) == NULL) {
      xfree(outbuff_malloc, ID_GETLINE);
      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;
  }
  xfree(outbuff_malloc, ID_GETLINE);
  return NULL;
}

int main(int argc, char *argv[]) {
  FILE *fp;
  char *p;
  if (argc != 3) {
    fprintf(stderr, "usage: %s <word> <file>\n", argv[0]);
    exit(1);
  }
  if ((fp = fopen(argv[2], "r")) != NULL) {
    while ((p = mygetline(fp)) != NULL) {
      if (strstr(p, argv[1]))
        printf("%s\n", p);
      xfree(p, ID_GETLINE);
    }
    fclose(fp);
  }
  xmallocdump();
  return 0;
}
/* end */


Output:
1
usage: /t <word> <file>


Create a new paste based on this one


Comments: