xref: /dragonfly/usr.bin/truncate/truncate.c (revision 12828dfc)
1 /*
2  * Copyright (c) 2000 Sheldon Hearn <sheldonh@FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.bin/truncate/truncate.c,v 1.6.2.1 2000/08/04 08:05:52 sheldonh Exp $
27  */
28 
29 #include <sys/stat.h>
30 
31 #include <ctype.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 static off_t	parselength(char *, off_t *);
40 static void	usage(void);
41 
42 static int	no_create;
43 static int	do_relative;
44 static int	do_refer;
45 static int	got_size;
46 
47 int
main(int argc,char ** argv)48 main(int argc, char **argv)
49 {
50 	struct stat	sb;
51 	mode_t	omode;
52 	off_t	oflow, rsize, sz, tsize;
53 	int	ch, error, fd, oflags;
54 	char   *fname, *rname;
55 
56 	rsize = tsize = sz = 0;
57 	error = 0;
58 	rname = NULL;
59 	while ((ch = getopt(argc, argv, "cr:s:")) != -1)
60 		switch (ch) {
61 		case 'c':
62 			no_create = 1;
63 			break;
64 		case 'r':
65 			do_refer = 1;
66 			rname = optarg;
67 			break;
68 		case 's':
69 			if (parselength(optarg, &sz) == -1)
70 				errx(EXIT_FAILURE,
71 				    "invalid size argument `%s'", optarg);
72 			if (*optarg == '+' || *optarg == '-')
73 				do_relative = 1;
74 			got_size = 1;
75 			break;
76 		default:
77 			usage();
78 			/* NOTREACHED */
79 		}
80 
81 	argv += optind;
82 	argc -= optind;
83 
84 	/*
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.
88 	 */
89 	if (do_refer + got_size != 1 || argc < 1)
90 		usage();
91 	if (do_refer) {
92 		if (stat(rname, &sb) == -1)
93 			err(EXIT_FAILURE, "%s", rname);
94 		tsize = sb.st_size;
95 	} else if (do_relative)
96 		rsize = sz;
97 	else
98 		tsize = sz;
99 
100 	if (no_create)
101 		oflags = O_WRONLY;
102 	else
103 		oflags = O_WRONLY | O_CREAT;
104 	omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
105 
106 	while ((fname = *argv++) != NULL) {
107 		if ((fd = open(fname, oflags, omode)) == -1) {
108 			if (errno != ENOENT) {
109 				warn("%s", fname);
110 				error++;
111 			}
112 			continue;
113 		}
114 		if (do_relative) {
115 			if (fstat(fd, &sb) == -1) {
116 				warn("%s", fname);
117 				error++;
118 				continue;
119 			}
120 			oflow = sb.st_size + rsize;
121 			if (oflow < (sb.st_size + rsize)) {
122 				errno = EFBIG;
123 				warn("%s", fname);
124 				error++;
125 				continue;
126 			}
127 			tsize = oflow;
128 		}
129 		if (tsize < 0)
130 			tsize = 0;
131 
132 		if (ftruncate(fd, tsize) == -1) {
133 			warn("%s", fname);
134 			error++;
135 			continue;
136 		}
137 
138 		close(fd);
139 	}
140 
141 	return error ? EXIT_FAILURE : EXIT_SUCCESS;
142 }
143 
144 /*
145  * Return the numeric value of a string given in the form [+-][0-9]+[GMK]
146  * or -1 on format error or overflow.
147  */
148 static off_t
parselength(char * ls,off_t * sz)149 parselength(char *ls, off_t *sz)
150 {
151 	off_t	length, oflow;
152 	int	lsign;
153 
154 	length = 0;
155 	lsign = 1;
156 
157 	switch (*ls) {
158 	case '-':
159 		lsign = -1;
160 		/* FALLTHROUGH */
161 	case '+':
162 		ls++;
163 	}
164 
165 #define	ASSIGN_CHK_OFLOW(x, y)	if (x < y) return -1; y = x
166 	/*
167 	 * Calculate the value of the decimal digit string, failing
168 	 * on overflow.
169 	 */
170 	while (isdigit(*ls)) {
171 		oflow = length * 10 + *ls++ - '0';
172 		ASSIGN_CHK_OFLOW(oflow, length);
173 	}
174 
175 	switch (*ls) {
176 	case 'T':
177 		oflow = length * 1024;
178 		ASSIGN_CHK_OFLOW(oflow, length);
179 		/* FALLTHROUGH */
180 	case 'G':
181 		oflow = length * 1024;
182 		ASSIGN_CHK_OFLOW(oflow, length);
183 		/* FALLTHROUGH */
184 	case 'M':
185 		oflow = length * 1024;
186 		ASSIGN_CHK_OFLOW(oflow, length);
187 		/* FALLTHROUGH */
188 	case 'K':
189 		if (ls[1] != '\0')
190 			return -1;
191 		oflow = length * 1024;
192 		ASSIGN_CHK_OFLOW(oflow, length);
193 	case '\0':
194 		break;
195 	default:
196 		return -1;
197 	}
198 
199 	*sz = length * lsign;
200 	return 0;
201 }
202 
203 static void
usage(void)204 usage(void)
205 {
206 	fprintf(stderr, "%s\n%s\n",
207 	    "usage: truncate [-c] -s [+|-]size[K|M|G|T] file ...",
208 	    "       truncate [-c] -r rfile file ...");
209 	exit(EXIT_FAILURE);
210 }
211