aboutsummaryrefslogtreecommitdiff
path: root/toys/posix
diff options
context:
space:
mode:
authorCem Keylan <cem@ckyln.com>2021-07-07 16:10:32 +0300
committerCem Keylan <cem@ckyln.com>2021-07-07 16:10:32 +0300
commit854ddfac002e753a6389b48d3b9296fe32665ad3 (patch)
treeff9a2be83f68dd66239ff3a798222dc2f0afc9b8 /toys/posix
parent5bc00f0da395aa4ec779bb802bd78bf893565143 (diff)
parent3fbacb1f5c5eab1727d0bed0bd50d659af854ec7 (diff)
downloadtoybox-854ddfac002e753a6389b48d3b9296fe32665ad3.tar.gz
Merge remote-tracking branch 'refs/remotes/origin/master'
Diffstat (limited to 'toys/posix')
-rw-r--r--toys/posix/split.c14
-rw-r--r--toys/posix/tail.c140
2 files changed, 69 insertions, 85 deletions
diff --git a/toys/posix/split.c b/toys/posix/split.c
index 5161affb..293343bf 100644
--- a/toys/posix/split.c
+++ b/toys/posix/split.c
@@ -22,7 +22,7 @@ config SPLIT
-a Suffix length (default 2)
-b BYTES/file (10, 10k, 10m, 10g...)
-l LINES/file (default 1000)
- -n PARTS
+ -n PARTS many equal length files
*/
#define FOR_split
@@ -42,14 +42,10 @@ static void do_split(int infd, char *in)
// posix doesn't cover permissions on output file, so copy input (or 0777)
st.st_mode = 0777;
+ st.st_size = 0;
fstat(infd, &st);
- if (!TT.b && TT.n) {
- if (lseek(infd, 0, SEEK_CUR) < 0)
- error_exit("cannot determine file size");
- TT.b = st.st_size / TT.n;
- }
-
+ if (TT.n && (TT.b = st.st_size/TT.n)<1) return error_msg("%s: no size", in);
len = pos = filenum = bytesleft = linesleft = 0;
for (;;) {
int i, j;
@@ -70,9 +66,7 @@ static void do_split(int infd, char *in)
j /= 26;
}
if (j) error_exit("bad suffix");
- bytesleft = TT.b;
- if (TT.n && filenum == TT.n && st.st_size % 2)
- ++bytesleft;
+ bytesleft = TT.b + ((filenum == TT.n) ? st.st_size%TT.n : 0);
linesleft = TT.l;
xclose(outfd);
outfd = xcreate(TT.outfile, O_RDWR|O_CREAT|O_TRUNC, st.st_mode & 0777);
diff --git a/toys/posix/tail.c b/toys/posix/tail.c
index 842814c0..0d13ab01 100644
--- a/toys/posix/tail.c
+++ b/toys/posix/tail.c
@@ -6,7 +6,7 @@
*
* Deviations from posix: -f waits for pipe/fifo on stdin (nonblock?).
-USE_TAIL(NEWTOY(tail, "?fFs#=1c-n-[-cn][-fF]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_TAIL(NEWTOY(tail, "?fFs:c-n-[-cn][-fF]", TOYFLAG_USR|TOYFLAG_BIN))
config TAIL
bool "tail"
@@ -27,18 +27,18 @@ config TAIL
#define FOR_tail
#include "toys.h"
-struct follow_file {
- char *path;
- int fd;
- dev_t dev;
- ino_t ino;
-};
-
GLOBALS(
- long n, c, s;
- int file_no, last_fd;
+ long n, c;
+ char *s;
+
+ int file_no, last_fd, ss;
struct xnotify *not;
- struct follow_file *F;
+ struct {
+ char *path;
+ int fd;
+ dev_t dev;
+ ino_t ino;
+ } *F;
)
struct line_list {
@@ -132,66 +132,53 @@ static int try_lseek(int fd, long bytes, long lines)
return 1;
}
-static void show_new(int fd, char *path)
-{
- int len;
-
- while ((len = read(fd, toybuf, sizeof(toybuf)))>0) {
- if (TT.last_fd != fd) {
- TT.last_fd = fd;
- xprintf("\n==> %s <==\n", path);
- }
- xwrite(1, toybuf, len);
- }
-}
-
-static void tail_F()
+// For -f and -F
+static void tail_continue()
{
+ long long pos;
+ char *path;
struct stat sb;
- int i, old_fd;
-
- for (;;) {
- for (i = 0; i<TT.file_no; ++i) {
- old_fd = TT.F[i].fd;
- if (stat(TT.F[i].path, &sb) != 0) {
- if (old_fd >= 0) {
- xprintf("tail: file inaccessible: %s\n", TT.F[i].path);
- close(old_fd);
+ int i = 0, fd, len;
+
+ for (i = 0; ; i++) {
+ if (FLAG(f)) fd = xnotify_wait(TT.not, &path);
+ else {
+ if (i == TT.file_no) {
+ i = 0;
+ msleep(TT.ss);
+ }
+ fd = TT.F[i].fd;
+ path = TT.F[i].path;
+
+ if (stat(TT.F[i].path, &sb)) {
+ if (fd >= 0) {
+ close(fd);
TT.F[i].fd = -1;
+ error_msg("file inaccessible: %s\n", TT.F[i].path);
}
continue;
}
- if (old_fd < 0 || sb.st_dev != TT.F[i].dev || sb.st_ino != TT.F[i].ino) {
- if (old_fd >= 0) close(old_fd);
- TT.F[i].fd = open(TT.F[i].path, O_RDONLY);
- if (TT.F[i].fd == -1) continue;
- else {
- xprintf("tail: following new file: %s\n", TT.F[i].path);
- TT.F[i].dev = sb.st_dev;
- TT.F[i].ino = sb.st_ino;
- }
- } else if (sb.st_size && sb.st_size < lseek(TT.F[i].fd, 0, SEEK_CUR)) {
- // If file was truncated, move to start.
- xprintf("tail: file truncated: %s\n", TT.F[i].path);
- xlseek(TT.F[i].fd, 0, SEEK_SET);
+ if (fd<0 || sb.st_dev!=TT.F[i].dev || sb.st_ino!=TT.F[i].ino) {
+ if (fd>=0) close(fd);
+ if (-1 == (TT.F[i].fd = fd = open(path, O_RDONLY))) continue;
+ error_msg("following new file: %s\n", path);
+ TT.F[i].dev = sb.st_dev;
+ TT.F[i].ino = sb.st_ino;
+ } else if (sb.st_size <= (pos = lseek(fd, 0, SEEK_CUR))) {
+ if (pos == sb.st_size) continue;
+ error_msg("file truncated: %s\n", path);
+ lseek(fd, 0, SEEK_SET);
}
-
- show_new(TT.F[i].fd, TT.F[i].path);
}
- sleep(TT.s);
- }
-}
-
-static void tail_f()
-{
- char *path;
- int fd;
-
- for (;;) {
- fd = xnotify_wait(TT.not, &path);
- show_new(fd, path);
+ while ((len = read(fd, toybuf, sizeof(toybuf)))>0) {
+ if (TT.file_no>1 && TT.last_fd != fd) {
+ TT.last_fd = fd;
+ xprintf("\n==> %s <==\n", path);
+ }
+ xwrite(1, toybuf, len);
+ }
}
}
@@ -201,6 +188,9 @@ static void do_tail(int fd, char *name)
long bytes = TT.c, lines = TT.n;
int linepop = 1;
+ if (FLAG(F)) {
+ if (!fd) perror_exit("no -F with '-'");
+ } else if (fd == -1) return;
if (FLAG(f) || FLAG(F)) {
char *s = name;
struct stat sb;
@@ -209,11 +199,13 @@ static void do_tail(int fd, char *name)
if (FLAG(f)) xnotify_add(TT.not, fd, s);
if (FLAG(F)) {
- if (fstat(fd, &sb) != 0) perror_exit("%s", name);
+ if (fd != -1) {
+ if (fstat(fd, &sb)) perror_exit("%s", name);
+ TT.F[TT.file_no].dev = sb.st_dev;
+ TT.F[TT.file_no].ino = sb.st_ino;
+ }
TT.F[TT.file_no].fd = fd;
TT.F[TT.file_no].path = s;
- TT.F[TT.file_no].dev = sb.st_dev;
- TT.F[TT.file_no].ino = sb.st_ino;
}
}
@@ -298,23 +290,21 @@ void tail_main(void)
if (!FLAG(n) && !FLAG(c)) {
char *arg = *args;
- // handle old "-42" style arguments
+ // handle old "-42" style arguments, else default to last 10 lines
if (arg && *arg == '-' && arg[1]) {
TT.n = atolx(*(args++));
toys.optc--;
- } else {
- // if nothing specified, default -n to -10
- TT.n = -10;
- }
+ } else TT.n = -10;
}
- if (FLAG(F)) TT.F = xmalloc(toys.optc * sizeof(struct follow_file));
+ if (FLAG(F)) TT.F = xzalloc(toys.optc*sizeof(*TT.F));
else if (FLAG(f)) TT.not = xnotify_init(toys.optc);
+ TT.ss = TT.s ? xparsemillitime(TT.s) : 1000;
- loopfiles_rw(args, O_RDONLY|WARN_ONLY|(O_CLOEXEC*!(FLAG(f) || FLAG(F))), 0, do_tail);
+ loopfiles_rw(args,
+ O_RDONLY|WARN_ONLY|LOOPFILES_ANYWAY|(O_CLOEXEC*!(FLAG(f) || FLAG(F))),
+ 0, do_tail);
- if (TT.file_no) {
- if (FLAG(F)) tail_F();
- else if (FLAG(f)) tail_f();
- }
+ // Wait for more data when following files
+ if (TT.file_no && (FLAG(F) || FLAG(f))) tail_continue();
}