aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--coreutils/cut.c11
-rw-r--r--coreutils/date.c2
-rw-r--r--coreutils/df.c7
-rw-r--r--coreutils/ls.c246
-rw-r--r--coreutils/md5_sha1_sum.c58
5 files changed, 219 insertions, 105 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c
index 93b58b493..d81f36bcd 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -143,15 +143,15 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
143 } else if (!opt_REGEX && *delim == '\n') { 143 } else if (!opt_REGEX && *delim == '\n') {
144 unsigned spos = cut_list[cl_pos].startpos; 144 unsigned spos = cut_list[cl_pos].startpos;
145 145
146 linenum++;
146 /* get out if we have no more ranges to process or if the lines 147 /* get out if we have no more ranges to process or if the lines
147 * are lower than what we're interested in */ 148 * are lower than what we're interested in */
148 if ((linenum < spos) || END_OF_LIST(cut_list[cl_pos])) 149 if (linenum <= spos || END_OF_LIST(cut_list[cl_pos]))
149 goto next_line; 150 goto next_line;
150 151
151 /* if the line we're looking for is lower than the one we were 152 /* if the line we're looking for is lower than the one we were
152 * passed, it means we displayed it already, so move on */ 153 * passed, it means we displayed it already, so move on */
153 while (spos < linenum) { 154 while (++spos < linenum) {
154 spos++;
155 /* go to the next list if we're at the end of this one */ 155 /* go to the next list if we're at the end of this one */
156 if (spos > cut_list[cl_pos].endpos) { 156 if (spos > cut_list[cl_pos].endpos) {
157 cl_pos++; 157 cl_pos++;
@@ -161,7 +161,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
161 spos = cut_list[cl_pos].startpos; 161 spos = cut_list[cl_pos].startpos;
162 /* get out if the current line is lower than the one 162 /* get out if the current line is lower than the one
163 * we just became interested in */ 163 * we just became interested in */
164 if (linenum < spos) 164 if (linenum <= spos)
165 goto next_line; 165 goto next_line;
166 } 166 }
167 } 167 }
@@ -280,7 +280,6 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
280 /* if we printed anything, finish with newline */ 280 /* if we printed anything, finish with newline */
281 putchar('\n'); 281 putchar('\n');
282 next_line: 282 next_line:
283 linenum++;
284 free(line); 283 free(line);
285 } /* while (got line) */ 284 } /* while (got line) */
286 285
@@ -399,7 +398,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
399 //if (nranges == 0) 398 //if (nranges == 0)
400 // bb_simple_error_msg_and_die("missing list of positions"); 399 // bb_simple_error_msg_and_die("missing list of positions");
401 //^^^ this is impossible since one of -bcfF is required, 400 //^^^ this is impossible since one of -bcfF is required,
402 // they populate LIST with non-empty string and when it is parsed, 401 // they populate LIST with non-NULL string and when it is parsed,
403 // cut_list[] gets at least one element. 402 // cut_list[] gets at least one element.
404 403
405 /* now that the lists are parsed, we need to sort them to make life 404 /* now that the lists are parsed, we need to sort them to make life
diff --git a/coreutils/date.c b/coreutils/date.c
index 3a89b6caf..ef482af1b 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -289,7 +289,7 @@ int date_main(int argc UNUSED_PARAM, char **argv)
289 289
290 /* if setting time, set it */ 290 /* if setting time, set it */
291 if ((opt & OPT_SET) && clock_settime(CLOCK_REALTIME, &ts) < 0) { 291 if ((opt & OPT_SET) && clock_settime(CLOCK_REALTIME, &ts) < 0) {
292 bb_simple_perror_msg("can't set date"); 292 bb_simple_perror_msg_and_die("can't set date");
293 } 293 }
294 } 294 }
295 295
diff --git a/coreutils/df.c b/coreutils/df.c
index 03aa78148..fba23246f 100644
--- a/coreutils/df.c
+++ b/coreutils/df.c
@@ -155,6 +155,9 @@ int df_main(int argc UNUSED_PARAM, char **argv)
155 , &opt_t 155 , &opt_t
156 IF_FEATURE_DF_FANCY(, &chp) 156 IF_FEATURE_DF_FANCY(, &chp)
157 ); 157 );
158 /* -k overrides $POSIXLY_CORRECT: */
159 if (opt & OPT_KILO)
160 df_disp_hr = 1024;
158 if (opt & OPT_MEGA) 161 if (opt & OPT_MEGA)
159 df_disp_hr = 1024*1024; 162 df_disp_hr = 1024*1024;
160 163
@@ -185,8 +188,8 @@ int df_main(int argc UNUSED_PARAM, char **argv)
185 if (disp_units_hdr == NULL) { 188 if (disp_units_hdr == NULL) {
186#if ENABLE_FEATURE_HUMAN_READABLE 189#if ENABLE_FEATURE_HUMAN_READABLE
187 disp_units_hdr = xasprintf("%s-blocks", 190 disp_units_hdr = xasprintf("%s-blocks",
188 /* print df_disp_hr, show no fractionals, 191 /* print df_disp_hr; show no fractionals;
189 * use suffixes if OPT_POSIX is set in opt */ 192 * if -P, unit=1 (print it in full, no KMG suffixes) */
190 make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX)) 193 make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
191 ); 194 );
192#else 195#else
diff --git a/coreutils/ls.c b/coreutils/ls.c
index cc809b797..c24561576 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -126,6 +126,8 @@
126//usage: "\n -F Append indicator (one of */=@|) to names" 126//usage: "\n -F Append indicator (one of */=@|) to names"
127//usage: ) 127//usage: )
128//usage: "\n -l Long format" 128//usage: "\n -l Long format"
129////usage: "\n -g Long format without group column"
130////TODO: support -G too ("suppress owner column", GNUism)
129//usage: "\n -i List inode numbers" 131//usage: "\n -i List inode numbers"
130//usage: "\n -n List numeric UIDs and GIDs instead of names" 132//usage: "\n -n List numeric UIDs and GIDs instead of names"
131//usage: "\n -s List allocated blocks" 133//usage: "\n -s List allocated blocks"
@@ -159,6 +161,8 @@
159//usage: IF_FEATURE_LS_WIDTH( 161//usage: IF_FEATURE_LS_WIDTH(
160//usage: "\n -w N Format N columns wide" 162//usage: "\n -w N Format N columns wide"
161//usage: ) 163//usage: )
164////usage: "\n -Q Double-quote names"
165////usage: "\n -q Replace unprintable chars with '?'"
162//usage: IF_FEATURE_LS_COLOR( 166//usage: IF_FEATURE_LS_COLOR(
163//usage: "\n --color[={always,never,auto}]" 167//usage: "\n --color[={always,never,auto}]"
164//usage: ) 168//usage: )
@@ -196,27 +200,47 @@ SPLIT_SUBDIR = 2,
196 200
197/* -Cadi1l Std options, busybox always supports */ 201/* -Cadi1l Std options, busybox always supports */
198/* -gnsxA Std options, busybox always supports */ 202/* -gnsxA Std options, busybox always supports */
199/* -Q GNU option, busybox always supports */ 203/* -Q GNU option, busybox always supports: */
200/* -k Std option, busybox always supports (by ignoring) */ 204/* -Q, --quote-name */
201/* It means "for -s, show sizes in kbytes" */ 205/* enclose entry names in double quotes */
202/* Seems to only affect "POSIXLY_CORRECT=1 ls -sk" */
203/* since otherwise -s shows kbytes anyway */
204/* -LHRctur Std options, busybox optionally supports */ 206/* -LHRctur Std options, busybox optionally supports */
205/* -Fp Std options, busybox optionally supports */ 207/* -Fp Std options, busybox optionally supports */
206/* -SXvhTw GNU options, busybox optionally supports */ 208/* -SXvhTw GNU options, busybox optionally supports */
207/* -T WIDTH Ignored (we don't use tabs on output) */ 209/* -T WIDTH Ignored (we don't use tabs on output) */
208/* -Z SELinux mandated option, busybox optionally supports */ 210/* -Z SELinux mandated option, busybox optionally supports */
211/* -q Std option, busybox always supports: */
212/* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html: */
213/* Force each instance of non-printable filename characters and */
214/* <tab> characters to be written as the <question-mark> ('?') */
215/* character. Implementations may provide this option by default */
216/* if the output is to a terminal device. */
217/* -k Std option, busybox always supports (by ignoring) */
218/* It means "for -s, show sizes in kbytes" */
219/* Seems to only affect "POSIXLY_CORRECT=1 ls -sk" */
220/* since otherwise -s shows kbytes anyway */
209#define ls_options \ 221#define ls_options \
210 "Cadi1lgnsxAk" /* 12 opts, total 12 */ \ 222 "Cadi1lgnsxA" /* 11 opts, total 11 */ \
211 IF_FEATURE_LS_FILETYPES("Fp") /* 2, 14 */ \ 223 IF_FEATURE_LS_FILETYPES("Fp") /* 2, 13 */ \
212 IF_FEATURE_LS_RECURSIVE("R") /* 1, 15 */ \ 224 IF_FEATURE_LS_RECURSIVE("R") /* 1, 14 */ \
213 IF_SELINUX("Z") /* 1, 16 */ \ 225 IF_SELINUX("Z") /* 1, 15 */ \
214 "Q" /* 1, 17 */ \ 226 "Q" /* 1, 16 */ \
215 IF_FEATURE_LS_TIMESTAMPS("ctu") /* 3, 20 */ \ 227 IF_FEATURE_LS_TIMESTAMPS("ctu") /* 3, 19 */ \
216 IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 24 */ \ 228 IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 23 */ \
217 IF_FEATURE_LS_FOLLOWLINKS("LH") /* 2, 26 */ \ 229 IF_FEATURE_LS_FOLLOWLINKS("LH") /* 2, 25 */ \
218 IF_FEATURE_HUMAN_READABLE("h") /* 1, 27 */ \ 230 IF_FEATURE_HUMAN_READABLE("h") /* 1, 26 */ \
219 IF_FEATURE_LS_WIDTH("T:w:") /* 2, 29 */ 231 IF_FEATURE_LS_WIDTH("T:w:") /* 2, 28 */ \
232 IF_LONG_OPTS("\xff") /* 1, 29 */ \
233 IF_LONG_OPTS("\xfe") /* 1, 30 */ \
234 IF_LONG_OPTS("\xfd") /* 1, 31 */ \
235 "qk" /* 2, 33 */
236
237#if ENABLE_LONG_OPTS
238static const char ls_longopts[] ALIGN1 =
239 "full-time\0" No_argument "\xff"
240 "group-directories-first\0" No_argument "\xfe"
241 IF_FEATURE_LS_COLOR("color\0" Optional_argument "\xfd")
242;
243#endif
220 244
221enum { 245enum {
222 OPT_C = (1 << 0), 246 OPT_C = (1 << 0),
@@ -230,29 +254,31 @@ enum {
230 OPT_s = (1 << 8), 254 OPT_s = (1 << 8),
231 OPT_x = (1 << 9), 255 OPT_x = (1 << 9),
232 OPT_A = (1 << 10), 256 OPT_A = (1 << 10),
233 //OPT_k = (1 << 11),
234 257
235 OPTBIT_F = 12, 258 OPTBIT_F = 11,
236 OPTBIT_p, /* 13 */ 259 OPTBIT_p, /* 12 */
237 OPTBIT_R = OPTBIT_F + 2 * ENABLE_FEATURE_LS_FILETYPES, 260 OPTBIT_R = OPTBIT_F + 2 * ENABLE_FEATURE_LS_FILETYPES,
238 OPTBIT_Z = OPTBIT_R + 1 * ENABLE_FEATURE_LS_RECURSIVE, 261 OPTBIT_Z = OPTBIT_R + 1 * ENABLE_FEATURE_LS_RECURSIVE,
239 OPTBIT_Q = OPTBIT_Z + 1 * ENABLE_SELINUX, 262 OPTBIT_Q = OPTBIT_Z + 1 * ENABLE_SELINUX,
240 OPTBIT_c, /* 17 */ 263 OPTBIT_c, /* 16 */
241 OPTBIT_t, /* 18 */ 264 OPTBIT_t, /* 17 */
242 OPTBIT_u, /* 19 */ 265 OPTBIT_u, /* 18 */
243 OPTBIT_S = OPTBIT_c + 3 * ENABLE_FEATURE_LS_TIMESTAMPS, 266 OPTBIT_S = OPTBIT_c + 3 * ENABLE_FEATURE_LS_TIMESTAMPS,
244 OPTBIT_X, /* 21 */ 267 OPTBIT_X, /* 20 */
245 OPTBIT_r, /* 22 */ 268 OPTBIT_r, /* 21 */
246 OPTBIT_v, /* 23 */ 269 OPTBIT_v, /* 22 */
247 OPTBIT_L = OPTBIT_S + 4 * ENABLE_FEATURE_LS_SORTFILES, 270 OPTBIT_L = OPTBIT_S + 4 * ENABLE_FEATURE_LS_SORTFILES,
248 OPTBIT_H, /* 25 */ 271 OPTBIT_H, /* 24 */
249 OPTBIT_h = OPTBIT_L + 2 * ENABLE_FEATURE_LS_FOLLOWLINKS, 272 OPTBIT_h = OPTBIT_L + 2 * ENABLE_FEATURE_LS_FOLLOWLINKS,
250 OPTBIT_T = OPTBIT_h + 1 * ENABLE_FEATURE_HUMAN_READABLE, 273 OPTBIT_T = OPTBIT_h + 1 * ENABLE_FEATURE_HUMAN_READABLE,
251 OPTBIT_w, /* 28 */ 274 OPTBIT_w, /* 27 */
252 OPTBIT_full_time = OPTBIT_T + 2 * ENABLE_FEATURE_LS_WIDTH, 275 OPTBIT_full_time = OPTBIT_T + 2 * ENABLE_FEATURE_LS_WIDTH,
253 OPTBIT_dirs_first, 276 OPTBIT_dirs_first,
254 OPTBIT_color, /* 31 */ 277 OPTBIT_color, /* 30 */
255 /* with long opts, we use all 32 bits */ 278 OPTBIT_q = OPTBIT_color + 1, /* 31 */
279 OPTBIT_k = OPTBIT_q + 1, /* 32 */
280 /* with all options enabled, we use all 32 bits and even one extra bit! */
281 /* this works because -k is ignored, and getopt32 allows such "ignore" options past 31th bit */
256 282
257 OPT_F = (1 << OPTBIT_F) * ENABLE_FEATURE_LS_FILETYPES, 283 OPT_F = (1 << OPTBIT_F) * ENABLE_FEATURE_LS_FILETYPES,
258 OPT_p = (1 << OPTBIT_p) * ENABLE_FEATURE_LS_FILETYPES, 284 OPT_p = (1 << OPTBIT_p) * ENABLE_FEATURE_LS_FILETYPES,
@@ -274,6 +300,8 @@ enum {
274 OPT_full_time = (1 << OPTBIT_full_time ) * ENABLE_LONG_OPTS, 300 OPT_full_time = (1 << OPTBIT_full_time ) * ENABLE_LONG_OPTS,
275 OPT_dirs_first = (1 << OPTBIT_dirs_first) * ENABLE_LONG_OPTS, 301 OPT_dirs_first = (1 << OPTBIT_dirs_first) * ENABLE_LONG_OPTS,
276 OPT_color = (1 << OPTBIT_color ) * ENABLE_FEATURE_LS_COLOR, 302 OPT_color = (1 << OPTBIT_color ) * ENABLE_FEATURE_LS_COLOR,
303 OPT_q = (1 << OPTBIT_q),
304 //-k is ignored: OPT_k = (1 << OPTBIT_k),
277}; 305};
278 306
279/* 307/*
@@ -327,6 +355,7 @@ struct globals {
327#endif 355#endif
328 smallint exit_code; 356 smallint exit_code;
329 smallint show_dirname; 357 smallint show_dirname;
358 smallint tty_out;
330#if ENABLE_FEATURE_LS_WIDTH 359#if ENABLE_FEATURE_LS_WIDTH
331 unsigned terminal_width; 360 unsigned terminal_width;
332# define G_terminal_width (G.terminal_width) 361# define G_terminal_width (G.terminal_width)
@@ -343,16 +372,21 @@ struct globals {
343 setup_common_bufsiz(); \ 372 setup_common_bufsiz(); \
344 /* we have to zero it out because of NOEXEC */ \ 373 /* we have to zero it out because of NOEXEC */ \
345 memset(&G, 0, sizeof(G)); \ 374 memset(&G, 0, sizeof(G)); \
346 IF_FEATURE_LS_WIDTH(G_terminal_width = TERMINAL_WIDTH;) \ 375 IF_FEATURE_LS_WIDTH(G_terminal_width = ~0U;) \
347 IF_FEATURE_LS_TIMESTAMPS(time(&G.current_time_t);) \ 376 IF_FEATURE_LS_TIMESTAMPS(time(&G.current_time_t);) \
348} while (0) 377} while (0)
349 378
350#define ESC "\033" 379#define ESC "\033"
351 380
381static int G_isatty(void)
382{
383 if (!G.tty_out) /* not known yet? */
384 G.tty_out = isatty(STDOUT_FILENO) + 1;
385 return (G.tty_out == 2);
386}
352 387
353/*** Output code ***/ 388/*** Output code ***/
354 389
355
356/* FYI type values: 1:fifo 2:char 4:dir 6:blk 8:file 10:link 12:socket 390/* FYI type values: 1:fifo 2:char 4:dir 6:blk 8:file 10:link 12:socket
357 * (various wacky OSes: 13:Sun door 14:BSD whiteout 5:XENIX named file 391 * (various wacky OSes: 13:Sun door 14:BSD whiteout 5:XENIX named file
358 * 3/7:multiplexed char/block device) 392 * 3/7:multiplexed char/block device)
@@ -415,56 +449,93 @@ static char append_char(mode_t mode)
415} 449}
416#endif 450#endif
417 451
452/* Return the number of used columns.
453 * Note that only columnar output uses return value.
454 * -l and -1 modes don't care.
455 * coreutils 7.2 also supports:
456 * ls -b (--escape) = octal escapes (although it doesn't look like working)
457 * ls -N (--literal) = not escape at all
458 */
418static unsigned calc_name_len(const char *name) 459static unsigned calc_name_len(const char *name)
419{ 460{
420 unsigned len; 461 unsigned len;
421 uni_stat_t uni_stat; 462 uni_stat_t uni_stat;
422 463
423 // TODO: quote tab as \t, etc, if -Q 464 if (!(option_mask32 & (OPT_q|OPT_Q)))
424 name = printable_string2(&uni_stat, name); 465 return strlen(name);
425 466
426 if (!(option_mask32 & OPT_Q)) { 467 if (!(option_mask32 & OPT_Q)) {
468 /* the most likely branch: "ls" to tty (it auto-enables -q behavior) */
469 printable_string2(&uni_stat, name);
427 return uni_stat.unicode_width; 470 return uni_stat.unicode_width;
428 } 471 }
429 472
430 len = 2 + uni_stat.unicode_width; 473 len = 2 + strlen(name);
431 while (*name) { 474 while (*name) {
475 unsigned char ch = (unsigned char)*name;
476 if (ch < ' ' || ch > 0x7e) {
477 ch -= 7;
478 if (ch <= 6) {
479 /* quote chars 7..13 as \a,b,t,n,v,f,r */
480 goto two;
481 }
482 /* other chars <32 or >126 as \ooo octal */
483 len += 3;
484 goto next;
485 }
432 if (*name == '"' || *name == '\\') { 486 if (*name == '"' || *name == '\\') {
487 two:
433 len++; 488 len++;
434 } 489 }
490 next:
435 name++; 491 name++;
436 } 492 }
437 return len; 493 return len;
438} 494}
439
440/* Return the number of used columns.
441 * Note that only columnar output uses return value.
442 * -l and -1 modes don't care.
443 * coreutils 7.2 also supports:
444 * ls -b (--escape) = octal escapes (although it doesn't look like working)
445 * ls -N (--literal) = not escape at all
446 */
447static unsigned print_name(const char *name) 495static unsigned print_name(const char *name)
448{ 496{
449 unsigned len; 497 unsigned len;
450 uni_stat_t uni_stat; 498 uni_stat_t uni_stat;
451 499
452 // TODO: quote tab as \t, etc, if -Q 500 if (!(option_mask32 & (OPT_q|OPT_Q))) {
453 name = printable_string2(&uni_stat, name); 501 fputs_stdout(name);
502 return strlen(name);
503 }
454 504
455 if (!(option_mask32 & OPT_Q)) { 505 if (!(option_mask32 & OPT_Q)) {
506 /* the most likely branch: "ls" to tty (it auto-enables -q behavior) */
507 name = printable_string2(&uni_stat, name);
456 fputs_stdout(name); 508 fputs_stdout(name);
457 return uni_stat.unicode_width; 509 return uni_stat.unicode_width;
458 } 510 }
459 511
460 len = 2 + uni_stat.unicode_width; 512 len = 2 + strlen(name);
461 putchar('"'); 513 putchar('"');
462 while (*name) { 514 while (*name) {
463 if (*name == '"' || *name == '\\') { 515 unsigned char ch = (unsigned char)*name;
516 if (ch < ' ' || ch > 0x7e) {
464 putchar('\\'); 517 putchar('\\');
518 ch -= 7;
519 if (ch <= 6) {
520 /* quote chars 7..13 as \a,b,t,n,v,f,r */
521 ch = c_escape_conv_str07[1 + 3 * ch];
522 goto two;
523 }
524 /* other chars <32 or >126 as \ooo octal */
525 ch = (unsigned char)*name;
526 putchar('0' + (ch>>6));
527 putchar('0' + ((ch>>3) & 7));
528 ch = '0' + (ch & 7);
529 len += 3;
530 goto put_ch;
531 }
532 if (ch == '"' || ch == '\\') {
533 putchar('\\');
534 two:
465 len++; 535 len++;
466 } 536 }
467 putchar(*name); 537 put_ch:
538 putchar(ch);
468 name++; 539 name++;
469 } 540 }
470 putchar('"'); 541 putchar('"');
@@ -646,7 +717,7 @@ static void display_files(struct dnode **dn, unsigned nfiles)
646 unsigned i, ncols, nrows, row, nc; 717 unsigned i, ncols, nrows, row, nc;
647 unsigned column; 718 unsigned column;
648 unsigned nexttab; 719 unsigned nexttab;
649 unsigned column_width = 0; /* used only by coulmnal output */ 720 unsigned column_width = 0; /* used only by columnar output */
650 721
651 if (option_mask32 & (OPT_l|OPT_1)) { 722 if (option_mask32 & (OPT_l|OPT_1)) {
652 ncols = 1; 723 ncols = 1;
@@ -691,6 +762,11 @@ static void display_files(struct dnode **dn, unsigned nfiles)
691 } 762 }
692 nexttab = column + column_width; 763 nexttab = column + column_width;
693 column += display_single(dn[i]); 764 column += display_single(dn[i]);
765 } else {
766 /* if -w999999999, ncols can be very large */
767 //bb_error_msg(" col:%u ncol:%u i:%i", nc, ncols, i); sleep1();
768 /* without "break", we loop millions of times here */
769 break;
694 } 770 }
695 } 771 }
696 putchar('\n'); 772 putchar('\n');
@@ -1090,25 +1166,11 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1090 /* need to initialize since --color has _an optional_ argument */ 1166 /* need to initialize since --color has _an optional_ argument */
1091 const char *color_opt = color_str; /* "always" */ 1167 const char *color_opt = color_str; /* "always" */
1092#endif 1168#endif
1093#if ENABLE_LONG_OPTS
1094 static const char ls_longopts[] ALIGN1 =
1095 "full-time\0" No_argument "\xff"
1096 "group-directories-first\0" No_argument "\xfe"
1097 IF_FEATURE_LS_COLOR("color\0" Optional_argument "\xfd")
1098 ;
1099#endif
1100 1169
1101 INIT_G(); 1170 INIT_G();
1102 1171
1103 init_unicode(); 1172 init_unicode();
1104 1173
1105#if ENABLE_FEATURE_LS_WIDTH
1106 /* obtain the terminal width */
1107 G_terminal_width = get_terminal_width(STDIN_FILENO);
1108 /* go one less... */
1109 G_terminal_width--;
1110#endif
1111
1112 /* process options */ 1174 /* process options */
1113 opt = getopt32long(argv, "^" 1175 opt = getopt32long(argv, "^"
1114 ls_options 1176 ls_options
@@ -1144,6 +1206,29 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1144 exit(0); 1206 exit(0);
1145#endif 1207#endif
1146 1208
1209 /* ftpd secret backdoor? */
1210 if (ENABLE_FTPD && applet_name[0] == 'f') {
1211 /* dirs first are much nicer */
1212 opt = option_mask32 |= OPT_dirs_first;
1213 /* don't show SEcontext */
1214 IF_SELINUX(opt = option_mask32 &= ~OPT_Z;)
1215 /* do not query stdout about size and tty-ness */
1216 IF_FEATURE_LS_WIDTH(G_terminal_width = INT_MAX;)
1217 G.tty_out = 1; /* not a tty */
1218 goto skip_if_ftpd;
1219 }
1220
1221#if ENABLE_FEATURE_LS_WIDTH
1222 if ((int)G_terminal_width < 0) {
1223 /* obtain the terminal width */
1224 G_terminal_width = get_terminal_width(STDIN_FILENO);
1225 /* go one less... */
1226 G_terminal_width--;
1227 }
1228 if (G_terminal_width == 0) /* -w0 */
1229 G_terminal_width = INT_MAX; /* "infinite" */
1230#endif
1231
1147#if ENABLE_SELINUX 1232#if ENABLE_SELINUX
1148 if (opt & OPT_Z) { 1233 if (opt & OPT_Z) {
1149 if (!is_selinux_enabled()) 1234 if (!is_selinux_enabled())
@@ -1157,7 +1242,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1157 char *p = getenv("LS_COLORS"); 1242 char *p = getenv("LS_COLORS");
1158 /* LS_COLORS is unset, or (not empty && not "none") ? */ 1243 /* LS_COLORS is unset, or (not empty && not "none") ? */
1159 if (!p || (p[0] && strcmp(p, "none") != 0)) { 1244 if (!p || (p[0] && strcmp(p, "none") != 0)) {
1160 if (isatty(STDOUT_FILENO)) { 1245 if (G_isatty()) {
1161 /* check isatty() last because it's expensive (syscall) */ 1246 /* check isatty() last because it's expensive (syscall) */
1162 G_show_color = 1; 1247 G_show_color = 1;
1163 } 1248 }
@@ -1166,23 +1251,28 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1166 if (opt & OPT_color) { 1251 if (opt & OPT_color) {
1167 if (color_opt[0] == 'n') 1252 if (color_opt[0] == 'n')
1168 G_show_color = 0; 1253 G_show_color = 0;
1169 else switch (index_in_substrings(color_str, color_opt)) { 1254 else if (!G_show_color) {
1170 case 3: 1255 /* if() is not needed, but avoids extra isatty() if G_show_color is already set */
1171 case 4: 1256 /* Check --color=COLOR_OPT and maybe set show_color=1 */
1172 case 5: 1257 switch (index_in_substrings(color_str, color_opt)) {
1173 if (!is_TERM_dumb() && isatty(STDOUT_FILENO)) { 1258 case 3: // auto
1174 case 0: 1259 case 4: // tty
1175 case 1: 1260 case 5: // if-tty
1176 case 2: 1261 if (!is_TERM_dumb() && G_isatty()) {
1177 G_show_color = 1; 1262 case 0: // always
1263 case 1: // yes
1264 case 2: // force
1265 G_show_color = 1;
1266 }
1178 } 1267 }
1179 } 1268 }
1180 } 1269 }
1181#endif 1270#endif
1271 skip_if_ftpd:
1182 1272
1183 /* sort out which command line options take precedence */ 1273 /* sort out which command line options take precedence */
1184 if (ENABLE_FEATURE_LS_RECURSIVE && (opt & OPT_d)) 1274 if (ENABLE_FEATURE_LS_RECURSIVE && (opt & OPT_d))
1185 option_mask32 &= ~OPT_R; /* no recurse if listing only dir */ 1275 opt = option_mask32 &= ~OPT_R; /* no recurse if listing only dir */
1186 if (!(opt & OPT_l)) { /* not -l? */ 1276 if (!(opt & OPT_l)) { /* not -l? */
1187 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) { 1277 if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
1188 /* when to sort by time? -t[cu] sorts by time even with -l */ 1278 /* when to sort by time? -t[cu] sorts by time even with -l */
@@ -1190,19 +1280,17 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
1190 /* without -l, bare -c or -u enable sort too */ 1280 /* without -l, bare -c or -u enable sort too */
1191 /* (with -l, bare -c or -u just select which time to show) */ 1281 /* (with -l, bare -c or -u just select which time to show) */
1192 if (opt & (OPT_c|OPT_u)) { 1282 if (opt & (OPT_c|OPT_u)) {
1193 option_mask32 |= OPT_t; 1283 opt = option_mask32 |= OPT_t;
1194 } 1284 }
1195 } 1285 }
1196 } 1286 }
1197 1287
1198 /* choose a display format if one was not already specified by an option */ 1288 /* choose a display format if one was not already specified by an option */
1199 if (!(option_mask32 & (OPT_l|OPT_1|OPT_x|OPT_C))) 1289 if (!(opt & (OPT_l|OPT_1|OPT_x|OPT_C)))
1200 option_mask32 |= (isatty(STDOUT_FILENO) ? OPT_C : OPT_1); 1290 opt = option_mask32 |= (G_isatty() ? OPT_C : OPT_1);
1201 1291
1202 if (ENABLE_FTPD && applet_name[0] == 'f') { 1292 if (!(opt & OPT_q) && G_isatty())
1203 /* ftpd secret backdoor. dirs first are much nicer */ 1293 opt = option_mask32 |= OPT_q;
1204 option_mask32 |= OPT_dirs_first;
1205 }
1206 1294
1207 argv += optind; 1295 argv += optind;
1208 if (!argv[0]) 1296 if (!argv[0])
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index 978d328f1..4506aeb56 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -23,6 +23,12 @@
23//config: help 23//config: help
24//config: Compute and check SHA256 message digest 24//config: Compute and check SHA256 message digest
25//config: 25//config:
26//config:config SHA384SUM
27//config: bool "sha384sum (7.3 kb)"
28//config: default y
29//config: help
30//config: Compute and check SHA384 message digest
31//config:
26//config:config SHA512SUM 32//config:config SHA512SUM
27//config: bool "sha512sum (7.3 kb)" 33//config: bool "sha512sum (7.3 kb)"
28//config: default y 34//config: default y
@@ -35,13 +41,13 @@
35//config: help 41//config: help
36//config: Compute and check SHA3 message digest 42//config: Compute and check SHA3 message digest
37//config: 43//config:
38//config:comment "Common options for md5sum, sha1sum, sha256sum, sha512sum, sha3sum" 44//config:comment "Common options for md5sum, sha1sum, sha256sum, ..., sha3sum"
39//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM 45//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA384SUM || SHA512SUM || SHA3SUM
40//config: 46//config:
41//config:config FEATURE_MD5_SHA1_SUM_CHECK 47//config:config FEATURE_MD5_SHA1_SUM_CHECK
42//config: bool "Enable -c, -s and -w options" 48//config: bool "Enable -c, -s and -w options"
43//config: default y 49//config: default y
44//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM 50//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA384SUM || SHA512SUM || SHA3SUM
45//config: help 51//config: help
46//config: Enabling the -c options allows files to be checked 52//config: Enabling the -c options allows files to be checked
47//config: against pre-calculated hash values. 53//config: against pre-calculated hash values.
@@ -51,11 +57,13 @@
51//applet:IF_SHA1SUM(APPLET_NOEXEC(sha1sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha1sum)) 57//applet:IF_SHA1SUM(APPLET_NOEXEC(sha1sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha1sum))
52//applet:IF_SHA3SUM(APPLET_NOEXEC(sha3sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha3sum)) 58//applet:IF_SHA3SUM(APPLET_NOEXEC(sha3sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha3sum))
53//applet:IF_SHA256SUM(APPLET_NOEXEC(sha256sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha256sum)) 59//applet:IF_SHA256SUM(APPLET_NOEXEC(sha256sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha256sum))
60//applet:IF_SHA384SUM(APPLET_NOEXEC(sha384sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha384sum))
54//applet:IF_SHA512SUM(APPLET_NOEXEC(sha512sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha512sum)) 61//applet:IF_SHA512SUM(APPLET_NOEXEC(sha512sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha512sum))
55 62
56//kbuild:lib-$(CONFIG_MD5SUM) += md5_sha1_sum.o 63//kbuild:lib-$(CONFIG_MD5SUM) += md5_sha1_sum.o
57//kbuild:lib-$(CONFIG_SHA1SUM) += md5_sha1_sum.o 64//kbuild:lib-$(CONFIG_SHA1SUM) += md5_sha1_sum.o
58//kbuild:lib-$(CONFIG_SHA256SUM) += md5_sha1_sum.o 65//kbuild:lib-$(CONFIG_SHA256SUM) += md5_sha1_sum.o
66//kbuild:lib-$(CONFIG_SHA384SUM) += md5_sha1_sum.o
59//kbuild:lib-$(CONFIG_SHA512SUM) += md5_sha1_sum.o 67//kbuild:lib-$(CONFIG_SHA512SUM) += md5_sha1_sum.o
60//kbuild:lib-$(CONFIG_SHA3SUM) += md5_sha1_sum.o 68//kbuild:lib-$(CONFIG_SHA3SUM) += md5_sha1_sum.o
61 69
@@ -99,6 +107,16 @@
99//usage: "\n -w Warn about improperly formatted checksum lines" 107//usage: "\n -w Warn about improperly formatted checksum lines"
100//usage: ) 108//usage: )
101//usage: 109//usage:
110//usage:#define sha384sum_trivial_usage
111//usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
112//usage:#define sha384sum_full_usage "\n\n"
113//usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA384 checksums"
114//usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
115//usage: "\n -c Check sums against list in FILEs"
116//usage: "\n -s Don't output anything, status code shows success"
117//usage: "\n -w Warn about improperly formatted checksum lines"
118//usage: )
119//usage:
102//usage:#define sha512sum_trivial_usage 120//usage:#define sha512sum_trivial_usage
103//usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..." 121//usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
104//usage:#define sha512sum_full_usage "\n\n" 122//usage:#define sha512sum_full_usage "\n\n"
@@ -130,11 +148,12 @@
130 148
131enum { 149enum {
132 /* 4th letter of applet_name is... */ 150 /* 4th letter of applet_name is... */
133 HASH_MD5 = 's', /* "md5>s<um" */ 151 HASH_MD5 = 's', /* "md5>s<um" */
134 HASH_SHA1 = '1', 152 HASH_SHA1 = '1',
135 HASH_SHA256 = '2', 153 HASH_SHA256 = '2',
136 HASH_SHA3 = '3', 154 HASH_SHA3 = '3',
137 HASH_SHA512 = '5', 155 HASH_SHA512 = '5',
156 /* unfortunately, sha384sum has the same '3' as sha3 */
138}; 157};
139 158
140#define FLAG_SILENT 1 159#define FLAG_SILENT 1
@@ -158,10 +177,11 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
158#endif 177#endif
159static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned sha3_width) 178static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned sha3_width)
160{ 179{
161 int src_fd, hash_len, count; 180 int src_fd, count;
162 union _ctx_ { 181 union _ctx_ {
163 sha3_ctx_t sha3; 182 sha3_ctx_t sha3;
164 sha512_ctx_t sha512; 183 sha512_ctx_t sha512;
184 sha384_ctx_t sha384;
165 sha256_ctx_t sha256; 185 sha256_ctx_t sha256;
166 sha1_ctx_t sha1; 186 sha1_ctx_t sha1;
167 md5_ctx_t md5; 187 md5_ctx_t md5;
@@ -183,25 +203,31 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
183 md5_begin(&context.md5); 203 md5_begin(&context.md5);
184 update = (void*)md5_hash; 204 update = (void*)md5_hash;
185 final = (void*)md5_end; 205 final = (void*)md5_end;
186 hash_len = 16;
187 } 206 }
188 else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) { 207 else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
189 sha1_begin(&context.sha1); 208 sha1_begin(&context.sha1);
190 update = (void*)sha1_hash; 209 update = (void*)sha1_hash;
191 final = (void*)sha1_end; 210 final = (void*)sha1_end;
192 hash_len = 20;
193 } 211 }
194 else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) { 212 else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
195 sha256_begin(&context.sha256); 213 sha256_begin(&context.sha256);
196 update = (void*)sha256_hash; 214 update = (void*)sha256_hash;
197 final = (void*)sha256_end; 215 final = (void*)sha256_end;
198 hash_len = 32; 216 }
217 else if (ENABLE_SHA384SUM
218 && (ENABLE_SHA3SUM
219 ? (applet_name[4] == '8') /* check for "sha384", but do not match "sha3" */
220 : (hash_algo == '3') /* applet_name = "sha3sum" is not possible */
221 )
222 ) {
223 sha384_begin(&context.sha384);
224 update = (void*)sha384_hash;
225 final = (void*)sha384_end;
199 } 226 }
200 else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) { 227 else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
201 sha512_begin(&context.sha512); 228 sha512_begin(&context.sha512);
202 update = (void*)sha512_hash; 229 update = (void*)sha512_hash;
203 final = (void*)sha512_end; 230 final = (void*)sha512_end;
204 hash_len = 64;
205 } 231 }
206#if ENABLE_SHA3SUM 232#if ENABLE_SHA3SUM
207 else if (ENABLE_SHA3SUM && hash_algo == HASH_SHA3) { 233 else if (ENABLE_SHA3SUM && hash_algo == HASH_SHA3) {
@@ -219,9 +245,7 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
219 ) { 245 ) {
220 bb_error_msg_and_die("bad -a%u", sha3_width); 246 bb_error_msg_and_die("bad -a%u", sha3_width);
221 } 247 }
222 sha3_width /= 4; 248 context.sha3.input_block_bytes = 1600/8 - sha3_width/4;
223 context.sha3.input_block_bytes = 1600/8 - sha3_width;
224 hash_len = sha3_width/2;
225 } 249 }
226#endif 250#endif
227 else { 251 else {
@@ -236,7 +260,7 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
236 if (count < 0) 260 if (count < 0)
237 bb_perror_msg("can't read '%s'", filename); 261 bb_perror_msg("can't read '%s'", filename);
238 else /* count == 0 */ { 262 else /* count == 0 */ {
239 final(&context, in_buf); 263 unsigned hash_len = final(&context, in_buf);
240 hash_value = hash_bin_to_hex(in_buf, hash_len); 264 hash_value = hash_bin_to_hex(in_buf, hash_len);
241 } 265 }
242 } 266 }
@@ -262,14 +286,14 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
262 /* -b "binary", -t "text" are ignored (shaNNNsum compat) */ 286 /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
263 /* -s and -w require -c */ 287 /* -s and -w require -c */
264#if ENABLE_SHA3SUM 288#if ENABLE_SHA3SUM
265 if (applet_name[3] == HASH_SHA3) 289 if (applet_name[3] == HASH_SHA3 && (!ENABLE_SHA384SUM || applet_name[4] != '8'))
266 flags = getopt32(argv, "^" "scwbta:+" "\0" "s?c:w?c", &sha3_width); 290 flags = getopt32(argv, "^" "scwbta:+" "\0" "s?c:w?c", &sha3_width);
267 else 291 else
268#endif 292#endif
269 flags = getopt32(argv, "^" "scwbt" "\0" "s?c:w?c"); 293 flags = getopt32(argv, "^" "scwbt" "\0" "s?c:w?c");
270 } else { 294 } else {
271#if ENABLE_SHA3SUM 295#if ENABLE_SHA3SUM
272 if (applet_name[3] == HASH_SHA3) 296 if (applet_name[3] == HASH_SHA3 && (!ENABLE_SHA384SUM || applet_name[4] != '8'))
273 getopt32(argv, "a:+", &sha3_width); 297 getopt32(argv, "a:+", &sha3_width);
274 else 298 else
275#endif 299#endif