aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/catv.c16
-rw-r--r--coreutils/ls.c2
-rw-r--r--coreutils/shuf.c153
-rw-r--r--coreutils/uname.c10
4 files changed, 169 insertions, 12 deletions
diff --git a/coreutils/catv.c b/coreutils/catv.c
index e3499c597..6bb73ba63 100644
--- a/coreutils/catv.c
+++ b/coreutils/catv.c
@@ -20,20 +20,22 @@
20 20
21#include "libbb.h" 21#include "libbb.h"
22 22
23int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24int catv_main(int argc UNUSED_PARAM, char **argv)
25{
26 int retval = EXIT_SUCCESS;
27 int fd;
28 unsigned opts;
29#define CATV_OPT_e (1<<0) 23#define CATV_OPT_e (1<<0)
30#define CATV_OPT_t (1<<1) 24#define CATV_OPT_t (1<<1)
31#define CATV_OPT_v (1<<2) 25#define CATV_OPT_v (1<<2)
32 typedef char BUG_const_mismatch[ 26struct BUG_const_mismatch {
27 char BUG_const_mismatch[
33 CATV_OPT_e == VISIBLE_ENDLINE && CATV_OPT_t == VISIBLE_SHOW_TABS 28 CATV_OPT_e == VISIBLE_ENDLINE && CATV_OPT_t == VISIBLE_SHOW_TABS
34 ? 1 : -1 29 ? 1 : -1
35 ]; 30 ];
31};
36 32
33int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
34int catv_main(int argc UNUSED_PARAM, char **argv)
35{
36 int retval = EXIT_SUCCESS;
37 int fd;
38 unsigned opts;
37 opts = getopt32(argv, "etv"); 39 opts = getopt32(argv, "etv");
38 argv += optind; 40 argv += optind;
39#if 0 /* These consts match, we can just pass "opts" to visible() */ 41#if 0 /* These consts match, we can just pass "opts" to visible() */
diff --git a/coreutils/ls.c b/coreutils/ls.c
index 166473d4d..1b63be56d 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -1032,7 +1032,7 @@ static void scan_and_display_dirs_recur(struct dnode **dn, int first)
1032 } 1032 }
1033 subdnp = scan_one_dir((*dn)->fullname, &nfiles); 1033 subdnp = scan_one_dir((*dn)->fullname, &nfiles);
1034#if ENABLE_DESKTOP 1034#if ENABLE_DESKTOP
1035 if ((G.all_fmt & STYLE_MASK) == STYLE_LONG) 1035 if ((G.all_fmt & STYLE_MASK) == STYLE_LONG || (G.all_fmt & LIST_BLOCKS))
1036 printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp)); 1036 printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp));
1037#endif 1037#endif
1038 if (nfiles > 0) { 1038 if (nfiles > 0) {
diff --git a/coreutils/shuf.c b/coreutils/shuf.c
new file mode 100644
index 000000000..6d0a68fc1
--- /dev/null
+++ b/coreutils/shuf.c
@@ -0,0 +1,153 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * shuf: Write a random permutation of the input lines to standard output.
4 *
5 * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola@gmail.com>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9
10//config:config SHUF
11//config: bool "shuf"
12//config: default y
13//config: help
14//config: Generate random permutations
15
16//kbuild:lib-$(CONFIG_SHUF) += shuf.o
17//applet:IF_SHUF(APPLET_NOEXEC(shuf, shuf, BB_DIR_USR_BIN, BB_SUID_DROP, shuf))
18
19//usage:#define shuf_trivial_usage
20//usage: "[-e|-i L-H] [-n NUM] [-o FILE] [-z] [FILE|ARG...]"
21//usage:#define shuf_full_usage "\n\n"
22//usage: "Randomly permute lines\n"
23//usage: "\n -e Treat ARGs as lines"
24//usage: "\n -i L-H Treat numbers L-H as lines"
25//usage: "\n -n NUM Output at most NUM lines"
26//usage: "\n -o FILE Write to FILE, not standard output"
27//usage: "\n -z End lines with zero byte, not newline"
28
29#include "libbb.h"
30
31/* This is a NOEXEC applet. Be very careful! */
32
33#define OPT_e (1 << 0)
34#define OPT_i (1 << 1)
35#define OPT_n (1 << 2)
36#define OPT_o (1 << 3)
37#define OPT_z (1 << 4)
38#define OPT_STR "ei:n:o:z"
39
40/*
41 * Use the Fisher-Yates shuffle algorithm on an array of lines.
42 */
43static void shuffle_lines(char **lines, unsigned numlines)
44{
45 unsigned i;
46 unsigned r;
47 char *tmp;
48
49 srand(monotonic_us());
50
51 for (i = numlines-1; i > 0; i--) {
52 r = rand();
53 /* RAND_MAX can be as small as 32767 */
54 if (i > RAND_MAX)
55 r ^= rand() << 15;
56 r %= i;
57 tmp = lines[i];
58 lines[i] = lines[r];
59 lines[r] = tmp;
60 }
61}
62
63int shuf_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
64int shuf_main(int argc, char **argv)
65{
66 unsigned opts;
67 char *opt_i_str, *opt_n_str, *opt_o_str;
68 unsigned i;
69 char **lines;
70 unsigned numlines;
71 char eol;
72
73 opt_complementary = "e--i:i--e"; /* mutually exclusive */
74 opts = getopt32(argv, OPT_STR, &opt_i_str, &opt_n_str, &opt_o_str);
75
76 argc -= optind;
77 argv += optind;
78
79 /* Prepare lines for shuffling - either: */
80 if (opts & OPT_e) {
81 /* make lines from command-line arguments */
82 numlines = argc;
83 lines = argv;
84 } else
85 if (opts & OPT_i) {
86 /* create a range of numbers */
87 char *dash;
88 unsigned lo, hi;
89
90 dash = strchr(opt_i_str, '-');
91 if (!dash) {
92 bb_error_msg_and_die("bad range '%s'", opt_i_str);
93 }
94 *dash = '\0';
95 lo = xatou(opt_i_str);
96 hi = xatou(dash + 1);
97 *dash = '-';
98 if (hi < lo) {
99 bb_error_msg_and_die("bad range '%s'", opt_i_str);
100 }
101
102 numlines = (hi+1) - lo;
103 lines = xmalloc(numlines * sizeof(lines[0]));
104 for (i = 0; i < numlines; i++) {
105 lines[i] = (char*)(uintptr_t)lo;
106 lo++;
107 }
108 } else {
109 /* default - read lines from stdin or the input file */
110 FILE *fp;
111
112 if (argc > 1)
113 bb_show_usage();
114
115 fp = xfopen_stdin(argv[0] ? argv[0] : "-");
116 lines = NULL;
117 numlines = 0;
118 for (;;) {
119 char *line = xmalloc_fgetline(fp);
120 if (!line)
121 break;
122 lines = xrealloc_vector(lines, 6, numlines);
123 lines[numlines++] = line;
124 }
125 fclose_if_not_stdin(fp);
126 }
127
128 if (numlines != 0)
129 shuffle_lines(lines, numlines);
130
131 if (opts & OPT_o)
132 xmove_fd(xopen(opt_o_str, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
133
134 if (opts & OPT_n) {
135 unsigned maxlines;
136 maxlines = xatou(opt_n_str);
137 if (numlines > maxlines)
138 numlines = maxlines;
139 }
140
141 eol = '\n';
142 if (opts & OPT_z)
143 eol = '\0';
144
145 for (i = 0; i < numlines; i++) {
146 if (opts & OPT_i)
147 printf("%u%c", (unsigned)(uintptr_t)lines[i], eol);
148 else
149 printf("%s%c", lines[i], eol);
150 }
151
152 fflush_stdout_and_exit(EXIT_SUCCESS);
153}
diff --git a/coreutils/uname.c b/coreutils/uname.c
index b4827de25..56d985eb0 100644
--- a/coreutils/uname.c
+++ b/coreutils/uname.c
@@ -49,16 +49,18 @@
49 */ 49 */
50 50
51//usage:#define uname_trivial_usage 51//usage:#define uname_trivial_usage
52//usage: "[-amnrspv]" 52//usage: "[-amnrspvio]"
53//usage:#define uname_full_usage "\n\n" 53//usage:#define uname_full_usage "\n\n"
54//usage: "Print system information\n" 54//usage: "Print system information\n"
55//usage: "\n -a Print all" 55//usage: "\n -a Print all"
56//usage: "\n -m The machine (hardware) type" 56//usage: "\n -m The machine (hardware) type"
57//usage: "\n -n Hostname" 57//usage: "\n -n Hostname"
58//usage: "\n -r OS release" 58//usage: "\n -r Kernel release"
59//usage: "\n -s OS name (default)" 59//usage: "\n -s Kernel name (default)"
60//usage: "\n -p Processor type" 60//usage: "\n -p Processor type"
61//usage: "\n -v OS version" 61//usage: "\n -v Kernel version"
62//usage: "\n -i The hardware platform"
63//usage: "\n -o OS name"
62//usage: 64//usage:
63//usage:#define uname_example_usage 65//usage:#define uname_example_usage
64//usage: "$ uname -a\n" 66//usage: "$ uname -a\n"