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