From c2d5b7e0fe1009f308f4136900c2154660258834 Mon Sep 17 00:00:00 2001 From: rocky Date: Thu, 2 Mar 2006 18:46:30 +0000 Subject: [PATCH] Simplify code. Add usage. --- example/C++/iso2.cpp | 61 ++++++++++++++++++++++++++------------------ example/C++/iso3.cpp | 56 +++++++++++++++++++++++++++------------- example/iso2.c | 57 ++++++++++++++++++++++++----------------- example/iso3.c | 34 ++++++++++++++++-------- 4 files changed, 131 insertions(+), 77 deletions(-) diff --git a/example/C++/iso2.cpp b/example/C++/iso2.cpp index e8b8e365..1ec2fa48 100644 --- a/example/C++/iso2.cpp +++ b/example/C++/iso2.cpp @@ -1,7 +1,7 @@ /* - $Id: iso2.cpp,v 1.6 2005/11/10 11:22:55 rocky Exp $ + $Id: iso2.cpp,v 1.7 2006/03/02 18:46:30 rocky Exp $ - Copyright (C) 2003, 2004, 2005 Rocky Bernstein + Copyright (C) 2003, 2004, 2005, 2006 Rocky Bernstein This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,7 +19,7 @@ */ /* Simple program to show using libiso9660 to extract a file from a - cue/bin CD-IMAGE. + CUE/BIN CD image. If a single argument is given, it is used as the CUE file of a CD image to use. Otherwise a compiled-in default image name (that @@ -69,6 +69,8 @@ #include #endif +#define CEILING(x, y) ((x+(y-1))/y) + #define my_exit(rc) \ fclose (p_outfd); \ free(p_statbuf); \ @@ -88,6 +90,12 @@ main(int argc, const char *argv[]) char untranslated_name[256] = ISO9660_PATH; CdIo_t *p_cdio; + if (argc > 3) { + printf("usage %s [CD-ROM-or-image [filename]]\n", argv[0]); + printf("Extracts filename from CD-ROM-or-image.\n"); + return 1; + } + if (argc > 1) psz_image = argv[1]; else @@ -128,30 +136,33 @@ main(int argc, const char *argv[]) } /* Copy the blocks from the ISO-9660 filesystem to the local filesystem. */ - for (i = 0; i < p_statbuf->size; i += ISO_BLOCKSIZE) - { - char buf[ISO_BLOCKSIZE]; - - memset (buf, 0, ISO_BLOCKSIZE); - - if ( 0 != cdio_read_data_sectors (p_cdio, buf, - p_statbuf->lsn + (i / ISO_BLOCKSIZE), - ISO_BLOCKSIZE, 1) ) + { + const unsigned int i_blocks = CEILING(p_statbuf->size, ISO_BLOCKSIZE); + for (i = 0; i < i_blocks; i ++) { - fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n", - (long unsigned int) p_statbuf->lsn + (i / ISO_BLOCKSIZE)); - my_exit(4); + char buf[ISO_BLOCKSIZE]; + const lsn_t lsn = p_statbuf->lsn + i; + + memset (buf, 0, ISO_BLOCKSIZE); + + if ( 0 != cdio_read_data_sectors (p_cdio, buf, lsn, ISO_BLOCKSIZE, 1) ) + { + fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n", + (long unsigned int) p_statbuf->lsn); + my_exit(4); + } + + + fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd); + + if (ferror (p_outfd)) + { + perror ("fwrite()"); + my_exit(5); + } } - - - fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd); - - if (ferror (p_outfd)) - { - perror ("fwrite()"); - my_exit(5); - } - } + } + fflush (p_outfd); diff --git a/example/C++/iso3.cpp b/example/C++/iso3.cpp index 195f558e..341ba083 100644 --- a/example/C++/iso3.cpp +++ b/example/C++/iso3.cpp @@ -1,7 +1,7 @@ /* - $Id: iso3.cpp,v 1.2 2005/11/10 11:22:55 rocky Exp $ + $Id: iso3.cpp,v 1.3 2006/03/02 18:46:30 rocky Exp $ - Copyright (C) 2004 Rocky Bernstein + Copyright (C) 2004, 2006 Rocky Bernstein This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,6 +20,10 @@ /* Simple program to show using libiso9660 to extract a file from an ISO-9660 image. + + If a single argument is given, it is used as the ISO 9660 image to + use in the extraction. Otherwise a compiled in default ISO 9660 image + name (that comes with the libcdio distribution) will be used. */ /* This is the ISO 9660 image. */ @@ -56,6 +60,8 @@ #include #endif +#define CEILING(x, y) ((x+(y-1))/y) + #define my_exit(rc) \ fclose (p_outfd); \ free(p_statbuf); \ @@ -67,34 +73,46 @@ main(int argc, const char *argv[]) { iso9660_stat_t *p_statbuf; FILE *p_outfd; - unsigned int i; + int i; + char const *psz_image; char const *psz_fname; iso9660_t *p_iso; + + if (argc > 3) { + printf("usage %s [ISO9660-image.ISO [filename]]\n", argv[0]); + printf("Extracts filename from ISO-9660-image.ISO.\n"); + return 1; + } if (argc > 1) - psz_fname = argv[1]; + psz_image = argv[1]; else - psz_fname = ISO9660_IMAGE; + psz_image = ISO9660_IMAGE; - p_iso = iso9660_open (psz_fname); + if (argc > 2) + psz_fname = argv[2]; + else + psz_fname = LOCAL_FILENAME; + + p_iso = iso9660_open (psz_image); if (NULL == p_iso) { - fprintf(stderr, "Sorry, couldn't open ISO 9660 image %s\n", ISO9660_IMAGE); + fprintf(stderr, "Sorry, couldn't open ISO 9660 image %s\n", psz_image); return 1; } - p_statbuf = iso9660_ifs_stat_translate (p_iso, LOCAL_FILENAME); + p_statbuf = iso9660_ifs_stat_translate (p_iso, psz_fname); if (NULL == p_statbuf) { fprintf(stderr, "Could not get ISO-9660 file information for file %s\n", - LOCAL_FILENAME); + psz_fname); iso9660_close(p_iso); return 2; } - if (!(p_outfd = fopen (LOCAL_FILENAME, "wb"))) + if (!(p_outfd = fopen (psz_fname, "wb"))) { perror ("fopen()"); free(p_statbuf); @@ -103,22 +121,22 @@ main(int argc, const char *argv[]) } /* Copy the blocks from the ISO-9660 filesystem to the local filesystem. */ - for (i = 0; i < p_statbuf->size; i += ISO_BLOCKSIZE) + { + const unsigned int i_blocks = CEILING(p_statbuf->size, ISO_BLOCKSIZE); + for (i = 0; i < i_blocks ; i++) { char buf[ISO_BLOCKSIZE]; + const lsn_t lsn = p_statbuf->lsn + i; memset (buf, 0, ISO_BLOCKSIZE); - if ( ISO_BLOCKSIZE != iso9660_iso_seek_read (p_iso, buf, p_statbuf->lsn - + (i / ISO_BLOCKSIZE), - 1) ) + if ( ISO_BLOCKSIZE != iso9660_iso_seek_read (p_iso, buf, lsn, 1) ) { - fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n", - (long unsigned int) p_statbuf->lsn + (i / ISO_BLOCKSIZE)); + fprintf(stderr, "Error reading ISO 9660 file %s at LSN %lu\n", + psz_fname, (long unsigned int) lsn); my_exit(4); } - fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd); if (ferror (p_outfd)) @@ -127,6 +145,7 @@ main(int argc, const char *argv[]) my_exit(5); } } + } fflush (p_outfd); @@ -136,7 +155,8 @@ main(int argc, const char *argv[]) if (ftruncate (fileno (p_outfd), p_statbuf->size)) perror ("ftruncate()"); - printf("Extraction of file 'copying' from %s successful.\n", ISO9660_IMAGE); + printf("Extraction of file '%s' from %s successful.\n", + psz_fname, psz_image); my_exit(0); } diff --git a/example/iso2.c b/example/iso2.c index fb68e011..64dde1d0 100644 --- a/example/iso2.c +++ b/example/iso2.c @@ -1,5 +1,5 @@ /* - $Id: iso2.c,v 1.10 2005/11/07 07:44:00 rocky Exp $ + $Id: iso2.c,v 1.11 2006/03/02 18:46:30 rocky Exp $ Copyright (C) 2003, 2004, 2005 Rocky Bernstein @@ -69,6 +69,8 @@ #include #endif +#define CEILING(x, y) ((x+(y-1))/y) + #define my_exit(rc) \ fclose (p_outfd); \ free(p_statbuf); \ @@ -88,6 +90,12 @@ main(int argc, const char *argv[]) char untranslated_name[256] = ISO9660_PATH; CdIo_t *p_cdio; + if (argc > 3) { + printf("usage %s [CD-ROM-or-image [filename]]\n", argv[0]); + printf("Extracts filename from CD-ROM-or-image.\n"); + return 1; + } + if (argc > 1) psz_image = argv[1]; else @@ -128,30 +136,33 @@ main(int argc, const char *argv[]) } /* Copy the blocks from the ISO-9660 filesystem to the local filesystem. */ - for (i = 0; i < p_statbuf->size; i += ISO_BLOCKSIZE) - { - char buf[ISO_BLOCKSIZE]; - - memset (buf, 0, ISO_BLOCKSIZE); - - if ( 0 != cdio_read_data_sectors (p_cdio, buf, - p_statbuf->lsn + (i / ISO_BLOCKSIZE), - ISO_BLOCKSIZE, 1) ) + { + const unsigned int i_blocks = CEILING(p_statbuf->size, ISO_BLOCKSIZE); + for (i = 0; i < i_blocks; i ++) { - fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n", - (long unsigned int) p_statbuf->lsn + (i / ISO_BLOCKSIZE)); - my_exit(4); + char buf[ISO_BLOCKSIZE]; + const lsn_t lsn = p_statbuf->lsn + i; + + memset (buf, 0, ISO_BLOCKSIZE); + + if ( 0 != cdio_read_data_sectors (p_cdio, buf, lsn, ISO_BLOCKSIZE, 1) ) + { + fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n", + (long unsigned int) p_statbuf->lsn); + my_exit(4); + } + + + fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd); + + if (ferror (p_outfd)) + { + perror ("fwrite()"); + my_exit(5); + } } - - - fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd); - - if (ferror (p_outfd)) - { - perror ("fwrite()"); - my_exit(5); - } - } + } + fflush (p_outfd); diff --git a/example/iso3.c b/example/iso3.c index cc4084a4..bad63651 100644 --- a/example/iso3.c +++ b/example/iso3.c @@ -1,7 +1,7 @@ /* - $Id: iso3.c,v 1.6 2006/03/02 01:28:58 rocky Exp $ + $Id: iso3.c,v 1.7 2006/03/02 18:46:30 rocky Exp $ - Copyright (C) 2004, 2005 Rocky Bernstein + Copyright (C) 2004, 2005, 2006 Rocky Bernstein This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -74,33 +74,45 @@ main(int argc, const char *argv[]) iso9660_stat_t *p_statbuf; FILE *p_outfd; int i; + char const *psz_image; char const *psz_fname; iso9660_t *p_iso; + + if (argc > 3) { + printf("usage %s [ISO9660-image.ISO [filename]]\n", argv[0]); + printf("Extracts filename from ISO-9660-image.ISO\n"); + return 1; + } if (argc > 1) - psz_fname = argv[1]; + psz_image = argv[1]; else - psz_fname = ISO9660_IMAGE; + psz_image = ISO9660_IMAGE; - p_iso = iso9660_open (psz_fname); + if (argc > 2) + psz_fname = argv[2]; + else + psz_fname = LOCAL_FILENAME; + + p_iso = iso9660_open (psz_image); if (NULL == p_iso) { - fprintf(stderr, "Sorry, couldn't open ISO 9660 image %s\n", ISO9660_IMAGE); + fprintf(stderr, "Sorry, couldn't open ISO 9660 image %s\n", psz_image); return 1; } - p_statbuf = iso9660_ifs_stat_translate (p_iso, LOCAL_FILENAME); + p_statbuf = iso9660_ifs_stat_translate (p_iso, psz_fname); if (NULL == p_statbuf) { fprintf(stderr, "Could not get ISO-9660 file information for file %s\n", - LOCAL_FILENAME); + psz_fname); iso9660_close(p_iso); return 2; } - if (!(p_outfd = fopen (LOCAL_FILENAME, "wb"))) + if (!(p_outfd = fopen (psz_fname, "wb"))) { perror ("fopen()"); free(p_statbuf); @@ -121,7 +133,7 @@ main(int argc, const char *argv[]) if ( ISO_BLOCKSIZE != iso9660_iso_seek_read (p_iso, buf, lsn, 1) ) { fprintf(stderr, "Error reading ISO 9660 file %s at LSN %lu\n", - LOCAL_FILENAME, (long unsigned int) lsn); + psz_fname, (long unsigned int) lsn); my_exit(4); } @@ -144,7 +156,7 @@ main(int argc, const char *argv[]) perror ("ftruncate()"); printf("Extraction of file '%s' from %s successful.\n", - LOCAL_FILENAME, ISO9660_IMAGE); + psz_fname, psz_image); my_exit(0); }