2 * Copyright (c) 2000 Sheldon Hearn <sheldonh@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static off_t
parselength(char *, off_t
*);
38 static void usage(void);
41 static int do_relative
;
46 main(int argc
, char **argv
)
50 off_t oflow
, rsize
, sz
, tsize
;
51 int ch
, error
, fd
, oflags
;
57 while ((ch
= getopt(argc
, argv
, "cr:s:")) != -1)
67 if (parselength(optarg
, &sz
) == -1) {
69 "invalid size argument `%s'", optarg
);
72 if (*optarg
== '+' || *optarg
== '-')
85 * Exactly one of do_refer or got_size must be specified. Since
86 * do_relative implies got_size, do_relative and do_refer are
87 * also mutually exclusive. See usage() for allowed invocations.
89 if (do_refer
+ got_size
!= 1 || argc
< 1)
92 if (stat(rname
, &sb
) == -1) {
93 fprintf(stderr
, "%s", rname
);
97 } else if (do_relative
)
105 oflags
= O_WRONLY
| O_CREAT
;
106 omode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
108 while ((fname
= *argv
++) != NULL
) {
109 if ((fd
= open(fname
, oflags
, omode
)) == -1) {
110 if (errno
!= ENOENT
) {
117 if (fstat(fd
, &sb
) == -1) {
122 oflow
= sb
.st_size
+ rsize
;
123 if (oflow
< (sb
.st_size
+ rsize
)) {
134 if (ftruncate(fd
, tsize
) == -1) {
143 return error
? EXIT_FAILURE
: EXIT_SUCCESS
;
147 * Return the numeric value of a string given in the form [+-][0-9]+[GMK]
148 * or -1 on format error or overflow.
151 parselength(char *ls
, off_t
*sz
)
166 #define ASSIGN_CHK_OFLOW(x, y) if (x < y) return -1; y = x
168 * Calculate the value of the decimal digit string, failing
171 while (isdigit(*ls
)) {
172 oflow
= length
* 10 + *ls
++ - '0';
173 ASSIGN_CHK_OFLOW(oflow
, length
);
179 oflow
= length
* 1024;
180 ASSIGN_CHK_OFLOW(oflow
, length
);
183 oflow
= length
* 1024;
184 ASSIGN_CHK_OFLOW(oflow
, length
);
189 oflow
= length
* 1024;
190 ASSIGN_CHK_OFLOW(oflow
, length
);
197 *sz
= length
* lsign
;
204 fprintf(stderr
, "%s\n%s\n",
205 "usage: truncate [-c] -s [+|-]size[K|k|M|m|G|g] file ...",
206 " truncate [-c] -r rfile file ...");