aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2013-03-19 11:18:39 +0000
committerRon Yorston <rmy@pobox.com>2013-03-19 11:18:39 +0000
commit63d2c5fead323df5f4250ed544d0bc03527c8936 (patch)
tree660979b139a4bc4b143c08843cb7efbc69bdcb4d /coreutils
parent27fc2d535588728ac3ca69337271471fb6fe3ee9 (diff)
parenta42f530e034b673726a91ea5d8202254e677f066 (diff)
downloadbusybox-w32-63d2c5fead323df5f4250ed544d0bc03527c8936.tar.gz
busybox-w32-63d2c5fead323df5f4250ed544d0bc03527c8936.tar.bz2
busybox-w32-63d2c5fead323df5f4250ed544d0bc03527c8936.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/Kbuild.src2
-rw-r--r--coreutils/head.c159
-rw-r--r--coreutils/head_tail.c14
-rw-r--r--coreutils/head_tail.h6
-rw-r--r--coreutils/hostid.c3
-rw-r--r--coreutils/id.c4
-rw-r--r--coreutils/readlink.c5
-rw-r--r--coreutils/tail.c18
8 files changed, 166 insertions, 45 deletions
diff --git a/coreutils/Kbuild.src b/coreutils/Kbuild.src
index b715b9c47..ec4ef7df2 100644
--- a/coreutils/Kbuild.src
+++ b/coreutils/Kbuild.src
@@ -35,7 +35,6 @@ lib-$(CONFIG_EXPAND) += expand.o
35lib-$(CONFIG_FALSE) += false.o 35lib-$(CONFIG_FALSE) += false.o
36lib-$(CONFIG_FOLD) += fold.o 36lib-$(CONFIG_FOLD) += fold.o
37lib-$(CONFIG_FSYNC) += fsync.o 37lib-$(CONFIG_FSYNC) += fsync.o
38lib-$(CONFIG_HEAD) += head.o
39lib-$(CONFIG_INSTALL) += install.o 38lib-$(CONFIG_INSTALL) += install.o
40#lib-$(CONFIG_LENGTH) += length.o 39#lib-$(CONFIG_LENGTH) += length.o
41lib-$(CONFIG_LN) += ln.o 40lib-$(CONFIG_LN) += ln.o
@@ -71,7 +70,6 @@ lib-$(CONFIG_STTY) += stty.o
71lib-$(CONFIG_SUM) += sum.o 70lib-$(CONFIG_SUM) += sum.o
72lib-$(CONFIG_SYNC) += sync.o 71lib-$(CONFIG_SYNC) += sync.o
73lib-$(CONFIG_TAC) += tac.o 72lib-$(CONFIG_TAC) += tac.o
74lib-$(CONFIG_TAIL) += tail.o
75lib-$(CONFIG_TEE) += tee.o 73lib-$(CONFIG_TEE) += tee.o
76lib-$(CONFIG_TRUE) += true.o 74lib-$(CONFIG_TRUE) += true.o
77lib-$(CONFIG_TTY) += tty.o 75lib-$(CONFIG_TTY) += tty.o
diff --git a/coreutils/head.c b/coreutils/head.c
index ec4512765..291e1ce37 100644
--- a/coreutils/head.c
+++ b/coreutils/head.c
@@ -11,6 +11,9 @@
11/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */ 11/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
12/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */ 12/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
13 13
14//kbuild:lib-$(CONFIG_HEAD) += head.o
15//kbuild:lib-$(CONFIG_HEAD) += head_tail.o
16
14//usage:#define head_trivial_usage 17//usage:#define head_trivial_usage
15//usage: "[OPTIONS] [FILE]..." 18//usage: "[OPTIONS] [FILE]..."
16//usage:#define head_full_usage "\n\n" 19//usage:#define head_full_usage "\n\n"
@@ -18,7 +21,8 @@
18//usage: "With more than one FILE, precede each with a filename header.\n" 21//usage: "With more than one FILE, precede each with a filename header.\n"
19//usage: "\n -n N[kbm] Print first N lines" 22//usage: "\n -n N[kbm] Print first N lines"
20//usage: IF_FEATURE_FANCY_HEAD( 23//usage: IF_FEATURE_FANCY_HEAD(
21//usage: "\n -c N[kbm] Print first N bytes" 24//usage: "\n -n -N[kbm] Print all except N last lines"
25//usage: "\n -c [-]N[kbm] Print first N bytes"
22//usage: "\n -q Never print headers" 26//usage: "\n -q Never print headers"
23//usage: "\n -v Always print headers" 27//usage: "\n -v Always print headers"
24//usage: ) 28//usage: )
@@ -31,9 +35,114 @@
31//usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n" 35//usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
32 36
33#include "libbb.h" 37#include "libbb.h"
38#include "head_tail.h"
34 39
35/* This is a NOEXEC applet. Be very careful! */ 40/* This is a NOEXEC applet. Be very careful! */
36 41
42#if !ENABLE_FEATURE_FANCY_HEAD
43# define print_first_N(fp,count,bytes) print_first_N(fp,count)
44#endif
45static void
46print_first_N(FILE *fp, unsigned long count, bool count_bytes)
47{
48#if !ENABLE_FEATURE_FANCY_HEAD
49 const int count_bytes = 0;
50#endif
51 while (count) {
52 int c = getc(fp);
53 if (c == EOF)
54 break;
55 if (count_bytes || (c == '\n'))
56 --count;
57 putchar(c);
58 }
59}
60
61#if ENABLE_FEATURE_FANCY_HEAD
62static void
63print_except_N_last_bytes(FILE *fp, unsigned count)
64{
65 unsigned char *circle = xmalloc(++count);
66 unsigned head = 0;
67 for(;;) {
68 int c;
69 c = getc(fp);
70 if (c == EOF)
71 goto ret;
72 circle[head++] = c;
73 if (head == count)
74 break;
75 }
76 for (;;) {
77 int c;
78 if (head == count)
79 head = 0;
80 putchar(circle[head]);
81 c = getc(fp);
82 if (c == EOF)
83 goto ret;
84 circle[head] = c;
85 head++;
86 }
87 ret:
88 free(circle);
89}
90
91static void
92print_except_N_last_lines(FILE *fp, unsigned count)
93{
94 char **circle = xzalloc((++count) * sizeof(circle[0]));
95 unsigned head = 0;
96 for(;;) {
97 char *c;
98 c = xmalloc_fgets(fp);
99 if (!c)
100 goto ret;
101 circle[head++] = c;
102 if (head == count)
103 break;
104 }
105 for (;;) {
106 char *c;
107 if (head == count)
108 head = 0;
109 fputs(circle[head], stdout);
110 c = xmalloc_fgets(fp);
111 if (!c)
112 goto ret;
113 free(circle[head]);
114 circle[head++] = c;
115 }
116 ret:
117 head = 0;
118 for(;;) {
119 free(circle[head++]);
120 if (head == count)
121 break;
122 }
123 free(circle);
124}
125#else
126/* Must never be called */
127void print_except_N_last_bytes(FILE *fp, unsigned count);
128void print_except_N_last_lines(FILE *fp, unsigned count);
129#endif
130
131#if !ENABLE_FEATURE_FANCY_HEAD
132# define eat_num(negative_N,p) eat_num(p)
133#endif
134static unsigned long
135eat_num(bool *negative_N, const char *p)
136{
137#if ENABLE_FEATURE_FANCY_HEAD
138 if (*p == '-') {
139 *negative_N = 1;
140 p++;
141 }
142#endif
143 return xatoul_sfx(p, head_tail_suffixes);
144}
145
37static const char head_opts[] ALIGN1 = 146static const char head_opts[] ALIGN1 =
38 "n:" 147 "n:"
39#if ENABLE_FEATURE_FANCY_HEAD 148#if ENABLE_FEATURE_FANCY_HEAD
@@ -41,29 +150,25 @@ static const char head_opts[] ALIGN1 =
41#endif 150#endif
42 ; 151 ;
43 152
44static const struct suffix_mult head_suffixes[] = {
45 { "b", 512 },
46 { "k", 1024 },
47 { "m", 1024*1024 },
48 { "", 0 }
49};
50
51#define header_fmt_str "\n==> %s <==\n" 153#define header_fmt_str "\n==> %s <==\n"
52 154
53int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 155int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
54int head_main(int argc, char **argv) 156int head_main(int argc, char **argv)
55{ 157{
56 unsigned long count = 10; 158 unsigned long count = 10;
57 unsigned long i;
58#if ENABLE_FEATURE_FANCY_HEAD 159#if ENABLE_FEATURE_FANCY_HEAD
59 int count_bytes = 0;
60 int header_threshhold = 1; 160 int header_threshhold = 1;
161 bool count_bytes = 0;
162 bool negative_N = 0;
163#else
164# define header_threshhold 1
165# define count_bytes 0
166# define negative_N 0
61#endif 167#endif
62 FILE *fp; 168 FILE *fp;
63 const char *fmt; 169 const char *fmt;
64 char *p; 170 char *p;
65 int opt; 171 int opt;
66 int c;
67 int retval = EXIT_SUCCESS; 172 int retval = EXIT_SUCCESS;
68 173
69#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD 174#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
@@ -73,7 +178,7 @@ int head_main(int argc, char **argv)
73 ) { 178 ) {
74 --argc; 179 --argc;
75 ++argv; 180 ++argv;
76 p = (*argv) + 1; 181 p = argv[0] + 1;
77 goto GET_COUNT; 182 goto GET_COUNT;
78 } 183 }
79#endif 184#endif
@@ -97,7 +202,7 @@ int head_main(int argc, char **argv)
97#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD 202#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
98 GET_COUNT: 203 GET_COUNT:
99#endif 204#endif
100 count = xatoul_sfx(p, head_suffixes); 205 count = eat_num(&negative_N, p);
101 break; 206 break;
102 default: 207 default:
103 bb_show_usage(); 208 bb_show_usage();
@@ -110,19 +215,17 @@ int head_main(int argc, char **argv)
110 *--argv = (char*)"-"; 215 *--argv = (char*)"-";
111 216
112 fmt = header_fmt_str + 1; 217 fmt = header_fmt_str + 1;
113#if ENABLE_FEATURE_FANCY_HEAD
114 if (argc <= header_threshhold) { 218 if (argc <= header_threshhold) {
219#if ENABLE_FEATURE_FANCY_HEAD
115 header_threshhold = 0; 220 header_threshhold = 0;
116 }
117#else 221#else
118 if (argc <= 1) {
119 fmt += 11; /* "" */ 222 fmt += 11; /* "" */
120 }
121 /* Now define some things here to avoid #ifdefs in the code below.
122 * These should optimize out of the if conditions below. */
123#define header_threshhold 1
124#define count_bytes 0
125#endif 223#endif
224 }
225 if (negative_N) {
226 if (count >= INT_MAX / sizeof(char*))
227 bb_error_msg("count is too big: %lu", count);
228 }
126 229
127 do { 230 do {
128 fp = fopen_or_warn_stdin(*argv); 231 fp = fopen_or_warn_stdin(*argv);
@@ -133,18 +236,20 @@ int head_main(int argc, char **argv)
133 if (header_threshhold) { 236 if (header_threshhold) {
134 printf(fmt, *argv); 237 printf(fmt, *argv);
135 } 238 }
136 i = count; 239 if (negative_N) {
137 while (i && ((c = getc(fp)) != EOF)) { 240 if (count_bytes) {
138 if (count_bytes || (c == '\n')) { 241 print_except_N_last_bytes(fp, count);
139 --i; 242 } else {
243 print_except_N_last_lines(fp, count);
140 } 244 }
141 putchar(c); 245 } else {
246 print_first_N(fp, count, count_bytes);
142 } 247 }
248 die_if_ferror_stdout();
143 if (fclose_if_not_stdin(fp)) { 249 if (fclose_if_not_stdin(fp)) {
144 bb_simple_perror_msg(*argv); 250 bb_simple_perror_msg(*argv);
145 retval = EXIT_FAILURE; 251 retval = EXIT_FAILURE;
146 } 252 }
147 die_if_ferror_stdout();
148 } else { 253 } else {
149 retval = EXIT_FAILURE; 254 retval = EXIT_FAILURE;
150 } 255 }
diff --git a/coreutils/head_tail.c b/coreutils/head_tail.c
new file mode 100644
index 000000000..1658c0d1b
--- /dev/null
+++ b/coreutils/head_tail.c
@@ -0,0 +1,14 @@
1/*
2 * Copyright (C) 2013 Denys Vlasenko
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
6#include "libbb.h"
7#include "head_tail.h"
8
9const struct suffix_mult head_tail_suffixes[] = {
10 { "b", 512 },
11 { "k", 1024 },
12 { "m", 1024*1024 },
13 { "", 0 }
14};
diff --git a/coreutils/head_tail.h b/coreutils/head_tail.h
new file mode 100644
index 000000000..df19e41e0
--- /dev/null
+++ b/coreutils/head_tail.h
@@ -0,0 +1,6 @@
1/*
2 * Copyright (C) 2013 Denys Vlasenko
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
6extern const struct suffix_mult head_tail_suffixes[];
diff --git a/coreutils/hostid.c b/coreutils/hostid.c
index 5c1a4e086..e5b1f5188 100644
--- a/coreutils/hostid.c
+++ b/coreutils/hostid.c
@@ -36,7 +36,8 @@ int hostid_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
36 bb_show_usage(); 36 bb_show_usage();
37 } 37 }
38 38
39 printf("%08lx\n", gethostid()); 39 /* POSIX says gethostid returns a "32-bit identifier" */
40 printf("%08x\n", (unsigned)(uint32_t)gethostid());
40 41
41 return fflush_all(); 42 return fflush_all();
42} 43}
diff --git a/coreutils/id.c b/coreutils/id.c
index 1f20b755e..1f3e1c4c2 100644
--- a/coreutils/id.c
+++ b/coreutils/id.c
@@ -64,12 +64,10 @@
64/* This is a NOEXEC applet. Be very careful! */ 64/* This is a NOEXEC applet. Be very careful! */
65 65
66#if !ENABLE_USE_BB_PWD_GRP 66#if !ENABLE_USE_BB_PWD_GRP
67#if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0) 67#if defined(__UCLIBC__) && UCLIBC_VERSION < KERNEL_VERSION(0, 9, 30)
68#if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 30)
69#error "Sorry, you need at least uClibc version 0.9.30 for id applet to build" 68#error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
70#endif 69#endif
71#endif 70#endif
72#endif
73 71
74enum { 72enum {
75 PRINT_REAL = (1 << 0), 73 PRINT_REAL = (1 << 0),
diff --git a/coreutils/readlink.c b/coreutils/readlink.c
index f7ad791ec..d73ef4ddb 100644
--- a/coreutils/readlink.c
+++ b/coreutils/readlink.c
@@ -39,7 +39,10 @@
39 * -q, --quiet, -s, --silent suppress most error messages 39 * -q, --quiet, -s, --silent suppress most error messages
40 * -v, --verbose report error messages 40 * -v, --verbose report error messages
41 * 41 *
42 * bbox supports: -f -n -v (fully), -q -s (accepts but ignores) 42 * bbox supports: -f (partially) -n -v (fully), -q -s (accepts but ignores)
43 * Note: we export the -f flag, but our -f behaves like coreutils' -e.
44 * Unfortunately, there isn't a C lib function we can leverage to get this
45 * behavior which means we'd have to implement the full stack ourselves :(.
43 */ 46 */
44 47
45int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 48int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
diff --git a/coreutils/tail.c b/coreutils/tail.c
index b376ec863..19fd8f695 100644
--- a/coreutils/tail.c
+++ b/coreutils/tail.c
@@ -24,6 +24,9 @@
24 * 7) lseek attempted when count==0 even if arg was +0 (from top) 24 * 7) lseek attempted when count==0 even if arg was +0 (from top)
25 */ 25 */
26 26
27//kbuild:lib-$(CONFIG_TAIL) += tail.o
28//kbuild:lib-$(CONFIG_TAIL) += head_tail.o
29
27//usage:#define tail_trivial_usage 30//usage:#define tail_trivial_usage
28//usage: "[OPTIONS] [FILE]..." 31//usage: "[OPTIONS] [FILE]..."
29//usage:#define tail_full_usage "\n\n" 32//usage:#define tail_full_usage "\n\n"
@@ -34,14 +37,13 @@
34//usage: "\n -s SECONDS Wait SECONDS between reads with -f" 37//usage: "\n -s SECONDS Wait SECONDS between reads with -f"
35//usage: ) 38//usage: )
36//usage: "\n -n N[kbm] Print last N lines" 39//usage: "\n -n N[kbm] Print last N lines"
40//usage: "\n -n +N[kbm] Start on Nth line and print the rest"
37//usage: IF_FEATURE_FANCY_TAIL( 41//usage: IF_FEATURE_FANCY_TAIL(
38//usage: "\n -c N[kbm] Print last N bytes" 42//usage: "\n -c [+]N[kbm] Print last N bytes"
39//usage: "\n -q Never print headers" 43//usage: "\n -q Never print headers"
40//usage: "\n -v Always print headers" 44//usage: "\n -v Always print headers"
41//usage: "\n" 45//usage: "\n"
42//usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)." 46//usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
43//usage: "\nIf N starts with a '+', output begins with the Nth item from the start"
44//usage: "\nof each file, not from the end."
45//usage: ) 47//usage: )
46//usage: 48//usage:
47//usage:#define tail_example_usage 49//usage:#define tail_example_usage
@@ -49,13 +51,7 @@
49//usage: "nameserver 10.0.0.1\n" 51//usage: "nameserver 10.0.0.1\n"
50 52
51#include "libbb.h" 53#include "libbb.h"
52 54#include "head_tail.h"
53static const struct suffix_mult tail_suffixes[] = {
54 { "b", 512 },
55 { "k", 1024 },
56 { "m", 1024*1024 },
57 { "", 0 }
58};
59 55
60struct globals { 56struct globals {
61 bool from_top; 57 bool from_top;
@@ -102,7 +98,7 @@ static unsigned eat_num(const char *p)
102 p++; 98 p++;
103 G.from_top = 1; 99 G.from_top = 1;
104 } 100 }
105 return xatou_sfx(p, tail_suffixes); 101 return xatou_sfx(p, head_tail_suffixes);
106} 102}
107 103
108int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 104int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;