aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2011-01-04 19:55:06 +0700
committerNguyễn Thái Ngọc Duy <pclouds@gmail.com>2011-01-04 19:55:06 +0700
commit3f357a9c754805c4c38793749927aeda82797735 (patch)
tree65bb50517515714b6baaa4d5d2debed1e716bc83 /coreutils
parent47a20f7daf954c90bbc77d2c108cb366171c650f (diff)
parent776509544123c68bbc128c0fdb2f699062d294cf (diff)
downloadbusybox-w32-3f357a9c754805c4c38793749927aeda82797735.tar.gz
busybox-w32-3f357a9c754805c4c38793749927aeda82797735.tar.bz2
busybox-w32-3f357a9c754805c4c38793749927aeda82797735.zip
Merge commit 'e4dcba1c103dc28e927e004791e331aaf604383d^'
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/cksum.c7
-rw-r--r--coreutils/md5_sha1_sum.c41
-rw-r--r--coreutils/mv.c46
-rw-r--r--coreutils/printf.c18
4 files changed, 63 insertions, 49 deletions
diff --git a/coreutils/cksum.c b/coreutils/cksum.c
index 7bf383e2d..7a37e6add 100644
--- a/coreutils/cksum.c
+++ b/coreutils/cksum.c
@@ -18,7 +18,6 @@ int cksum_main(int argc UNUSED_PARAM, char **argv)
18 off_t length, filesize; 18 off_t length, filesize;
19 int bytes_read; 19 int bytes_read;
20 int exit_code = EXIT_SUCCESS; 20 int exit_code = EXIT_SUCCESS;
21 uint8_t *cp;
22 21
23#if ENABLE_DESKTOP 22#if ENABLE_DESKTOP
24 getopt32(argv, ""); /* coreutils 6.9 compat */ 23 getopt32(argv, ""); /* coreutils 6.9 compat */
@@ -39,11 +38,7 @@ int cksum_main(int argc UNUSED_PARAM, char **argv)
39 38
40#define read_buf bb_common_bufsiz1 39#define read_buf bb_common_bufsiz1
41 while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) { 40 while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) {
42 cp = (uint8_t *) read_buf; 41 crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
43 length += bytes_read;
44 do {
45 crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *cp++];
46 } while (--bytes_read);
47 } 42 }
48 close(fd); 43 close(fd);
49 44
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index e79210c0d..d3d294de9 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -10,13 +10,13 @@
10 10
11/* This is a NOEXEC applet. Be very careful! */ 11/* This is a NOEXEC applet. Be very careful! */
12 12
13typedef enum { 13enum {
14 /* 4th letter of applet_name is... */ 14 /* 4th letter of applet_name is... */
15 HASH_MD5 = 's', /* "md5>s<um" */ 15 HASH_MD5 = 's', /* "md5>s<um" */
16 HASH_SHA1 = '1', 16 HASH_SHA1 = '1',
17 HASH_SHA256 = '2', 17 HASH_SHA256 = '2',
18 HASH_SHA512 = '5', 18 HASH_SHA512 = '5',
19} hash_algo_t; 19};
20 20
21#define FLAG_SILENT 1 21#define FLAG_SILENT 1
22#define FLAG_CHECK 2 22#define FLAG_CHECK 2
@@ -32,7 +32,7 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
32 return (unsigned char *)hex_value; 32 return (unsigned char *)hex_value;
33} 33}
34 34
35static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/) 35static uint8_t *hash_file(const char *filename)
36{ 36{
37 int src_fd, hash_len, count; 37 int src_fd, hash_len, count;
38 union _ctx_ { 38 union _ctx_ {
@@ -45,13 +45,15 @@ static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
45 RESERVE_CONFIG_UBUFFER(in_buf, 4096); 45 RESERVE_CONFIG_UBUFFER(in_buf, 4096);
46 void FAST_FUNC (*update)(void*, const void*, size_t); 46 void FAST_FUNC (*update)(void*, const void*, size_t);
47 void FAST_FUNC (*final)(void*, void*); 47 void FAST_FUNC (*final)(void*, void*);
48 hash_algo_t hash_algo = applet_name[3]; 48 char hash_algo;
49 49
50 src_fd = open_or_warn_stdin(filename); 50 src_fd = open_or_warn_stdin(filename);
51 if (src_fd < 0) { 51 if (src_fd < 0) {
52 return NULL; 52 return NULL;
53 } 53 }
54 54
55 hash_algo = applet_name[3];
56
55 /* figure specific hash algorithims */ 57 /* figure specific hash algorithims */
56 if (ENABLE_MD5SUM && hash_algo == HASH_MD5) { 58 if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
57 md5_begin(&context.md5); 59 md5_begin(&context.md5);
@@ -74,10 +76,10 @@ static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
74 final = (void*)sha512_end; 76 final = (void*)sha512_end;
75 hash_len = 64; 77 hash_len = 64;
76 } else { 78 } else {
77 bb_error_msg_and_die("algorithm not supported"); 79 xfunc_die(); /* can't reach this */
78 } 80 }
79 81
80 while (0 < (count = safe_read(src_fd, in_buf, 4096))) { 82 while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
81 update(&context, in_buf, count); 83 update(&context, in_buf, count);
82 } 84 }
83 85
@@ -99,27 +101,26 @@ int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
99int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) 101int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
100{ 102{
101 int return_value = EXIT_SUCCESS; 103 int return_value = EXIT_SUCCESS;
102 uint8_t *hash_value;
103 unsigned flags; 104 unsigned flags;
104 /*hash_algo_t hash_algo = applet_name[3];*/
105 105
106 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) { 106 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
107 /* -b "binary", -t "text" are ignored (shaNNNsum compat) */ 107 /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
108 flags = getopt32(argv, "scwbt"); 108 flags = getopt32(argv, "scwbt");
109 argv += optind;
110 //argc -= optind;
111 } else {
112 argv += 1;
113 //argc -= 1;
109 } 114 }
110 else optind = 1;
111 argv += optind;
112 //argc -= optind;
113 if (!*argv) 115 if (!*argv)
114 *--argv = (char*)"-"; 116 *--argv = (char*)"-";
115 117
116 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) { 118 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
117 if (flags & FLAG_SILENT) { 119 if (flags & FLAG_SILENT) {
118 bb_error_msg_and_die 120 bb_error_msg_and_die("-%c is meaningful only with -c", 's');
119 ("-%c is meaningful only when verifying checksums", 's'); 121 }
120 } else if (flags & FLAG_WARN) { 122 if (flags & FLAG_WARN) {
121 bb_error_msg_and_die 123 bb_error_msg_and_die("-%c is meaningful only with -c", 'w');
122 ("-%c is meaningful only when verifying checksums", 'w');
123 } 124 }
124 } 125 }
125 126
@@ -130,13 +131,13 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
130 char *line; 131 char *line;
131 132
132 if (argv[1]) { 133 if (argv[1]) {
133 bb_error_msg_and_die 134 bb_error_msg_and_die("only one argument may be specified with -c");
134 ("only one argument may be specified when using -c");
135 } 135 }
136 136
137 pre_computed_stream = xfopen_stdin(argv[0]); 137 pre_computed_stream = xfopen_stdin(argv[0]);
138 138
139 while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) { 139 while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
140 uint8_t *hash_value;
140 char *filename_ptr; 141 char *filename_ptr;
141 142
142 count_total++; 143 count_total++;
@@ -157,7 +158,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
157 *filename_ptr = '\0'; 158 *filename_ptr = '\0';
158 filename_ptr += 2; 159 filename_ptr += 2;
159 160
160 hash_value = hash_file(filename_ptr /*, hash_algo*/); 161 hash_value = hash_file(filename_ptr);
161 162
162 if (hash_value && (strcmp((char*)hash_value, line) == 0)) { 163 if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
163 if (!(flags & FLAG_SILENT)) 164 if (!(flags & FLAG_SILENT))
@@ -183,7 +184,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
183 */ 184 */
184 } else { 185 } else {
185 do { 186 do {
186 hash_value = hash_file(*argv/*, hash_algo*/); 187 uint8_t *hash_value = hash_file(*argv);
187 if (hash_value == NULL) { 188 if (hash_value == NULL) {
188 return_value = EXIT_FAILURE; 189 return_value = EXIT_FAILURE;
189 } else { 190 } else {
diff --git a/coreutils/mv.c b/coreutils/mv.c
index 7f49d5bab..245639bd0 100644
--- a/coreutils/mv.c
+++ b/coreutils/mv.c
@@ -16,15 +16,30 @@
16#include "libbb.h" 16#include "libbb.h"
17#include "libcoreutils/coreutils.h" 17#include "libcoreutils/coreutils.h"
18 18
19//usage:#define mv_trivial_usage
20//usage: "[-fin] SOURCE DEST\n"
21//usage: "or: mv [-fin] SOURCE... DIRECTORY"
22//usage:#define mv_full_usage "\n\n"
23//usage: "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY\n"
24//usage: "\nOptions:"
25//usage: "\n -f Don't prompt before overwriting"
26//usage: "\n -i Interactive, prompt before overwrite"
27//usage: "\n -n Don't overwrite an existing file"
28//usage:
29//usage:#define mv_example_usage
30//usage: "$ mv /tmp/foo /bin/bar\n"
31
19#if ENABLE_FEATURE_MV_LONG_OPTIONS 32#if ENABLE_FEATURE_MV_LONG_OPTIONS
20static const char mv_longopts[] ALIGN1 = 33static const char mv_longopts[] ALIGN1 =
21 "interactive\0" No_argument "i" 34 "interactive\0" No_argument "i"
22 "force\0" No_argument "f" 35 "force\0" No_argument "f"
36 "no-clobber\0" No_argument "n"
23 ; 37 ;
24#endif 38#endif
25 39
26#define OPT_FILEUTILS_FORCE 1 40#define OPT_FILEUTILS_FORCE 1
27#define OPT_FILEUTILS_INTERACTIVE 2 41#define OPT_FILEUTILS_INTERACTIVE 2
42#define OPT_FILEUTILS_NOCLOBBER 4
28 43
29int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 44int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
30int mv_main(int argc, char **argv) 45int mv_main(int argc, char **argv)
@@ -40,10 +55,11 @@ int mv_main(int argc, char **argv)
40#if ENABLE_FEATURE_MV_LONG_OPTIONS 55#if ENABLE_FEATURE_MV_LONG_OPTIONS
41 applet_long_options = mv_longopts; 56 applet_long_options = mv_longopts;
42#endif 57#endif
43 // Need at least two arguments 58 /* Need at least two arguments.
44 // -f unsets -i, -i unsets -f 59 * If more than one of -f, -i, -n is specified , only the final one
45 opt_complementary = "-2:f-i:i-f"; 60 * takes effect (it unsets previous options). */
46 flags = getopt32(argv, "fi"); 61 opt_complementary = "-2:f-in:i-fn:n-fi";
62 flags = getopt32(argv, "fin");
47 argc -= optind; 63 argc -= optind;
48 argv += optind; 64 argv += optind;
49 last = argv[argc - 1]; 65 last = argv[argc - 1];
@@ -68,18 +84,22 @@ int mv_main(int argc, char **argv)
68 } 84 }
69 85
70 DO_MOVE: 86 DO_MOVE:
71 if (dest_exists 87 if (dest_exists) {
72 && !(flags & OPT_FILEUTILS_FORCE) 88 if (flags & OPT_FILEUTILS_NOCLOBBER)
73 && ((access(dest, W_OK) < 0 && isatty(0))
74 || (flags & OPT_FILEUTILS_INTERACTIVE))
75 ) {
76 if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
77 goto RET_1; /* Ouch! fprintf failed! */
78 }
79 if (!bb_ask_confirmation()) {
80 goto RET_0; 89 goto RET_0;
90 if (!(flags & OPT_FILEUTILS_FORCE)
91 && ((access(dest, W_OK) < 0 && isatty(0))
92 || (flags & OPT_FILEUTILS_INTERACTIVE))
93 ) {
94 if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
95 goto RET_1; /* Ouch! fprintf failed! */
96 }
97 if (!bb_ask_confirmation()) {
98 goto RET_0;
99 }
81 } 100 }
82 } 101 }
102
83 if (rename(*argv, dest) < 0) { 103 if (rename(*argv, dest) < 0) {
84 struct stat source_stat; 104 struct stat source_stat;
85 int source_exists; 105 int source_exists;
diff --git a/coreutils/printf.c b/coreutils/printf.c
index 2cc238439..c8395fa89 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -122,16 +122,14 @@ static double my_xstrtod(const char *arg)
122 return result; 122 return result;
123} 123}
124 124
125static void print_esc_string(char *str) 125static void print_esc_string(const char *str)
126{ 126{
127 while (*str) { 127 char c;
128 if (*str == '\\') { 128 while ((c = *str) != '\0') {
129 str++; 129 str++;
130 bb_putchar(bb_process_escape_sequence((const char **)&str)); 130 if (c == '\\')
131 } else { 131 c = bb_process_escape_sequence(&str);
132 bb_putchar(*str); 132 putchar(c);
133 str++;
134 }
135 } 133 }
136} 134}
137 135
@@ -344,7 +342,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
344 f--; 342 f--;
345 break; 343 break;
346 default: 344 default:
347 bb_putchar(*f); 345 putchar(*f);
348 } 346 }
349 } 347 }
350 348