annotate toys/other/truncate.c @ 944:b4faf2ae39e8

This inlines CRC64, and nothing more. The functions involved were called only once.
author Isaac Dunham <idunham@lavabit.com>
date Sat, 06 Jul 2013 11:26:15 -0500
parents 6cc69be43c42
children e5b52720f539
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.
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
38 loopfiles_rw(toys.optargs, O_WRONLY|(cr ? O_CREAT : 0), 0666, cr,
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 }