xref: /illumos-gate/usr/src/cmd/fs.d/hsfs/labelit/labelit.c (revision a38ddfee9c8c6b6c5a2947ff52fd2338362a4444)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * labelit [option=value ...] cdimage
29  * where options are:
30  *      sysid		system identifier               (a characters, 32 max)
31  *      volid:          volume identifier               (d-characters, 32 max)
32  *      volsetid:       volume set identifier           (d-characters, 128 max)
33  *      pubid:          publisher identifier            (d-characters, 128 max)
34  *      prepid:         data preparer identifier        (d-charcter, 128 max)
35  *      applid:         application identifier          (d-charcter, 128 max)
36  *      copyfile:       copyright file identifier       (d-characters, 128 max)
37  *      absfile:        abstract file identifier        (d-characters, 37 max)
38  *      bibfile:        bibliographic file identifier   (d-charcters, 37 max)
39  */
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <sys/file.h>
51 #include <dirent.h>
52 #include "hsfs_spec.h"
53 #include "iso_spec.h"
54 #include "iso_impl.h"
55 
56 #define	PUTSECTOR(buf, secno, nosec) (putdisk(buf, (secno)*ISO_SECTOR_SIZE, \
57 	(nosec)*ISO_SECTOR_SIZE))
58 #define	GETSECTOR(buf, secno, nosec) (getdisk(buf, (secno)*ISO_SECTOR_SIZE, \
59 	(nosec)*ISO_SECTOR_SIZE))
60 
61 char *string;
62 #define	MAXERRSTRNG	80
63 char	errstrng[MAXERRSTRNG];
64 char	callname[160];
65 
66 int  cdfd;
67 int cd_type;
68 char hs_buf[ISO_SECTOR_SIZE];
69 int  hs_pvd_sec_no;
70 char iso_buf[ISO_SECTOR_SIZE];
71 int  iso_pvd_sec_no;
72 char unix_buf[ISO_SECTOR_SIZE];
73 int  unix_pvd_sec_no;
74 char *vdp;
75 char *sysid;
76 char *volid;
77 char *volsetid;
78 char *pubid;
79 char *prepid;
80 char *applid;
81 char *copyfile;
82 char *absfile;
83 char *bibfile;
84 int volsetsize;
85 int volsetseq;
86 int blksize;
87 int volsize;
88 
89 static int match(char *s);
90 static void usage(void);
91 static void putdisk(char *buf, int daddr, int size);
92 static void getdisk(char *buf, int daddr, int size);
93 static void prntstring(char *heading, char *s, int maxlen);
94 static void copystring(char *from, char *to, int size);
95 static void prntlabel(void);
96 static void updatelabel(void);
97 static void ckvoldesc(void);
98 
99 int
100 main(int argc, char **argv)
101 {
102 	int c;
103 	int openopt;
104 
105 	strcpy(callname, argv[0]);
106 	for (c = 1; c < argc; c++) {
107 		string = argv[c];
108 		if (match("sysid=")) {
109 			sysid = string;
110 			continue;
111 		}
112 		if (match("volid=")) {
113 			volid = string;
114 			continue;
115 		}
116 		if (match("volsetid=")) {
117 			volsetid = string;
118 			continue;
119 		}
120 		if (match("pubid=")) {
121 			pubid = string;
122 			continue;
123 		}
124 		if (match("prepid=")) {
125 			prepid = string;
126 			continue;
127 		}
128 		if (match("applid=")) {
129 			applid = string;
130 			continue;
131 		}
132 		if (match("copyfile=")) {
133 			copyfile = string;
134 			continue;
135 		}
136 		if (match("absfile=")) {
137 			absfile = string;
138 			continue;
139 		}
140 		if (match("bibfile=")) {
141 			bibfile = string;
142 			continue;
143 		}
144 		break;
145 	}
146 	/* the last argument must be the cdrom iamge file */
147 	if (argc != c+1) {
148 		if (argc > 1)
149 			fprintf(stderr, "%s: Illegal option %s in input\n",
150 			    callname, string);
151 			usage();
152 	}
153 
154 	/* open image file in read write only if necessary */
155 	if (argc == 2) openopt = O_RDONLY;
156 	else openopt = O_RDWR;
157 
158 	if ((cdfd = open(argv[c], openopt)) < 0) {
159 		if (strchr(argv[c], '=') ||
160 			strchr(argv[c], '-')) usage();
161 		sprintf(errstrng, "%s: main: open(): ", callname);
162 		perror(errstrng);
163 		exit(32);
164 	}
165 
166 	/* check volume descriptor */
167 	(void) ckvoldesc();
168 
169 	if (cd_type < 0) {
170 		fprintf(stderr, "%s: unknown cdrom format label\n", callname);
171 		exit(32);
172 	}
173 
174 	/* update label, if needed */
175 	if (argc != 2) updatelabel();
176 
177 	/* print the (updated) image label */
178 	prntlabel();
179 
180 	close(cdfd);
181 	return (0);
182 }
183 
184 static void
185 usage(void)
186 {
187 	fprintf(stderr, "usage: %s [-F ufs] [option=value ...] cdimage\n",
188 		callname);
189 	exit(32);
190 }
191 
192 /*
193  * findhsvol: check if the disk is in high sierra format
194  *            return(1) if found, (0) otherwise
195  *	      if found, volp will point to the descriptor
196  *
197  */
198 int
199 findhsvol(volp)
200 char *volp;
201 {
202 int secno;
203 int i;
204 
205 	secno = HS_VOLDESC_SEC;
206 	GETSECTOR(volp, secno++, 1);
207 	while (HSV_DESC_TYPE(volp) != VD_EOV) {
208 		for (i = 0; i < HSV_ID_STRLEN; i++)
209 			if (HSV_STD_ID(volp)[i] != HSV_ID_STRING[i])
210 				goto cantfind;
211 		if (HSV_STD_VER(volp) != HSV_ID_VER)
212 			goto cantfind;
213 		switch (HSV_DESC_TYPE(volp)) {
214 		case VD_SFS:
215 			hs_pvd_sec_no = secno-1;
216 			return (1);
217 		case VD_EOV:
218 			goto cantfind;
219 		}
220 		GETSECTOR(volp, secno++, 1);
221 	}
222 cantfind:
223 	return (0);
224 }
225 
226 /*
227  * findisovol: check if the disk is in ISO 9660 format
228  *            return(1) if found, (0) otherwise
229  *	      if found, volp will point to the descriptor
230  *
231  */
232 int
233 findisovol(volp)
234 char *volp;
235 {
236 int secno;
237 int i;
238 
239 	secno = ISO_VOLDESC_SEC;
240 	GETSECTOR(volp, secno++, 1);
241 	while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
242 		for (i = 0; i < ISO_ID_STRLEN; i++)
243 			if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
244 				goto cantfind;
245 		if (ISO_STD_VER(volp) != ISO_ID_VER)
246 			goto cantfind;
247 		switch (ISO_DESC_TYPE(volp)) {
248 		case ISO_VD_PVD:
249 			iso_pvd_sec_no = secno-1;
250 			return (1);
251 		case ISO_VD_EOV:
252 			goto cantfind;
253 		}
254 		GETSECTOR(volp, secno++, 1);
255 	}
256 cantfind:
257 	return (0);
258 }
259 
260 /*
261  * findunixvol: check if the disk is in UNIX extension format
262  *            return(1) if found, (0) otherwise
263  *	      if found, volp will point to the descriptor
264  *
265  */
266 int
267 findunixvol(volp)
268 char *volp;
269 {
270 int secno;
271 int i;
272 
273 	secno = ISO_VOLDESC_SEC;
274 	GETSECTOR(volp, secno++, 1);
275 	while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
276 		for (i = 0; i < ISO_ID_STRLEN; i++)
277 			if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
278 				goto cantfind;
279 		if (ISO_STD_VER(volp) != ISO_ID_VER)
280 			goto cantfind;
281 		switch (ISO_DESC_TYPE(volp)) {
282 		case ISO_VD_UNIX:
283 			unix_pvd_sec_no = secno-1;
284 			return (1);
285 		case ISO_VD_EOV:
286 			goto cantfind;
287 		}
288 		GETSECTOR(volp, secno++, 1);
289 	}
290 cantfind:
291 	return (0);
292 }
293 
294 static void
295 ckvoldesc(void)
296 {
297 	if (findhsvol(hs_buf))
298 		cd_type = 0;
299 	else if (findisovol(iso_buf)) {
300 		if (findunixvol(unix_buf))
301 			cd_type = 2;
302 		else cd_type = 1;
303 	} else {
304 		cd_type = -1;
305 	}
306 }
307 
308 static void
309 updatelabel(void)
310 {
311 	switch (cd_type) {
312 	case 0:
313 		copystring(sysid, (char *)HSV_sys_id(hs_buf), 32);
314 		copystring(volid, (char *)HSV_vol_id(hs_buf), 32);
315 		copystring(volsetid, (char *)HSV_vol_set_id(hs_buf), 128);
316 		copystring(pubid, (char *)HSV_pub_id(hs_buf), 128);
317 		copystring(prepid, (char *)HSV_prep_id(hs_buf), 128);
318 		copystring(applid, (char *)HSV_appl_id(hs_buf), 128);
319 		copystring(copyfile, (char *)HSV_copyr_id(hs_buf), 37);
320 		copystring(absfile, (char *)HSV_abstr_id(hs_buf), 37);
321 		PUTSECTOR(hs_buf, hs_pvd_sec_no, 1);
322 		break;
323 	case 2:
324 		copystring(sysid, (char *)ISO_sys_id(unix_buf), 32);
325 		copystring(volid, (char *)ISO_vol_id(unix_buf), 32);
326 		copystring(volsetid, (char *)ISO_vol_set_id(unix_buf), 128);
327 		copystring(pubid, (char *)ISO_pub_id(unix_buf), 128);
328 		copystring(prepid, (char *)ISO_prep_id(unix_buf), 128);
329 		copystring(applid, (char *)ISO_appl_id(unix_buf), 128);
330 		copystring(copyfile, (char *)ISO_copyr_id(unix_buf), 37);
331 		copystring(absfile, (char *)ISO_abstr_id(unix_buf), 37);
332 		copystring(bibfile, (char *)ISO_bibli_id(unix_buf), 37);
333 		PUTSECTOR(unix_buf, unix_pvd_sec_no, 1);
334 		/*
335 		 * after update unix volume descriptor,
336 		 * fall thru to update the iso primary vol descriptor
337 		 */
338 	case 1:
339 		copystring(sysid, (char *)ISO_sys_id(iso_buf), 32);
340 		copystring(volid, (char *)ISO_vol_id(iso_buf), 32);
341 		copystring(volsetid, (char *)ISO_vol_set_id(iso_buf), 128);
342 		copystring(pubid, (char *)ISO_pub_id(iso_buf), 128);
343 		copystring(prepid, (char *)ISO_prep_id(iso_buf), 128);
344 		copystring(applid, (char *)ISO_appl_id(iso_buf), 128);
345 		copystring(copyfile, (char *)ISO_copyr_id(iso_buf), 37);
346 		copystring(absfile, (char *)ISO_abstr_id(iso_buf), 37);
347 		copystring(bibfile, (char *)ISO_bibli_id(iso_buf), 37);
348 		PUTSECTOR(iso_buf, iso_pvd_sec_no, 1);
349 		break;
350 	}
351 }
352 
353 static void
354 prntlabel(void)
355 {
356 	int i;
357 	switch (cd_type) {
358 	case 0:
359 		printf("CD-ROM is in High Sierra format\n");
360 		sysid = (char *)HSV_sys_id(hs_buf);
361 		volid = (char *)HSV_vol_id(hs_buf);
362 		volsetid = (char *)HSV_vol_set_id(hs_buf);
363 		pubid = (char *)HSV_pub_id(hs_buf);
364 		prepid = (char *)HSV_prep_id(hs_buf);
365 		applid = (char *)HSV_appl_id(hs_buf);
366 		copyfile = (char *)HSV_copyr_id(hs_buf);
367 		absfile = (char *)HSV_abstr_id(hs_buf);
368 		bibfile = NULL;
369 		volsetsize = HSV_SET_SIZE(hs_buf);
370 		volsetseq = HSV_SET_SEQ(hs_buf);
371 		blksize = HSV_BLK_SIZE(hs_buf);
372 		volsize = HSV_VOL_SIZE(hs_buf);
373 		break;
374 	case 1:
375 		printf("CD-ROM is in ISO 9660 format\n");
376 		sysid = (char *)ISO_sys_id(iso_buf);
377 		volid = (char *)ISO_vol_id(iso_buf);
378 		volsetid = (char *)ISO_vol_set_id(iso_buf);
379 		pubid = (char *)ISO_pub_id(iso_buf);
380 		prepid = (char *)ISO_prep_id(iso_buf);
381 		applid = (char *)ISO_appl_id(iso_buf);
382 		copyfile = (char *)ISO_copyr_id(iso_buf);
383 		absfile = (char *)ISO_abstr_id(iso_buf);
384 		bibfile = (char *)ISO_bibli_id(iso_buf);
385 		volsetsize = ISO_SET_SIZE(iso_buf);
386 		volsetseq = ISO_SET_SEQ(iso_buf);
387 		blksize = ISO_BLK_SIZE(iso_buf);
388 		volsize = ISO_VOL_SIZE(iso_buf);
389 		break;
390 	case 2:
391 		printf("CD-ROM is in ISO 9660 format with UNIX extension\n");
392 		sysid = (char *)ISO_sys_id(unix_buf);
393 		volid = (char *)ISO_vol_id(unix_buf);
394 		volsetid = (char *)ISO_vol_set_id(unix_buf);
395 		pubid = (char *)ISO_pub_id(unix_buf);
396 		prepid = (char *)ISO_prep_id(unix_buf);
397 		applid = (char *)ISO_appl_id(unix_buf);
398 		copyfile = (char *)ISO_copyr_id(unix_buf);
399 		absfile = (char *)ISO_abstr_id(unix_buf);
400 		bibfile = (char *)ISO_bibli_id(unix_buf);
401 		volsetsize = ISO_SET_SIZE(unix_buf);
402 		volsetseq = ISO_SET_SEQ(unix_buf);
403 		blksize = ISO_BLK_SIZE(unix_buf);
404 		volsize = ISO_VOL_SIZE(unix_buf);
405 		break;
406 	default:
407 		return;
408 	}
409 	/* system id */
410 	prntstring("System id", sysid, 32);
411 	/* read volume id */
412 	prntstring("Volume id", volid, 32);
413 	/* read volume set id */
414 	prntstring("Volume set id", volsetid, 128);
415 	/* publisher id */
416 	prntstring("Publisher id", pubid, 128);
417 	/* data preparer id */
418 	prntstring("Data preparer id", prepid, 128);
419 	/* application id */
420 	prntstring("Application id", applid, 128);
421 	/* copyright file identifier */
422 	prntstring("Copyright File id", copyfile, 37);
423 	/* Abstract file identifier */
424 	prntstring("Abstract File id", absfile, 37);
425 	/* Bibliographic file identifier */
426 	prntstring("Bibliographic File id", bibfile, 37);
427 	/* print volume set size */
428 	printf("Volume set size is %d\n", volsetsize);
429 	/* print volume set sequnce number */
430 	printf("Volume set sequence number is %d\n", volsetseq);
431 	/* print logical block size */
432 	printf("Logical block size is %d\n", blksize);
433 	/* print volume size */
434 	printf("Volume size is %d\n", volsize);
435 }
436 
437 static void
438 copystring(char *from, char *to, int size)
439 {
440 	int i;
441 
442 	if (from == NULL)
443 		return;
444 	for (i = 0; i < size; i++) {
445 		if (*from == '\0')
446 			break;
447 		else *to++ = *from++;
448 	}
449 	for (; i < size; i++) *to++ = ' ';
450 }
451 
452 static void
453 prntstring(char *heading, char *s, int maxlen)
454 {
455 	int i;
456 	if (maxlen < 1)
457 		return;
458 	if (heading == NULL || s == NULL)
459 		return;
460 	/* print heading */
461 	printf("%s: ", heading);
462 
463 	/* strip off trailing zeros */
464 	for (i = maxlen-1; i >= 0; i--)
465 		if (s[i] != ' ')
466 			break;
467 
468 	maxlen = i+1;
469 	for (i = 0; i < maxlen; i++)
470 		printf("%c", s[i]);
471 	printf("\n");
472 }
473 
474 static int
475 match(char *s)
476 {
477 	char *cs;
478 
479 	cs = string;
480 	while (*cs++ == *s)
481 		if (*s++ == '\0')
482 			goto true;
483 	if (*s != '\0')
484 		return (0);
485 
486 true:
487 	cs--;
488 	string = cs;
489 	return (1);
490 }
491 
492 /* readdisk - read from cdrom image file */
493 static void
494 getdisk(char *buf, int daddr, int size)
495 {
496 
497 	if (lseek(cdfd, daddr, L_SET) == -1) {
498 		sprintf(errstrng, "%s: getdisk: lseek()", callname);
499 		perror(errstrng);
500 		exit(32);
501 	}
502 	if (read(cdfd, buf, size) != size) {
503 		sprintf(errstrng, "%s: getdisk: read()", callname);
504 		perror(errstrng);
505 		exit(32);
506 	}
507 }
508 
509 /* putdisk - write to cdrom image file */
510 static void
511 putdisk(char *buf, int daddr, int size)
512 {
513 
514 	if (lseek(cdfd, daddr, L_SET) == -1) {
515 		sprintf(errstrng, "%s: putdisk: lseek()", callname);
516 		perror(errstrng);
517 		exit(32);
518 	}
519 	if (write(cdfd, buf, size) != size) {
520 		sprintf(errstrng, "%s: putdisk: write()", callname);
521 		perror(errstrng);
522 		exit(32);
523 	}
524 }
525