[hackers] [sbase] cut: improvements || Hiltjo Posthuma
commit 97ca7c8b6df0c90e600cbbd258b74bd524d78aef
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun Jun 1 14:39:34 2014 +0200
cut: improvements
- use agetline().
- code style.
- free allocated list.
- don't close stdin if "-" is given.
diff --git a/cut.c b/cut.c
index d21fcbe..64081f9 100644
--- a/cut.c
+++ b/cut.c
_AT_@ -31,19 +31,23 @@ insert(Range *r)
Range *l, *p, *t;
for(p = NULL, l = list; l; p = l, l = l->next) {
- if(r->max && r->max+1 < l->min) {
+ if(r->max && r->max + 1 < l->min) {
r->next = l;
break;
- } else if(!l->max || r->min < l->max+2) {
+ } else if(!l->max || r->min < l->max + 2) {
l->min = MIN(r->min, l->min);
for(p = l, t = l->next; t; p = t, t = t->next)
- if(r->max && r->max+1 < t->min) break;
+ if(r->max && r->max + 1 < t->min)
+ break;
l->max = (p->max && r->max) ? MAX(p->max, r->max) : 0;
l->next = t;
return;
}
}
- if(p) p->next = r; else list = r;
+ if(p)
+ p->next = r;
+ else
+ list = r;
}
static void
_AT_@ -54,8 +58,10 @@ parselist(char *str)
Range *r;
for(s = str; *s; s++) {
- if(*s == ' ') *s = ',';
- if(*s == ',') n++;
+ if(*s == ' ')
+ *s = ',';
+ if(*s == ',')
+ n++;
}
if(!(r = malloc(n * sizeof(Range))))
eprintf("malloc:");
_AT_@ -69,6 +75,18 @@ parselist(char *str)
}
}
+static void
+freelist(void) {
+ Range *l = list, *next;
+
+ while(l) {
+ next = l->next;
+ free(l);
+ l->next = NULL;
+ l = next;
+ }
+}
+
static size_t
seek(const char *s, size_t pos, size_t *prev, size_t count)
{
_AT_@ -79,15 +97,18 @@ seek(const char *s, size_t pos, size_t *prev, size_t count)
if((t = memchr(s, 0, n)))
return t - s;
if(nflag)
- while(n && !UTF8_POINT(s[n])) n--;
+ while(n && !UTF8_POINT(s[n]))
+ n--;
*prev += n;
return n;
} else if(mode == 'c') {
for(n++, t = s; *t; t++)
- if(UTF8_POINT(*t) && !--n) break;
+ if(UTF8_POINT(*t) && !--n)
+ break;
} else {
- for(t = (count < 2) ? s : s+1; n && *t; t++)
- if(*t == delim && !--n && count) break;
+ for(t = (count < 2) ? s : s + 1; n && *t; t++)
+ if(*t == delim && !--n && count)
+ break;
}
*prev = pos;
return t - s;
_AT_@ -96,15 +117,14 @@ seek(const char *s, size_t pos, size_t *prev, size_t count)
static void
cut(FILE *fp)
{
- char *buf = NULL;
- size_t size = 0;
- char *s;
- size_t i, n, p;
+ char *buf = NULL, *s;
+ size_t size = 0, i, n, p;
+ ssize_t len;
Range *r;
- while(afgets(&buf, &size, fp)) {
- if(buf[i = strlen(buf)-1] == '
')
- buf[i] = 0;
+ while((len = agetline(&buf, &size, fp)) != -1) {
+ if(len && buf[len - 1] == '
')
+ buf[len - 1] = '
Received on Sun Jun 01 2014 - 19:02:17 CEST
This archive was generated by hypermail 2.3.0
: Sun Jun 01 2014 - 19:12:12 CEST