1 /* $OpenBSD: devopen.c,v 1.7 1998/09/04 16:57:16 millert Exp $ */ 2 /* $NetBSD: devopen.c,v 1.1 1995/11/23 02:39:37 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Ralph Campbell. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)devopen.c 8.1 (Berkeley) 6/10/93 40 */ 41 42 #include <lib/libsa/stand.h> 43 44 /* 45 * Decode the string 'fname', open the device and return the remaining 46 * file name if any. 47 */ 48 devopen(f, fname, file) 49 struct open_file *f; 50 const char *fname; 51 char **file; /* out */ 52 { 53 register char *cp; 54 register char *ncp; 55 register struct devsw *dp; 56 register int c, i; 57 int ctlr = 0, unit = 0, part = 0; 58 char namebuf[20]; 59 int rc; 60 61 cp = (char *)fname; 62 ncp = namebuf; 63 64 /* look for a string like 'disk(0,0,0)bsd' */ 65 if (strchr(cp, '(')) { 66 while ((c = *cp) != '\0') { 67 if (c == '(') { 68 cp++; 69 break; 70 } 71 if (ncp < namebuf + sizeof(namebuf) - 1) 72 *ncp++ = c; 73 cp++; 74 } 75 76 /* get controller number */ 77 if ((c = *cp) >= '0' && c <= '9') { 78 ctlr = c - '0'; 79 c = *++cp; 80 } 81 82 if (c == ',') { 83 /* get SCSI device number */ 84 if ((c = *++cp) >= '0' && c <= '9') { 85 unit = c - '0'; 86 c = *++cp; 87 } 88 89 if (c == ',') { 90 /* get partition number */ 91 if ((c = *++cp) >= '0' && c <= '9') { 92 part = c - '0'; 93 c = *++cp; 94 } 95 } 96 } 97 if (c != ')') 98 goto defdev; 99 cp++; 100 *ncp = '\0'; 101 } else { 102 defdev: 103 /* No valid device specification */ 104 cp = (char *)fname; 105 ncp = namebuf; 106 dp = devsw; 107 ctlr = unit = part = 0; 108 goto fnd; 109 } 110 111 for (dp = devsw, i = 0; i < ndevs; dp++, i++) 112 if (dp->dv_name && strcmp(namebuf, dp->dv_name) == 0) 113 goto fnd; 114 printf("Unknown device '%s'\nKnown devices are:", namebuf); 115 for (dp = devsw, i = 0; i < ndevs; dp++, i++) 116 if (dp->dv_name) 117 printf(" %s", dp->dv_name); 118 printf("\n"); 119 return (ENXIO); 120 121 fnd: 122 rc = (dp->dv_open)(f, ctlr, unit, part); 123 if (rc) 124 return (rc); 125 126 f->f_dev = dp; 127 if (file && *cp != '\0') 128 *file = cp; 129 return (0); 130 } 131