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