2011-06-02 12:03:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-04-12 15:53:10 +00:00
|
|
|
#include <ctype.h>
|
2011-06-02 12:03:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-06-02 12:03:34 +00:00
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
struct keydef {
|
2014-05-06 11:35:06 +00:00
|
|
|
int start_column;
|
|
|
|
int end_column;
|
|
|
|
int start_char;
|
|
|
|
int end_char;
|
2014-05-06 14:07:05 +00:00
|
|
|
int flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MOD_N = 1 << 1,
|
|
|
|
MOD_STARTB = 1 << 2,
|
|
|
|
MOD_ENDB = 1 << 3,
|
2014-11-13 17:29:30 +00:00
|
|
|
MOD_R = 1 << 4,
|
2014-04-12 15:53:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct kdlist {
|
|
|
|
struct keydef keydef;
|
|
|
|
struct kdlist *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kdlist *head = NULL;
|
2014-05-06 16:47:02 +00:00
|
|
|
static struct kdlist *tail = NULL;
|
2014-04-12 15:53:10 +00:00
|
|
|
|
2014-05-06 14:07:05 +00:00
|
|
|
static void addkeydef(char *, int);
|
2014-11-23 19:35:56 +00:00
|
|
|
static void check(FILE *);
|
2011-06-02 12:03:34 +00:00
|
|
|
static int linecmp(const char **, const char **);
|
2014-05-06 14:07:05 +00:00
|
|
|
static int parse_flags(char **, int *, int);
|
|
|
|
static int parse_keydef(struct keydef *, char *, int);
|
2015-02-01 20:00:37 +00:00
|
|
|
static char *skipblank(char *);
|
|
|
|
static char *skipnonblank(char *);
|
|
|
|
static char *skipcolumn(char *, char *, int);
|
2015-05-05 11:41:43 +00:00
|
|
|
static size_t columns(char *, const struct keydef *, char **, size_t *);
|
2011-06-02 12:03:34 +00:00
|
|
|
|
2014-11-23 19:35:56 +00:00
|
|
|
static int Cflag = 0, cflag = 0, uflag = 0;
|
2014-05-15 18:08:17 +00:00
|
|
|
static char *fieldsep = NULL;
|
2015-05-05 11:41:43 +00:00
|
|
|
static char *col1, *col2;
|
|
|
|
static size_t col1siz, col2siz;
|
2012-05-21 20:09:44 +00:00
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
static void
|
2014-05-06 14:07:05 +00:00
|
|
|
addkeydef(char *def, int flags)
|
2014-04-12 15:53:10 +00:00
|
|
|
{
|
|
|
|
struct kdlist *node;
|
|
|
|
|
2015-02-11 01:08:17 +00:00
|
|
|
node = enmalloc(2, sizeof(*node));
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!head)
|
2014-04-12 15:53:10 +00:00
|
|
|
head = node;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (parse_keydef(&node->keydef, def, flags))
|
2014-05-03 17:06:20 +00:00
|
|
|
enprintf(2, "faulty key definition\n");
|
2014-11-13 17:29:30 +00:00
|
|
|
if (tail)
|
2014-05-06 16:47:02 +00:00
|
|
|
tail->next = node;
|
2014-04-12 15:53:10 +00:00
|
|
|
node->next = NULL;
|
2014-05-06 16:47:02 +00:00
|
|
|
tail = node;
|
2014-04-12 15:53:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 19:35:56 +00:00
|
|
|
static void
|
|
|
|
check(FILE *fp)
|
|
|
|
{
|
|
|
|
static struct { char *buf; size_t size; } prev, cur, tmp;
|
|
|
|
|
2015-03-27 13:49:48 +00:00
|
|
|
if (!prev.buf && getline(&prev.buf, &prev.size, fp) < 0)
|
|
|
|
eprintf("getline:");
|
|
|
|
while (getline(&cur.buf, &cur.size, fp) > 0) {
|
2014-11-23 19:35:56 +00:00
|
|
|
if (uflag > linecmp((const char **) &cur.buf, (const char **) &prev.buf)) {
|
|
|
|
if (!Cflag)
|
|
|
|
weprintf("disorder: %s", cur.buf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
tmp = cur;
|
|
|
|
cur = prev;
|
|
|
|
prev = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
static int
|
2011-06-02 12:03:34 +00:00
|
|
|
linecmp(const char **a, const char **b)
|
|
|
|
{
|
2014-04-12 15:53:10 +00:00
|
|
|
int res = 0;
|
2015-01-31 18:12:18 +00:00
|
|
|
long double x, y;
|
2014-04-12 15:53:10 +00:00
|
|
|
struct kdlist *node;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (node = head; node && res == 0; node = node->next) {
|
2015-05-05 11:41:43 +00:00
|
|
|
columns((char *)*a, &node->keydef, &col1, &col1siz);
|
|
|
|
columns((char *)*b, &node->keydef, &col2, &col2siz);
|
2014-04-12 15:53:10 +00:00
|
|
|
|
2014-05-06 16:47:02 +00:00
|
|
|
/* if -u is given, don't use default key definition
|
|
|
|
* unless it is the only one */
|
2015-01-31 18:12:18 +00:00
|
|
|
if (uflag && node == tail && head != tail) {
|
2014-04-12 15:53:10 +00:00
|
|
|
res = 0;
|
2015-01-31 18:12:18 +00:00
|
|
|
} else if (node->keydef.flags & MOD_N) {
|
2015-05-05 11:41:43 +00:00
|
|
|
x = strtold(col1, NULL);
|
|
|
|
y = strtold(col2, NULL);
|
2015-01-31 18:12:18 +00:00
|
|
|
res = x < y ? -1 : x > y;
|
|
|
|
} else {
|
2015-05-05 11:41:43 +00:00
|
|
|
res = strcmp(col1, col2);
|
2015-01-31 18:12:18 +00:00
|
|
|
}
|
2014-04-12 15:53:10 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (node->keydef.flags & MOD_R)
|
2014-05-06 14:07:05 +00:00
|
|
|
res = -res;
|
2014-04-12 15:53:10 +00:00
|
|
|
}
|
2015-03-22 22:37:37 +00:00
|
|
|
|
2014-05-06 14:07:05 +00:00
|
|
|
return res;
|
2014-04-12 15:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-05-06 14:07:05 +00:00
|
|
|
parse_flags(char **s, int *flags, int bflag)
|
|
|
|
{
|
2015-03-23 19:30:07 +00:00
|
|
|
while (isalpha((int)**s)) {
|
2014-11-13 17:29:30 +00:00
|
|
|
switch (*((*s)++)) {
|
2014-05-06 14:07:05 +00:00
|
|
|
case 'b':
|
|
|
|
*flags |= bflag;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
*flags |= MOD_N;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
*flags |= MOD_R;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-22 22:37:37 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 14:07:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_keydef(struct keydef *kd, char *s, int flags)
|
2014-04-12 15:53:10 +00:00
|
|
|
{
|
|
|
|
char *rest = s;
|
2014-05-03 17:06:20 +00:00
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
kd->start_column = 1;
|
|
|
|
kd->start_char = 1;
|
|
|
|
/* 0 means end of line */
|
|
|
|
kd->end_column = 0;
|
|
|
|
kd->end_char = 0;
|
2014-05-06 14:07:05 +00:00
|
|
|
kd->flags = flags;
|
2014-04-12 15:53:10 +00:00
|
|
|
|
2014-05-06 11:35:06 +00:00
|
|
|
kd->start_column = strtol(rest, &rest, 10);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->start_column < 1)
|
2014-05-06 11:35:06 +00:00
|
|
|
return -1;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*rest == '.')
|
2014-05-06 11:35:06 +00:00
|
|
|
kd->start_char = strtol(rest+1, &rest, 10);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->start_char < 1)
|
2014-05-06 11:35:06 +00:00
|
|
|
return -1;
|
2014-11-19 19:59:37 +00:00
|
|
|
if (parse_flags(&rest, &kd->flags, MOD_STARTB) < 0)
|
2014-05-06 14:07:05 +00:00
|
|
|
return -1;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*rest == ',') {
|
2014-05-06 11:35:06 +00:00
|
|
|
kd->end_column = strtol(rest+1, &rest, 10);
|
2015-04-05 19:36:04 +00:00
|
|
|
if (kd->end_column < 1)
|
2014-05-06 11:35:06 +00:00
|
|
|
return -1;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*rest == '.') {
|
2014-05-06 11:35:06 +00:00
|
|
|
kd->end_char = strtol(rest+1, &rest, 10);
|
2015-04-05 18:33:07 +00:00
|
|
|
if (kd->end_char < 0)
|
2014-05-06 11:35:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-19 19:59:37 +00:00
|
|
|
if (parse_flags(&rest, &kd->flags, MOD_ENDB) < 0)
|
2014-05-06 14:07:05 +00:00
|
|
|
return -1;
|
2013-12-12 13:08:49 +00:00
|
|
|
}
|
2015-03-22 22:37:37 +00:00
|
|
|
|
|
|
|
return -(*rest);
|
2011-06-02 12:03:34 +00:00
|
|
|
}
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2014-04-12 15:53:10 +00:00
|
|
|
static char *
|
2014-05-15 18:08:17 +00:00
|
|
|
skipblank(char *s)
|
2014-04-12 15:53:10 +00:00
|
|
|
{
|
2015-02-01 20:00:37 +00:00
|
|
|
while (isblank(*s))
|
2014-04-30 14:08:11 +00:00
|
|
|
s++;
|
2015-02-01 20:00:37 +00:00
|
|
|
return s;
|
|
|
|
}
|
2015-03-22 22:37:37 +00:00
|
|
|
|
2015-02-01 20:00:37 +00:00
|
|
|
static char *
|
|
|
|
skipnonblank(char *s)
|
|
|
|
{
|
|
|
|
while (*s && *s != '\n' && !isblank(*s))
|
|
|
|
s++;
|
2014-04-12 15:53:10 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2015-02-01 20:00:37 +00:00
|
|
|
skipcolumn(char *s, char *eol, int next_col)
|
2014-04-12 15:53:10 +00:00
|
|
|
{
|
2015-02-01 20:00:37 +00:00
|
|
|
if (fieldsep) {
|
|
|
|
if ((s = strstr(s, fieldsep)))
|
|
|
|
s += next_col ? strlen(fieldsep) : 0;
|
2014-05-15 18:08:17 +00:00
|
|
|
else
|
2015-02-01 20:00:37 +00:00
|
|
|
s = eol;
|
|
|
|
} else {
|
|
|
|
s = skipblank(s);
|
|
|
|
s = skipnonblank(s);
|
2014-05-03 16:34:51 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-05-05 11:41:43 +00:00
|
|
|
static size_t
|
|
|
|
columns(char *line, const struct keydef *kd, char **col, size_t *colsiz)
|
2014-04-12 15:53:10 +00:00
|
|
|
{
|
2015-02-01 20:00:37 +00:00
|
|
|
char *start, *end, *eol = strchr(line, '\n');
|
2015-05-05 11:41:43 +00:00
|
|
|
size_t len;
|
2014-05-15 18:08:17 +00:00
|
|
|
int i;
|
2014-04-18 16:21:31 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 1, start = line; i < kd->start_column; i++)
|
2015-02-01 20:00:37 +00:00
|
|
|
start = skipcolumn(start, eol, 1);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->flags & MOD_STARTB)
|
2014-05-15 18:08:17 +00:00
|
|
|
start = skipblank(start);
|
2015-02-01 20:00:37 +00:00
|
|
|
start = MIN(eol, start + kd->start_char - 1);
|
2014-04-12 15:53:10 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->end_column) {
|
|
|
|
for (i = 1, end = line; i < kd->end_column; i++)
|
2015-02-01 20:00:37 +00:00
|
|
|
end = skipcolumn(end, eol, 1);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->flags & MOD_ENDB)
|
2014-05-15 18:08:17 +00:00
|
|
|
end = skipblank(end);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (kd->end_char)
|
2015-02-01 20:00:37 +00:00
|
|
|
end = MIN(eol, end + kd->end_char);
|
2014-04-18 16:21:31 +00:00
|
|
|
else
|
2015-02-01 20:00:37 +00:00
|
|
|
end = skipcolumn(end, eol, 0);
|
2014-05-03 16:34:51 +00:00
|
|
|
} else {
|
2015-02-01 20:00:37 +00:00
|
|
|
end = eol;
|
2014-05-03 16:34:51 +00:00
|
|
|
}
|
2015-05-05 11:41:43 +00:00
|
|
|
len = start > end ? 0 : end - start;
|
|
|
|
if (!*col || *colsiz < len)
|
|
|
|
*col = erealloc(*col, len + 1);
|
|
|
|
memcpy(*col, start, len);
|
|
|
|
(*col)[len] = '\0';
|
|
|
|
if (*colsiz < len)
|
|
|
|
*colsiz = len;
|
|
|
|
return len;
|
2014-04-12 15:53:10 +00:00
|
|
|
}
|
2015-03-07 14:39:39 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-22 22:37:37 +00:00
|
|
|
enprintf(2, "usage: %s [-Cbcmnru] [-o outfile] [-t delim] [-k def]... [file ...]\n", argv0);
|
2015-03-07 14:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-03-22 22:37:37 +00:00
|
|
|
FILE *fp, *ofp = stdout;
|
2015-03-07 14:39:39 +00:00
|
|
|
struct linebuf linebuf = EMPTY_LINEBUF;
|
2015-03-22 22:37:37 +00:00
|
|
|
size_t i;
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 19:25:17 +00:00
|
|
|
int global_flags = 0, ret = 0;
|
2015-03-22 22:37:37 +00:00
|
|
|
char *outfile = NULL;
|
2015-03-07 14:39:39 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'C':
|
|
|
|
Cflag = 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
global_flags |= MOD_STARTB | MOD_ENDB;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
addkeydef(EARGF(usage()), global_flags);
|
|
|
|
break;
|
2015-03-22 22:37:37 +00:00
|
|
|
case 'm':
|
2015-05-05 11:41:43 +00:00
|
|
|
/* more or less for free, but for performance-reasons,
|
2015-03-22 22:37:37 +00:00
|
|
|
* we should keep this flag in mind and maybe some later
|
|
|
|
* day implement it properly so we don't run out of memory
|
|
|
|
* while merging large sorted files.
|
|
|
|
*/
|
|
|
|
break;
|
2015-03-07 14:39:39 +00:00
|
|
|
case 'n':
|
|
|
|
global_flags |= MOD_N;
|
|
|
|
break;
|
2015-03-22 22:37:37 +00:00
|
|
|
case 'o':
|
|
|
|
outfile = EARGF(usage());
|
|
|
|
break;
|
2015-03-07 14:39:39 +00:00
|
|
|
case 'r':
|
|
|
|
global_flags |= MOD_R;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
fieldsep = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2015-04-05 18:31:28 +00:00
|
|
|
/* -b shall only apply to custom key definitions */
|
2015-03-07 14:39:39 +00:00
|
|
|
if (!head && global_flags)
|
2015-04-05 18:31:28 +00:00
|
|
|
addkeydef("1", global_flags & ~(MOD_STARTB|MOD_ENDB));
|
2015-03-07 14:39:39 +00:00
|
|
|
addkeydef("1", global_flags & MOD_R);
|
|
|
|
|
2015-03-22 22:37:37 +00:00
|
|
|
if (!argc) {
|
2015-03-07 14:39:39 +00:00
|
|
|
if (Cflag || cflag) {
|
|
|
|
check(stdin);
|
|
|
|
} else {
|
|
|
|
getlines(stdin, &linebuf);
|
|
|
|
}
|
2015-03-22 22:37:37 +00:00
|
|
|
} else for (; *argv; argc--, argv++) {
|
2015-05-19 15:44:15 +00:00
|
|
|
if (!strcmp(*argv, "-")) {
|
2015-05-15 11:28:39 +00:00
|
|
|
*argv = "<stdin>";
|
|
|
|
fp = stdin;
|
|
|
|
} else if (!(fp = fopen(*argv, "r"))) {
|
2015-03-22 22:37:37 +00:00
|
|
|
enprintf(2, "fopen %s:", *argv);
|
2015-03-07 14:39:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Cflag || cflag) {
|
|
|
|
check(fp);
|
|
|
|
} else {
|
|
|
|
getlines(fp, &linebuf);
|
|
|
|
}
|
2015-05-15 11:28:39 +00:00
|
|
|
if (fp != stdin && fshut(fp, *argv))
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 19:25:17 +00:00
|
|
|
ret = 1;
|
2015-03-07 14:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Cflag && !cflag) {
|
2015-03-22 22:37:37 +00:00
|
|
|
if (outfile && !(ofp = fopen(outfile, "w")))
|
|
|
|
eprintf("fopen %s:", outfile);
|
|
|
|
|
2015-03-07 14:39:39 +00:00
|
|
|
qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines,
|
|
|
|
(int (*)(const void *, const void *))linecmp);
|
|
|
|
|
|
|
|
for (i = 0; i < linebuf.nlines; i++) {
|
|
|
|
if (!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i],
|
|
|
|
(const char **)&linebuf.lines[i-1])) {
|
2015-03-22 22:37:37 +00:00
|
|
|
fputs(linebuf.lines[i], ofp);
|
2015-03-07 14:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 23:33:19 +00:00
|
|
|
if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"))
|
|
|
|
ret = 2;
|
|
|
|
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 19:25:17 +00:00
|
|
|
return ret;
|
2015-03-07 14:39:39 +00:00
|
|
|
}
|