"Fossies" - the Fresh Open Source Software Archive

Member "libcdio-2.1.0/example/C++/isofile2.cpp" (12 Aug 2018, 3786 Bytes) of package /linux/privat/libcdio-2.1.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2   Copyright (C) 2004, 2006, 2008, 2009 Rocky Bernstein <rocky@gnu.org>
    3   
    4   This program is free software: you can redistribute it and/or modify
    5   it under the terms of the GNU General Public License as published by
    6   the Free Software Foundation, either version 3 of the License, or
    7   (at your option) any later version.
    8 
    9   This program is distributed in the hope that it will be useful,
   10   but WITHOUT ANY WARRANTY; without even the implied warranty of
   11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12   GNU General Public License for more details.
   13 
   14   You should have received a copy of the GNU General Public License
   15   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   16 */
   17 
   18 /* Simple program to show using libiso9660 to extract a file from an
   19    ISO-9660 image.
   20 
   21    If a single argument is given, it is used as the ISO 9660 image to
   22    use in the extraction. Otherwise a compiled in default ISO 9660 image
   23    name (that comes with the libcdio distribution) will be used.
   24  */
   25 
   26 /* This is the ISO 9660 image. */
   27 #define ISO9660_IMAGE_PATH "../../"
   28 #define ISO9660_IMAGE ISO9660_IMAGE_PATH "test/copying.iso"
   29 
   30 #define LOCAL_FILENAME "copying"
   31 
   32 #include "portable.h"
   33 
   34 #ifdef HAVE_STDIO_H
   35 #include <stdio.h>
   36 #endif
   37 #ifdef HAVE_ERRNO_H
   38 #include <errno.h>
   39 #endif
   40 #ifdef HAVE_STDLIB_H
   41 #include <stdlib.h>
   42 #endif
   43 #ifdef HAVE_STRING_H
   44 #include <string.h>
   45 #endif
   46 #ifdef HAVE_UNISTD_H
   47 #include <unistd.h>
   48 #endif
   49 #ifdef HAVE_SYS_TYPES_H
   50 #include <sys/types.h>
   51 #endif
   52 
   53 #include <cdio/cdio.h>
   54 #include <cdio/iso9660.h>
   55 
   56 #define CEILING(x, y) ((x+(y-1))/y)
   57 
   58 #define my_exit(rc)             \
   59   fclose (p_outfd);             \
   60   free(p_statbuf);              \
   61   iso9660_close(p_iso);             \
   62   return rc;                    \
   63 
   64 int
   65 main(int argc, const char *argv[])
   66 {
   67   iso9660_stat_t *p_statbuf;
   68   FILE *p_outfd;
   69   unsigned int i;
   70   char const *psz_image;
   71   char const *psz_fname;
   72   iso9660_t *p_iso;
   73 
   74   if (argc > 3) {
   75     printf("usage %s [ISO9660-image.ISO [filename]]\n", argv[0]);
   76     printf("Extracts filename from ISO-9660-image.ISO.\n");
   77     return 1;
   78   }
   79   
   80   if (argc > 1) 
   81     psz_image = argv[1];
   82   else 
   83     psz_image = ISO9660_IMAGE;
   84 
   85   if (argc > 2) 
   86     psz_fname = argv[2];
   87   else 
   88     psz_fname = LOCAL_FILENAME;
   89 
   90   p_iso = iso9660_open (psz_image);
   91   
   92   if (NULL == p_iso) {
   93     fprintf(stderr, "Sorry, couldn't open ISO 9660 image %s\n", psz_image);
   94     return 1;
   95   }
   96 
   97   p_statbuf = iso9660_ifs_stat_translate (p_iso, psz_fname);
   98 
   99   if (NULL == p_statbuf) 
  100     {
  101       fprintf(stderr, 
  102           "Could not get ISO-9660 file information for file %s\n",
  103           psz_fname);
  104       iso9660_close(p_iso);
  105       return 2;
  106     }
  107 
  108   if (!(p_outfd = fopen (psz_fname, "wb")))
  109     {
  110       perror ("fopen()");
  111       free(p_statbuf);
  112       iso9660_close(p_iso);
  113       return 3;
  114     }
  115 
  116   /* Copy the blocks from the ISO-9660 filesystem to the local filesystem. */
  117   {
  118     const unsigned int i_blocks = CEILING(p_statbuf->size, ISO_BLOCKSIZE);
  119     for (i = 0; i < i_blocks ; i++) 
  120     {
  121       char buf[ISO_BLOCKSIZE];
  122       const lsn_t lsn = p_statbuf->lsn + i;
  123 
  124       memset (buf, 0, ISO_BLOCKSIZE);
  125       
  126       if ( ISO_BLOCKSIZE != iso9660_iso_seek_read (p_iso, buf, lsn, 1) )
  127       {
  128     fprintf(stderr, "Error reading ISO 9660 file %s at LSN %lu\n",
  129         psz_fname, (long unsigned int) lsn);
  130     my_exit(4);
  131       }
  132       
  133       fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd);
  134       
  135       if (ferror (p_outfd))
  136     {
  137       perror ("fwrite()");
  138       my_exit(5);
  139     }
  140     }
  141   }
  142   
  143   fflush (p_outfd);
  144 
  145   /* Make sure the file size has the exact same byte size. Without the
  146      truncate below, the file will a multiple of ISO_BLOCKSIZE.
  147    */
  148   if (ftruncate (fileno (p_outfd), p_statbuf->size))
  149     perror ("ftruncate()");
  150 
  151   printf("Extraction of file '%s' from %s successful.\n", 
  152      psz_fname, psz_image);
  153 
  154   my_exit(0);
  155 }