2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Sheldon Hearn <sheldonh@FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 static const char rcsid[] =
45 static void usage(void);
48 main(int argc, char **argv)
52 off_t oflow, rsize, sz, tsize, round;
54 int ch, error, fd, oflags;
63 rsize = tsize = sz = 0;
64 no_create = do_relative = do_round = do_refer = got_size = 0;
67 while ((ch = getopt(argc, argv, "cr:s:")) != -1)
77 if (*optarg == '+' || *optarg == '-') {
79 } else if (*optarg == '%' || *optarg == '/') {
82 if (expand_number(do_relative || do_round ?
84 &usz) == -1 || (off_t)usz < 0)
86 "invalid size argument `%s'", optarg);
88 sz = (*optarg == '-' || *optarg == '/') ?
89 -(off_t)usz : (off_t)usz;
101 * Exactly one of do_refer or got_size must be specified. Since
102 * do_relative implies got_size, do_relative and do_refer are
103 * also mutually exclusive. See usage() for allowed invocations.
105 if (do_refer + got_size != 1 || argc < 1)
108 if (stat(rname, &sb) == -1)
109 err(EXIT_FAILURE, "%s", rname);
111 } else if (do_relative || do_round)
119 oflags = O_WRONLY | O_CREAT;
120 omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
122 while ((fname = *argv++) != NULL) {
125 if ((fd = open(fname, oflags, omode)) == -1) {
126 if (errno != ENOENT) {
133 if (fstat(fd, &sb) == -1) {
138 oflow = sb.st_size + rsize;
139 if (oflow < (sb.st_size + rsize)) {
148 if (fstat(fd, &sb) == -1) {
156 if (sb.st_size % sz) {
157 round = sb.st_size / sz;
158 if (round != sz && rsize < 0)
162 tsize = (round < 0 ? 0 : round) * sz;
169 if (ftruncate(fd, tsize) == -1) {
178 return error ? EXIT_FAILURE : EXIT_SUCCESS;
184 fprintf(stderr, "%s\n%s\n",
185 "usage: truncate [-c] -s [+|-|%|/]size[K|k|M|m|G|g|T|t] file ...",
186 " truncate [-c] -r rfile file ...");