[libdvdnav-devel] [PATCH] Cleanup indentation, spacing for DVDOpen()

beandog at gentoo.org beandog at gentoo.org
Thu Oct 2 20:22:22 CEST 2014


From: Steve Dibb <steve.dibb at gmail.com>

---
 src/dvd_reader.c | 411 +++++++++++++++++++++++++++----------------------------
 1 file changed, 204 insertions(+), 207 deletions(-)

diff --git a/src/dvd_reader.c b/src/dvd_reader.c
index fb045b3..28e3409 100644
--- a/src/dvd_reader.c
+++ b/src/dvd_reader.c
@@ -344,272 +344,269 @@ static char *bsd_block2char( const char *path )
 #endif
 
 
-dvd_reader_t *DVDOpen( const char *ppath )
+dvd_reader_t *DVDOpen(const char *ppath)
 {
-  struct stat fileinfo;
-  int ret, have_css, retval, cdir = -1;
-  dvd_reader_t *ret_val = NULL;
-  char *dev_name = NULL;
-  char *path = NULL, *new_path = NULL, *path_copy = NULL;
+
+    struct stat fileinfo;
+    int ret, have_css, retval, cdir = -1;
+    dvd_reader_t *ret_val = NULL;
+    char *dev_name = NULL;
+    char *path = NULL, *new_path = NULL, *path_copy = NULL;
 
 #if defined(_WIN32) || defined(__OS2__)
       int len;
 #endif
 
-  if( ppath == NULL )
-    goto DVDOpen_error;
+    if(ppath == NULL)
+        goto DVDOpen_error;
+
+    path = strdup(ppath);
 
-      path = strdup(ppath);
-  if( path == NULL )
-    goto DVDOpen_error;
+    if(path == NULL)
+        goto DVDOpen_error;
 
-  /* Try to open libdvdcss or fall back to standard functions */
-  have_css = dvdinput_setup();
+    /* Try to open libdvdcss or fall back to standard functions */
+    have_css = dvdinput_setup();
 
 #if defined(_WIN32) || defined(__OS2__)
-  /* Strip off the trailing \ if it is not a drive */
-  len = strlen(path);
-  if ((len > 1) &&
-      (path[len - 1] == '\\')  &&
-      (path[len - 2] != ':'))
-  {
-    path[len-1] = '\0';
-  }
+    /* Strip off the trailing \ if it is not a drive */
+    len = strlen(path);
+    if ((len > 1) && (path[len - 1] == '\\')  && (path[len - 2] != ':'))
+        path[len-1] = '\0';
 #endif
 
-  ret = stat( path, &fileinfo );
+    ret = stat(path, &fileinfo);
 
-  if( ret < 0 ) {
+    if(ret < 0) {
 
-    /* maybe "host:port" url? try opening it with acCeSS library */
-    if( strchr(path,':') ) {
-                    ret_val = DVDOpenImageFile( path, have_css );
-                    free(path);
+        /* maybe "host:port" url? try opening it with acCeSS library */
+        if(strchr(path, ':')) {
+            ret_val = DVDOpenImageFile(path, have_css);
+            free(path);
             return ret_val;
+        }
+
+        /* If we can't stat the file, give up */
+        fprintf(stderr, "libdvdread: Can't stat %s\n", path);
+        perror("");
+        goto DVDOpen_error;
+
     }
 
-    /* If we can't stat the file, give up */
-    fprintf( stderr, "libdvdread: Can't stat %s\n", path );
-    perror("");
-    goto DVDOpen_error;
-  }
+    /* First check if this is a block/char device or a file*/
+    if(S_ISBLK(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) || S_ISREG(fileinfo.st_mode)) {
 
-  /* First check if this is a block/char device or a file*/
-  if( S_ISBLK( fileinfo.st_mode ) ||
-      S_ISCHR( fileinfo.st_mode ) ||
-      S_ISREG( fileinfo.st_mode ) ) {
+        /* Block devices and regular files are assumed to be DVD-Video images. */
+        dvd_reader_t *dvd = NULL;
 
-    /**
-     * Block devices and regular files are assumed to be DVD-Video images.
-     */
-    dvd_reader_t *dvd = NULL;
 #if defined(__sun)
-    dev_name = sun_block2char( path );
+        dev_name = sun_block2char(path);
 #elif defined(SYS_BSD)
-    dev_name = bsd_block2char( path );
+        dev_name = bsd_block2char(path);
 #else
-    dev_name = strdup( path );
+        dev_name = strdup(path);
 #endif
-    if(!dev_name)
-        goto DVDOpen_error;
-    dvd = DVDOpenImageFile( dev_name, have_css );
-    free( dev_name );
-    free(path);
-    return dvd;
-  } else if( S_ISDIR( fileinfo.st_mode ) ) {
-    dvd_reader_t *auth_drive = 0;
+
+        if(!dev_name)
+            goto DVDOpen_error;
+
+        dvd = DVDOpenImageFile(dev_name, have_css);
+        free(dev_name);
+        free(path);
+
+        return dvd;
+
+    } else if(S_ISDIR(fileinfo.st_mode)) {
+
+        dvd_reader_t *auth_drive = 0;
+
 #if defined(SYS_BSD)
-    struct fstab* fe;
+        struct fstab* fe;
 #elif defined(__sun) || defined(__linux__)
-    FILE *mntfile;
+        FILE *mntfile;
 #endif
 
-    /* XXX: We should scream real loud here. */
-    if( !(path_copy = strdup( path ) ) )
-      goto DVDOpen_error;
+        /* XXX: We should scream real loud here. */
+        if(!(path_copy = strdup(path)))
+            goto DVDOpen_error;
 
 #ifndef WIN32 /* don't have fchdir, and getcwd( NULL, ... ) is strange */
               /* Also WIN32 does not have symlinks, so we don't need this bit of code. */
 
-    /* Resolve any symlinks and get the absolute dir name. */
-    {
-      if( ( cdir  = open( ".", O_RDONLY ) ) >= 0 ) {
-        if( chdir( path_copy ) == -1 ) {
-          goto DVDOpen_error;
-        }
-        new_path = malloc(PATH_MAX+1);
-        if(!new_path) {
-          goto DVDOpen_error;
-        }
-        if( getcwd( new_path, PATH_MAX ) == NULL ) {
-          goto DVDOpen_error;
-        }
-        retval = fchdir( cdir );
-        close( cdir );
-        cdir = -1;
-        if( retval == -1 ) {
-          goto DVDOpen_error;
+        /* Resolve any symlinks and get the absolute dir name. */
+        if((cdir = open(".", O_RDONLY)) >= 0) {
+
+            if(chdir(path_copy) == -1)
+                goto DVDOpen_error;
+
+            new_path = malloc(PATH_MAX + 1);
+
+            if(!new_path)
+                goto DVDOpen_error;
+
+            if(getcwd(new_path, PATH_MAX) == NULL)
+                goto DVDOpen_error;
+
+            retval = fchdir(cdir);
+            close(cdir);
+            cdir = -1;
+
+            if(retval == -1)
+                goto DVDOpen_error;
+
+            free(path_copy);
+            path_copy = new_path;
+            new_path = NULL;
+
         }
-        free(path_copy);
-        path_copy = new_path;
-        new_path = NULL;
-      }
-    }
 #endif
 
-    /**
-     * If we're being asked to open a directory, check if that directory
-     * is the mount point for a DVD-ROM which we can use instead.
-     */
+        /**
+         * If we're being asked to open a directory, check if that directory
+         * is the mount point for a DVD-ROM which we can use instead.
+         */
 
-    if( strlen( path_copy ) > 1 ) {
-      if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) {
-        path_copy[ strlen( path_copy ) - 1 ] = '\0';
-      }
-    }
+        if(strlen(path_copy) > 1) {
+            if(path_copy[strlen(path_copy) - 1] == '/')
+                path_copy[strlen(path_copy) - 1] = '\0';
+        }
 
 #if defined(_WIN32) || defined(__OS2__)
-    if(strlen(path_copy) > TITLES_MAX) {
-      if(!strcasecmp(&(path_copy[strlen( path_copy ) - TITLES_MAX]),
-                       "\\video_ts"))
-        path_copy[strlen(path_copy) - (TITLES_MAX-1)] = '\0';
-    }
+
+        if(strlen(path_copy) > TITLES_MAX) {
+            if(!strcasecmp(&(path_copy[strlen(path_copy) - TITLES_MAX]), "\\video_ts"))
+                path_copy[strlen(path_copy) - (TITLES_MAX - 1)] = '\0';
+        }
+
 #endif
-    if( strlen( path_copy ) > TITLES_MAX ) {
-      if( !strcasecmp( &(path_copy[ strlen( path_copy ) - TITLES_MAX ]),
-                       "/video_ts" ) ) {
-        path_copy[ strlen( path_copy ) - TITLES_MAX ] = '\0';
-      }
-    }
 
-    if(path_copy[0] == '\0') {
-      path_copy[0] = '/';
-      path_copy[1] = '\0';
-    }
+        if(strlen(path_copy) > TITLES_MAX) {
+            if(!strcasecmp(&(path_copy[strlen(path_copy) - TITLES_MAX]), "/video_ts"))
+                path_copy[ strlen( path_copy ) - TITLES_MAX ] = '\0';
+        }
+
+        if(path_copy[0] == '\0') {
+            path_copy[0] = '/';
+            path_copy[1] = '\0';
+        }
 
 #if defined(__APPLE__)
-    struct statfs s[128];
-    int r = getfsstat(NULL, 0, MNT_NOWAIT);
-    if (r > 0) {
-        if (r > 128)
-            r = 128;
-        r = getfsstat(s, r * sizeof(s[0]), MNT_NOWAIT);
-        int i;
-        for (i=0; i<r; i++) {
-            if (!strcmp(path_copy, s[i].f_mntonname)) {
-                dev_name = bsd_block2char(s[i].f_mntfromname);
-                fprintf( stderr,
-                        "libdvdread: Attempting to use device %s"
-                        " mounted on %s for CSS authentication\n",
-                        dev_name,
-                        s[i].f_mntonname);
-                auth_drive = DVDOpenImageFile( dev_name, have_css );
-                break;
+
+        struct statfs s[128];
+        int r = getfsstat(NULL, 0, MNT_NOWAIT);
+
+        if (r > 0) {
+            if (r > 128)
+                r = 128;
+
+            r = getfsstat(s, r * sizeof(s[0]), MNT_NOWAIT);
+
+            int i;
+
+            for (i = 0; i < r; i++) {
+                if (!strcmp(path_copy, s[i].f_mntonname)) {
+                    dev_name = bsd_block2char(s[i].f_mntfromname);
+                    fprintf(stderr, "libdvdread: Attempting to use device %s mounted on %s for CSS authentication\n", dev_name, s[i].f_mntonname);
+                    auth_drive = DVDOpenImageFile(dev_name, have_css);
+                    break;
+                }
             }
         }
-    }
+
 #elif defined(SYS_BSD)
-    if( ( fe = getfsfile( path_copy ) ) ) {
-      dev_name = bsd_block2char( fe->fs_spec );
-      fprintf( stderr,
-               "libdvdread: Attempting to use device %s"
-               " mounted on %s for CSS authentication\n",
-               dev_name,
-               fe->fs_file );
-      auth_drive = DVDOpenImageFile( dev_name, have_css );
-    }
+
+        if(fe = getfsfile(path_copy)) {
+            dev_name = bsd_block2char(fe->fs_spec);
+            fprintf(stderr, "libdvdread: Attempting to use device %s mounted on %s for CSS authentication\n", dev_name, fe->fs_file);
+            auth_drive = DVDOpenImageFile(dev_name, have_css);
+        }
+
 #elif defined(__sun)
-    mntfile = fopen( MNTTAB, "r" );
-    if( mntfile ) {
-      struct mnttab mp;
-      int res;
-
-      while( ( res = getmntent( mntfile, &mp ) ) != -1 ) {
-        if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) {
-          dev_name = sun_block2char( mp.mnt_special );
-          fprintf( stderr,
-                   "libdvdread: Attempting to use device %s"
-                   " mounted on %s for CSS authentication\n",
-                   dev_name,
-                   mp.mnt_mountp );
-          auth_drive = DVDOpenImageFile( dev_name, have_css );
-          break;
+
+        mntfile = fopen(MNTTAB, "r");
+
+        if(mntfile) {
+            struct mnttab mp;
+            int res;
+
+            while((res = getmntent(mntfile, &mp)) != -1) {
+                if(res == 0 && !strcmp(mp.mnt_mountp, path_copy)) {
+                    dev_name = sun_block2char(mp.mnt_special);
+                    fprintf( stderr, "libdvdread: Attempting to use device %s mounted on %s for CSS authentication\n", dev_name, mp.mnt_mountp);
+                    auth_drive = DVDOpenImageFile(dev_name, have_css);
+                    break;
+                }
+            }
+            fclose(mntfile);
         }
-      }
-      fclose( mntfile );
-    }
+
 #elif defined(__linux__)
-    mntfile = fopen( _PATH_MOUNTED, "r" );
-    if( mntfile ) {
-      struct mntent *me;
-
-      while( ( me = getmntent( mntfile ) ) ) {
-        if( !strcmp( me->mnt_dir, path_copy ) ) {
-          fprintf( stderr,
-                   "libdvdread: Attempting to use device %s"
-                   " mounted on %s for CSS authentication\n",
-                   me->mnt_fsname,
-                   me->mnt_dir );
-          auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css );
-          dev_name = strdup(me->mnt_fsname);
-          break;
+
+        mntfile = fopen(_PATH_MOUNTED, "r");
+
+        if(mntfile) {
+            struct mntent *me;
+            while(me = getmntent(mntfile)) {
+                if(!strcmp(me->mnt_dir, path_copy)) {
+                    fprintf(stderr, "libdvdread: Attempting to use device %s mounted on %s for CSS authentication\n", me->mnt_fsname, me->mnt_dir);
+                    auth_drive = DVDOpenImageFile(me->mnt_fsname, have_css);
+                    dev_name = strdup(me->mnt_fsname);
+                    break;
+                }
+            }
+            fclose(mntfile);
         }
-      }
-      fclose( mntfile );
-    }
+
 #elif defined(_WIN32) || defined(__OS2__)
 #ifdef __OS2__
-    /* Use DVDOpenImageFile() only if it is a drive */
-    if(isalpha(path[0]) && path[1] == ':' &&
-        ( !path[2] ||
-          ((path[2] == '\\' || path[2] == '/') && !path[3])))
+
+        /* Use DVDOpenImageFile() only if it is a drive */
+        if(isalpha(path[0]) && path[1] == ':' && ( !path[2] || ((path[2] == '\\' || path[2] == '/') && !path[3])))
+
 #endif
-    auth_drive = DVDOpenImageFile( path, have_css );
+            auth_drive = DVDOpenImageFile(path, have_css);
 #endif
 
 #if !defined(_WIN32) && !defined(__OS2__)
-    if( !dev_name ) {
-      fprintf( stderr, "libdvdread: Couldn't find device name.\n" );
-    } else if( !auth_drive ) {
-      fprintf( stderr, "libdvdread: Device %s inaccessible, "
-               "CSS authentication not available.\n", dev_name );
-    }
+            if(!dev_name)
+                fprintf(stderr, "libdvdread: Couldn't find device name.\n");
+            else if(!auth_drive)
+                fprintf(stderr, "libdvdread: Device %s inaccessible, CSS authentication not available.\n", dev_name);
 #else
-    if( !auth_drive ) {
-        fprintf( stderr, "libdvdread: Device %s inaccessible, "
-                 "CSS authentication not available.\n", path );
-    }
+            if(!auth_drive)
+                fprintf(stderr, "libdvdread: Device %s inaccessible, CSS authentication not available.\n", path);
 #endif
 
-    free( dev_name );
-    dev_name = NULL;
-    free( path_copy );
-    path_copy = NULL;
-
-    /**
-     * If we've opened a drive, just use that.
-     */
-    if( auth_drive ) {
-      free(path);
-      return auth_drive;
+        free(dev_name);
+        dev_name = NULL;
+
+        free(path_copy);
+        path_copy = NULL;
+
+        /* If we've opened a drive, just use that. */
+        if(auth_drive) {
+            free(path);
+            return auth_drive;
+        }
+
+        /* Otherwise, we now try to open the directory tree instead. */
+        ret_val = DVDOpenPath(path);
+        free(path);
+        return ret_val;
+
     }
-    /**
-     * Otherwise, we now try to open the directory tree instead.
-     */
-    ret_val = DVDOpenPath( path );
-      free( path );
-      return ret_val;
-  }
 
-DVDOpen_error:
-  /* If it's none of the above, screw it. */
-  fprintf( stderr, "libdvdread: Could not open %s\n", path );
-  free( path );
-  free( path_copy );
-  if ( cdir >= 0 )
-    close( cdir );
-  free( new_path );
-  return NULL;
+    /* If it's none of the above, screw it. */
+    DVDOpen_error:
+
+        fprintf(stderr, "libdvdread: Could not open %s\n", path);
+        free(path);
+        free(path_copy);
+        if (cdir >= 0)
+            close(cdir);
+        free(new_path);
+        return NULL;
+
 }
 
 void DVDClose( dvd_reader_t *dvd )
-- 
2.0.4



More information about the libdvdnav-devel mailing list