xref: /freebsd/usr.bin/truncate/truncate.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
1 /*
2  * Copyright (c) 2000 FreeBSD, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 
31 #include <ctype.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 
40 static off_t	parselength __P((char *, off_t *));
41 static void	usage __P((void));
42 
43 static int	no_create;
44 static int	do_relative;
45 static int	do_refer;
46 static int	got_size;
47 
48 int
49 main(int argc, char **argv)
50 {
51 	struct stat	sb;
52 	mode_t	omode;
53 	off_t	oflow, rsize, sz, tsize;
54 	int	ch, error, fd, oflags;
55 	char   *fname, *rname;
56 
57 	error = 0;
58 	while ((ch = getopt(argc, argv, "cr:s:")) != -1)
59 		switch (ch) {
60 		case 'c':
61 			no_create++;
62 			break;
63 		case 'r':
64 			do_refer++;
65 			rname = optarg;
66 			break;
67 		case 's':
68 			if (parselength(optarg, &sz) == -1)
69 				errx(EXIT_FAILURE,
70 				    "invalid size argument `%s'", optarg);
71 			if (*optarg == '+' || *optarg == '-')
72 				do_relative++;
73 			got_size++;
74 			break;
75 		default:
76 			usage();
77 			/* NOTREACHED */
78 		}
79 
80 	argv += optind;
81 	argc -= optind;
82 
83 	/*
84 	 * Exactly one of do_refer or got_size must be specified.  Since
85 	 * do_relative implies got_size, do_relative and do_refer are
86 	 * also mutually exclusive.  See usage() for allowed invocations.
87 	 */
88 	if (!(do_refer || got_size) || (do_refer && got_size) || argc < 1)
89 		usage();
90 	if (do_refer) {
91 		if (stat(rname, &sb) == -1)
92 			err(EXIT_FAILURE, "%s", rname);
93 		tsize = sb.st_size;
94 	} else if (got_size) {
95 		if (do_relative)
96 			rsize = sz;
97 		else
98 			tsize = sz;
99 	}
100 
101 	if (no_create)
102 		oflags = O_WRONLY;
103 	else
104 		oflags = O_WRONLY | O_CREAT;
105 	omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
106 
107 	while ((fname = *argv++) != NULL) {
108 		if ((fd = open(fname, oflags, omode)) == -1) {
109 			if (errno != ENOENT) {
110 				warn("%s", fname);
111 				error++;
112 			}
113 			continue;
114 		}
115 		if (do_relative) {
116 			if (fstat(fd, &sb) == -1) {
117 				warn("%s", fname);
118 				error++;
119 				continue;
120 			}
121 			oflow = sb.st_size + rsize;
122 			if (oflow < (sb.st_size + rsize)) {
123 				errno = EFBIG;
124 				warn("%s", fname);
125 				error++;
126 				continue;
127 			}
128 			tsize = oflow;
129 		}
130 		if (tsize < 0)
131 			tsize = 0;
132 
133 		if (ftruncate(fd, tsize) == -1) {
134 			warn("%s", fname);
135 			error++;
136 			continue;
137 		}
138 	}
139 
140 	return error ? EXIT_FAILURE : EXIT_SUCCESS;
141 }
142 
143 /*
144  * Return the numeric value of a string given in the form [+-][0-9]+[GMK]
145  * or -1 on format error or overflow.
146  */
147 static off_t
148 parselength(char *ls, off_t *sz)
149 {
150 	off_t	length, oflow;
151 	int	lsign;
152 
153 	length = 0;
154 	lsign = 1;
155 
156 	switch (*ls) {
157 	case '-':
158 		lsign = -1;
159 	case '+':
160 		ls++;
161 	}
162 
163 #define	ASSIGN_CHK_OFLOW(x, y)	if (x < y) return -1; y = x
164 	/*
165 	 * Calculate the value of the decimal digit string, failing
166 	 * on overflow.
167 	 */
168 	while (isdigit(*ls)) {
169 		oflow = length * 10 + *ls++ - '0';
170 		ASSIGN_CHK_OFLOW(oflow, length);
171 	}
172 
173 	switch (*ls) {
174 	case 'G':
175 		oflow = length * 1024;
176 		ASSIGN_CHK_OFLOW(oflow, length);
177 	case 'M':
178 		oflow = length * 1024;
179 		ASSIGN_CHK_OFLOW(oflow, length);
180 	case 'K':
181 		if (ls[1] != '\0')
182 			return -1;
183 		oflow = length * 1024;
184 		ASSIGN_CHK_OFLOW(oflow, length);
185 	case '\0':
186 		break;
187 	default:
188 		return -1;
189 	}
190 
191 	*sz = length * lsign;
192 	return 0;
193 }
194 
195 static void
196 usage(void)
197 {
198 	fprintf(stderr, "%s\n%s\n",
199 	    "usage: truncate [-c] -s [+|-]size[K|M|G] file ...",
200 	    "       truncate [-c] -r rfile file ...");
201 	exit(EXIT_FAILURE);
202 }
203