From 3f16dee9336114efc02ffb63e5f4d2e6f46e66a3 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 6 Oct 2020 13:29:10 -0400 Subject: implement -s and -f skipping --- uniq.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/uniq.c b/uniq.c index e96fd7a..b1fabd1 100644 --- a/uniq.c +++ b/uniq.c @@ -23,6 +23,7 @@ */ #define _XOPEN_SOURCE 700 +#include #include #include #include @@ -61,10 +62,43 @@ static void print_line(FILE *out, const char *line, uintmax_t count, unsigned in fprintf(out, "%s", line); } -int are_duplicates(const char *s1, const char *s2, size_t fields, size_t chars) +static char *skip_fields(const char *s, size_t fields) { - /* TODO: skip first fields */ - /* TODO: skip first chars */ + while (fields-- > 0) { + if (*s == '\0') { + break; + } + + while (*s && !isblank(*s)) { + s++; + } + + while (*s && isblank(*s)) { + s++; + } + } + return (char*)s; +} + +static char *skip_chars(const char *s, size_t chars) +{ + while (chars-- > 0) { + if (*s == '\0') { + break; + } + s++; + } + return (char*)s; +} + +static int are_duplicates(const char *s1, const char *s2, size_t fields, size_t chars) +{ + s1 = skip_fields(s1, fields); + s2 = skip_fields(s2, fields); + + s1 = skip_chars(s1, chars); + s2 = skip_chars(s2, chars); + return strcmp(s1, s2) == 0; } -- cgit v1.2.1