=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/uniq/uniq.c,v retrieving revision 1.27 retrieving revision 1.28 diff -c -r1.27 -r1.28 *** src/usr.bin/uniq/uniq.c 2018/07/31 02:55:57 1.27 --- src/usr.bin/uniq/uniq.c 2021/11/01 23:20:35 1.28 *************** *** 1,4 **** ! /* $OpenBSD: uniq.c,v 1.27 2018/07/31 02:55:57 deraadt Exp $ */ /* $NetBSD: uniq.c,v 1.7 1995/08/31 22:03:48 jtc Exp $ */ /* --- 1,4 ---- ! /* $OpenBSD: uniq.c,v 1.28 2021/11/01 23:20:35 cheloha Exp $ */ /* $NetBSD: uniq.c,v 1.7 1995/08/31 22:03:48 jtc Exp $ */ /* *************** *** 45,52 **** #include #include - #define MAXLINELEN (8 * 1024) - int cflag, dflag, iflag, uflag; int numchars, numfields, repeats; --- 45,50 ---- *************** *** 59,68 **** int main(int argc, char *argv[]) { ! char *t1, *t2; FILE *ifp = NULL, *ofp = NULL; int ch; - char *prevline, *thisline; setlocale(LC_CTYPE, ""); --- 57,66 ---- int main(int argc, char *argv[]) { ! char *prevline, *t1, *t2, *thisline; FILE *ifp = NULL, *ofp = NULL; + size_t prevsize, thissize, tmpsize; int ch; setlocale(LC_CTYPE, ""); *************** *** 133,147 **** if (pledge("stdio", NULL) == -1) err(1, "pledge"); ! prevline = malloc(MAXLINELEN); ! thisline = malloc(MAXLINELEN); ! if (prevline == NULL || thisline == NULL) ! err(1, "malloc"); ! ! if (fgets(prevline, MAXLINELEN, ifp) == NULL) exit(0); ! ! while (fgets(thisline, MAXLINELEN, ifp)) { /* If requested get the chosen fields + character offsets. */ if (numfields || numchars) { t1 = skip(thisline); --- 131,148 ---- if (pledge("stdio", NULL) == -1) err(1, "pledge"); ! prevsize = 0; ! prevline = NULL; ! if (getline(&prevline, &prevsize, ifp) == -1) { ! free(prevline); ! if (ferror(ifp)) ! err(1, "getline"); exit(0); ! } ! ! thissize = 0; ! thisline = NULL; ! while (getline(&thisline, &thissize, ifp) != -1) { /* If requested get the chosen fields + character offsets. */ if (numfields || numchars) { t1 = skip(thisline); *************** *** 157,167 **** --- 158,177 ---- t1 = prevline; prevline = thisline; thisline = t1; + tmpsize = prevsize; + prevsize = thissize; + thissize = tmpsize; repeats = 0; } else ++repeats; } + free(thisline); + if (ferror(ifp)) + err(1, "getline"); + show(ofp, prevline); + free(prevline); + exit(0); }