]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/truncate.c
xfsprogs: Release v6.7.0
[thirdparty/xfsprogs-dev.git] / io / truncate.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "command.h"
8 #include "input.h"
9 #include "init.h"
10 #include "io.h"
11
12 static cmdinfo_t truncate_cmd;
13
14 static int
15 truncate_f(
16 int argc,
17 char **argv)
18 {
19 off64_t offset;
20 size_t blocksize, sectsize;
21
22 init_cvtnum(&blocksize, &sectsize);
23 offset = cvtnum(blocksize, sectsize, argv[1]);
24 if (offset < 0) {
25 printf(_("non-numeric truncate argument -- %s\n"), argv[1]);
26 return 0;
27 }
28
29 if (ftruncate(file->fd, offset) < 0) {
30 perror("ftruncate");
31 return 0;
32 }
33 return 0;
34 }
35
36 void
37 truncate_init(void)
38 {
39 truncate_cmd.name = "truncate";
40 truncate_cmd.altname = "t";
41 truncate_cmd.cfunc = truncate_f;
42 truncate_cmd.argmin = 1;
43 truncate_cmd.argmax = 1;
44 truncate_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
45 truncate_cmd.args = _("off");
46 truncate_cmd.oneline =
47 _("truncates the current file at the given offset");
48
49 add_command(&truncate_cmd);
50 }