aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2018-09-10 14:37:07 +0100
committerRon Yorston <rmy@pobox.com>2018-09-10 14:59:33 +0100
commitd89ced75b204f0eb5611f522864beb81d1b393f5 (patch)
tree5daa31427e287fe079a0ef551097753773fdb266 /libbb
parentf72845d9332fa6311a46dbcad3180d5008182982 (diff)
parent05b18065ab9c375f6185b65a3631d4c6cc1a4be9 (diff)
downloadbusybox-w32-d89ced75b204f0eb5611f522864beb81d1b393f5.tar.gz
busybox-w32-d89ced75b204f0eb5611f522864beb81d1b393f5.tar.bz2
busybox-w32-d89ced75b204f0eb5611f522864beb81d1b393f5.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'libbb')
-rw-r--r--libbb/copy_file.c3
-rw-r--r--libbb/duration.c78
-rw-r--r--libbb/get_line_from_file.c4
-rw-r--r--libbb/remove_file.c2
-rw-r--r--libbb/unicode.c2
5 files changed, 86 insertions, 3 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index 7cd9cd978..299cd7bea 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -391,14 +391,15 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
391 char *lpath = xmalloc_readlink_or_warn(source); 391 char *lpath = xmalloc_readlink_or_warn(source);
392 if (lpath) { 392 if (lpath) {
393 int r = symlink(lpath, dest); 393 int r = symlink(lpath, dest);
394 free(lpath);
395 if (r < 0) { 394 if (r < 0) {
396 /* shared message */ 395 /* shared message */
397 bb_perror_msg("can't create %slink '%s' to '%s'", 396 bb_perror_msg("can't create %slink '%s' to '%s'",
398 "sym", dest, lpath 397 "sym", dest, lpath
399 ); 398 );
399 free(lpath);
400 return -1; 400 return -1;
401 } 401 }
402 free(lpath);
402 if (flags & FILEUTILS_PRESERVE_STATUS) 403 if (flags & FILEUTILS_PRESERVE_STATUS)
403 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) 404 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
404 bb_perror_msg("can't preserve %s of '%s'", "ownership", dest); 405 bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
diff --git a/libbb/duration.c b/libbb/duration.c
new file mode 100644
index 000000000..5acd0dba3
--- /dev/null
+++ b/libbb/duration.c
@@ -0,0 +1,78 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 2018 Denys Vlasenko
6 *
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
9//config:config FLOAT_DURATION
10//config: bool "Enable fractional duration arguments"
11//config: default y
12//config: help
13//config: Allow sleep N.NNN, top -d N.NNN etc.
14
15//kbuild:lib-$(CONFIG_SLEEP) += duration.o
16//kbuild:lib-$(CONFIG_TOP) += duration.o
17//kbuild:lib-$(CONFIG_TIMEOUT) += duration.o
18//kbuild:lib-$(CONFIG_PING) += duration.o
19//kbuild:lib-$(CONFIG_PING6) += duration.o
20
21#include "libbb.h"
22
23static const struct suffix_mult duration_suffixes[] = {
24 { "s", 1 },
25 { "m", 60 },
26 { "h", 60*60 },
27 { "d", 24*60*60 },
28 { "", 0 }
29};
30
31#if ENABLE_FLOAT_DURATION
32duration_t FAST_FUNC parse_duration_str(char *str)
33{
34 duration_t duration;
35
36 if (strchr(str, '.')) {
37 double d;
38 char *pp;
39 int len = strspn(str, "0123456789.");
40 char sv = str[len];
41 str[len] = '\0';
42 errno = 0;
43 d = strtod(str, &pp);
44 if (errno || *pp)
45 bb_show_usage();
46 str += len;
47 *str-- = sv;
48 sv = *str;
49 *str = '1';
50 duration = d * xatoul_sfx(str, duration_suffixes);
51 *str = sv;
52 } else {
53 duration = xatoul_sfx(str, duration_suffixes);
54 }
55
56 return duration;
57}
58void FAST_FUNC sleep_for_duration(duration_t duration)
59{
60 struct timespec ts;
61
62 ts.tv_sec = MAXINT(typeof(ts.tv_sec));
63 ts.tv_nsec = 0;
64 if (duration >= 0 && duration < ts.tv_sec) {
65 ts.tv_sec = duration;
66 ts.tv_nsec = (duration - ts.tv_sec) * 1000000000;
67 }
68 do {
69 errno = 0;
70 nanosleep(&ts, &ts);
71 } while (errno == EINTR);
72}
73#else
74duration_t FAST_FUNC parse_duration_str(char *str)
75{
76 return xatou_range_sfx(str, 0, UINT_MAX, duration_suffixes);
77}
78#endif
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
index 09ccfba67..929bab78a 100644
--- a/libbb/get_line_from_file.c
+++ b/libbb/get_line_from_file.c
@@ -47,7 +47,9 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end)
47/* Get line, including trailing \n if any */ 47/* Get line, including trailing \n if any */
48char* FAST_FUNC xmalloc_fgets(FILE *file) 48char* FAST_FUNC xmalloc_fgets(FILE *file)
49{ 49{
50 return bb_get_chunk_from_file(file, NULL); 50 size_t i;
51
52 return bb_get_chunk_from_file(file, &i);
51} 53}
52/* Get line. Remove trailing \n */ 54/* Get line. Remove trailing \n */
53char* FAST_FUNC xmalloc_fgetline(FILE *file) 55char* FAST_FUNC xmalloc_fgetline(FILE *file)
diff --git a/libbb/remove_file.c b/libbb/remove_file.c
index 86c9a5c56..cea5d47e6 100644
--- a/libbb/remove_file.c
+++ b/libbb/remove_file.c
@@ -73,7 +73,7 @@ int FAST_FUNC remove_file(const char *path, int flags)
73 return status; 73 return status;
74 } 74 }
75 75
76 if (rmdir(path) < 0) { 76 if (status == 0 && rmdir(path) < 0) {
77 bb_perror_msg("can't remove '%s'", path); 77 bb_perror_msg("can't remove '%s'", path);
78 return -1; 78 return -1;
79 } 79 }
diff --git a/libbb/unicode.c b/libbb/unicode.c
index 9c4da50d3..d378175a4 100644
--- a/libbb/unicode.c
+++ b/libbb/unicode.c
@@ -1121,6 +1121,8 @@ static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char
1121 dst[dst_len++] = ' '; 1121 dst[dst_len++] = ' ';
1122 } 1122 }
1123 } 1123 }
1124 if (!dst) /* for example, if input was "" */
1125 dst = xzalloc(1);
1124 dst[dst_len] = '\0'; 1126 dst[dst_len] = '\0';
1125 if (stats) { 1127 if (stats) {
1126 stats->byte_count = dst_len; 1128 stats->byte_count = dst_len;