]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0 |
e246ba5f | 2 | /* |
da23017d NS |
3 | * Copyright (c) 2003-2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
e246ba5f NS |
5 | */ |
6 | ||
6b803e5a CH |
7 | #include "command.h" |
8 | #include "input.h" | |
e246ba5f | 9 | #include "init.h" |
48c46ee3 | 10 | #include "io.h" |
e246ba5f NS |
11 | |
12 | static cmdinfo_t truncate_cmd; | |
13 | ||
14 | static int | |
15 | truncate_f( | |
48c46ee3 NS |
16 | int argc, |
17 | char **argv) | |
e246ba5f | 18 | { |
9e726740 | 19 | off_t offset; |
2c2f6d79 | 20 | size_t blocksize, sectsize; |
e246ba5f | 21 | |
48c46ee3 | 22 | init_cvtnum(&blocksize, §size); |
f72d20ad | 23 | offset = cvtnum(blocksize, sectsize, argv[1]); |
638473d8 | 24 | if (offset < 0) { |
e246ba5f | 25 | printf(_("non-numeric truncate argument -- %s\n"), argv[1]); |
9e1595e6 | 26 | exitcode = 1; |
e246ba5f NS |
27 | return 0; |
28 | } | |
29 | ||
dde67673 | 30 | if (ftruncate(file->fd, offset) < 0) { |
e246ba5f | 31 | perror("ftruncate"); |
9e1595e6 | 32 | exitcode = 1; |
e246ba5f NS |
33 | return 0; |
34 | } | |
35 | return 0; | |
36 | } | |
37 | ||
38 | void | |
39 | truncate_init(void) | |
40 | { | |
ad765595 AM |
41 | truncate_cmd.name = "truncate"; |
42 | truncate_cmd.altname = "t"; | |
e246ba5f NS |
43 | truncate_cmd.cfunc = truncate_f; |
44 | truncate_cmd.argmin = 1; | |
45 | truncate_cmd.argmax = 1; | |
48c46ee3 | 46 | truncate_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK; |
e246ba5f NS |
47 | truncate_cmd.args = _("off"); |
48 | truncate_cmd.oneline = | |
49 | _("truncates the current file at the given offset"); | |
50 | ||
51 | add_command(&truncate_cmd); | |
52 | } |