annotate toys/other/truncate.c @ 1523:e5b52720f539 draft

Use O_CLOEXEC instead of O_RDONLY to signal loopfiles_rw() to close filehandles.
author Rob Landley <rob@landley.net>
date Tue, 14 Oct 2014 14:16:34 -0500
parents 6cc69be43c42
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* truncate.c - set file length, extending sparsely if necessary
396
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2011 Rob Landley <rob@landley.net>
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
4
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 USE_TRUNCATE(NEWTOY(truncate, "<1s#|c", TOYFLAG_BIN))
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
6
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 config TRUNCATE
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
8 bool "truncate"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
9 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
10 help
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
11 usage: truncate [-c] -s file...
396
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
12
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
13 Set length of file(s), extending sparsely if necessary.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
14
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
15 -c Don't create file if it doesn't exist.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
16 -s New size
396
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 */
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
19 #define FOR_truncate
396
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 #include "toys.h"
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
21
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
22 GLOBALS(
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
23 long size;
396
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 )
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
25
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 static void do_truncate(int fd, char *name)
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
28 if (fd<0) return;
780
6cc69be43c42 Have error_msg() and friends set TT.exitval to 1 if it's still 0, clean out other places that were setting it that no longer need to.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
29 if (ftruncate(fd, TT.size)) perror_msg("'%s' to '%ld'", name, TT.size);
396
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 }
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 void truncate_main(void)
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
34 int cr = !(toys.optflags&1);
396
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
35
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
36 // Create files with mask rwrwrw.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
37 // Nonexistent files are only an error if we're supposed to create them.
1523
e5b52720f539 Use O_CLOEXEC instead of O_RDONLY to signal loopfiles_rw() to close filehandles.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
38 loopfiles_rw(toys.optargs, O_WRONLY|O_CLOEXEC|(cr ? O_CREAT : 0), 0666, cr,
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
39 do_truncate);
396
7ca3bef07f0d Implement truncate.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 }