xref: /freebsd/usr.bin/truncate/truncate.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
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, tsize;
58 	int64_t sz;
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 			if (expand_number(optarg, &sz) == -1)
77 				errx(EXIT_FAILURE,
78 				    "invalid size argument `%s'", optarg);
79 			if (*optarg == '+' || *optarg == '-')
80 				do_relative = 1;
81 			got_size = 1;
82 			break;
83 		default:
84 			usage();
85 			/* NOTREACHED */
86 		}
87 
88 	argv += optind;
89 	argc -= optind;
90 
91 	/*
92 	 * Exactly one of do_refer or got_size must be specified.  Since
93 	 * do_relative implies got_size, do_relative and do_refer are
94 	 * also mutually exclusive.  See usage() for allowed invocations.
95 	 */
96 	if (do_refer + got_size != 1 || argc < 1)
97 		usage();
98 	if (do_refer) {
99 		if (stat(rname, &sb) == -1)
100 			err(EXIT_FAILURE, "%s", rname);
101 		tsize = sb.st_size;
102 	} else if (do_relative)
103 		rsize = sz;
104 	else
105 		tsize = sz;
106 
107 	if (no_create)
108 		oflags = O_WRONLY;
109 	else
110 		oflags = O_WRONLY | O_CREAT;
111 	omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
112 
113 	while ((fname = *argv++) != NULL) {
114 		if (fd != -1)
115 			close(fd);
116 		if ((fd = open(fname, oflags, omode)) == -1) {
117 			if (errno != ENOENT) {
118 				warn("%s", fname);
119 				error++;
120 			}
121 			continue;
122 		}
123 		if (do_relative) {
124 			if (fstat(fd, &sb) == -1) {
125 				warn("%s", fname);
126 				error++;
127 				continue;
128 			}
129 			oflow = sb.st_size + rsize;
130 			if (oflow < (sb.st_size + rsize)) {
131 				errno = EFBIG;
132 				warn("%s", fname);
133 				error++;
134 				continue;
135 			}
136 			tsize = oflow;
137 		}
138 		if (tsize < 0)
139 			tsize = 0;
140 
141 		if (ftruncate(fd, tsize) == -1) {
142 			warn("%s", fname);
143 			error++;
144 			continue;
145 		}
146 	}
147 	if (fd != -1)
148 		close(fd);
149 
150 	return error ? EXIT_FAILURE : EXIT_SUCCESS;
151 }
152 
153 static void
154 usage(void)
155 {
156 	fprintf(stderr, "%s\n%s\n",
157 	    "usage: truncate [-c] -s [+|-]size[K|k|M|m|G|g|T|t] file ...",
158 	    "       truncate [-c] -r rfile file ...");
159 	exit(EXIT_FAILURE);
160 }
161