hsfs/labelit: remove -Wno-implicit-function-declaration
[unleashed/lotheac.git] / usr / src / cmd / fs.d / hsfs / labelit / labelit.c
blob9e0cd4584cf1d7e7c6041cd9dd78a6138a3d8895
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
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)
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/types.h>
47 #include <sys/file.h>
48 #include <dirent.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
53 #include <sys/fs/hsfs_isospec.h>
54 #include <sys/fs/hsfs_spec.h>
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))
61 char *string;
62 #define MAXERRSTRNG 80
63 char errstrng[MAXERRSTRNG];
64 char callname[160];
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;
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);
99 int
100 main(int argc, char **argv)
102 int c;
103 int openopt;
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;
112 if (match("volid=")) {
113 volid = string;
114 continue;
116 if (match("volsetid=")) {
117 volsetid = string;
118 continue;
120 if (match("pubid=")) {
121 pubid = string;
122 continue;
124 if (match("prepid=")) {
125 prepid = string;
126 continue;
128 if (match("applid=")) {
129 applid = string;
130 continue;
132 if (match("copyfile=")) {
133 copyfile = string;
134 continue;
136 if (match("absfile=")) {
137 absfile = string;
138 continue;
140 if (match("bibfile=")) {
141 bibfile = string;
142 continue;
144 break;
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();
154 /* open image file in read write only if necessary */
155 if (argc == 2) openopt = O_RDONLY;
156 else openopt = O_RDWR;
158 if ((cdfd = open(argv[c], openopt)) < 0) {
159 if (strchr(argv[c], '=') ||
160 strchr(argv[c], '-')) {
161 usage();
163 sprintf(errstrng, "%s: main: open(): ", callname);
164 perror(errstrng);
165 exit(32);
168 /* check volume descriptor */
169 (void) ckvoldesc();
171 if (cd_type < 0) {
172 fprintf(stderr, "%s: unknown cdrom format label\n", callname);
173 exit(32);
176 /* update label, if needed */
177 if (argc != 2) updatelabel();
179 /* print the (updated) image label */
180 prntlabel();
182 close(cdfd);
183 return (0);
186 static void
187 usage(void)
189 fprintf(stderr, "usage: %s [-F ufs] [option=value ...] cdimage\n",
190 callname);
191 exit(32);
195 * findhsvol: check if the disk is in high sierra format
196 * return(1) if found, (0) otherwise
197 * if found, volp will point to the descriptor
201 findhsvol(volp)
202 char *volp;
204 int secno;
205 int i;
207 secno = HS_VOLDESC_SEC;
208 GETSECTOR(volp, secno++, 1);
209 while (HSV_DESC_TYPE(volp) != VD_EOV) {
210 for (i = 0; i < HSV_ID_STRLEN; i++)
211 if (HSV_STD_ID(volp)[i] != HSV_ID_STRING[i])
212 goto cantfind;
213 if (HSV_STD_VER(volp) != HSV_ID_VER)
214 goto cantfind;
215 switch (HSV_DESC_TYPE(volp)) {
216 case VD_SFS:
217 hs_pvd_sec_no = secno-1;
218 return (1);
219 case VD_EOV:
220 goto cantfind;
222 GETSECTOR(volp, secno++, 1);
224 cantfind:
225 return (0);
229 * findisovol: check if the disk is in ISO 9660 format
230 * return(1) if found, (0) otherwise
231 * if found, volp will point to the descriptor
235 findisovol(volp)
236 char *volp;
238 int secno;
239 int i;
241 secno = ISO_VOLDESC_SEC;
242 GETSECTOR(volp, secno++, 1);
243 while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
244 for (i = 0; i < ISO_ID_STRLEN; i++)
245 if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
246 goto cantfind;
247 if (ISO_STD_VER(volp) != ISO_ID_VER)
248 goto cantfind;
249 switch (ISO_DESC_TYPE(volp)) {
250 case ISO_VD_PVD:
251 iso_pvd_sec_no = secno-1;
252 return (1);
253 case ISO_VD_EOV:
254 goto cantfind;
256 GETSECTOR(volp, secno++, 1);
258 cantfind:
259 return (0);
263 * findunixvol: check if the disk is in UNIX extension format
264 * return(1) if found, (0) otherwise
265 * if found, volp will point to the descriptor
269 findunixvol(volp)
270 char *volp;
272 int secno;
273 int i;
275 secno = ISO_VOLDESC_SEC;
276 GETSECTOR(volp, secno++, 1);
277 while (ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
278 for (i = 0; i < ISO_ID_STRLEN; i++)
279 if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
280 goto cantfind;
281 if (ISO_STD_VER(volp) != ISO_ID_VER)
282 goto cantfind;
283 switch (ISO_DESC_TYPE(volp)) {
284 case ISO_VD_UNIX:
285 unix_pvd_sec_no = secno-1;
286 return (1);
287 case ISO_VD_EOV:
288 goto cantfind;
290 GETSECTOR(volp, secno++, 1);
292 cantfind:
293 return (0);
296 static void
297 ckvoldesc(void)
299 if (findhsvol(hs_buf))
300 cd_type = 0;
301 else if (findisovol(iso_buf)) {
302 if (findunixvol(unix_buf))
303 cd_type = 2;
304 else cd_type = 1;
305 } else {
306 cd_type = -1;
310 static void
311 updatelabel(void)
313 switch (cd_type) {
314 case 0:
315 copystring(sysid, (char *)HSV_sys_id(hs_buf), 32);
316 copystring(volid, (char *)HSV_vol_id(hs_buf), 32);
317 copystring(volsetid, (char *)HSV_vol_set_id(hs_buf), 128);
318 copystring(pubid, (char *)HSV_pub_id(hs_buf), 128);
319 copystring(prepid, (char *)HSV_prep_id(hs_buf), 128);
320 copystring(applid, (char *)HSV_appl_id(hs_buf), 128);
321 copystring(copyfile, (char *)HSV_copyr_id(hs_buf), 37);
322 copystring(absfile, (char *)HSV_abstr_id(hs_buf), 37);
323 PUTSECTOR(hs_buf, hs_pvd_sec_no, 1);
324 break;
325 case 2:
326 copystring(sysid, (char *)ISO_sys_id(unix_buf), 32);
327 copystring(volid, (char *)ISO_vol_id(unix_buf), 32);
328 copystring(volsetid, (char *)ISO_vol_set_id(unix_buf), 128);
329 copystring(pubid, (char *)ISO_pub_id(unix_buf), 128);
330 copystring(prepid, (char *)ISO_prep_id(unix_buf), 128);
331 copystring(applid, (char *)ISO_appl_id(unix_buf), 128);
332 copystring(copyfile, (char *)ISO_copyr_id(unix_buf), 37);
333 copystring(absfile, (char *)ISO_abstr_id(unix_buf), 37);
334 copystring(bibfile, (char *)ISO_bibli_id(unix_buf), 37);
335 PUTSECTOR(unix_buf, unix_pvd_sec_no, 1);
337 * after update unix volume descriptor,
338 * fall thru to update the iso primary vol descriptor
340 /* FALLTHROUGH */
341 case 1:
342 copystring(sysid, (char *)ISO_sys_id(iso_buf), 32);
343 copystring(volid, (char *)ISO_vol_id(iso_buf), 32);
344 copystring(volsetid, (char *)ISO_vol_set_id(iso_buf), 128);
345 copystring(pubid, (char *)ISO_pub_id(iso_buf), 128);
346 copystring(prepid, (char *)ISO_prep_id(iso_buf), 128);
347 copystring(applid, (char *)ISO_appl_id(iso_buf), 128);
348 copystring(copyfile, (char *)ISO_copyr_id(iso_buf), 37);
349 copystring(absfile, (char *)ISO_abstr_id(iso_buf), 37);
350 copystring(bibfile, (char *)ISO_bibli_id(iso_buf), 37);
351 PUTSECTOR(iso_buf, iso_pvd_sec_no, 1);
352 break;
356 static void
357 prntlabel(void)
359 int i;
360 switch (cd_type) {
361 case 0:
362 printf("CD-ROM is in High Sierra format\n");
363 sysid = (char *)HSV_sys_id(hs_buf);
364 volid = (char *)HSV_vol_id(hs_buf);
365 volsetid = (char *)HSV_vol_set_id(hs_buf);
366 pubid = (char *)HSV_pub_id(hs_buf);
367 prepid = (char *)HSV_prep_id(hs_buf);
368 applid = (char *)HSV_appl_id(hs_buf);
369 copyfile = (char *)HSV_copyr_id(hs_buf);
370 absfile = (char *)HSV_abstr_id(hs_buf);
371 bibfile = NULL;
372 volsetsize = HSV_SET_SIZE(hs_buf);
373 volsetseq = HSV_SET_SEQ(hs_buf);
374 blksize = HSV_BLK_SIZE(hs_buf);
375 volsize = HSV_VOL_SIZE(hs_buf);
376 break;
377 case 1:
378 printf("CD-ROM is in ISO 9660 format\n");
379 sysid = (char *)ISO_sys_id(iso_buf);
380 volid = (char *)ISO_vol_id(iso_buf);
381 volsetid = (char *)ISO_vol_set_id(iso_buf);
382 pubid = (char *)ISO_pub_id(iso_buf);
383 prepid = (char *)ISO_prep_id(iso_buf);
384 applid = (char *)ISO_appl_id(iso_buf);
385 copyfile = (char *)ISO_copyr_id(iso_buf);
386 absfile = (char *)ISO_abstr_id(iso_buf);
387 bibfile = (char *)ISO_bibli_id(iso_buf);
388 volsetsize = ISO_SET_SIZE(iso_buf);
389 volsetseq = ISO_SET_SEQ(iso_buf);
390 blksize = ISO_BLK_SIZE(iso_buf);
391 volsize = ISO_VOL_SIZE(iso_buf);
392 break;
393 case 2:
394 printf("CD-ROM is in ISO 9660 format with UNIX extension\n");
395 sysid = (char *)ISO_sys_id(unix_buf);
396 volid = (char *)ISO_vol_id(unix_buf);
397 volsetid = (char *)ISO_vol_set_id(unix_buf);
398 pubid = (char *)ISO_pub_id(unix_buf);
399 prepid = (char *)ISO_prep_id(unix_buf);
400 applid = (char *)ISO_appl_id(unix_buf);
401 copyfile = (char *)ISO_copyr_id(unix_buf);
402 absfile = (char *)ISO_abstr_id(unix_buf);
403 bibfile = (char *)ISO_bibli_id(unix_buf);
404 volsetsize = ISO_SET_SIZE(unix_buf);
405 volsetseq = ISO_SET_SEQ(unix_buf);
406 blksize = ISO_BLK_SIZE(unix_buf);
407 volsize = ISO_VOL_SIZE(unix_buf);
408 break;
409 default:
410 return;
412 /* system id */
413 prntstring("System id", sysid, 32);
414 /* read volume id */
415 prntstring("Volume id", volid, 32);
416 /* read volume set id */
417 prntstring("Volume set id", volsetid, 128);
418 /* publisher id */
419 prntstring("Publisher id", pubid, 128);
420 /* data preparer id */
421 prntstring("Data preparer id", prepid, 128);
422 /* application id */
423 prntstring("Application id", applid, 128);
424 /* copyright file identifier */
425 prntstring("Copyright File id", copyfile, 37);
426 /* Abstract file identifier */
427 prntstring("Abstract File id", absfile, 37);
428 /* Bibliographic file identifier */
429 prntstring("Bibliographic File id", bibfile, 37);
430 /* print volume set size */
431 printf("Volume set size is %d\n", volsetsize);
432 /* print volume set sequnce number */
433 printf("Volume set sequence number is %d\n", volsetseq);
434 /* print logical block size */
435 printf("Logical block size is %d\n", blksize);
436 /* print volume size */
437 printf("Volume size is %d\n", volsize);
440 static void
441 copystring(char *from, char *to, int size)
443 int i;
445 if (from == NULL)
446 return;
447 for (i = 0; i < size; i++) {
448 if (*from == '\0')
449 break;
450 else *to++ = *from++;
452 for (; i < size; i++) *to++ = ' ';
455 static void
456 prntstring(char *heading, char *s, int maxlen)
458 int i;
459 if (maxlen < 1)
460 return;
461 if (heading == NULL || s == NULL)
462 return;
463 /* print heading */
464 printf("%s: ", heading);
466 /* strip off trailing zeros */
467 for (i = maxlen-1; i >= 0; i--)
468 if (s[i] != ' ')
469 break;
471 maxlen = i+1;
472 for (i = 0; i < maxlen; i++)
473 printf("%c", s[i]);
474 printf("\n");
477 static int
478 match(char *s)
480 char *cs;
482 cs = string;
483 while (*cs++ == *s)
484 if (*s++ == '\0')
485 goto true;
486 if (*s != '\0')
487 return (0);
489 true:
490 cs--;
491 string = cs;
492 return (1);
495 /* readdisk - read from cdrom image file */
496 static void
497 getdisk(char *buf, int daddr, int size)
500 if (lseek(cdfd, daddr, L_SET) == -1) {
501 sprintf(errstrng, "%s: getdisk: lseek()", callname);
502 perror(errstrng);
503 exit(32);
505 if (read(cdfd, buf, size) != size) {
506 sprintf(errstrng, "%s: getdisk: read()", callname);
507 perror(errstrng);
508 exit(32);
512 /* putdisk - write to cdrom image file */
513 static void
514 putdisk(char *buf, int daddr, int size)
517 if (lseek(cdfd, daddr, L_SET) == -1) {
518 sprintf(errstrng, "%s: putdisk: lseek()", callname);
519 perror(errstrng);
520 exit(32);
522 if (write(cdfd, buf, size) != size) {
523 sprintf(errstrng, "%s: putdisk: write()", callname);
524 perror(errstrng);
525 exit(32);