* Remove the SINGLEUSE feature for telldir(), it does not conform to the
[dragonfly.git] / lib / libc / gen / disklabel.c
1 /*
2  * Copyright (c) 1983, 1987, 1993
3  *      The Regents of the University of California.  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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)disklabel.c      8.2 (Berkeley) 5/3/95
34  * $FreeBSD: src/lib/libc/gen/disklabel.c,v 1.9.2.1 2001/03/05 08:40:47 obrien Exp $
35  * $DragonFly: src/lib/libc/gen/disklabel.c,v 1.13 2007/06/18 05:13:34 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #define DKTYPENAMES
40 #include <sys/disklabel32.h>
41 #include <sys/dtype.h>
42 #include <vfs/ufs/dinode.h>
43 #include <vfs/ufs/fs.h>
44
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <ctype.h>
51 #include <disktab.h>
52
53 static int      gettype (const char *, const char **);
54
55 struct disklabel32 *
56 getdiskbyname(const char *name)
57 {
58         static struct disklabel32 disk;
59         struct  disklabel32 *dp = &disk;
60         struct partition32 *pp;
61         char    *buf;
62         char    *db_array[2] = { _PATH_DISKTAB, 0 };
63         char    *cp, *cq;       /* can't be register */
64         char    p, max, psize[3], pbsize[3],
65                 pfsize[3], poffset[3], ptype[3];
66         u_int32_t *dx;
67
68         if (cgetent(&buf, db_array, (char *)name) < 0)
69                 return NULL;
70
71         bzero((char *)&disk, sizeof(disk));
72         /*
73          * typename
74          */
75         cq = dp->d_typename;
76         cp = buf;
77         while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 &&
78             (*cq = *cp) && *cq != '|' && *cq != ':')
79                 cq++, cp++;
80         *cq = '\0';
81
82 #define getnumdflt(field, dname, dflt) \
83         { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; }
84
85         getnumdflt(dp->d_secsize, "se", DEV_BSIZE);
86         getnumdflt(dp->d_ntracks, "nt", 0);
87         getnumdflt(dp->d_nsectors, "ns", 0);
88         getnumdflt(dp->d_ncylinders, "nc", 0);
89
90         if (cgetstr(buf, "dt", &cq) > 0)
91                 dp->d_type = gettype(cq, dktypenames);
92         else
93                 getnumdflt(dp->d_type, "dt", 0);
94         getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks);
95         getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders);
96         getnumdflt(dp->d_rpm, "rm", 3600);
97         getnumdflt(dp->d_interleave, "il", 1);
98         getnumdflt(dp->d_trackskew, "sk", 0);
99         getnumdflt(dp->d_cylskew, "cs", 0);
100         getnumdflt(dp->d_headswitch, "hs", 0);
101         getnumdflt(dp->d_trkseek, "ts", 0);
102         getnumdflt(dp->d_bbsize, "bs", BBSIZE);
103         getnumdflt(dp->d_sbsize, "sb", SBSIZE);
104         strcpy(psize, "px");
105         strcpy(pbsize, "bx");
106         strcpy(pfsize, "fx");
107         strcpy(poffset, "ox");
108         strcpy(ptype, "tx");
109         max = 'a' - 1;
110         pp = &dp->d_partitions[0];
111         for (p = 'a'; p < 'a' + MAXPARTITIONS32; p++, pp++) {
112                 long l;
113                 psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p;
114                 if (cgetnum(buf, psize, &l) == -1)
115                         pp->p_size = 0;
116                 else {
117                         pp->p_size = l;
118                         cgetnum(buf, poffset, &l);
119                         pp->p_offset = l;
120                         getnumdflt(pp->p_fsize, pfsize, 0);
121                         if (pp->p_fsize) {
122                                 long bsize;
123
124                                 if (cgetnum(buf, pbsize, &bsize) == 0)
125                                         pp->p_frag = bsize / pp->p_fsize;
126                                 else
127                                         pp->p_frag = 8;
128                         }
129                         getnumdflt(pp->p_fstype, ptype, 0);
130                         if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0)
131                                 pp->p_fstype = gettype(cq, fstypenames);
132                         max = p;
133                 }
134         }
135         dp->d_npartitions = max + 1 - 'a';
136         strcpy(psize, "dx");
137         dx = dp->d_drivedata;
138         for (p = '0'; p < '0' + NDDATA32; p++, dx++) {
139                 psize[1] = p;
140                 getnumdflt(*dx, psize, 0);
141         }
142         dp->d_magic = DISKMAGIC32;
143         dp->d_magic2 = DISKMAGIC32;
144         free(buf);
145         return (dp);
146 }
147
148 static int
149 gettype(const char *t, const char **names)
150 {
151         const char **nm;
152
153         for (nm = names; *nm; nm++)
154                 if (strcasecmp(t, *nm) == 0)
155                         return (nm - names);
156         if (isdigit((unsigned char)*t))
157                 return (atoi(t));
158         return (0);
159 }