[hackers] [sbase] uniq: add ascii implementation of -f and -s flags || Tai Chi Minh Ralph Eastwood

From: <git_AT_suckless.org>
Date: Wed, 11 Feb 2015 12:55:27 +0100 (CET)

commit e0755735b0c0487c0f1270ed155e05563d3c26e2
Author: Tai Chi Minh Ralph Eastwood <tcmreastwood_AT_gmail.com>
Date: Wed Feb 11 06:02:54 2015 +0000

    uniq: add ascii implementation of -f and -s flags

diff --git a/uniq.c b/uniq.c
index 8600adb..5685b40 100644
--- a/uniq.c
+++ b/uniq.c
_AT_@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <ctype.h>
 
 #include "text.h"
 #include "util.h"
_AT_@ -14,8 +15,11 @@ static void uniqfinish(void);
 static const char *countfmt = "";
 static int dflag = 0;
 static int uflag = 0;
+static int fskip = 0;
+static int sskip = 0;
 
 static char *prevline = NULL;
+static char *prevoffset = NULL;
 static long prevlinecount = 0;
 
 static void
_AT_@ -41,6 +45,12 @@ main(int argc, char *argv[])
         case 'u':
                 uflag = 1;
                 break;
+ case 'f':
+ fskip = estrtonum(EARGF(usage()), 0, INT_MAX);
+ break;
+ case 's':
+ sskip = estrtonum(EARGF(usage()), 0, INT_MAX);
+ break;
         default:
                 usage();
         } ARGEND;
_AT_@ -59,12 +69,29 @@ main(int argc, char *argv[])
         return 0;
 }
 
+static char *
+uniqskip(char *l)
+{
+ char *lo = l;
+ int f = fskip, s = sskip;
+ for (; f; --f) {
+ while (isblank(*lo))
+ lo++;
+ while (*lo && !isblank(*lo))
+ lo++;
+ }
+ for (; s && *lo && *lo != '\n'; --s, ++lo);
+ return lo;
+}
+
 static void
 uniqline(char *l)
 {
+ char *loffset = l ? uniqskip(l) : l;
+
         int linesequel = (!l || !prevline)
                 ? l == prevline
- : !strcmp(l, prevline);
+ : !strcmp(loffset, prevoffset);
 
         if (linesequel) {
                 ++prevlinecount;
_AT_@ -78,11 +105,13 @@ uniqline(char *l)
                         fputs(prevline, stdout);
                 }
                 free(prevline);
- prevline = NULL;
+ prevline = prevoffset = NULL;
         }
 
- if (l)
+ if (l) {
                 prevline = estrdup(l);
+ prevoffset = prevline + (loffset - l);
+ }
         prevlinecount = 1;
 }
 
Received on Wed Feb 11 2015 - 12:55:27 CET

This archive was generated by hypermail 2.3.0 : Wed Feb 11 2015 - 13:00:15 CET