Mercurial > hg > toybox
comparison toys/truncate.c @ 396:7ca3bef07f0d
Implement truncate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 11 Dec 2011 03:58:43 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
395:32c7b6af5b29 | 396:7ca3bef07f0d |
---|---|
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 * Not in SUSv4 | |
8 | |
9 USE_TRUNCATE(NEWTOY(truncate, "<1s#|c", TOYFLAG_BIN)) | |
10 | |
11 config TRUNCATE | |
12 bool "truncate" | |
13 default y | |
14 help | |
15 usage: truncate [-c] -s file... | |
16 Set length of file(s), extending sparsely if necessary. | |
17 | |
18 -c Don't create file if it doesn't exist. | |
19 -s New size | |
20 */ | |
21 | |
22 #include "toys.h" | |
23 | |
24 DEFINE_GLOBALS( | |
25 long size; | |
26 ) | |
27 | |
28 #define TT this.truncate | |
29 | |
30 static void do_truncate(int fd, char *name) | |
31 { | |
32 if (fd<0) return; | |
33 if (ftruncate(fd, TT.size)) { | |
34 perror_msg("failed to set '%s' to '%ld'", name, TT.size); | |
35 toys.exitval = EXIT_FAILURE; | |
36 } | |
37 } | |
38 | |
39 void truncate_main(void) | |
40 { | |
41 int cr = !(toys.optflags&1); | |
42 | |
43 // Create files with mask rwrwrw. | |
44 // Nonexistent files are only an error if we're supposed to create them. | |
45 loopfiles_rw(toys.optargs, O_WRONLY|(cr ? O_CREAT : 0), 0666, cr, | |
46 do_truncate); | |
47 } |