xref: /freebsd/usr.bin/truncate/truncate.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
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
29 static const char rcsid[] =
30     "$FreeBSD$";
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 
43 #include <libutil.h>
44 
45 static void	usage(void);
46 
47 static int	no_create;
48 static int	do_relative;
49 static int	do_refer;
50 static int	got_size;
51 
52 int
53 main(int argc, char **argv)
54 {
55 	struct stat	sb;
56 	mode_t	omode;
57 	off_t	oflow, rsize, sz, tsize;
58 	uint64_t usz;
59 	int	ch, error, fd, oflags;
60 	char   *fname, *rname;
61 
62 	fd = -1;
63 	rsize = tsize = sz = 0;
64 	error = 0;
65 	rname = NULL;
66 	while ((ch = getopt(argc, argv, "cr:s:")) != -1)
67 		switch (ch) {
68 		case 'c':
69 			no_create = 1;
70 			break;
71 		case 'r':
72 			do_refer = 1;
73 			rname = optarg;
74 			break;
75 		case 's':
76 			do_relative = *optarg == '+' || *optarg == '-';
77 			if (expand_number(do_relative ? optarg + 1 : optarg,
78 			    &usz) == -1 || (off_t)usz < 0)
79 				errx(EXIT_FAILURE,
80 				    "invalid size argument `%s'", optarg);
81 
82 			sz = (*optarg == '-') ? -(off_t)usz : (off_t)usz;
83 			got_size = 1;
84 			break;
85 		default:
86 			usage();
87 			/* NOTREACHED */
88 		}
89 
90 	argv += optind;
91 	argc -= optind;
92 
93 	/*
94 	 * Exactly one of do_refer or got_size must be specified.  Since
95 	 * do_relative implies got_size, do_relative and do_refer are
96 	 * also mutually exclusive.  See usage() for allowed invocations.
97 	 */
98 	if (do_refer + got_size != 1 || argc < 1)
99 		usage();
100 	if (do_refer) {
101 		if (stat(rname, &sb) == -1)
102 			err(EXIT_FAILURE, "%s", rname);
103 		tsize = sb.st_size;
104 	} else if (do_relative)
105 		rsize = sz;
106 	else
107 		tsize = sz;
108 
109 	if (no_create)
110 		oflags = O_WRONLY;
111 	else
112 		oflags = O_WRONLY | O_CREAT;
113 	omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
114 
115 	while ((fname = *argv++) != NULL) {
116 		if (fd != -1)
117 			close(fd);
118 		if ((fd = open(fname, oflags, omode)) == -1) {
119 			if (errno != ENOENT) {
120 				warn("%s", fname);
121 				error++;
122 			}
123 			continue;
124 		}
125 		if (do_relative) {
126 			if (fstat(fd, &sb) == -1) {
127 				warn("%s", fname);
128 				error++;
129 				continue;
130 			}
131 			oflow = sb.st_size + rsize;
132 			if (oflow < (sb.st_size + rsize)) {
133 				errno = EFBIG;
134 				warn("%s", fname);
135 				error++;
136 				continue;
137 			}
138 			tsize = oflow;
139 		}
140 		if (tsize < 0)
141 			tsize = 0;
142 
143 		if (ftruncate(fd, tsize) == -1) {
144 			warn("%s", fname);
145 			error++;
146 			continue;
147 		}
148 	}
149 	if (fd != -1)
150 		close(fd);
151 
152 	return error ? EXIT_FAILURE : EXIT_SUCCESS;
153 }
154 
155 static void
156 usage(void)
157 {
158 	fprintf(stderr, "%s\n%s\n",
159 	    "usage: truncate [-c] -s [+|-]size[K|k|M|m|G|g|T|t] file ...",
160 	    "       truncate [-c] -r rfile file ...");
161 	exit(EXIT_FAILURE);
162 }
163