2012-05-21 18:05:09 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 15:02:41 -05:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2015-01-30 10:52:44 -05:00
|
|
|
#include <limits.h>
|
2015-01-31 19:24:03 -05:00
|
|
|
#include <stdint.h>
|
2012-05-21 18:05:09 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 13:54:28 -05:00
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2012-05-21 18:05:09 -04:00
|
|
|
#include "text.h"
|
2014-12-05 16:32:08 -05:00
|
|
|
#include "utf.h"
|
2012-05-21 18:05:09 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-08 14:33:46 -04:00
|
|
|
eprintf("usage: %s [-c num] [file ...]\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2012-05-21 18:05:09 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2015-03-08 14:33:46 -04:00
|
|
|
struct winsize w;
|
|
|
|
struct linebuf b = EMPTY_LINEBUF;
|
|
|
|
size_t chars = 65, maxlen = 0, i, j, k, len, bytes, cols, rows;
|
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 15:25:17 -04:00
|
|
|
int cflag = 0, ret = 0;
|
2015-02-19 06:24:28 -05:00
|
|
|
char *p;
|
2012-05-21 18:05:09 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'c':
|
2014-01-20 07:03:01 -05:00
|
|
|
cflag = 1;
|
2015-03-08 14:33:46 -04:00
|
|
|
chars = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2012-05-21 18:05:09 -04:00
|
|
|
|
2015-03-08 14:33:46 -04:00
|
|
|
if (!cflag) {
|
2015-02-19 06:24:28 -05:00
|
|
|
if ((p = getenv("COLUMNS")))
|
|
|
|
chars = estrtonum(p, 1, MIN(LLONG_MAX, SIZE_MAX));
|
|
|
|
else if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) && w.ws_col > 0)
|
2014-01-20 07:03:01 -05:00
|
|
|
chars = w.ws_col;
|
|
|
|
}
|
|
|
|
|
2015-03-08 14:33:46 -04:00
|
|
|
if (!argc) {
|
2012-05-21 18:05:09 -04:00
|
|
|
getlines(stdin, &b);
|
2015-03-08 14:33:46 -04:00
|
|
|
} else {
|
|
|
|
for (; *argv; argc--, argv++) {
|
2015-05-15 07:28:39 -04:00
|
|
|
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
|
|
|
*argv = "<stdin>";
|
|
|
|
fp = stdin;
|
|
|
|
} else if (!(fp = fopen(*argv, "r"))) {
|
2015-03-08 14:33:46 -04:00
|
|
|
weprintf("fopen %s:", *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 15:25:17 -04:00
|
|
|
ret = 1;
|
2015-03-08 14:33:46 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
getlines(fp, &b);
|
2015-05-15 07:28:39 -04: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 15:25:17 -04:00
|
|
|
ret = 1;
|
2015-03-08 14:33:46 -04:00
|
|
|
}
|
2012-05-21 18:05:09 -04:00
|
|
|
}
|
|
|
|
|
2015-03-08 14:33:46 -04:00
|
|
|
for (i = 0; i < b.nlines; i++) {
|
|
|
|
len = utflen(b.lines[i]);
|
|
|
|
bytes = strlen(b.lines[i]);
|
|
|
|
if (len && bytes && b.lines[i][bytes - 1] == '\n') {
|
|
|
|
b.lines[i][bytes - 1] = '\0';
|
|
|
|
len--;
|
2014-12-05 16:32:08 -05:00
|
|
|
}
|
2014-11-13 12:29:30 -05:00
|
|
|
if (len > maxlen)
|
2012-05-21 18:05:09 -04:00
|
|
|
maxlen = len;
|
|
|
|
}
|
|
|
|
|
2015-03-08 14:33:46 -04:00
|
|
|
for (cols = 1; (cols + 1) * maxlen + cols <= chars; cols++);
|
|
|
|
rows = b.nlines / cols + (b.nlines % cols > 0);
|
2012-05-21 18:05:09 -04:00
|
|
|
|
2015-03-08 14:33:46 -04:00
|
|
|
for (i = 0; i < rows; i++) {
|
|
|
|
for (j = 0; j < cols && i + j * rows < b.nlines; j++) {
|
|
|
|
len = utflen(b.lines[i + j * rows]);
|
|
|
|
fputs(b.lines[i + j * rows], stdout);
|
|
|
|
if (j < cols - 1)
|
|
|
|
for (k = len; k < maxlen + 1; k++)
|
|
|
|
putchar(' ');
|
2012-05-21 18:05:09 -04:00
|
|
|
}
|
2015-03-08 14:33:46 -04:00
|
|
|
putchar('\n');
|
2012-05-21 18:05:09 -04:00
|
|
|
}
|
|
|
|
|
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 15:25:17 -04:00
|
|
|
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
|
2012-05-21 18:05:09 -04:00
|
|
|
}
|