xref: /titanic_44/usr/src/cmd/fs.d/hsfs/labelit/labelit.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 /*      @(#)labelit.c 1.1 90/01/22 SMI      */
23 /*
24  * Copyright (c) 1989, 1990 by Sun Microsystems, Inc.
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 main(argc, argv)
90 int	argc;
91 char	**argv;
92 {
93 	register c;
94 	int openopt;
95 
96 	strcpy(callname, argv[0]);
97 	for(c=1; c<argc; c++) {
98 		string = argv[c];
99 		if(match("sysid=")) {
100 			sysid = string;
101 			continue;
102 		}
103 		if(match("volid=")) {
104 			volid = string;
105 			continue;
106 		}
107 		if(match("volsetid=")) {
108 			volsetid = string;
109 			continue;
110 		}
111 		if (match("pubid=")) {
112 			pubid = string;
113 			continue;
114 		}
115 		if(match("prepid=")) {
116 			prepid = string;
117 			continue;
118 		}
119 		if(match("applid=")) {
120 			applid = string;
121 			continue;
122 		}
123 		if(match("copyfile=")) {
124 			copyfile = string;
125 			continue;
126 		}
127 		if(match("absfile=")) {
128 			absfile = string;
129 			continue;
130 		}
131 		if(match("bibfile=")) {
132 			bibfile = string;
133 			continue;
134 		}
135 		break;
136 	}
137 	/* the last argument must be the cdrom iamge file */
138 	if (argc != c+1) {
139 		if (argc > 1)
140 			fprintf(stderr, "%s: Illegal option %s in input\n",
141 				callname, string);
142 		 usage();
143 	}
144 
145 	/* open image file in read write only if necessary */
146 	if (argc == 2) openopt = O_RDONLY;
147 	else openopt = O_RDWR;
148 
149 	if ((cdfd = open(argv[c], openopt)) < 0) {
150 		if (strchr(argv[c], '=') ||
151 			strchr(argv[c], '-')) usage();
152 		sprintf(errstrng, "%s: main: open(): ", callname);
153 		perror(errstrng);
154 		exit(32);
155 	}
156 
157 	/* check volume descriptor */
158 	(void) ckvoldesc();
159 
160 	if (cd_type < 0) {
161 		fprintf(stderr, "%s: unknown cdrom format label\n", callname);
162 		exit(32);
163 	}
164 
165 	/* update label, if needed */
166 	if (argc != 2) updatelabel();
167 
168 	/* print the (updated) image label */
169 	prntlabel();
170 
171 	close(cdfd);
172 	exit(0);
173 }
174 
175 usage()
176 {
177 	fprintf(stderr, "usage: %s [-F ufs] [option=value ...] cdimage\n",
178 		callname);
179 	exit(32);
180 }
181 
182 /*
183  * findhsvol: check if the disk is in high sierra format
184  *            return(1) if found, (0) otherwise
185  *	      if found, volp will point to the descriptor
186  *
187  */
188 int
189 findhsvol(volp)
190 char *volp;
191 {
192 int secno;
193 int i;
194 
195 	secno = HS_VOLDESC_SEC;
196 	GETSECTOR(volp, secno++, 1);
197 	while (HSV_DESC_TYPE(volp) != VD_EOV) {
198 		for (i = 0; i < HSV_ID_STRLEN; i++)
199 			if (HSV_STD_ID(volp)[i] != HSV_ID_STRING[i])
200 				goto cantfind;
201 		if (HSV_STD_VER(volp) != HSV_ID_VER)
202 			goto cantfind;
203 		switch (HSV_DESC_TYPE(volp)) {
204 		case VD_SFS:
205 			hs_pvd_sec_no = secno-1;
206 			return (1);
207 		case VD_EOV:
208 			goto cantfind;
209 		}
210 		GETSECTOR(volp, secno++, 1);
211 	}
212 cantfind:
213 	return (0);
214 }
215 
216 /*
217  * findisovol: check if the disk is in ISO 9660 format
218  *            return(1) if found, (0) otherwise
219  *	      if found, volp will point to the descriptor
220  *
221  */
222 int
223 findisovol(volp)
224 char *volp;
225 {
226 int secno;
227 int i;
228 
229 	secno = ISO_VOLDESC_SEC;
230 	GETSECTOR(volp, secno++, 1);
231 	while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
232 		for (i = 0; i < ISO_ID_STRLEN; i++)
233 			if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
234 				goto cantfind;
235 		if (ISO_STD_VER(volp) != ISO_ID_VER)
236 			goto cantfind;
237 		switch (ISO_DESC_TYPE(volp)) {
238 		case ISO_VD_PVD:
239 			iso_pvd_sec_no = secno-1;
240 			return (1);
241 		case ISO_VD_EOV:
242 			goto cantfind;
243 		}
244 		GETSECTOR(volp, secno++, 1);
245 	}
246 cantfind:
247 	return (0);
248 }
249 
250 /*
251  * findunixvol: check if the disk is in UNIX extension format
252  *            return(1) if found, (0) otherwise
253  *	      if found, volp will point to the descriptor
254  *
255  */
256 int
257 findunixvol(volp)
258 char *volp;
259 {
260 int secno;
261 int i;
262 
263 	secno = ISO_VOLDESC_SEC;
264 	GETSECTOR(volp, secno++, 1);
265 	while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
266 		for (i = 0; i < ISO_ID_STRLEN; i++)
267 			if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
268 				goto cantfind;
269 		if (ISO_STD_VER(volp) != ISO_ID_VER)
270 			goto cantfind;
271 		switch (ISO_DESC_TYPE(volp)) {
272 		case ISO_VD_UNIX:
273 			unix_pvd_sec_no = secno-1;
274 			return (1);
275 		case ISO_VD_EOV:
276 			goto cantfind;
277 		}
278 		GETSECTOR(volp, secno++, 1);
279 	}
280 cantfind:
281 	return (0);
282 }
283 
284 ckvoldesc()
285 {
286 	if (findhsvol(hs_buf))
287 		cd_type = 0;
288 	else if (findisovol(iso_buf)) {
289 		if (findunixvol(unix_buf))
290 			cd_type = 2;
291 		else cd_type = 1;
292 	}
293 	else cd_type = -1;
294 }
295 
296 updatelabel()
297 {
298 	switch (cd_type) {
299 	case 0:
300 		copystring(sysid, HSV_sys_id(hs_buf), 32);
301 		copystring(volid, HSV_vol_id(hs_buf), 32);
302 		copystring(volsetid, HSV_vol_set_id(hs_buf), 128);
303 		copystring(pubid, HSV_pub_id(hs_buf), 128);
304 		copystring(prepid, HSV_prep_id(hs_buf), 128);
305 		copystring(applid, HSV_appl_id(hs_buf), 128);
306 		copystring(copyfile, HSV_copyr_id(hs_buf), 37);
307 		copystring(absfile, HSV_abstr_id(hs_buf), 37);
308 		PUTSECTOR(hs_buf, hs_pvd_sec_no, 1);
309 		break;
310 	case 2:
311 		copystring(sysid, ISO_sys_id(unix_buf), 32);
312 		copystring(volid, ISO_vol_id(unix_buf), 32);
313 		copystring(volsetid, ISO_vol_set_id(unix_buf), 128);
314 		copystring(pubid, ISO_pub_id(unix_buf), 128);
315 		copystring(prepid, ISO_prep_id(unix_buf), 128);
316 		copystring(applid, ISO_appl_id(unix_buf), 128);
317 		copystring(copyfile, ISO_copyr_id(unix_buf), 37);
318 		copystring(absfile, ISO_abstr_id(unix_buf), 37);
319 		copystring(bibfile, ISO_bibli_id(unix_buf), 37);
320 		PUTSECTOR(unix_buf, unix_pvd_sec_no, 1);
321 		/* after update unix volume descriptor,
322 		   fall thru to update the iso primary vol descriptor */
323 	case 1:
324 		copystring(sysid, ISO_sys_id(iso_buf), 32);
325 		copystring(volid, ISO_vol_id(iso_buf), 32);
326 		copystring(volsetid, ISO_vol_set_id(iso_buf), 128);
327 		copystring(pubid, ISO_pub_id(iso_buf), 128);
328 		copystring(prepid, ISO_prep_id(iso_buf), 128);
329 		copystring(applid, ISO_appl_id(iso_buf), 128);
330 		copystring(copyfile, ISO_copyr_id(iso_buf), 37);
331 		copystring(absfile, ISO_abstr_id(iso_buf), 37);
332 		copystring(bibfile, ISO_bibli_id(iso_buf), 37);
333 		PUTSECTOR(iso_buf, iso_pvd_sec_no, 1);
334 		break;
335 	}
336 }
337 
338 prntlabel()
339 {
340 	int i;
341 	switch (cd_type) {
342 	case 0:
343 		printf("CD-ROM is in High Sierra format\n");
344 		sysid=(char *)HSV_sys_id(hs_buf);
345 		volid=(char *)HSV_vol_id(hs_buf);
346 		volsetid=(char *)HSV_vol_set_id(hs_buf);
347 		pubid=(char *)HSV_pub_id(hs_buf);
348 		prepid=(char *)HSV_prep_id(hs_buf);
349 		applid=(char *)HSV_appl_id(hs_buf);
350 		copyfile=(char *)HSV_copyr_id(hs_buf);
351 		absfile=(char *)HSV_abstr_id(hs_buf);
352 		bibfile=NULL;
353 		volsetsize= HSV_SET_SIZE(hs_buf);
354 		volsetseq= HSV_SET_SEQ(hs_buf);
355 		blksize= HSV_BLK_SIZE(hs_buf);
356 		volsize= HSV_VOL_SIZE(hs_buf);
357 		break;
358 	case 1:
359 		printf("CD-ROM is in ISO 9660 format\n");
360 		sysid=(char *) ISO_sys_id(iso_buf);
361 		volid=(char *)ISO_vol_id(iso_buf);
362 		volsetid=(char *)ISO_vol_set_id(iso_buf);
363 		pubid=(char *)ISO_pub_id(iso_buf);
364 		prepid=(char *)ISO_prep_id(iso_buf);
365 		applid=(char *)ISO_appl_id(iso_buf);
366 		copyfile=(char *)ISO_copyr_id(iso_buf);
367 		absfile=(char *)ISO_abstr_id(iso_buf);
368 		bibfile=(char *)ISO_bibli_id(iso_buf);
369 		volsetsize=ISO_SET_SIZE(iso_buf);
370 		volsetseq=ISO_SET_SEQ(iso_buf);
371 		blksize=ISO_BLK_SIZE(iso_buf);
372 		volsize=ISO_VOL_SIZE(iso_buf);
373 		break;
374 	case 2:
375 		printf("CD-ROM is in ISO 9660 format with UNIX extension\n");
376 		sysid=(char *)ISO_sys_id(unix_buf);
377 		volid=(char *)ISO_vol_id(unix_buf);
378 		volsetid=(char *)ISO_vol_set_id(unix_buf);
379 		pubid=(char *)ISO_pub_id(unix_buf);
380 		prepid=(char *)ISO_prep_id(unix_buf);
381 		applid=(char *)ISO_appl_id(unix_buf);
382 		copyfile=(char *)ISO_copyr_id(unix_buf);
383 		absfile=(char *)ISO_abstr_id(unix_buf);
384 		bibfile=(char *)ISO_bibli_id(unix_buf);
385 		volsetsize=ISO_SET_SIZE(unix_buf);
386 		volsetseq=ISO_SET_SEQ(unix_buf);
387 		blksize=ISO_BLK_SIZE(unix_buf);
388 		volsize=ISO_VOL_SIZE(unix_buf);
389 		break;
390 	default:
391 		return;
392 	}
393 	/* system id */
394 	prntstring("System id", sysid, 32);
395 	/* read volume id */
396 	prntstring("Volume id", volid, 32);
397 	/* read volume set id */
398 	prntstring("Volume set id", volsetid, 128);
399 	/* publisher id */
400 	prntstring("Publisher id", pubid, 128);
401 	/* data preparer id */
402 	prntstring("Data preparer id", prepid, 128);
403 	/* application id */
404 	prntstring("Application id", applid, 128);
405 	/* copyright file identifier */
406 	prntstring("Copyright File id", copyfile, 37);
407 	/* Abstract file identifier */
408 	prntstring("Abstract File id", absfile, 37);
409 	/* Bibliographic file identifier */
410 	prntstring("Bibliographic File id", bibfile, 37);
411 	/* print volume set size */
412 	printf("Volume set size is %d\n", volsetsize);
413 	/* print volume set sequnce number */
414 	printf("Volume set sequence number is %d\n", volsetseq);
415 	/* print logical block size */
416 	printf("Logical block size is %d\n", blksize);
417 	/* print volume size */
418 	printf("Volume size is %d\n", volsize);
419 }
420 
421 copystring (from, to, size)
422 char *from;
423 char *to;
424 int size;
425 {
426 int i;
427 
428 	if (from == NULL) return;
429 	for (i=0;i<size;i++) {
430 		if (*from == '\0') break;
431 		else *to++=*from++;
432 	}
433 	for (;i<size;i++) *to++=' ';
434 }
435 
436 prntstring(heading, s, maxlen)
437 char * heading;
438 char *s;
439 int maxlen;
440 {
441 int i;
442 	if (maxlen < 1) return;
443 	if (heading == NULL || s == NULL) return;
444 	/* print heading */
445 	printf("%s: ", heading);
446 
447 	/* strip off trailing zeros */
448 	for (i=maxlen-1;i >= 0; i--)
449 		if (s[i] != ' ') break;
450 
451 	maxlen = i+1;
452 	for (i=0;i<maxlen;i++) printf("%c", s[i]);
453 	printf("\n");
454 }
455 
456 match(s)
457 char *s;
458 {
459 	register char *cs;
460 
461 	cs = string;
462 	while(*cs++ == *s)
463 		if(*s++ == '\0')
464 			goto true;
465 	if(*s != '\0')
466 		return(0);
467 
468 true:
469 	cs--;
470 	string = cs;
471 	return(1);
472 }
473 
474 /*readdisk - read from cdrom image file */
475 getdisk(buf, daddr, size)
476 char *buf; /* buffer area */
477 int daddr; /* disk addr */
478 int size; /* no. of byte */
479 {
480 
481         if (lseek(cdfd, daddr, L_SET) == -1) {
482 		sprintf(errstrng, "%s: getdisk: lseek()", callname);
483                 perror(errstrng);
484                 exit(32);
485         }
486         if (read(cdfd, buf, size) != size) {
487 		sprintf(errstrng, "%s: getdisk: read()", callname );
488                 perror(errstrng);
489                 exit(32);
490         }
491 
492 }
493 
494 /*putdisk - write to cdrom image file */
495 putdisk(buf, daddr, size)
496 char *buf; /* buffer area */
497 int daddr; /* disk addr */
498 int size; /* no. of byte */
499 {
500 
501 	if (lseek(cdfd, daddr, L_SET) == -1) {
502 		sprintf(errstrng, "%s: putdisk: lseek()", callname);
503 		perror(errstrng);
504 		exit(32);
505 	}
506 	if (write(cdfd, buf, size) != size) {
507 		sprintf(errstrng, "%s: putdisk: write()", callname);
508 		perror(errstrng);
509 		exit(32);
510 	}
511 }
512 
513