xref: /freebsd/usr.bin/truncate/truncate.c (revision dc318a4ffabcbfa23bb56a33403aad36e6de30af)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Sheldon Hearn <sheldonh@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  */
29 
30 static const char rcsid[] =
31     "$FreeBSD$";
32 
33 #include <sys/stat.h>
34 
35 #include <ctype.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 
43 #include <libutil.h>
44 
45 static void	usage(void);
46 
47 int
48 main(int argc, char **argv)
49 {
50 	struct stat sb;
51 	mode_t omode;
52 	off_t oflow, rsize, sz, tsize, round;
53 	uint64_t usz;
54 	int ch, error, fd, oflags;
55 	int no_create;
56 	int do_relative;
57 	int do_round;
58 	int do_refer;
59 	int got_size;
60 	char *fname, *rname;
61 
62 	fd = -1;
63 	rsize = tsize = sz = 0;
64 	no_create = do_relative = do_round = do_refer = got_size = 0;
65 	error = 0;
66 	rname = NULL;
67 	while ((ch = getopt(argc, argv, "cr:s:")) != -1)
68 		switch (ch) {
69 		case 'c':
70 			no_create = 1;
71 			break;
72 		case 'r':
73 			do_refer = 1;
74 			rname = optarg;
75 			break;
76 		case 's':
77 			if (*optarg == '+' || *optarg == '-') {
78 				do_relative = 1;
79 			} else if (*optarg == '%' || *optarg == '/') {
80 				do_round = 1;
81 			}
82 			if (expand_number(do_relative || do_round ?
83 			    optarg + 1 : optarg,
84 			    &usz) == -1 || (off_t)usz < 0)
85 				errx(EXIT_FAILURE,
86 				    "invalid size argument `%s'", optarg);
87 
88 			sz = (*optarg == '-' || *optarg == '/') ?
89 				-(off_t)usz : (off_t)usz;
90 			got_size = 1;
91 			break;
92 		default:
93 			usage();
94 			/* NOTREACHED */
95 		}
96 
97 	argv += optind;
98 	argc -= optind;
99 
100 	/*
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.
104 	 */
105 	if (do_refer + got_size != 1 || argc < 1)
106 		usage();
107 	if (do_refer) {
108 		if (stat(rname, &sb) == -1)
109 			err(EXIT_FAILURE, "%s", rname);
110 		tsize = sb.st_size;
111 	} else if (do_relative || do_round)
112 		rsize = sz;
113 	else
114 		tsize = sz;
115 
116 	if (no_create)
117 		oflags = O_WRONLY;
118 	else
119 		oflags = O_WRONLY | O_CREAT;
120 	omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
121 
122 	while ((fname = *argv++) != NULL) {
123 		if (fd != -1)
124 			close(fd);
125 		if ((fd = open(fname, oflags, omode)) == -1) {
126 			if (errno != ENOENT) {
127 				warn("%s", fname);
128 				error++;
129 			}
130 			continue;
131 		}
132 		if (do_relative) {
133 			if (fstat(fd, &sb) == -1) {
134 				warn("%s", fname);
135 				error++;
136 				continue;
137 			}
138 			oflow = sb.st_size + rsize;
139 			if (oflow < (sb.st_size + rsize)) {
140 				errno = EFBIG;
141 				warn("%s", fname);
142 				error++;
143 				continue;
144 			}
145 			tsize = oflow;
146 		}
147 		if (do_round) {
148 			if (fstat(fd, &sb) == -1) {
149 				warn("%s", fname);
150 				error++;
151 				continue;
152 			}
153 			sz = rsize;
154 			if (sz < 0)
155 				sz = -sz;
156 			if (sb.st_size % sz) {
157 				round = sb.st_size / sz;
158 				if (round != sz && rsize < 0)
159 					round--;
160 				else if (rsize > 0)
161 					round++;
162 				tsize = (round < 0 ? 0 : round) * sz;
163 			} else
164 				tsize = sb.st_size;
165 		}
166 		if (tsize < 0)
167 			tsize = 0;
168 
169 		if (ftruncate(fd, tsize) == -1) {
170 			warn("%s", fname);
171 			error++;
172 			continue;
173 		}
174 	}
175 	if (fd != -1)
176 		close(fd);
177 
178 	return error ? EXIT_FAILURE : EXIT_SUCCESS;
179 }
180 
181 static void
182 usage(void)
183 {
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 ...");
187 	exit(EXIT_FAILURE);
188 }
189