"Fossies" - the Fresh Open Source Software Archive

Member "libcdio-2.1.0/example/udf2.c" (12 Aug 2018, 3663 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) 2005, 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 libudf to extract a file.
   19 
   20    This program can be compiled with either a C or C++ compiler. In 
   21    the distribution we prefer C++ just to make sure we haven't broken
   22    things on the C++ side.
   23  */
   24 
   25 /* This is the UDF image. */
   26 #define UDF_IMAGE_PATH "../"
   27 #define UDF_IMAGE "/src2/cd-images/udf/test2.iso"
   28 #define UDF_FILENAME "/parse/cue.L"
   29 
   30 #ifdef HAVE_CONFIG_H
   31 #include "config.h"
   32 #define __CDIO_CONFIG_H__ 1
   33 #endif
   34 
   35 #ifdef HAVE_STDIO_H
   36 #include <stdio.h>
   37 #endif
   38 #ifdef HAVE_STDLIB_H
   39 #include <stdlib.h>
   40 #endif
   41 #ifdef HAVE_STRING_H
   42 #include <string.h>
   43 #endif
   44 #ifdef HAVE_UNISTD_H
   45 #include <unistd.h>
   46 #endif
   47 #ifdef HAVE_SYS_TYPES_H
   48 #include <sys/types.h>
   49 #endif
   50 
   51 #include <cdio/cdio.h>
   52 #include <cdio/udf.h>
   53 
   54 #define CEILING(x, y) ((x+(y-1))/y)
   55 
   56 #define udf_PATH_DELIMITERS "/\\"
   57 
   58 static void 
   59 print_file_info(const udf_dirent_t *p_udf_dirent, const char* psz_dirname)
   60 {
   61   time_t mod_time = udf_get_modification_time(p_udf_dirent);
   62   char psz_mode[11]="invalid";
   63   const char *psz_fname= psz_dirname 
   64     ? psz_dirname : udf_get_filename(p_udf_dirent);
   65 
   66   /* Print directory attributes*/
   67   printf("%s ", udf_mode_string(udf_get_posix_filemode(p_udf_dirent),
   68                 psz_mode));
   69   printf("%4d ", udf_get_link_count(p_udf_dirent));
   70   printf("%ld ", (long unsigned int) udf_get_file_length(p_udf_dirent));
   71   printf("%s %s\n",  *psz_fname ? psz_fname : "/", ctime(&mod_time));
   72 }
   73 
   74 int
   75 main(int argc, const char *argv[])
   76 {
   77   udf_t *p_udf;
   78   char const *psz_udf_image;
   79   char const *psz_udf_fname;
   80 
   81   if (argc > 1) 
   82     psz_udf_image = argv[1];
   83   else 
   84     psz_udf_image = UDF_IMAGE;
   85 
   86   if (argc > 2) 
   87     psz_udf_fname = argv[2];
   88   else 
   89     psz_udf_fname = UDF_FILENAME;
   90 
   91 
   92   p_udf = udf_open (psz_udf_image);
   93   
   94   if (NULL == p_udf) {
   95     fprintf(stderr, "Sorry, couldn't open %s as something using UDF\n", 
   96         psz_udf_image);
   97     return 1;
   98   } else {
   99     udf_dirent_t *p_udf_root = udf_get_root(p_udf, true, 0);
  100     udf_dirent_t *p_udf_file = NULL;
  101     if (NULL == p_udf_root) {
  102       fprintf(stderr, "Sorry, couldn't find / in %s\n", 
  103           psz_udf_image);
  104       return 1;
  105     }
  106     
  107     p_udf_file = udf_fopen(p_udf_root, psz_udf_fname);
  108     if (!p_udf_file) {
  109       fprintf(stderr, "Sorry, couldn't find %s in %s\n", 
  110           psz_udf_fname, psz_udf_image);
  111       return 2;
  112       
  113     }
  114     print_file_info(p_udf_file, udf_get_filename(p_udf_file));
  115     {
  116       long unsigned int i_file_length = udf_get_file_length(p_udf_file);
  117       char *p_buf;
  118       unsigned int i_blocks = CEILING(i_file_length, UDF_BLOCKSIZE);
  119       char fmt_string[100] = {'\0'};
  120       snprintf(fmt_string, sizeof(fmt_string), "%%%lus", i_file_length);
  121       
  122       p_buf = (char *) calloc(sizeof(char), UDF_BLOCKSIZE*i_blocks);
  123       udf_read_block(p_udf_file, p_buf, i_blocks);
  124       printf(fmt_string, p_buf);
  125       free(p_buf);
  126     }
  127     udf_dirent_free(p_udf_file);
  128     udf_dirent_free(p_udf_root);
  129   }
  130   
  131   udf_close(p_udf);
  132   return 0;
  133 }
  134