xref: /netbsd-src/usr.sbin/mscdlabel/iso9660.c (revision fb8d4ae653e6a29b4dc8d7d2ce1989cf65e29c65)
1*fb8d4ae6Sdrochner /* $NetBSD: iso9660.c,v 1.2 2005/09/14 09:41:24 drochner Exp $ */
2f375ea19Sdrochner 
3f375ea19Sdrochner #include <sys/types.h>
4f375ea19Sdrochner #include <stdlib.h>
5f375ea19Sdrochner #include <unistd.h>
6f375ea19Sdrochner #include <string.h>
7f375ea19Sdrochner #include <stdio.h>
8f375ea19Sdrochner #include <err.h>
9f375ea19Sdrochner #include <isofs/cd9660/iso.h>
10f375ea19Sdrochner 
11f375ea19Sdrochner #include "mscdlabel.h"
12f375ea19Sdrochner 
13f375ea19Sdrochner #define BLKSIZ ISO_DEFAULT_BLOCK_SIZE
14f375ea19Sdrochner 
15f375ea19Sdrochner static void
printinfo(struct iso_primary_descriptor * vd)16f375ea19Sdrochner printinfo(struct iso_primary_descriptor *vd)
17f375ea19Sdrochner {
18f375ea19Sdrochner 	char label[32 + 1], date[] = "yyyy/mm/dd hh:mm", *d;
19f375ea19Sdrochner 
20f375ea19Sdrochner 	strlcpy(label, vd->volume_id, sizeof(label));
21*fb8d4ae6Sdrochner 	/* strip trailing blanks */
22*fb8d4ae6Sdrochner 	d = label + strlen(label);
23*fb8d4ae6Sdrochner 	while (d > label && *(d - 1) == ' ')
24*fb8d4ae6Sdrochner 		d--;
25*fb8d4ae6Sdrochner 	*d = '\0';
26*fb8d4ae6Sdrochner 
27f375ea19Sdrochner 	d = vd->creation_date;
28f375ea19Sdrochner 	memcpy(date, d, 4); /* year */
29f375ea19Sdrochner 	memcpy(date + 5, d + 4, 2); /* month */
30f375ea19Sdrochner 	memcpy(date + 8, d + 6, 2); /* day */
31f375ea19Sdrochner 	memcpy(date + 11, d + 8, 2); /* hour */
32f375ea19Sdrochner 	memcpy(date + 14, d + 10, 2); /* min */
33f375ea19Sdrochner 	printf("ISO filesystem, label \"%s\", creation time: %s\n",
34f375ea19Sdrochner 	       label, date);
35f375ea19Sdrochner }
36f375ea19Sdrochner 
37f375ea19Sdrochner int
check_primary_vd(int fd,int start,int len)38f375ea19Sdrochner check_primary_vd(int fd, int start, int len)
39f375ea19Sdrochner {
40f375ea19Sdrochner 	int i, res, isiso;
41f375ea19Sdrochner 	struct iso_primary_descriptor *vd;
42f375ea19Sdrochner 
43f375ea19Sdrochner 	isiso = 0;
44f375ea19Sdrochner 	vd = malloc(BLKSIZ);
45f375ea19Sdrochner 
46f375ea19Sdrochner 	for (i = 16; (i < 100) && (i < len); i++) {
47f375ea19Sdrochner 		res = pread(fd, vd, BLKSIZ, (start + i) * BLKSIZ);
48f375ea19Sdrochner 		if (res < 0) {
49f375ea19Sdrochner 			warn("read CD sector %d", start + i);
50f375ea19Sdrochner 			break;
51f375ea19Sdrochner 		}
52f375ea19Sdrochner 
53f375ea19Sdrochner 		if (memcmp(vd->id, ISO_STANDARD_ID, sizeof(vd->id)))
54f375ea19Sdrochner 			continue;
55f375ea19Sdrochner 		if (isonum_711(vd->type) == ISO_VD_PRIMARY) {
56f375ea19Sdrochner 			printinfo(vd);
57f375ea19Sdrochner 			isiso = 1;
58f375ea19Sdrochner 			break;
59f375ea19Sdrochner 		} else if (isonum_711(vd->type) == ISO_VD_END)
60f375ea19Sdrochner 			break;
61f375ea19Sdrochner 	}
62f375ea19Sdrochner 
63f375ea19Sdrochner 	free(vd);
64f375ea19Sdrochner 	return (isiso);
65f375ea19Sdrochner }
66