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 */
377 if (path
[0] == '/') {
378 if (strlen(path
) == 1) return dir_ptr
;
379 else name_index
= 1; /* first name in path */
382 /* Keep searching for the path elements until all are found */
383 while (!last_in_path
)
385 /* Get next name in path */
386 for (i
= name_index
; i
< strlen(path
); i
++)
388 if (path
[i
] == '/') break;
389 name
[i
- name_index
] = path
[i
];
392 (i
== strlen(path
) || (i
== strlen(path
) - 1 && path
[i
] == '/'));
393 name
[i
-name_index
] = '\0';
396 /* Get block of next directory */
397 block
= iso_733(dir_ptr
->first_block
) + iso_711(dir_ptr
->ext_attr_length
);
398 nr_of_blocks
= (iso_733(dir_ptr
->size
) + (BLOCK_SIZE
-1)) >> BLOCK_SHIFT
;
400 /* Search for file in dir entry */
402 for (j
=0; j
< nr_of_blocks
&& !found
; j
++)
404 /* Read a directory block */
405 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
408 dir_ptr
= (struct dir_entry
*) Buffer
;
411 /* Compare with all entries in this block */
412 while (iso_711(dir_ptr
->length
) > 0 && offset
< BLOCK_SIZE
)
414 if (iso_cmp(name
, dir_ptr
,
415 (Read_Dir
|| !last_in_path
)) == 0)
421 offset
+= iso_711(dir_ptr
->length
);
422 dir_ptr
= (struct dir_entry
*) (Buffer
+ offset
);
425 if (!found
) return NULL_DIR
; /* path element not found */
431 void recurse_dir(path
, dir_ptr
)
433 struct dir_entry
*dir_ptr
;
435 /* Recursively descend all directories starting with dir_ptr */
437 char tmp_path
[MAX_PATH_LENGTH
];
438 int i
,j
, path_length
;
439 long block
, saveblock
, dblock
;
444 /* Save block number and nr of blocks of current dir entry because
445 * list_dir changes dir_ptr
447 block
= iso_733(dir_ptr
->first_block
) + iso_711(dir_ptr
->ext_attr_length
);
448 nr_of_blocks
= (iso_733(dir_ptr
->size
) + (BLOCK_SIZE
-1)) >> BLOCK_SHIFT
;
450 /* Add a trailing / to path if necessary */
451 path_length
= strlen(path
);
452 if (path
[path_length
-1] != '/')
454 path
[path_length
++] = '/';
455 path
[path_length
] = '\0';
458 /* Print current path of directory, and list contents of directory */
459 fprintf(STDOUT
,"directory %s:\n\n", path
);
461 fprintf(STDOUT
,"\n\n");
463 for (j
=0; j
< nr_of_blocks
; j
++)
465 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
468 /* Save buffer, because the next recursive call destroys
471 dir_ptr
= (struct dir_entry
*) Buffer
;
473 /* Search this dir entry for directories */
475 while (iso_711(dir_ptr
->length
) != 0 && offset
< BLOCK_SIZE
)
477 /* Is current file a directory and not the . or .. entries */
478 if (IS_DIR(dir_ptr
) && !IS_DOT(dir_ptr
) && !IS_DOT_DOT(dir_ptr
))
480 /* setup path for next recursive call */
481 for (i
=0; i
<path_length
; i
++) tmp_path
[i
] = path
[i
];
482 for (i
=0;i
<iso_711(dir_ptr
->name_length
) && dir_ptr
->name
[i
] != ';';i
++)
483 tmp_path
[i
+path_length
] = LOWER_CASE(dir_ptr
->name
[i
]);
484 tmp_path
[i
+path_length
] = '/';
485 tmp_path
[i
+1+path_length
] = '\0';
487 /* Read block of directory we found */
488 dblock
= iso_733(dir_ptr
->first_block
);
489 read_device(dblock
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
491 /* And start all over again with this entry */
492 recurse_dir(tmp_path
, (struct dir_entry
*) Buffer
);
494 /* get the block we were looking at */
495 read_device(saveblock
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
498 /* Go to the next file in this directory */
499 offset
+= iso_711(dir_ptr
->length
);
500 dir_ptr
= (struct dir_entry
*) (Buffer
+ offset
);
506 void list_dir(dir_ptr
)
507 struct dir_entry
*dir_ptr
;
509 /* List all entries in a directory */
515 char name
[NR_OF_CHARS
+NR_OF_BLANKS
+1];
520 tty
= isatty(STDOUT_FILENO
);
521 /* Get first block of directory */
522 block
= iso_733(dir_ptr
->first_block
) + iso_711(dir_ptr
->ext_attr_length
);
523 nr_of_blocks
= (iso_733(dir_ptr
->size
) + (BLOCK_SIZE
-1)) >> BLOCK_SHIFT
;
525 /* Read all directory blocks and display their contents */
526 for (j
=0; j
< nr_of_blocks
; j
++)
528 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
531 dir_ptr
= (struct dir_entry
*) (Buffer
);
533 while (iso_711(dir_ptr
->length
) != 0 && offset
< BLOCK_SIZE
)
538 name
[name_len
++] = '.';
539 if (!Verbose
) skip
= 1;
543 if (IS_DOT_DOT(dir_ptr
))
545 name
[name_len
++] = '.';
546 name
[name_len
++] = '.';
547 if (!Verbose
) skip
= 1;
551 for (i
=0; i
<iso_711(dir_ptr
->name_length
) &&
554 if (dir_ptr
->name
[i
] == ';') break;
555 name
[name_len
++] = LOWER_CASE(dir_ptr
->name
[i
]);
557 if (IS_DIR(dir_ptr
) && tty
) name
[name_len
++] = '/';
564 fprintf (STDOUT
, "%10ld ",
565 (iso_733(dir_ptr
->first_block
) + iso_711(dir_ptr
->ext_attr_length
))
568 if (Verbose
|| ByteOffset
)
570 fprintf (STDOUT
, "%10ld ", iso_733(dir_ptr
->size
));
574 print_dir_date(dir_ptr
->date
);
575 fprintf (STDOUT
, " ");
578 name
[name_len
] = '\0';
580 for(i
=name_len
; i
<(NR_OF_CHARS
+NR_OF_BLANKS
); i
++) name
[i
] = ' ';
581 name
[NR_OF_CHARS
+NR_OF_BLANKS
] = '\0';
583 fprintf(STDOUT
, "%s", name
);
584 if (!(Verbose
|| ByteOffset
))
587 if (column
>= NR_OF_COLS
|| !tty
)
590 fprintf(STDOUT
,"\n");
593 else fprintf(STDOUT
,"\n");
596 offset
+= iso_711(dir_ptr
->length
);
597 dir_ptr
= (struct dir_entry
*) (Buffer
+offset
);
600 if (!Verbose
&& column
) fprintf(STDOUT
,"\n");
604 void print_dir_date(date
)
607 /* Print date in a directory entry */
611 m
= iso_711(&date
[1]) - 1;
613 fprintf(STDOUT
, " ");
615 fprintf(STDOUT
,"%.3s",&months
[m
*3]);
617 fprintf (STDOUT
, " %02d %04d %02d:%02d:%02d",
626 void list_file(dir_ptr
)
627 struct dir_entry
*dir_ptr
;
629 /* List contents of a file */
636 block
= iso_733(dir_ptr
->first_block
);
637 size
= iso_733(dir_ptr
->size
);
640 fprintf(STDOUT
, "%ld %ld\n", block
*BLOCK_SIZE
, size
);
646 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
647 for (i
=0; ((i
< size
) && (i
< BLOCK_SIZE
)); i
++)
648 if (Buffer
[i
] != '\r') fprintf(STDOUT
, "%c", Buffer
[i
]);
652 read_device(block
*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
653 for (i
=0; ((i
< size
) && (i
< BLOCK_SIZE
)); i
++)
654 fprintf(STDOUT
, "%c", Buffer
[i
]);
661 void print_date(date
)
664 /* Print the date in a volume descriptor */
666 fprintf (STDOUT
, "%c%c-%c%c-%c%c%c%c %c%c:%c%c:%c%c",
683 void iso_info(vol_desc
)
684 struct iso9660_descriptor
*vol_desc
;
688 fprintf (STDOUT
, "Format: ISO9660 \n");
689 fprintf (STDOUT
, "System id: ");
690 for (i
=0; i
< sizeof(vol_desc
->system_id
); i
++)
691 fprintf(STDOUT
, "%c", vol_desc
->system_id
[i
]);
692 fprintf (STDOUT
, "\n");
693 fprintf (STDOUT
, "Volume id: ");
694 for (i
=0; i
< sizeof(vol_desc
->volume_id
); i
++)
695 fprintf(STDOUT
, "%c", vol_desc
->volume_id
[i
]);
696 fprintf (STDOUT
, "\n");
697 fprintf (STDOUT
, "Volume size: %ld Kb\n", iso_733(vol_desc
->volume_size
)*2);
698 fprintf (STDOUT
, "Block size: %d bytes \n", iso_723(vol_desc
->block_size
));
699 fprintf (STDOUT
, "Creation date: ");
700 print_date(vol_desc
->creation_date
);
701 fprintf(STDOUT
, "\n");
702 fprintf (STDOUT
, "Modification date: ");
703 print_date(vol_desc
->mod_date
);
704 fprintf (STDOUT
, "\n");
705 fprintf (STDOUT
, "Expiration date: ");
706 print_date(vol_desc
->exp_date
);
707 fprintf (STDOUT
, "\n");
708 fprintf (STDOUT
, "Effective date: ");
709 print_date(vol_desc
->eff_date
);
710 fprintf (STDOUT
, "\n");
714 void hs_info(vol_desc
)
715 struct high_sierra_descriptor
*vol_desc
;
719 fprintf (STDOUT
, "Format: HIGH SIERRA \n");
720 fprintf (STDOUT
, "System id: ");
721 for (i
=0; i
< sizeof(vol_desc
->system_id
); i
++)
722 fprintf(STDOUT
, "%c", vol_desc
->system_id
[i
]);
723 fprintf (STDOUT
, "\n");
724 fprintf (STDOUT
, "Volume id: ");
725 for (i
=0; i
< sizeof(vol_desc
->volume_id
); i
++)
726 fprintf(STDOUT
, "%c", vol_desc
->volume_id
[i
]);
727 fprintf (STDOUT
, "\n");
728 fprintf (STDOUT
, "Volume size: %ld Kb\n", (iso_733(vol_desc
->volume_size
)*2));
729 fprintf (STDOUT
, "Block size: %d bytes \n", iso_723(vol_desc
->block_size
));
738 /* search for a volume descriptor */
739 for (i
=16; i
<100; i
++)
742 read_device((long)(i
)*BLOCK_SIZE
, BLOCK_SIZE
, Buffer
);
744 Iso_Vol_Desc
= (struct iso9660_descriptor
*) Buffer
;
745 Hs_Vol_Desc
= (struct high_sierra_descriptor
*) Buffer
;
747 if (strncmp(Iso_Vol_Desc
->id
, ISO9660_ID
, sizeof Iso_Vol_Desc
->id
) == 0)
749 /* iso_info(Iso_Vol_Desc); */
754 if (strncmp(Hs_Vol_Desc
->id
, HIGH_SIERRA_ID
, sizeof Hs_Vol_Desc
->id
) == 0)
756 /* hs_info(Hs_Vol_Desc); */
762 if (i
>= 100) return 0;
767 void read_device(offset
, nr_of_bytes
, buffer
)
774 if (lseek(Device
, offset
, SEEK_SET
) == -1)
777 fprintf (STDERR
, "seek error: %s\n", strerror(errno
));
781 bytes_read
= read(Device
, buffer
, nr_of_bytes
);
782 if (bytes_read
!= nr_of_bytes
)
784 fprintf (STDERR
, "read error: %s\n",
785 bytes_read
>= 0 ? "Short read" : strerror(errno
));
791 /* The ISO9660 functions */
806 if (n
& 0x80) n
|= 0xffffff00;
813 return ((c
[0] & 0xff) | ((c
[1] & 0xff) << 8));
819 return (((c
[0] & 0xff) << 8) | (c
[1] & 0xff));
825 if (c
[0] != c
[3] || c
[1] != c
[2])
827 fprintf (STDERR
, "Invalid ISO 7.2.3 number\n");
830 return (iso_721 (c
));
836 return ((long)(c
[0] & 0xff)
837 | ((long)(c
[1] & 0xff) << 8)
838 | ((long)(c
[2] & 0xff) << 16)
839 | ((long)(c
[3] & 0xff) << 24));
846 return (((long)(c
[0] & 0xff) << 24)
847 | (((long)c
[1] & 0xff) << 16)
848 | (((long)c
[2] & 0xff) << 8)
849 | ((long)c
[3] & 0xff));
857 for (i
= 0; i
< 4; i
++)
861 fprintf (STDERR
, "Invalid ISO 7.3.3 number\n");