aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/dd.c31
-rw-r--r--coreutils/truncate.c77
2 files changed, 83 insertions, 25 deletions
diff --git a/coreutils/dd.c b/coreutils/dd.c
index a01722040..589547e77 100644
--- a/coreutils/dd.c
+++ b/coreutils/dd.c
@@ -99,25 +99,6 @@ enum {
99 ofd = STDOUT_FILENO, 99 ofd = STDOUT_FILENO,
100}; 100};
101 101
102static const struct suffix_mult dd_suffixes[] = {
103 { "c", 1 },
104 { "w", 2 },
105 { "b", 512 },
106 { "kB", 1000 },
107 { "kD", 1000 },
108 { "k", 1024 },
109 { "K", 1024 }, /* compat with coreutils dd (it also accepts KB and KD, TODO?) */
110 { "MB", 1000000 },
111 { "MD", 1000000 },
112 { "M", 1024*1024 },
113 { "GB", 1000000000 },
114 { "GD", 1000000000 },
115 { "G", 1024*1024*1024 },
116 /* "D" suffix for decimal is not in coreutils manpage, looks like it's deprecated */
117 /* coreutils also understands TPEZY suffixes for tera- and so on, with B suffix for decimal */
118 { "", 0 }
119};
120
121struct globals { 102struct globals {
122 off_t out_full, out_part, in_full, in_part; 103 off_t out_full, out_part, in_full, in_part;
123#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE 104#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
@@ -327,11 +308,11 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
327#if ENABLE_FEATURE_DD_IBS_OBS 308#if ENABLE_FEATURE_DD_IBS_OBS
328 if (what == OP_ibs) { 309 if (what == OP_ibs) {
329 /* Must fit into positive ssize_t */ 310 /* Must fit into positive ssize_t */
330 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); 311 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
331 /*continue;*/ 312 /*continue;*/
332 } 313 }
333 if (what == OP_obs) { 314 if (what == OP_obs) {
334 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); 315 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
335 /*continue;*/ 316 /*continue;*/
336 } 317 }
337 if (what == OP_conv) { 318 if (what == OP_conv) {
@@ -357,22 +338,22 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
357 } 338 }
358#endif 339#endif
359 if (what == OP_bs) { 340 if (what == OP_bs) {
360 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); 341 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
361 obs = ibs; 342 obs = ibs;
362 /*continue;*/ 343 /*continue;*/
363 } 344 }
364 /* These can be large: */ 345 /* These can be large: */
365 if (what == OP_count) { 346 if (what == OP_count) {
366 G.flags |= FLAG_COUNT; 347 G.flags |= FLAG_COUNT;
367 count = XATOU_SFX(val, dd_suffixes); 348 count = XATOU_SFX(val, cwbkMG_suffixes);
368 /*continue;*/ 349 /*continue;*/
369 } 350 }
370 if (what == OP_seek) { 351 if (what == OP_seek) {
371 seek = XATOU_SFX(val, dd_suffixes); 352 seek = XATOU_SFX(val, cwbkMG_suffixes);
372 /*continue;*/ 353 /*continue;*/
373 } 354 }
374 if (what == OP_skip) { 355 if (what == OP_skip) {
375 skip = XATOU_SFX(val, dd_suffixes); 356 skip = XATOU_SFX(val, cwbkMG_suffixes);
376 /*continue;*/ 357 /*continue;*/
377 } 358 }
378 if (what == OP_if) { 359 if (what == OP_if) {
diff --git a/coreutils/truncate.c b/coreutils/truncate.c
new file mode 100644
index 000000000..0e36daba3
--- /dev/null
+++ b/coreutils/truncate.c
@@ -0,0 +1,77 @@
1/*
2 * Mini truncate implementation for busybox
3 *
4 * Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com>
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 */
8
9//config:config TRUNCATE
10//config: bool "truncate"
11//config: default y
12//config: help
13//config: truncate truncates files to a given size. If a file does
14//config: not exist, it is created unless told otherwise.
15
16//kbuild:lib-$(CONFIG_TRUNCATE) += truncate.o
17//applet:IF_TRUNCATE(APPLET_NOFORK(truncate, truncate, BB_DIR_USR_BIN, BB_SUID_DROP, truncate))
18
19//usage:#define truncate_trivial_usage
20//usage: "[-c] -s SIZE FILE..."
21//usage:#define truncate_full_usage "\n\n"
22//usage: "Truncate FILEs to the given size\n"
23//usage: "\n -c Do not create files"
24//usage: "\n -s SIZE Truncate to SIZE"
25//usage:
26//usage:#define truncate_example_usage
27//usage: "$ truncate -s 1G foo"
28
29#include "libbb.h"
30
31#if ENABLE_LFS
32# define XATOU_SFX xatoull_sfx
33#else
34# define XATOU_SFX xatoul_sfx
35#endif
36
37/* This is a NOFORK applet. Be very careful! */
38
39int truncate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
40int truncate_main(int argc UNUSED_PARAM, char **argv)
41{
42 unsigned opts;
43 int flags = O_RDWR;
44 int ret = EXIT_SUCCESS;
45 char *size_str;
46 off_t size;
47
48 enum {
49 OPT_NOCREATE = (1 << 0),
50 OPT_SIZE = (1 << 1),
51 };
52
53 opt_complementary = "s:-1";
54 opts = getopt32(argv, "cs:", &size_str);
55
56 if (!(opts & OPT_NOCREATE))
57 flags |= O_CREAT;
58
59 // TODO: coreutils 8.17 also support "m" (lowercase) suffix
60 // with truncate, but not with dd!
61 // We share kMG_suffixes[], so we can't make both tools
62 // compatible at once...
63 size = XATOU_SFX(size_str, kMG_suffixes);
64
65 argv += optind;
66 while (*argv) {
67 int fd = xopen(*argv, flags);
68 if (ftruncate(fd, size) == -1) {
69 bb_perror_msg("%s: ftruncate", *argv);
70 ret = EXIT_FAILURE;
71 }
72 xclose(fd);
73 ++argv;
74 }
75
76 return ret;
77}