commit d965985a5219ed5dbe09fc83177beb5bb56f73fd
parent a62a2197a8e49f2c40f6c7c10a28339f1dd28dbe
Author: Jakob Kramer <jakob.kramer@gmx.de>
Date: Sat, 3 May 2014 20:24:08 +0200
sort: add -b flag; don't use it as default
Diffstat:
M | sort.c | | | 33 | +++++++++++++++++++++++++++------ |
1 file changed, 27 insertions(+), 6 deletions(-)
diff --git a/sort.c b/sort.c
@@ -30,11 +30,13 @@ static char *next_nonblank(char *);
static char *next_blank(char *);
static int parse_keydef(struct keydef *, char *);
static char *skip_columns(char *, size_t);
+static char *end_column(char *);
static char *columns(char *, const struct keydef *);
static bool rflag = false;
static bool uflag = false;
static bool nflag = false;
+static bool bflag = false;
static void
usage(void)
@@ -59,6 +61,9 @@ main(int argc, char *argv[])
case 'u':
uflag = true;
break;
+ case 'b':
+ bflag = true;
+ break;
case 'k':
addkeydef(EARGF(usage()));
break;
@@ -169,7 +174,7 @@ parse_keydef(struct keydef *kd, char *s)
kd->start_char = strtoul(rest+1, &rest, 10);
if(*rest == ',') {
kd->end_column = strtoul(rest+1, &rest, 10);
- if(kd->end_column < kd->start_column)
+ if(kd->end_column && kd->end_column < kd->start_column)
enprintf(2, ",%u is too small\n", kd->end_column);
if(*rest == '.')
kd->end_char = strtoul(rest+1, &rest, 10);
@@ -201,29 +206,45 @@ skip_columns(char *s, size_t n)
size_t i;
for(i = 0; i < n; i++) {
- if(i != 0)
+ if(bflag) {
+ if(i != 0)
+ s = next_blank(s);
+ s = next_nonblank(s);
+ } else {
+ if(i == 0)
+ continue;
+ s = next_nonblank(s);
s = next_blank(s);
- s = next_nonblank(s);
+ }
}
return s;
}
static char *
+end_column(char *s)
+{
+ if(bflag)
+ return next_blank(s);
+ else
+ return next_blank(next_nonblank(s));
+}
+
+static char *
columns(char *line, const struct keydef *kd)
{
char *start, *end;
char *res;
start = skip_columns(line, kd->start_column);
- start += MIN(kd->start_char, next_blank(start) - start) - 1;
+ start += MIN(kd->start_char, end_column(start) - start) - 1;
if(kd->end_column) {
end = skip_columns(line, kd->end_column);
if(kd->end_char)
- end += MIN(kd->end_char, next_blank(end) - end);
+ end += MIN(kd->end_char, end_column(end) - end);
else
- end = next_blank(end);
+ end = end_column(end);
} else {
end = line + strlen(line);
}