396
|
1 /* vi: set sw=4 ts=4:
|
|
2 *
|
|
3 * truncate.c - set file length, extending sparsely if necessary
|
|
4 *
|
|
5 * Copyright 2011 Rob Landley <rob@landley.net>
|
|
6
|
|
7 USE_TRUNCATE(NEWTOY(truncate, "<1s#|c", TOYFLAG_BIN))
|
|
8
|
|
9 config TRUNCATE
|
|
10 bool "truncate"
|
|
11 default y
|
|
12 help
|
|
13 usage: truncate [-c] -s file...
|
|
14 Set length of file(s), extending sparsely if necessary.
|
|
15
|
|
16 -c Don't create file if it doesn't exist.
|
|
17 -s New size
|
|
18 */
|
|
19
|
|
20 #include "toys.h"
|
|
21
|
|
22 DEFINE_GLOBALS(
|
|
23 long size;
|
|
24 )
|
|
25
|
|
26 #define TT this.truncate
|
|
27
|
|
28 static void do_truncate(int fd, char *name)
|
|
29 {
|
|
30 if (fd<0) return;
|
|
31 if (ftruncate(fd, TT.size)) {
|
|
32 perror_msg("failed to set '%s' to '%ld'", name, TT.size);
|
|
33 toys.exitval = EXIT_FAILURE;
|
|
34 }
|
|
35 }
|
|
36
|
|
37 void truncate_main(void)
|
|
38 {
|
|
39 int cr = !(toys.optflags&1);
|
|
40
|
|
41 // Create files with mask rwrwrw.
|
|
42 // Nonexistent files are only an error if we're supposed to create them.
|
|
43 loopfiles_rw(toys.optargs, O_WRONLY|(cr ? O_CREAT : 0), 0666, cr,
|
|
44 do_truncate);
|
|
45 }
|