xref: /netbsd/sbin/disklabel/printlabel.c (revision c4a72b64)
1 /*	$NetBSD: printlabel.c,v 1.5 2002/09/28 00:47:26 dbj Exp $	*/
2 
3 /*
4  * Copyright (c) 1987, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Symmetric Computer Systems.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: printlabel.c,v 1.5 2002/09/28 00:47:26 dbj Exp $");
42 #endif	/* not lint */
43 
44 #include <sys/param.h>
45 
46 #define DKTYPENAMES
47 #define FSTYPENAMES
48 #include <sys/disklabel.h>
49 
50 #include <stdio.h>
51 
52 #include "extern.h"
53 
54 
55 void
56 showinfo(FILE *f, struct disklabel *lp, const char *specialname)
57 {
58 	int	i, j;
59 
60 	(void)fprintf(f, "# %s:\n", specialname);
61 	if ((unsigned) lp->d_type < DKMAXTYPES)
62 		(void)fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
63 	else
64 		(void)fprintf(f, "type: %d\n", lp->d_type);
65 	(void)fprintf(f, "disk: %.*s\n", (int) sizeof(lp->d_typename),
66 	    lp->d_typename);
67 	(void)fprintf(f, "label: %.*s\n", (int) sizeof(lp->d_packname),
68 	    lp->d_packname);
69 	(void)fprintf(f, "flags:");
70 	if (lp->d_flags & D_REMOVABLE)
71 		(void)fprintf(f, " removable");
72 	if (lp->d_flags & D_ECC)
73 		(void)fprintf(f, " ecc");
74 	if (lp->d_flags & D_BADSECT)
75 		(void)fprintf(f, " badsect");
76 	(void)fprintf(f, "\n");
77 	(void)fprintf(f, "bytes/sector: %ld\n", (long) lp->d_secsize);
78 	(void)fprintf(f, "sectors/track: %ld\n", (long) lp->d_nsectors);
79 	(void)fprintf(f, "tracks/cylinder: %ld\n", (long) lp->d_ntracks);
80 	(void)fprintf(f, "sectors/cylinder: %ld\n", (long) lp->d_secpercyl);
81 	(void)fprintf(f, "cylinders: %ld\n", (long) lp->d_ncylinders);
82 	(void)fprintf(f, "total sectors: %ld\n", (long) lp->d_secperunit);
83 	(void)fprintf(f, "rpm: %ld\n", (long) lp->d_rpm);
84 	(void)fprintf(f, "interleave: %ld\n", (long) lp->d_interleave);
85 	(void)fprintf(f, "trackskew: %ld\n", (long) lp->d_trackskew);
86 	(void)fprintf(f, "cylinderskew: %ld\n", (long) lp->d_cylskew);
87 	(void)fprintf(f, "headswitch: %ld\t\t# microseconds\n",
88 	    (long)lp->d_headswitch);
89 	(void)fprintf(f, "track-to-track seek: %ld\t# microseconds\n",
90 	    (long)lp->d_trkseek);
91 	(void)fprintf(f, "drivedata: ");
92 	for (i = NDDATA - 1; i >= 0; i--)
93 		if (lp->d_drivedata[i])
94 			break;
95 	if (i < 0)
96 		i = 0;
97 	for (j = 0; j <= i; j++)
98 		(void)fprintf(f, "%d ", lp->d_drivedata[j]);
99 	(void)fprintf(f, "\n\n");
100 	(void)fflush(f);
101 }
102 
103 void
104 showpartition(FILE *f, struct disklabel *lp, int i, int ctsformat)
105 {
106 	struct partition *pp = lp->d_partitions + i;
107 	if (pp->p_size == 0)
108 		return;
109 
110 	if (ctsformat && lp->d_secpercyl && lp->d_nsectors) {
111 		char sbuf[64], obuf[64];
112 
113 		(void)snprintf(sbuf, sizeof(sbuf), "%d/%d/%d",
114 		    pp->p_size/lp->d_secpercyl,
115 		    (pp->p_size%lp->d_secpercyl) / lp->d_nsectors,
116 		    pp->p_size%lp->d_nsectors);
117 
118 		(void)snprintf(obuf, sizeof(obuf), "%d/%d/%d",
119 		    pp->p_offset/lp->d_secpercyl,
120 		    (pp->p_offset%lp->d_secpercyl) / lp->d_nsectors,
121 		    pp->p_offset%lp->d_nsectors);
122 
123 		(void)fprintf(f, " %c: %9s %9s ",
124 		    'a' + i, sbuf, obuf);
125 	} else {
126 		(void)fprintf(f, " %c: %9d %9d ", 'a' + i,
127 		    pp->p_size, pp->p_offset);
128 	}
129 
130 	if ((unsigned) pp->p_fstype < FSMAXTYPES)
131 		(void)fprintf(f, "%10.10s", fstypenames[pp->p_fstype]);
132 	else
133 		(void)fprintf(f, "%10d", pp->p_fstype);
134 
135 	switch (pp->p_fstype) {
136 	case FS_UNUSED:				/* XXX */
137 		(void)fprintf(f, "  %5d %5d %5.5s ",
138 		    pp->p_fsize, pp->p_fsize * pp->p_frag, "");
139 		break;
140 
141 	case FS_BSDFFS:
142 	case FS_ADOS:
143 	case FS_APPLEUFS:
144 		(void)fprintf(f, "  %5d %5d %5d ",
145 		    pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_cpg);
146 		break;
147 
148 	case FS_BSDLFS:
149 		(void)fprintf(f, "  %5d %5d %5d ",
150 		    pp->p_fsize, pp->p_fsize * pp->p_frag, pp->p_sgs);
151 		break;
152 
153 	case FS_EX2FS:
154 		(void)fprintf(f, "  %5d %5d       ",
155 		    pp->p_fsize, pp->p_fsize * pp->p_frag);
156 		break;
157 
158 	case FS_ISO9660:
159 		(void)fprintf(f, "  %6d            ", pp->p_cdsession);
160 		break;
161 
162 	default:
163 		(void)fprintf(f, "%20.20s", "");
164 		break;
165 	}
166 	if (lp->d_secpercyl != 0) {
167 		(void)fprintf(f, "  # (Cyl. %4d",
168 		    pp->p_offset / lp->d_secpercyl);
169 
170 		if (pp->p_offset % lp->d_secpercyl)
171 		    putc('*', f);
172 		else
173 		    putc(' ', f);
174 
175 		(void)fprintf(f, "- %d",
176 		    (pp->p_offset +
177 		    pp->p_size + lp->d_secpercyl - 1) /
178 		    lp->d_secpercyl - 1);
179 
180 		if ((pp->p_offset + pp->p_size) % lp->d_secpercyl)
181 		    putc('*', f);
182 
183 		(void)fprintf(f, ")\n");
184 	} else
185 		(void)fprintf(f, "\n");
186 }
187 
188 void
189 showpartitions(FILE *f, struct disklabel *lp, int ctsformat)
190 {
191 	int	i;
192 
193 	(void)fprintf(f, "%d partitions:\n", lp->d_npartitions);
194 	(void)fprintf(f,
195 	    "#        size    offset     fstype  [fsize bsize cpg/sgs]\n");
196 
197 	for (i = 0; i < lp->d_npartitions; i++)
198 		showpartition(f, lp, i, ctsformat);
199 	(void)fflush(f);
200 }
201