4 * isoread reads a file system in ISO9660 or HIGH SIERRA format from
7 * Apr 5 1995 Michel R. Prevenier
8 * Nov 16 1996 Kees J. Bot -- bug fix: isoread filename matching
9 * Dec 7 1997 Albert S. Woodhull -- bug fix: return values
10 * " " : isodir filename handling
11 * -- added : isoread -a option
12 * Mar 21 2000 Michael A. Temari -- bug fix: look_up only searched first
13 * : block of directory
14 * : stack overflow in recurse_dir
15 * : and various other bugs
16 * Apr 14 2002 Michael A. Temari -- bug fix: fixed recursing directories
17 * : and printing dates 2000 and
19 * May 14 2002 Kees J. Bot -- bug fix: fixed error messages
20 * Mar 14 2003 Kees J. Bot -- added : iso{dir,read} -B option
21 * Jul 24 2003 Michael A. Temari -- bug fix: bytes to blocks roundup fix
27 #include <sys/types.h>
34 #include <sys/times.h>
38 * definitions used by the ISO9660 and HIGH SIERRA file system
41 #define ISO9660_ID "CD001"
42 #define HIGH_SIERRA_ID "CDROM"
43 #define BLOCK_SIZE 2048
44 #define BLOCK_SHIFT 11
47 /* Fields in a ISO9660 volume descriptor */
48 struct iso9660_descriptor
59 char volume_set_size
[4];
60 char volume_seq_nr
[4];
62 char path_table_size
[8];
63 char type_l_path_table
[4];
64 char opt_type_l_path_table
[4];
65 char type_m_path_table
[4];
66 char opt_type_m_path_table
[4];
67 char root_dir_entry
[34];
72 char copyright_file_id
[37];
73 char abstract_file_id
[37];
74 char bibl_file_id
[37];
75 char creation_date
[17];
79 char file_struc_version
[1];
86 /* Fields in a High Sierra volume descriptor */
87 struct high_sierra_descriptor
100 char volume_seq_nr
[4];
102 char path_table_size
[8];
103 char type_l_path_table
[4];
105 char root_dir_entry
[34];
109 /* Fields in a directory entry */
113 char ext_attr_length
[1];
118 char file_unit_size
[1];
120 char volume_seq_nr
[4];
126 #define STDOUT stdout
127 #define STDERR stderr
128 #define NULL_DIR (struct dir_entry *) 0
129 #define MAX_NAME_LENGTH 255
130 #define MAX_PATH_LENGTH 1024
132 #define NR_OF_CHARS 13
133 #define NR_OF_BLANKS 2
134 #define NR_OF_COLS (80 / (NR_OF_CHARS + NR_OF_BLANKS))
136 /* This macro always returns a lower case character */
137 #define LOWER_CASE(CHR) (CHR >= 'A' && CHR <= 'Z' ? CHR | 0x20 : CHR)
139 /* Macro's for determining . , .. and normal directory entries */
140 #define IS_DOT(PTR) (PTR->name_length[0] == 1 && PTR->name[0] == 0 ? 1 : 0)
141 #define IS_DOT_DOT(PTR) (PTR->name_length[0] == 1 && PTR->name[0] == 1 ? 1 : 0)
142 #define IS_DIR(PTR) (PTR->flags[-High_Sierra] & 2 ? 1 : 0)
145 int main(int argc
, char **argv
);
146 int iso_cmp(char *name
, struct dir_entry
*dir_ptr
, int dir_flag
);
147 void list_dir(struct dir_entry
*dir_ptr
);
148 void list_file(struct dir_entry
*dir_ptr
);
149 struct dir_entry
*look_up(char *name
);
150 void recurse_dir(char *path
, struct dir_entry
*dir_ptr
);
151 void read_device(long offset
, int nr_of_bytes
, char *buffer
);
154 void print_date(char *date
);
155 void print_dir_date(char *date
);
156 void iso_info(struct iso9660_descriptor
*vol_desc
);
157 void hs_info(struct high_sierra_descriptor
*vol_desc
);
158 int iso_711(char *c
);
159 int iso_712(char *c
);
160 int iso_721(char *c
);
161 int iso_722(char *c
);
162 int iso_723(char *c
);
163 long iso_731(char *c
);
164 long iso_732(char *c
);
165 long iso_733(char *c
);
168 char Buffer
[BLOCK_SIZE
]; /* buffer to hold read data */
169 int Device
; /* global file descriptor */
170 struct iso9660_descriptor
*Iso_Vol_Desc
; /* iso9660 volume descriptor */
171 struct high_sierra_descriptor
*Hs_Vol_Desc
; /* high sierra volume descriptor */
172 int High_Sierra
= 0; /* 1 = high sierra format */
173 int Iso9660
= 0; /* 1 = iso9660 format */
175 /* This comes in handy when printing the date */
176 char months
[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
178 /* Flags displaying what to do */
179 int Read_File
= 0; /* 1 = Read file */
180 int Read_Dir
= 0; /* 1 = Read directory entry */
181 int Read_Info
= 0; /* 1 = Read volume descriptor */
182 int Recurse
= 0; /* 1 = Recursively descend directories */
183 int Verbose
= 0; /* 1 = Print all info on directories */
184 int ByteOffset
= 0; /* 1 = Print byte offset and length of files */
185 int Aflag
= 0; /* 1 = Suppress output of \r */
187 int iso_cmp(name
, dir_ptr
, dir_flag
)
189 struct dir_entry
*dir_ptr
;
192 /* Compare name with directory entries, looking for match with a dirname.
193 * An iso9660 filename is terminated by ";n", where n will probably
194 * be 1. A directory name is not terminated by anything special, it may be
195 * followed by a \0 if padding is needed to put the following directory
196 * entry on an even address.
201 /* First match the filename */
203 if (len
> iso_711(dir_ptr
->name_length
)) return 1;
204 for (i
= 0; i
< len
; i
++)
206 if (dir_ptr
->name
[i
] == ';') return 1; /* found end of a filename */
207 if (name
[i
] != LOWER_CASE(dir_ptr
->name
[i
])) return 1; /* match failed */
209 if (dir_ptr
->name
[i
] != ';' && i
!= len
) return 1; /* incomplete match */
211 /* The filename is ok, now look at the file type */
212 if (dir_flag
&& !IS_DIR(dir_ptr
)) return 1; /* File type not correct */
221 fprintf (STDERR
, "Usage: isodir [-lrB] inputfile [dir]\n");
223 fprintf (STDERR
, "Usage: isoinfo inputfile\n");
225 fprintf (STDERR
, "Usage: isoread [-a] inputfile file\n");
234 struct dir_entry
*entry
;
235 char path
[MAX_PATH_LENGTH
];
243 while (*argv
[0] != '\0')
244 if (*argv
[0]++ == '/') basename
= argv
[0];
246 if (strcmp(basename
,"isodir") == 0) Read_Dir
= 1;
247 else if (strcmp(basename
,"isoinfo") == 0) Read_Info
= 1;
250 if ((argc
> 5 && Read_Dir
) || (argc
!= 2 && Read_Info
) ||
251 (argc
> 4 && Read_File
)) usage();
255 while (i
< argc
&& argv
[i
][0] == '-')
257 char *opt
= argv
[i
++] + 1;
259 if (opt
[0] == '-' && opt
[1] == '\0') break;
263 if (Read_Info
) usage();
267 case 'r': Recurse
= 1; break;
268 case 'l': Verbose
= 1; break;
269 case 'B': ByteOffset
= 1; break;
275 case 'a': Aflag
= 1; break;
276 case 'B': ByteOffset
= 1; break;
282 if (i
>= argc
) usage();
283 input_file
= argv
[i
++];
287 if (i
>= argc
) usage();
288 file_name
= argv
[i
++];
296 file_name
= argv
[i
++];
300 if (i
< argc
) usage();
302 if (Read_File
|| Read_Dir
)
304 for (i
=0; file_name
[i
] != '\0'; i
++)
305 path
[i
] = LOWER_CASE(file_name
[i
]);
309 /* Open file system (file or device) */
310 if ((Device
= open(input_file
, O_RDONLY
)) < 0)
312 fprintf (STDERR
, "cannot open %s: %s\n", input_file
, strerror(errno
));
319 fprintf (STDERR
, "File system not in ISO9660 or HIGH SIERRA format \n");
327 iso_info(Iso_Vol_Desc
);
329 hs_info(Hs_Vol_Desc
);
334 if ((entry
= look_up(path
)) != NULL_DIR
)
337 if (Recurse
) recurse_dir(path
,entry
);
338 else list_dir(entry
);
345 fprintf (STDERR
, "Directory");
347 fprintf (STDERR
, "File");
348 fprintf (STDERR
, " %s not found\n", path
);
355 struct dir_entry
*look_up(path
)
358 /* Lookup a file name */
360 struct dir_entry
*dir_ptr
;
364 char name
[MAX_NAME_LENGTH
+ 1];
366 int last_in_path
= 0;
370 /* Get the right dir entry structure */
372 dir_ptr
= (struct dir_entry
*) Iso_Vol_Desc
->root_dir_entry
;
374 dir_ptr
= (struct dir_entry
*) Hs_Vol_Desc
->root_dir_entry
;
376 /* If we look for the root we already have the right entry */
378 if (strlen(path
) == 1) return dir_ptr
;
379 else name_index
= 1; /* first name in path */
381 /* Keep searching for the path elements until all are found */
382 while (!last_in_path
)
384 /* Get next name in path */
385 for (i
= name_index
; i
< strlen(path
); i
++)
387 if (path
[i
] == '/') break;
388 name
[i
- name_index
] = path
[i
];
391 (i
== strlen(path
) || (i
== strlen(path
) - 1 && path
[i
] == '/'));
392 name
[i
-name_index
] = '\0';
395 /* Get block of next directory */
396 block
= iso_733(dir_ptr
->first_block
) + iso_711(dir_ptr
->ext_attr_length
);
397 nr_of_blocks
= (iso_733(dir_ptr
->size
) + (BLOCK_SIZE
-1)) >> BLOCK_SHIFT
;
399 /* Search for file in dir entry */
401 for (j
=0; j
< nr_of_blocks
&& !found
; j
++)
403 /* Read a directory block */
404 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
407 dir_ptr
= (struct dir_entry
*) Buffer
;
410 /* Compare with all entries in this block */
411 while (iso_711(dir_ptr
->length
) > 0 && offset
< BLOCK_SIZE
)
413 if (iso_cmp(name
, dir_ptr
,
414 (Read_Dir
|| (!Read_Dir
&& !last_in_path
))) == 0)
420 offset
+= iso_711(dir_ptr
->length
);
421 dir_ptr
= (struct dir_entry
*) (Buffer
+ offset
);
424 if (!found
) return NULL_DIR
; /* path element not found */
430 void recurse_dir(path
, dir_ptr
)
432 struct dir_entry
*dir_ptr
;
434 /* Recursively descend all directories starting with dir_ptr */
436 char tmp_path
[MAX_PATH_LENGTH
];
437 int i
,j
, path_length
;
438 long block
, saveblock
, dblock
;
443 /* Save block number and nr of blocks of current dir entry because
444 * list_dir changes dir_ptr
446 block
= iso_733(dir_ptr
->first_block
) + iso_711(dir_ptr
->ext_attr_length
);
447 nr_of_blocks
= (iso_733(dir_ptr
->size
) + (BLOCK_SIZE
-1)) >> BLOCK_SHIFT
;
449 /* Add a trailing / to path if necessary */
450 path_length
= strlen(path
);
451 if (path
[path_length
-1] != '/')
453 path
[path_length
++] = '/';
454 path
[path_length
] = '\0';
457 /* Print current path of directory, and list contents of directory */
458 fprintf(STDOUT
,"directory %s:\n\n", path
);
460 fprintf(STDOUT
,"\n\n");
462 for (j
=0; j
< nr_of_blocks
; j
++)
464 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
467 /* Save buffer, because the next recursive call destroys
470 dir_ptr
= (struct dir_entry
*) Buffer
;
472 /* Search this dir entry for directories */
474 while (iso_711(dir_ptr
->length
) != 0 && offset
< BLOCK_SIZE
)
476 /* Is current file a directory and not the . or .. entries */
477 if (IS_DIR(dir_ptr
) && !IS_DOT(dir_ptr
) && !IS_DOT_DOT(dir_ptr
))
479 /* setup path for next recursive call */
480 for (i
=0; i
<path_length
; i
++) tmp_path
[i
] = path
[i
];
481 for (i
=0;i
<iso_711(dir_ptr
->name_length
) && dir_ptr
->name
[i
] != ';';i
++)
482 tmp_path
[i
+path_length
] = LOWER_CASE(dir_ptr
->name
[i
]);
483 tmp_path
[i
+path_length
] = '/';
484 tmp_path
[i
+1+path_length
] = '\0';
486 /* Read block of directory we found */
487 dblock
= iso_733(dir_ptr
->first_block
);
488 read_device(dblock
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
490 /* And start all over again with this entry */
491 recurse_dir(tmp_path
, (struct dir_entry
*) Buffer
);
493 /* get the block we were looking at */
494 read_device(saveblock
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
497 /* Go to the next file in this directory */
498 offset
+= iso_711(dir_ptr
->length
);
499 dir_ptr
= (struct dir_entry
*) (Buffer
+ offset
);
505 void list_dir(dir_ptr
)
506 struct dir_entry
*dir_ptr
;
508 /* List all entries in a directory */
514 char name
[NR_OF_CHARS
+NR_OF_BLANKS
+1];
519 tty
= isatty(STDOUT_FILENO
);
520 /* Get first block of directory */
521 block
= iso_733(dir_ptr
->first_block
) + iso_711(dir_ptr
->ext_attr_length
);
522 nr_of_blocks
= (iso_733(dir_ptr
->size
) + (BLOCK_SIZE
-1)) >> BLOCK_SHIFT
;
524 /* Read all directory blocks and display their contents */
525 for (j
=0; j
< nr_of_blocks
; j
++)
527 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
530 dir_ptr
= (struct dir_entry
*) (Buffer
);
532 while (iso_711(dir_ptr
->length
) != 0 && offset
< BLOCK_SIZE
)
537 name
[name_len
++] = '.';
538 if (!Verbose
) skip
= 1;
542 if (IS_DOT_DOT(dir_ptr
))
544 name
[name_len
++] = '.';
545 name
[name_len
++] = '.';
546 if (!Verbose
) skip
= 1;
550 for (i
=0; i
<iso_711(dir_ptr
->name_length
) &&
553 if (dir_ptr
->name
[i
] == ';') break;
554 name
[name_len
++] = LOWER_CASE(dir_ptr
->name
[i
]);
556 if (IS_DIR(dir_ptr
) && tty
) name
[name_len
++] = '/';
563 fprintf (STDOUT
, "%10ld ",
564 (iso_733(dir_ptr
->first_block
) + iso_711(dir_ptr
->ext_attr_length
))
567 if (Verbose
|| ByteOffset
)
569 fprintf (STDOUT
, "%10ld ", iso_733(dir_ptr
->size
));
573 print_dir_date(dir_ptr
->date
);
574 fprintf (STDOUT
, " ");
577 name
[name_len
] = '\0';
579 for(i
=name_len
; i
<(NR_OF_CHARS
+NR_OF_BLANKS
); i
++) name
[i
] = ' ';
580 name
[NR_OF_CHARS
+NR_OF_BLANKS
] = '\0';
582 fprintf(STDOUT
, "%s", name
);
583 if (!(Verbose
|| ByteOffset
))
586 if (column
>= NR_OF_COLS
|| !tty
)
589 fprintf(STDOUT
,"\n");
592 else fprintf(STDOUT
,"\n");
595 offset
+= iso_711(dir_ptr
->length
);
596 dir_ptr
= (struct dir_entry
*) (Buffer
+offset
);
599 if (!Verbose
&& column
) fprintf(STDOUT
,"\n");
603 void print_dir_date(date
)
606 /* Print date in a directory entry */
610 m
= iso_711(&date
[1]) - 1;
612 fprintf(STDOUT
, " ");
614 fprintf(STDOUT
,"%.3s",&months
[m
*3]);
616 fprintf (STDOUT
, " %02d %04d %02d:%02d:%02d",
625 void list_file(dir_ptr
)
626 struct dir_entry
*dir_ptr
;
628 /* List contents of a file */
635 block
= iso_733(dir_ptr
->first_block
);
636 size
= iso_733(dir_ptr
->size
);
639 fprintf(STDOUT
, "%ld %ld\n", block
*BLOCK_SIZE
, size
);
645 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
646 for (i
=0; ((i
< size
) && (i
< BLOCK_SIZE
)); i
++)
647 if (Buffer
[i
] != '\r') fprintf(STDOUT
, "%c", Buffer
[i
]);
651 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
652 for (i
=0; ((i
< size
) && (i
< BLOCK_SIZE
)); i
++)
653 fprintf(STDOUT
, "%c", Buffer
[i
]);
660 void print_date(date
)
663 /* Print the date in a volume descriptor */
665 fprintf (STDOUT
, "%c%c-%c%c-%c%c%c%c %c%c:%c%c:%c%c",
682 void iso_info(vol_desc
)
683 struct iso9660_descriptor
*vol_desc
;
687 fprintf (STDOUT
, "Format: ISO9660 \n");
688 fprintf (STDOUT
, "System id: ");
689 for (i
=0; i
< sizeof(vol_desc
->system_id
); i
++)
690 fprintf(STDOUT
, "%c", vol_desc
->system_id
[i
]);
691 fprintf (STDOUT
, "\n");
692 fprintf (STDOUT
, "Volume id: ");
693 for (i
=0; i
< sizeof(vol_desc
->volume_id
); i
++)
694 fprintf(STDOUT
, "%c", vol_desc
->volume_id
[i
]);
695 fprintf (STDOUT
, "\n");
696 fprintf (STDOUT
, "Volume size: %ld Kb\n", iso_733(vol_desc
->volume_size
)*2);
697 fprintf (STDOUT
, "Block size: %d bytes \n", iso_723(vol_desc
->block_size
));
698 fprintf (STDOUT
, "Creation date: ");
699 print_date(vol_desc
->creation_date
);
700 fprintf(STDOUT
, "\n");
701 fprintf (STDOUT
, "Modification date: ");
702 print_date(vol_desc
->mod_date
);
703 fprintf (STDOUT
, "\n");
704 fprintf (STDOUT
, "Expiration date: ");
705 print_date(vol_desc
->exp_date
);
706 fprintf (STDOUT
, "\n");
707 fprintf (STDOUT
, "Effective date: ");
708 print_date(vol_desc
->eff_date
);
709 fprintf (STDOUT
, "\n");
713 void hs_info(vol_desc
)
714 struct high_sierra_descriptor
*vol_desc
;
718 fprintf (STDOUT
, "Format: HIGH SIERRA \n");
719 fprintf (STDOUT
, "System id: ");
720 for (i
=0; i
< sizeof(vol_desc
->system_id
); i
++)
721 fprintf(STDOUT
, "%c", vol_desc
->system_id
[i
]);
722 fprintf (STDOUT
, "\n");
723 fprintf (STDOUT
, "Volume id: ");
724 for (i
=0; i
< sizeof(vol_desc
->volume_id
); i
++)
725 fprintf(STDOUT
, "%c", vol_desc
->volume_id
[i
]);
726 fprintf (STDOUT
, "\n");
727 fprintf (STDOUT
, "Volume size: %ld Kb\n", (iso_733(vol_desc
->volume_size
)*2));
728 fprintf (STDOUT
, "Block size: %d bytes \n", iso_723(vol_desc
->block_size
));
737 /* search for a volume descriptor */
738 for (i
=16; i
<100; i
++)
741 read_device((long)(i
)*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
743 Iso_Vol_Desc
= (struct iso9660_descriptor
*) Buffer
;
744 Hs_Vol_Desc
= (struct high_sierra_descriptor
*) Buffer
;
746 if (strncmp(Iso_Vol_Desc
->id
, ISO9660_ID
, sizeof Iso_Vol_Desc
->id
) == 0)
748 /* iso_info(Iso_Vol_Desc); */
753 if (strncmp(Hs_Vol_Desc
->id
, HIGH_SIERRA_ID
, sizeof Hs_Vol_Desc
->id
) == 0)
755 /* hs_info(Hs_Vol_Desc); */
761 if (i
>= 100) return 0;
766 void read_device(offset
, nr_of_bytes
, buffer
)
773 if (lseek(Device
, offset
, SEEK_SET
) == -1)
776 fprintf (STDERR
, "seek error: %s\n", strerror(errno
));
780 bytes_read
= read(Device
, buffer
, nr_of_bytes
);
781 if (bytes_read
!= nr_of_bytes
)
783 fprintf (STDERR
, "read error: %s\n",
784 bytes_read
>= 0 ? "Short read" : strerror(errno
));
790 /* The ISO9660 functions */
805 if (n
& 0x80) n
|= 0xffffff00;
812 return ((c
[0] & 0xff) | ((c
[1] & 0xff) << 8));
818 return (((c
[0] & 0xff) << 8) | (c
[1] & 0xff));
824 if (c
[0] != c
[3] || c
[1] != c
[2])
826 fprintf (STDERR
, "Invalid ISO 7.2.3 number\n");
829 return (iso_721 (c
));
835 return ((long)(c
[0] & 0xff)
836 | ((long)(c
[1] & 0xff) << 8)
837 | ((long)(c
[2] & 0xff) << 16)
838 | ((long)(c
[3] & 0xff) << 24));
845 return (((long)(c
[0] & 0xff) << 24)
846 | (((long)c
[1] & 0xff) << 16)
847 | (((long)c
[2] & 0xff) << 8)
848 | ((long)c
[3] & 0xff));
856 for (i
= 0; i
< 4; i
++)
860 fprintf (STDERR
, "Invalid ISO 7.3.3 number\n");