Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[e660eb]: src / cd-drive.c Maximize Restore History

Download this file

cd-drive.c    356 lines (289 with data), 9.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
$Id: cd-drive.c,v 1.25 2005/10/06 09:37:11 rocky Exp $
Copyright (C) 2004, 2005 Rocky Bernstein <rocky@panix.com>
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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Program to show drivers installed and capibilites of CD drives. */
#include "util.h"
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <getopt.h>
#include <cdio/cdio.h>
#include <cdio/scsi_mmc.h>
/* Used by `main' to communicate with `parse_opt'. And global options
*/
struct arguments
{
uint32_t debug_level;
int version_only;
int silent;
source_image_t source_image;
} opts;
/* Configuration option codes */
enum {
OP_HANDLED,
OP_SOURCE_DEVICE,
OP_USAGE,
/* These are the remaining configuration options */
OP_VERSION,
};
/* Parse all options. */
static bool
parse_options (int argc, char *argv[])
{
int opt;
const char* helpText =
"Usage: %s [OPTION...]\n"
" -d, --debug=INT Set debugging to LEVEL\n"
" -i, --cdrom-device[=DEVICE] show only info about CD-ROM device\n"
" -q, --quiet Don't produce warning output\n"
" -V, --version display version and copyright information\n"
" and exit\n"
"\n"
"Help options:\n"
" -?, --help Show this help message\n"
" --usage Display brief usage message\n";
const char* usageText =
"Usage: %s [-d|--debug INT] [-i|--cdrom-device DEVICE] [-q|--quiet]\n"
" [-V|--version] [-?|--help] [--usage]\n";
const char* optionsString = "d:i::qV?";
struct option optionsTable[] = {
{"debug", required_argument, NULL, 'd' },
{"cdrom-device", optional_argument, NULL, 'i' },
{"quiet", no_argument, NULL, 'q' },
{"version", no_argument, NULL, 'V' },
{"help", no_argument, NULL, '?' },
{"usage", no_argument, NULL, OP_USAGE },
{NULL, 0, NULL, 0 }
};
program_name = strrchr(argv[0],'/');
program_name = program_name ? strdup(program_name+1) : strdup(argv[0]);
while ((opt = getopt_long(argc, argv, optionsString, optionsTable, NULL)) != -1) {
switch (opt) {
case 'd':
opts.debug_level = atoi(optarg);
break;
case 'i':
if (opts.source_image != DRIVER_UNKNOWN) {
/* NOTE: The libpopt version already set source_name by this time.
To restore this behavior, fall through to the else{} block.
*/
report( stderr, "%s: another source type option given before.\n",
program_name );
report( stderr, "%s: give only one source type option.\n",
program_name );
break;
} else {
opts.source_image = DRIVER_DEVICE;
if (optarg != NULL) {
source_name = fillout_device_name(optarg);
}
break;
}
break;
case 'q':
opts.silent = 1;
break;
case 'V':
opts.version_only = 1;
break;
case '?':
fprintf(stdout, helpText, program_name);
free(program_name);
exit(EXIT_INFO);
break;
case OP_USAGE:
fprintf(stderr, usageText, program_name);
free(program_name);
exit(EXIT_FAILURE);
break;
case OP_HANDLED:
break;
default:
return false;
}
}
if (optind < argc) {
const char *remaining_arg = argv[optind++];
/* NOTE: A bug in the libpopt version checked source_image, which
rendered the subsequent source_image test useless.
*/
if (source_name != NULL) {
report( stderr, "%s: Source specified in option %s and as %s\n",
program_name, source_name, remaining_arg);
free(program_name);
exit (EXIT_FAILURE);
}
if (opts.source_image == DRIVER_DEVICE)
source_name = fillout_device_name(remaining_arg);
else
source_name = strdup(remaining_arg);
if (optind < argc) {
report( stderr, "%s: Source specified in previously %s and %s\n",
program_name, source_name, remaining_arg);
free(program_name);
exit (EXIT_FAILURE);
}
}
return true;
}
/* CDIO logging routines */
static void
_log_handler (cdio_log_level_t level, const char message[])
{
if (level == CDIO_LOG_DEBUG && opts.debug_level < 2)
return;
if (level == CDIO_LOG_INFO && opts.debug_level < 1)
return;
if (level == CDIO_LOG_WARN && opts.silent)
return;
gl_default_cdio_log_handler (level, message);
}
/*! Prints out SCSI-MMC drive features */
static void
print_mmc_drive_level(CdIo_t *p_cdio)
{
cdio_mmc_level_t mmc_level = mmc_get_drive_mmc_cap(p_cdio);
printf( "CD-ROM drive supports " );
switch(mmc_level) {
case CDIO_MMC_LEVEL_WEIRD:
printf("some nonstandard or degenerate set of MMC\n");
break;
case CDIO_MMC_LEVEL_1:
printf("MMC 1\n");
break;
case CDIO_MMC_LEVEL_2:
printf("MMC 2\n");
break;
case CDIO_MMC_LEVEL_3:
printf("MMC 3\n");
break;
case CDIO_MMC_LEVEL_NONE:
printf("no MMC\n");
break;
}
printf("\n");
}
/* Initialize global variables. */
static void
init(void)
{
gl_default_cdio_log_handler = cdio_log_set_handler (_log_handler);
/* Default option values. */
opts.silent = false;
opts.debug_level = 0;
opts.source_image = DRIVER_UNKNOWN;
}
int
main(int argc, char *argv[])
{
CdIo_t *p_cdio=NULL;
init();
/* Parse our arguments; every option seen by `parse_opt' will
be reflected in `arguments'. */
parse_options(argc, argv);
print_version(program_name, CDIO_VERSION, false, opts.version_only);
if (opts.debug_level == 3) {
cdio_loglevel_default = CDIO_LOG_INFO;
} else if (opts.debug_level >= 4) {
cdio_loglevel_default = CDIO_LOG_DEBUG;
}
if (NULL == source_name) {
char *default_device;
p_cdio = cdio_open (NULL, DRIVER_DEVICE);
if (NULL == p_cdio) {
printf("No loaded CD-ROM device accessible.\n");
} else {
default_device = cdio_get_default_device(p_cdio);
printf("The driver selected is %s\n", cdio_get_driver_name(p_cdio));
if (default_device) {
printf("The default device for this driver is %s\n", default_device);
}
free(default_device);
cdio_destroy(p_cdio);
p_cdio=NULL;
printf("\n");
}
}
/* Print out a drivers available */
{
driver_id_t driver_id;
printf("Drivers available...\n");
for (driver_id=CDIO_MIN_DRIVER; driver_id<=CDIO_MAX_DRIVER; driver_id++)
if (cdio_have_driver(driver_id)) {
printf(" %-35s\n", cdio_driver_describe(driver_id));
}
printf("\n");
}
if (NULL == source_name) {
/* Print out a list of CD-drives */
char **ppsz_cdrives=NULL, **ppsz_cd;
driver_id_t driver_id = DRIVER_DEVICE;
ppsz_cdrives = cdio_get_devices_ret(&driver_id);
if (NULL != ppsz_cdrives)
for( ppsz_cd = ppsz_cdrives; *ppsz_cd != NULL; ppsz_cd++ ) {
cdio_drive_read_cap_t i_read_cap;
cdio_drive_write_cap_t i_write_cap;
cdio_drive_misc_cap_t i_misc_cap;
cdio_hwinfo_t hwinfo;
CdIo_t *p_cdio = cdio_open(*ppsz_cd, driver_id);
print_mmc_drive_level(p_cdio);
printf("%28s: %s\n", "Drive", *ppsz_cd);
if (p_cdio) {
if (cdio_get_hwinfo(p_cdio, &hwinfo)) {
printf("%-28s: %s\n%-28s: %s\n%-28s: %s\n",
"Vendor" , hwinfo.psz_vendor,
"Model" , hwinfo.psz_model,
"Revision", hwinfo.psz_revision);
}
print_mmc_drive_features(p_cdio);
cdio_get_drive_cap(p_cdio, &i_read_cap, &i_write_cap,
&i_misc_cap);
print_drive_capabilities(i_read_cap, i_write_cap, i_misc_cap);
}
printf("\n");
if (p_cdio) cdio_destroy(p_cdio);
}
cdio_free_device_list(ppsz_cdrives);
ppsz_cdrives = NULL;
} else {
/* Print CD-drive info for given source */
cdio_drive_read_cap_t i_read_cap;
cdio_drive_write_cap_t i_write_cap;
cdio_drive_misc_cap_t i_misc_cap;
cdio_hwinfo_t hwinfo;
printf("Drive %s\n", source_name);
p_cdio = cdio_open (source_name, DRIVER_UNKNOWN);
if (p_cdio) {
print_mmc_drive_level(p_cdio);
if (cdio_get_hwinfo(p_cdio, &hwinfo)) {
printf("%-28s: %s\n%-28s: %s\n%-28s: %s\n",
"Vendor" , hwinfo.psz_vendor,
"Model" , hwinfo.psz_model,
"Revision", hwinfo.psz_revision);
}
print_mmc_drive_features(p_cdio);
}
cdio_get_drive_cap_dev(source_name, &i_read_cap, &i_write_cap,
&i_misc_cap);
print_drive_capabilities(i_read_cap, i_write_cap, i_misc_cap);
printf("\n");
}
myexit(p_cdio, EXIT_SUCCESS);
/* Not reached:*/
return(EXIT_SUCCESS);
}