Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
thd_filestuff.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 #include "mrilib.h"
00008 #include "thd.h"
00009 
00010 
00011 
00012 
00013 time_t THD_file_mtime( char *pathname )  
00014 {
00015    static struct stat buf ; int ii ;
00016 
00017    if( pathname == NULL || *pathname == '\0' ) return 0 ;
00018    ii = stat( pathname , &buf ) ; if( ii != 0 ) return 0 ;
00019    return (time_t)buf.st_mtime ;
00020 }
00021 
00022 
00023 
00024 
00025 int THD_is_ondisk( char *pathname )  
00026 {
00027    static struct stat buf ; int ii ;
00028 
00029    if( pathname == NULL || *pathname == '\0' ) return 0 ;
00030    ii = stat( pathname , &buf ) ;
00031    return (ii == 0) ;
00032 }
00033 
00034 
00035 
00036 
00037 int THD_cwd( char *pathname )    
00038 {
00039    if( pathname == NULL || *pathname == '\0' ) return 0 ;
00040    return ( chdir(pathname) == 0 ) ;
00041 }
00042 
00043 
00044 
00045 
00046 int THD_mkdir( char *pathname )  
00047 {
00048    int lp , ii , jj ;
00049    char *pnam ;
00050 
00051    
00052 
00053    if( !THD_filename_ok(pathname) ) return 0 ;
00054    if(  THD_is_ondisk  (pathname) ) return THD_is_directory(pathname) ;
00055 
00056    pnam = strdup(pathname) ;  
00057    lp = strlen(pnam) ; ii = 0 ;
00058 
00059    
00060 
00061    while(1){
00062 
00063      
00064 
00065 
00066      ii += strspn(pnam+ii,"/") ; ii += strcspn(pnam+ii,"/") ;
00067 
00068      
00069 
00070      if( ii < lp ) pnam[ii] = '\0' ;
00071 
00072      
00073 
00074      if( !THD_is_directory(pnam) ){
00075        jj = mkdir( pnam , 0755 ) ;
00076        if( jj != 0 ){ free(pnam); return 0; } 
00077      }
00078 
00079      
00080 
00081      if( ii == lp ){ free(pnam); return 1; }  
00082 
00083      
00084 
00085      pnam[ii] = '/' ;
00086    }
00087 
00088    return 0 ; 
00089 }
00090 
00091 
00092 
00093 
00094 int THD_is_file( char *pathname )
00095 {
00096    static struct stat buf ; int ii ;
00097 
00098    if( pathname == NULL || *pathname == '\0' ) return 0 ;
00099    ii = stat( pathname , &buf ) ; if( ii != 0 ) return 0 ;
00100    ii = (buf.st_mode & S_IFREG) != 0 ; return ii ;
00101 }
00102 
00103 
00104 
00105 
00106 int THD_is_symlink( char *pathname )  
00107 {
00108    char buf[32] ; int ii ;
00109 
00110    if( pathname == NULL || *pathname == '\0' ) return 0 ;
00111    ii = readlink( pathname , buf , 32 ) ;
00112    return (ii > 0) ;
00113 }
00114 
00115 
00116 
00117 
00118 unsigned long THD_filesize( char *pathname )
00119 {
00120    static struct stat buf ; int ii ;
00121 
00122    if( pathname == NULL || *pathname == '\0' ) return 0 ;
00123    ii = stat( pathname , &buf ) ; if( ii != 0 ) return 0 ;
00124    return buf.st_size ;
00125 }
00126 
00127 
00128 
00129 
00130 int THD_is_directory( char *pathname )
00131 {
00132    static struct stat buf ; int ii ;
00133 
00134    if( pathname == NULL || *pathname == '\0' ) return 0 ;
00135    ii = stat( pathname , &buf ) ; if( ii != 0 ) return 0 ;
00136    ii = (buf.st_mode & S_IFDIR) != 0 ; return ii ;
00137 }
00138 
00139 
00140 
00141 
00142 int THD_is_executable( char *pathname )  
00143 {
00144    static struct stat buf ; int ii ;
00145 
00146    if( pathname == NULL || *pathname == '\0' )  return 0  ;
00147    ii = stat( pathname , &buf )      ; if( ii ) return 0  ;
00148    ii = (buf.st_mode & S_IXOTH) != 0 ; if( ii ) return ii ;
00149 
00150    
00151 
00152    ii = ( getuid() == buf.st_uid       &&
00153           (buf.st_mode & S_IXUSR) != 0   ) ;
00154    return ii ;
00155 }
00156 
00157 
00158 
00159 
00160 int THD_equiv_files( char *path1 , char *path2 )
00161 {
00162    static struct stat buf1 , buf2 ; int ii ;
00163 
00164    if( path1 == NULL || path2 == NULL ) return -1 ;
00165    ii = stat( path1 , &buf1 ) ; if( ii != 0 ) return -1 ;
00166    ii = stat( path2 , &buf2 ) ; if( ii != 0 ) return -1 ;
00167 
00168    ii = (buf1.st_dev == buf2.st_dev) && (buf1.st_ino == buf2.st_ino) ;
00169    return ii ;
00170 }
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 char * THD_trailname( char *fname , int lev )
00186 {
00187    int fpos , flen , flev ;
00188 
00189    if( fname == NULL || (flen=strlen(fname)) <= 1 ) return fname ;
00190 
00191    if( lev < 0 ) lev = 0 ;
00192 
00193    flev = 0 ;
00194    fpos = flen ;
00195    if( fname[fpos-1] == '/' ) fpos-- ;  
00196 
00197    
00198 
00199 
00200 
00201    while( fpos > 0 ){
00202 
00203       if( fname[fpos-1] == '/' ){
00204          flev++ ; if( flev >  lev ) break ;  
00205       }
00206       fpos-- ;  
00207    }
00208 
00209    return (fname+fpos) ;
00210 }
00211 
00212 
00213 
00214 
00215 
00216 
00217 
00218 
00219 
00220 int THD_filename_ok( char *name )  
00221 {
00222    int ll , ii ;
00223 
00224    if( name == NULL ) return 0 ;
00225    ll = strlen( name ) ; if( ll == 0 ) return 0 ;
00226 
00227    for( ii=0 ; ii < ll ; ii++ )
00228       if( iscntrl(name[ii]) || isspace(name[ii]) ||
00229                                name[ii] == ';'   ||
00230           name[ii] == '*'   || name[ii] == '?'   ||
00231           name[ii] == '&'   || name[ii] == '|'   ||
00232           name[ii] == '"'   || name[ii] == '>'   ||
00233           name[ii] == '<'   || name[ii] == '\''  ||
00234           name[ii] == '['   || name[ii] == ']'   ||
00235           name[ii] == '('   || name[ii] == ')'   ||
00236           name[ii] == '{'   || name[ii] == '}'   ||
00237           name[ii] == '!'   || (name[ii] & 128) != 0 ) return 0 ;
00238 
00239    return 1 ;
00240 }
00241 
00242 
00243 
00244 
00245 int THD_filename_pure( char *name )  
00246 {
00247    int ii ;
00248 
00249    ii = THD_filename_ok( name ) ;
00250    if( ii ) ii = (strstr(name,"/") == NULL) ;
00251    return ii ;
00252 }
00253 
00254 
00255 
00256 #undef FNAME
00257 #undef FSTYP
00258 #undef BSIZE
00259 #undef BFREE
00260 
00261 #if defined(DARWIN) || defined(FreeBSD)  
00262 #  include <sys/param.h>
00263 #  include <sys/mount.h>
00264 #  define FNAME(a,b) statfs(a,b)
00265 #  define FSTYP      statfs
00266 #  define BSIZE      f_bsize
00267 #  define BFREE      f_bavail
00268 #elif defined(LINUX)                     
00269 #  include <sys/vfs.h>
00270 #  define FNAME(a,b) statfs(a,b)
00271 #  define FSTYP      statfs
00272 #  define BSIZE      f_bsize
00273 #  define BFREE      f_bavail
00274 #elif defined(SOLARIS) || defined(SGI)   
00275 #  include <sys/types.h>
00276 #  include <sys/statvfs.h>
00277 #  define FNAME(a,b) statvfs64(a,b)
00278 #  define FSTYP      statvfs64
00279 #  define BSIZE      f_bsize
00280 #  define BFREE      f_bavail
00281 #endif
00282 
00283 
00284 
00285 
00286 
00287 
00288 int THD_freemegabytes( char *pathname )
00289 {
00290 #ifdef FNAME
00291    int ii ; struct FSTYP buf ;
00292    if( pathname == NULL || *pathname == '\0' ) return -1 ;
00293    ii = FNAME( pathname , &buf ) ;
00294    if( ii ) return -1 ;
00295    ii = (int)((double)(buf.BFREE) * (double)(buf.BSIZE) / (1024.0*1024.0)) ;
00296    return ii ;
00297 #else
00298    return -1 ;
00299 #endif
00300 }