A memory leak is left (this tool deserves a reengineering)

This commit is contained in:
Pierre Pronchery 2006-10-27 10:45:55 +00:00
parent 653f354b82
commit a3c61e5358

View File

@ -58,6 +58,7 @@ static void _do_count(int opts, unsigned int skip, char * line, FILE * fp);
static int _uniq_do(int opts, char const * fields, unsigned int skip, static int _uniq_do(int opts, char const * fields, unsigned int skip,
FILE * infp, FILE * outfp) FILE * infp, FILE * outfp)
{ {
int ret = 0;
#define BUF 80 #define BUF 80
char * line = NULL; char * line = NULL;
int len = 0; int len = 0;
@ -68,13 +69,14 @@ static int _uniq_do(int opts, char const * fields, unsigned int skip,
if((p = realloc(line, len + BUF + 1)) == NULL) if((p = realloc(line, len + BUF + 1)) == NULL)
{ {
free(line); free(line);
return _uniq_error("malloc", 1); ret = _uniq_error("malloc", 1);
break;
} }
line = p; line = p;
if(fgets(&line[len], BUF + 1, infp) == NULL) if(fgets(&line[len], BUF + 1, infp) == NULL)
{ {
if(!feof(infp)) if(!feof(infp))
_uniq_error("fread", 0); ret = _uniq_error("fread", 1);
break; break;
} }
for(p = &line[len]; *p != '\0' && *p != '\n'; p++); for(p = &line[len]; *p != '\0' && *p != '\n'; p++);
@ -91,8 +93,7 @@ static int _uniq_do(int opts, char const * fields, unsigned int skip,
len = 0; len = 0;
} }
_do_count(opts, skip, NULL, outfp); _do_count(opts, skip, NULL, outfp);
free(line); return ret;
return 0;
} }
static int _count_repeated(char * lastline, char * line, unsigned int skip); static int _count_repeated(char * lastline, char * line, unsigned int skip);
@ -104,10 +105,12 @@ static void _do_count(int opts, unsigned int skip, char * line, FILE * fp)
if(lastline == NULL) if(lastline == NULL)
{ {
lastline = line; lastline = line;
cnt = 1;
return; return;
} }
if(line != NULL && _count_repeated(lastline, line, skip)) if(line != NULL && _count_repeated(lastline, line, skip))
{ {
free(line);
cnt++; cnt++;
return; return;
} }