1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 #include "hdf5.h"
15 #include "H5private.h"
16 #include "h5tools.h"
17 #include "h5tools_utils.h"
18 
19 /* Name of tool */
20 #define PROGRAMNAME "tellub"
21 
22 /*
23  * Command-line options: The user can specify short or long-named
24  * parameters. The long-named ones can be partially spelled. When
25  * adding more, make sure that they don't clash with each other.
26  */
27 static const char *s_opts = "h";
28 static struct long_options l_opts[] = {
29   {"help", no_arg, 'h'},
30   {"hel", no_arg, 'h'},
31   {NULL, 0, '\0'}
32 };
33 
34 /*-------------------------------------------------------------------------
35  * Function:    usage
36  *
37  * Purpose:     Print the usage message
38  *
39  * Return:      void
40  *
41  * Programmer:
42  *
43  * Modifications:
44  *
45  *-------------------------------------------------------------------------
46  */
47 static void
usage(const char * prog)48 usage (const char *prog)
49 {
50   fflush (stdout);
51   fprintf (stdout, "usage: %s h5_file\n", prog);
52   fprintf (stdout,
53      "           Check that h5_fil is HDF5 file and print size of user block \n");
54   fprintf (stdout, "       %s -h\n", prog);
55   fprintf (stdout, "           Print a usage message and exit\n");
56 }
57 
58 /*-------------------------------------------------------------------------
59  * Function:    parse_command_line
60  *
61  * Purpose:     Parse the command line for the h5dumper.
62  *
63  * Return:      Success:
64  *
65  *              Failure:    Exits program with EXIT_FAILURE value.
66  *
67  * Programmer:
68  *
69  * Modifications:
70  *
71  *-------------------------------------------------------------------------
72  */
73 
74 static void
parse_command_line(int argc,const char * argv[])75 parse_command_line (int argc, const char *argv[])
76 {
77   int opt;
78 
79   /* parse command line options */
80   while ((opt = get_option (argc, argv, s_opts, l_opts)) != EOF)
81     {
82       switch ((char) opt)
83       {
84       case 'h':
85         usage (h5tools_getprogname());
86         exit (EXIT_SUCCESS);
87       case '?':
88       default:
89         usage (h5tools_getprogname());
90         exit (EXIT_FAILURE);
91       }
92     }
93 
94   /* check for file name to be processed */
95   if (argc <= opt_ind)
96     {
97       error_msg("missing file name\n");
98       usage (h5tools_getprogname());
99       exit (EXIT_FAILURE);
100     }
101 }
102 
103 /*-------------------------------------------------------------------------
104  * Function:    main
105  *
106  * Purpose:     HDF5 user block unjammer
107  *
108  * Return:      Success:    0
109  *              Failure:    1
110  *
111  * Programmer:
112  *
113  * Modifications:
114  *
115  *-------------------------------------------------------------------------
116  */
117 int
main(int argc,const char * argv[])118 main (int argc, const char *argv[])
119 {
120   char *ifname;
121   void *edata;
122   H5E_auto2_t func;
123   hid_t ifile;
124   hsize_t usize;
125   htri_t testval;
126   herr_t status;
127   hid_t plist;
128 
129   h5tools_setprogname(PROGRAMNAME);
130   h5tools_setstatus(EXIT_SUCCESS);
131 
132   /* Initialize h5tools lib */
133   h5tools_init();
134 
135   /* Disable error reporting */
136   H5Eget_auto2(H5E_DEFAULT, &func, &edata);
137   H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
138 
139   parse_command_line (argc, argv);
140 
141   if (argc <= (opt_ind))
142     {
143       error_msg("missing file name\n");
144       usage (h5tools_getprogname());
145       return (EXIT_FAILURE);
146     }
147 
148   ifname = HDstrdup (argv[opt_ind]);
149 
150   testval = H5Fis_hdf5 (ifname);
151 
152   if (testval <= 0)
153     {
154       error_msg("Input HDF5 file is not HDF \"%s\"\n", ifname);
155       return (EXIT_FAILURE);
156     }
157 
158   ifile = H5Fopen (ifname, H5F_ACC_RDONLY, H5P_DEFAULT);
159 
160   if (ifile < 0)
161     {
162       error_msg("Can't open input HDF5 file \"%s\"\n", ifname);
163       return (EXIT_FAILURE);
164     }
165 
166   plist = H5Fget_create_plist (ifile);
167   if (plist < 0)
168     {
169       error_msg("Can't get file creation plist for file \"%s\"\n",
170      ifname);
171       return (EXIT_FAILURE);
172     }
173 
174   status = H5Pget_userblock (plist, &usize);
175   if (status < 0)
176     {
177       error_msg("Can't get user block for file \"%s\"\n", ifname);
178       return (EXIT_FAILURE);
179     }
180 
181   printf ("%ld\n", (long) usize);
182 
183   H5Pclose (plist);
184   H5Fclose (ifile);
185 
186   return (EXIT_SUCCESS);
187 }
188 
189