1 /* $NetBSD: fsutil.c,v 1.5 1997/10/31 09:11:55 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __RCSID("$NetBSD: fsutil.c,v 1.5 1997/10/31 09:11:55 mycroft Exp $"); 39 #endif /* not lint */ 40 41 #include <stdio.h> 42 #include <string.h> 43 #include <stdlib.h> 44 #if __STDC__ 45 #include <stdarg.h> 46 #else 47 #include <varargs.h> 48 #endif 49 #include <errno.h> 50 #include <fstab.h> 51 #include <err.h> 52 53 #include <sys/types.h> 54 #include <sys/stat.h> 55 56 #include "fsutil.h" 57 58 static const char *dev = NULL; 59 static int hot = 0; 60 static int preen = 0; 61 62 extern char *__progname; 63 64 static void vmsg __P((int, const char *, va_list)); 65 66 void 67 setcdevname(cd, pr) 68 const char *cd; 69 int pr; 70 { 71 dev = cd; 72 preen = pr; 73 } 74 75 const char * 76 cdevname() 77 { 78 return dev; 79 } 80 81 int 82 hotroot() 83 { 84 return hot; 85 } 86 87 /*VARARGS*/ 88 void 89 #if __STDC__ 90 errexit(const char *fmt, ...) 91 #else 92 errexit(va_alist) 93 va_dcl 94 #endif 95 { 96 va_list ap; 97 98 #if __STDC__ 99 va_start(ap, fmt); 100 #else 101 const char *fmt; 102 103 va_start(ap); 104 fmt = va_arg(ap, const char *); 105 #endif 106 (void) vfprintf(stderr, fmt, ap); 107 va_end(ap); 108 exit(8); 109 } 110 111 static void 112 vmsg(fatal, fmt, ap) 113 int fatal; 114 const char *fmt; 115 va_list ap; 116 { 117 if (!fatal && preen) 118 (void) printf("%s: ", dev); 119 120 (void) vprintf(fmt, ap); 121 122 if (fatal && preen) 123 (void) printf("\n"); 124 125 if (fatal && preen) { 126 (void) printf( 127 "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n", 128 dev, __progname); 129 exit(8); 130 } 131 } 132 133 /*VARARGS*/ 134 void 135 #if __STDC__ 136 pfatal(const char *fmt, ...) 137 #else 138 pfatal(va_alist) 139 va_dcl 140 #endif 141 { 142 va_list ap; 143 144 #if __STDC__ 145 va_start(ap, fmt); 146 #else 147 const char *fmt; 148 149 va_start(ap); 150 fmt = va_arg(ap, const char *); 151 #endif 152 vmsg(1, fmt, ap); 153 va_end(ap); 154 } 155 156 /*VARARGS*/ 157 void 158 #if __STDC__ 159 pwarn(const char *fmt, ...) 160 #else 161 pwarn(va_alist) 162 va_dcl 163 #endif 164 { 165 va_list ap; 166 #if __STDC__ 167 va_start(ap, fmt); 168 #else 169 const char *fmt; 170 171 va_start(ap); 172 fmt = va_arg(ap, const char *); 173 #endif 174 vmsg(0, fmt, ap); 175 va_end(ap); 176 } 177 178 void 179 perror(s) 180 const char *s; 181 { 182 pfatal("%s (%s)", s, strerror(errno)); 183 } 184 185 void 186 #if __STDC__ 187 panic(const char *fmt, ...) 188 #else 189 panic(va_alist) 190 va_dcl 191 #endif 192 { 193 va_list ap; 194 195 #if __STDC__ 196 va_start(ap, fmt); 197 #else 198 const char *fmt; 199 200 va_start(ap); 201 fmt = va_arg(ap, const char *); 202 #endif 203 vmsg(1, fmt, ap); 204 va_end(ap); 205 exit(8); 206 } 207 208 char * 209 unrawname(name) 210 char *name; 211 { 212 char *dp; 213 struct stat stb; 214 215 if ((dp = strrchr(name, '/')) == 0) 216 return (name); 217 if (stat(name, &stb) < 0) 218 return (name); 219 if (!S_ISCHR(stb.st_mode)) 220 return (name); 221 if (dp[1] != 'r') 222 return (name); 223 (void)strcpy(&dp[1], &dp[2]); 224 return (name); 225 } 226 227 char * 228 rawname(name) 229 char *name; 230 { 231 static char rawbuf[32]; 232 char *dp; 233 234 if ((dp = strrchr(name, '/')) == 0) 235 return (0); 236 *dp = 0; 237 (void)strcpy(rawbuf, name); 238 *dp = '/'; 239 (void)strcat(rawbuf, "/r"); 240 (void)strcat(rawbuf, &dp[1]); 241 return (rawbuf); 242 } 243 244 char * 245 blockcheck(origname) 246 char *origname; 247 { 248 struct stat stslash, stblock, stchar; 249 char *newname, *raw; 250 struct fstab *fsp; 251 int retried = 0; 252 253 hot = 0; 254 if (stat("/", &stslash) < 0) { 255 perror("/"); 256 printf("Can't stat root\n"); 257 return (origname); 258 } 259 newname = origname; 260 retry: 261 if (stat(newname, &stblock) < 0) { 262 perror(newname); 263 printf("Can't stat %s\n", newname); 264 return (origname); 265 } 266 if (S_ISBLK(stblock.st_mode)) { 267 if (stslash.st_dev == stblock.st_rdev) 268 hot++; 269 raw = rawname(newname); 270 if (stat(raw, &stchar) < 0) { 271 perror(raw); 272 printf("Can't stat %s\n", raw); 273 return (origname); 274 } 275 if (S_ISCHR(stchar.st_mode)) { 276 return (raw); 277 } else { 278 printf("%s is not a character device\n", raw); 279 return (origname); 280 } 281 } else if (S_ISCHR(stblock.st_mode) && !retried) { 282 newname = unrawname(newname); 283 retried++; 284 goto retry; 285 } else if ((fsp = getfsfile(newname)) != 0 && !retried) { 286 newname = fsp->fs_spec; 287 retried++; 288 goto retry; 289 } 290 /* 291 * Not a block or character device, just return name and 292 * let the user decide whether to use it. 293 */ 294 return (origname); 295 } 296 297 298 void * 299 emalloc(s) 300 size_t s; 301 { 302 void *p; 303 304 p = malloc(s); 305 if (p == NULL) 306 err(1, "malloc failed"); 307 return (p); 308 } 309 310 311 void * 312 erealloc(p, s) 313 void *p; 314 size_t s; 315 { 316 void *q; 317 318 q = realloc(p, s); 319 if (q == NULL) 320 err(1, "realloc failed"); 321 return (q); 322 } 323 324 325 char * 326 estrdup(s) 327 const char *s; 328 { 329 char *p; 330 331 p = strdup(s); 332 if (p == NULL) 333 err(1, "strdup failed"); 334 return (p); 335 } 336