annotate toys/truncate.c @ 562:4d802d438983

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