comparison dvd_reader.c @ 37:a57cd30a83bb src

modified DVDOpen() to use a unified DVDOpen_error label; patch by Erik Hovland
author nicodvb
date Tue, 28 Apr 2009 13:19:51 +0000
parents 79c9b639bf9d
children 9f1804080f76
comparison
equal deleted inserted replaced
36:79c9b639bf9d 37:a57cd30a83bb
328 328
329 329
330 dvd_reader_t *DVDOpen( const char *ppath ) 330 dvd_reader_t *DVDOpen( const char *ppath )
331 { 331 {
332 struct stat fileinfo; 332 struct stat fileinfo;
333 int ret, have_css; 333 int ret, have_css, retval, cdir = 0;
334 dvd_reader_t *ret_val = NULL; 334 dvd_reader_t *ret_val = NULL;
335 char *dev_name = NULL; 335 char *dev_name = NULL;
336 char *path; 336 char *path = NULL, *new_path = NULL, *path_copy = NULL;
337 337
338 #ifdef _MSC_VER 338 #ifdef _MSC_VER
339 int len; 339 int len;
340 #endif 340 #endif
341 341
342 if( ppath == NULL ) 342 if( ppath == NULL )
343 return 0; 343 goto DVDOpen_error;
344 344
345 path = strdup(ppath); 345 path = strdup(ppath);
346 if( path == NULL ) 346 if( path == NULL )
347 return 0; 347 goto DVDOpen_error;
348 348
349 /* Try to open libdvdcss or fall back to standard functions */ 349 /* Try to open libdvdcss or fall back to standard functions */
350 have_css = dvdinput_setup(); 350 have_css = dvdinput_setup();
351 351
352 #ifdef _MSC_VER 352 #ifdef _MSC_VER
372 } 372 }
373 373
374 /* If we can't stat the file, give up */ 374 /* If we can't stat the file, give up */
375 fprintf( stderr, "libdvdread: Can't stat %s\n", path ); 375 fprintf( stderr, "libdvdread: Can't stat %s\n", path );
376 perror(""); 376 perror("");
377 free(path); 377 goto DVDOpen_error;
378 return NULL;
379 } 378 }
380 379
381 /* First check if this is a block/char device or a file*/ 380 /* First check if this is a block/char device or a file*/
382 if( S_ISBLK( fileinfo.st_mode ) || 381 if( S_ISBLK( fileinfo.st_mode ) ||
383 S_ISCHR( fileinfo.st_mode ) || 382 S_ISCHR( fileinfo.st_mode ) ||
397 free(path); 396 free(path);
398 return ret_val; 397 return ret_val;
399 398
400 } else if( S_ISDIR( fileinfo.st_mode ) ) { 399 } else if( S_ISDIR( fileinfo.st_mode ) ) {
401 dvd_reader_t *auth_drive = 0; 400 dvd_reader_t *auth_drive = 0;
402 char *path_copy;
403 #if defined(SYS_BSD) 401 #if defined(SYS_BSD)
404 struct fstab* fe; 402 struct fstab* fe;
405 #elif defined(__sun) || defined(__linux__) 403 #elif defined(__sun) || defined(__linux__)
406 FILE *mntfile; 404 FILE *mntfile;
407 #endif 405 #endif
408 406
409 /* XXX: We should scream real loud here. */ 407 /* XXX: We should scream real loud here. */
410 if( !(path_copy = strdup( path ) ) ) { 408 if( !(path_copy = strdup( path ) ) )
411 free(path); 409 goto DVDOpen_error;
412 return NULL;
413 }
414 410
415 #ifndef WIN32 /* don't have fchdir, and getcwd( NULL, ... ) is strange */ 411 #ifndef WIN32 /* don't have fchdir, and getcwd( NULL, ... ) is strange */
416 /* Also WIN32 does not have symlinks, so we don't need this bit of code. */ 412 /* Also WIN32 does not have symlinks, so we don't need this bit of code. */
417 413
418 /* Resolve any symlinks and get the absolute dir name. */ 414 /* Resolve any symlinks and get the absolute dir name. */
419 { 415 {
420 char *new_path; 416 if( ( cdir = open( ".", O_RDONLY ) ) >= 0 ) {
421 int cdir = open( ".", O_RDONLY ); 417 if( chdir( path_copy ) == -1 ) {
422 418 goto DVDOpen_error;
423 if( cdir >= 0 ) { 419 }
424 chdir( path_copy );
425 new_path = malloc(PATH_MAX+1); 420 new_path = malloc(PATH_MAX+1);
426 if(!new_path) { 421 if(!new_path) {
427 free(path); 422 goto DVDOpen_error;
428 return NULL;
429 } 423 }
430 getcwd(new_path, PATH_MAX ); 424 if( getcwd( new_path, PATH_MAX ) == NULL ) {
431 fchdir( cdir ); 425 goto DVDOpen_error;
426 }
427 retval = fchdir( cdir );
432 close( cdir ); 428 close( cdir );
433 free( path_copy ); 429 cdir = -1;
434 path_copy = new_path; 430 if( retval == -1 ) {
431 goto DVDOpen_error;
432 }
433 path_copy = new_path;
434 new_path = NULL;
435 } 435 }
436 } 436 }
437 #endif 437 #endif
438 438
439 /** 439 /**
525 "CSS authentication not available.\n", dev_name ); 525 "CSS authentication not available.\n", dev_name );
526 } 526 }
527 #endif 527 #endif
528 528
529 free( dev_name ); 529 free( dev_name );
530 dev_name = NULL;
530 free( path_copy ); 531 free( path_copy );
532 path_copy = NULL;
531 533
532 /** 534 /**
533 * If we've opened a drive, just use that. 535 * If we've opened a drive, just use that.
534 */ 536 */
535 if( auth_drive ) { 537 if( auth_drive ) {
542 ret_val = DVDOpenPath( path ); 544 ret_val = DVDOpenPath( path );
543 free( path ); 545 free( path );
544 return ret_val; 546 return ret_val;
545 } 547 }
546 548
549 DVDOpen_error:
547 /* If it's none of the above, screw it. */ 550 /* If it's none of the above, screw it. */
548 fprintf( stderr, "libdvdread: Could not open %s\n", path ); 551 fprintf( stderr, "libdvdread: Could not open %s\n", path );
552 if( path != NULL )
549 free( path ); 553 free( path );
554 if ( path_copy != NULL )
555 free( path_copy );
556 if ( cdir >= 0 )
557 close( cdir );
558 if ( new_path != NULL )
559 free( new_path );
550 return NULL; 560 return NULL;
551 } 561 }
552 562
553 void DVDClose( dvd_reader_t *dvd ) 563 void DVDClose( dvd_reader_t *dvd )
554 { 564 {