"Fossies" - the Fresh Open Source Software Archive

Member "radmind-1.15.3/radmind-/mkprefix.c" (30 Nov 2020, 2143 Bytes) of package /linux/privat/radmind-1.15.3.tar.gz:


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) 2003 Regents of The University of Michigan.
    3  * All Rights Reserved.  See COPYRIGHT.
    4  */
    5 
    6 #include "config.h"
    7 
    8 #include <sys/types.h>
    9 #include <sys/stat.h>
   10 #include <errno.h>
   11 #include <stdio.h>
   12 #include <string.h>
   13 #include <unistd.h>
   14 #include <sys/param.h>
   15 
   16 #include "mkprefix.h"
   17 
   18 int extern  quiet;
   19 int extern  showprogress;
   20 
   21 /* mkprefix attempts to create intermediate directories of path.
   22  * Intermediate directories are created with the permission of the
   23  * mode and UID of the last pre-existing parent directory. 
   24  */
   25 
   26     int 
   27 mkprefix( char *path ) 
   28 {
   29     char    *p, parent_path[ MAXPATHLEN * 2 ];
   30     int     saved_errno, parent_stats = 0;
   31     uid_t   e_uid;
   32     struct stat st, parent_st;
   33     mode_t  mode = 0777;
   34 
   35     e_uid = geteuid();
   36 
   37     /* Move past any leading /'s */
   38     for ( p = path; *p == '/'; p++ )
   39     ;
   40 
   41     /* Attempt to create each intermediate directory of path */
   42     for ( p = strchr( p, '/' ); p != NULL; p = strchr( p, '/' )) {
   43     *p = '\0';
   44     if ( mkdir( path, mode ) < 0 ) {
   45         /* Only error if path exists and it's not a directory */
   46         saved_errno = errno;
   47         if ( stat( path, &st ) != 0 ) {
   48         errno = saved_errno;
   49         return( -1 );
   50         }
   51         if ( !S_ISDIR( st.st_mode )) {
   52         errno = EEXIST;
   53         return( -1 );
   54         }
   55         errno = 0;
   56         *p++ = '/';
   57         continue;
   58     }
   59 
   60     /* Get stats from parent of first missing directory */
   61     if ( !parent_stats ) {
   62         if ( snprintf( parent_path, MAXPATHLEN, "%s/..", path)
   63             > MAXPATHLEN ) {
   64         fprintf( stderr, "%s/..: path too long\n", path );
   65         *p++ = '/';
   66         return( -1 );
   67         }
   68         if ( stat( parent_path, &parent_st ) != 0 ) {
   69         return( -1 );
   70         }
   71         parent_stats = 1;
   72     }
   73 
   74     /* Set mode to that of last preexisting parent */
   75     if ( mode != parent_st.st_mode ) {
   76         if ( chmod( path, parent_st.st_mode ) != 0 ) {
   77         return( -1 );
   78         }
   79     }
   80 
   81     /* Set uid to that of last preexisting parent */
   82     if ( e_uid != parent_st.st_uid ) {
   83         if ( chown( path, parent_st.st_uid, parent_st.st_gid ) != 0 ) {
   84         return( -1 );
   85         }
   86     }
   87 
   88     if ( !quiet && !showprogress ) {
   89         printf( "%s: created missing prefix\n", path );
   90     }
   91 
   92     *p++ = '/';
   93     }
   94     return( 0 );
   95 }