sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 0fa5a3e5bb40b92e4141bdfcc37603805d520923
parent 54d3f3b3a5a24276b054d573bee1325da1f4495a
Author: FRIGN <dev@frign.de>
Date:   Mon,  7 Mar 2016 02:04:29 +0100

Rename struct linebufline to struct line and add linecmp()

This simplifies the handling in sort(1) and comm(1) quite a bit.

Diffstat:
MMakefile | 1+
Mcomm.c | 9+++------
Msort.c | 24++++++++++--------------
Mtext.h | 5+++--
4 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/Makefile b/Makefile @@ -57,6 +57,7 @@ LIBUTILSRC =\ libutil/fshut.c\ libutil/getlines.c\ libutil/human.c\ + libutil/linecmp.c\ libutil/md5.c\ libutil/memmem.c\ libutil/mkdirp.c\ diff --git a/comm.c b/comm.c @@ -9,7 +9,7 @@ static int show = 0x07; static void -printline(int pos, struct linebufline *line) +printline(int pos, struct line *line) { int i; @@ -33,7 +33,7 @@ int main(int argc, char *argv[]) { FILE *fp[2]; - static struct linebufline line[2]; + static struct line line[2]; size_t linecap[2] = { 0, 0 }; ssize_t len; int ret = 0, i, diff = 0, seenline = 0; @@ -83,10 +83,7 @@ main(int argc, char *argv[]) eprintf("getline %s:", argv[!i]); goto end; } - if (!(diff = memcmp(line[0].data, line[1].data, - MIN(line[0].len, line[1].len)))) { - diff = (line[0].len > line[1].len); - } + diff = linecmp(&line[0], &line[1]); LIMIT(diff, -1, 1); seenline = 0; printline((2 - diff) % 3, &line[MAX(0, diff)]); diff --git a/sort.c b/sort.c @@ -33,10 +33,10 @@ static TAILQ_HEAD(kdhead, keydef) kdhead = TAILQ_HEAD_INITIALIZER(kdhead); static int Cflag = 0, cflag = 0, uflag = 0; static char *fieldsep = NULL; static size_t fieldseplen = 0; -static struct linebufline col1, col2; +static struct line col1, col2; static void -skipblank(struct linebufline *a) +skipblank(struct line *a) { while (a->len && (*(a->data) == ' ' || *(a->data) == '\t')) { a->data++; @@ -45,7 +45,7 @@ skipblank(struct linebufline *a) } static void -skipnonblank(struct linebufline *a) +skipnonblank(struct line *a) { while (a->len && (*(a->data) != '\n' && *(a->data) != ' ' && *(a->data) != '\t')) { @@ -55,7 +55,7 @@ skipnonblank(struct linebufline *a) } static void -skipcolumn(struct linebufline *a, int skip_to_next_col) +skipcolumn(struct line *a, int skip_to_next_col) { char *s; @@ -77,10 +77,10 @@ skipcolumn(struct linebufline *a, int skip_to_next_col) } static size_t -columns(struct linebufline *line, const struct keydef *kd, struct linebufline *col) +columns(struct line *line, const struct keydef *kd, struct line *col) { Rune r; - struct linebufline start, end; + struct line start, end; size_t len, utflen, rlen; int i; @@ -130,7 +130,7 @@ columns(struct linebufline *line, const struct keydef *kd, struct linebufline *c } static int -skipmodcmp(struct linebufline *a, struct linebufline *b, int flags) +skipmodcmp(struct line *a, struct line *b, int flags) { Rune r1, r2; size_t offa = 0, offb = 0; @@ -171,7 +171,7 @@ skipmodcmp(struct linebufline *a, struct linebufline *b, int flags) } static int -slinecmp(struct linebufline *a, struct linebufline *b) +slinecmp(struct line *a, struct line *b) { int res = 0; long double x, y; @@ -193,11 +193,7 @@ slinecmp(struct linebufline *a, struct linebufline *b) } else if (kd->flags & (MOD_D | MOD_F | MOD_I)) { res = skipmodcmp(&col1, &col2, kd->flags); } else { - if (!(res = memcmp(col1.data, col2.data, - MIN(col1.len, col2.len)))) { - res = col1.data[MIN(col1.len, col2.len)] - - col2.data[MIN(col1.len, col2.len)]; - } + res = linecmp(&col1, &col2); } if (kd->flags & MOD_R) @@ -212,7 +208,7 @@ slinecmp(struct linebufline *a, struct linebufline *b) static int check(FILE *fp, const char *fname) { - static struct linebufline prev, cur, tmp; + static struct line prev, cur, tmp; static size_t prevsize, cursize, tmpsize; if (!prev.data && (prev.len = getline(&prev.data, &prevsize, fp)) < 0) diff --git a/text.h b/text.h @@ -1,12 +1,12 @@ /* See LICENSE file for copyright and license details. */ -struct linebufline { +struct line { char *data; size_t len; }; struct linebuf { - struct linebufline *lines; + struct line *lines; size_t nlines; size_t capacity; }; @@ -14,3 +14,4 @@ struct linebuf { void getlines(FILE *, struct linebuf *); void concat(FILE *, const char *, FILE *, const char *); +int linecmp(struct line *, struct line *);