truncate.c revision 188887
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 */
27
28#ifndef lint
29static const char rcsid[] =
30    "$FreeBSD: head/usr.bin/truncate/truncate.c 188887 2009-02-21 03:42:31Z delphij $";
31#endif
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
43static int	parselength(char *, off_t *);
44static void	usage(void);
45
46static int	no_create;
47static int	do_relative;
48static int	do_refer;
49static int	got_size;
50
51int
52main(int argc, char **argv)
53{
54	struct stat	sb;
55	mode_t	omode;
56	off_t	oflow, rsize, sz, tsize;
57	int	ch, error, fd, oflags;
58	char   *fname, *rname;
59
60	fd = -1;
61	rsize = tsize = sz = 0;
62	error = 0;
63	rname = NULL;
64	while ((ch = getopt(argc, argv, "cr:s:")) != -1)
65		switch (ch) {
66		case 'c':
67			no_create = 1;
68			break;
69		case 'r':
70			do_refer = 1;
71			rname = optarg;
72			break;
73		case 's':
74			if (parselength(optarg, &sz) == -1)
75				errx(EXIT_FAILURE,
76				    "invalid size argument `%s'", optarg);
77			if (*optarg == '+' || *optarg == '-')
78				do_relative = 1;
79			got_size = 1;
80			break;
81		default:
82			usage();
83			/* NOTREACHED */
84		}
85
86	argv += optind;
87	argc -= optind;
88
89	/*
90	 * Exactly one of do_refer or got_size must be specified.  Since
91	 * do_relative implies got_size, do_relative and do_refer are
92	 * also mutually exclusive.  See usage() for allowed invocations.
93	 */
94	if (do_refer + got_size != 1 || argc < 1)
95		usage();
96	if (do_refer) {
97		if (stat(rname, &sb) == -1)
98			err(EXIT_FAILURE, "%s", rname);
99		tsize = sb.st_size;
100	} else if (do_relative)
101		rsize = sz;
102	else
103		tsize = sz;
104
105	if (no_create)
106		oflags = O_WRONLY;
107	else
108		oflags = O_WRONLY | O_CREAT;
109	omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
110
111	while ((fname = *argv++) != NULL) {
112		if (fd != -1)
113			close(fd);
114		if ((fd = open(fname, oflags, omode)) == -1) {
115			if (errno != ENOENT) {
116				warn("%s", fname);
117				error++;
118			}
119			continue;
120		}
121		if (do_relative) {
122			if (fstat(fd, &sb) == -1) {
123				warn("%s", fname);
124				error++;
125				continue;
126			}
127			oflow = sb.st_size + rsize;
128			if (oflow < (sb.st_size + rsize)) {
129				errno = EFBIG;
130				warn("%s", fname);
131				error++;
132				continue;
133			}
134			tsize = oflow;
135		}
136		if (tsize < 0)
137			tsize = 0;
138
139		if (ftruncate(fd, tsize) == -1) {
140			warn("%s", fname);
141			error++;
142			continue;
143		}
144	}
145	if (fd != -1)
146		close(fd);
147
148	return error ? EXIT_FAILURE : EXIT_SUCCESS;
149}
150
151/*
152 * Return the numeric value of a string given in the form [+-][0-9]+[GMKT]
153 * or -1 on format error or overflow.
154 */
155static int
156parselength(char *ls, off_t *sz)
157{
158	off_t	length, oflow;
159	int	lsign;
160
161	length = 0;
162	lsign = 1;
163
164	switch (*ls) {
165	case '-':
166		lsign = -1;
167	case '+':
168		ls++;
169	}
170
171#define	ASSIGN_CHK_OFLOW(x, y)	if (x < y) return -1; y = x
172	/*
173	 * Calculate the value of the decimal digit string, failing
174	 * on overflow.
175	 */
176	while (isdigit(*ls)) {
177		oflow = length * 10 + *ls++ - '0';
178		ASSIGN_CHK_OFLOW(oflow, length);
179	}
180
181	switch (*ls) {
182	case 'T':
183	case 't':
184		oflow = length * 1024;
185		ASSIGN_CHK_OFLOW(oflow, length);
186	case 'G':
187	case 'g':
188		oflow = length * 1024;
189		ASSIGN_CHK_OFLOW(oflow, length);
190	case 'M':
191	case 'm':
192		oflow = length * 1024;
193		ASSIGN_CHK_OFLOW(oflow, length);
194	case 'K':
195	case 'k':
196		if (ls[1] != '\0')
197			return -1;
198		oflow = length * 1024;
199		ASSIGN_CHK_OFLOW(oflow, length);
200	case '\0':
201		break;
202	default:
203		return -1;
204	}
205
206	*sz = length * lsign;
207	return 0;
208}
209
210static void
211usage(void)
212{
213	fprintf(stderr, "%s\n%s\n",
214	    "usage: truncate [-c] -s [+|-]size[K|k|M|m|G|g|T|t] file ...",
215	    "       truncate [-c] -r rfile file ...");
216	exit(EXIT_FAILURE);
217}
218