Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

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    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 #include "mrilib.h"
00008 #include "thd.h"
00009 
00010 /*-------------------------------------------------------------*/
00011 /*! Return the time at which the file was last modified. */
00012 
00013 time_t THD_file_mtime( char *pathname )  /* 05 Dec 2001 */
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 /*! Determine if this exists at all (file, directory, ...). */
00024 
00025 int THD_is_ondisk( char *pathname )  /* 19 Dec 2002 */
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 /*! Change working directory. */
00036 
00037 int THD_cwd( char *pathname )    /* 19 Dec 2002 */
00038 {
00039    if( pathname == NULL || *pathname == '\0' ) return 0 ;
00040    return ( chdir(pathname) == 0 ) ;
00041 }
00042 
00043 /*-----------------------------------------------------------*/
00044 /*! Create a directory.  Returns 1 if OK, 0 if not. */
00045 
00046 int THD_mkdir( char *pathname )  /* 19 Dec 2002 */
00047 {
00048    int lp , ii , jj ;
00049    char *pnam ;
00050 
00051    /* check if input is OK, or if it already exists */
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) ;  /* modifiable copy */
00057    lp = strlen(pnam) ; ii = 0 ;
00058 
00059    /* loop over path segments, creating them if needed */
00060 
00061    while(1){
00062 
00063      /* advance ii to point to end of next path segment,
00064         at the next '/' character, or at the end of pnam */
00065 
00066      ii += strspn(pnam+ii,"/") ; ii += strcspn(pnam+ii,"/") ;
00067 
00068      /* insert a NUL to replace the '/', temporarily */
00069 
00070      if( ii < lp ) pnam[ii] = '\0' ;
00071 
00072      /* if this segment doesn't already exist, create it */
00073 
00074      if( !THD_is_directory(pnam) ){
00075        jj = mkdir( pnam , 0755 ) ;
00076        if( jj != 0 ){ free(pnam); return 0; } /* bad */
00077      }
00078 
00079      /* if reached end of path string, we're done */
00080 
00081      if( ii == lp ){ free(pnam); return 1; }  /* good */
00082 
00083      /* reinsert '/' if it was excised */
00084 
00085      pnam[ii] = '/' ;
00086    }
00087 
00088    return 0 ; /* unreachable */
00089 }
00090 
00091 /*-----------------------------------------------------------*/
00092 /*! Determine if this is really a regular file or not. */
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 /*! Determine if this is really a symbolic link or not. */
00105 
00106 int THD_is_symlink( char *pathname )  /* 03 Mar 1999 */
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 /*! Return the file length (0 if file not found). */
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 /*! Determine if this is really a directory or not. */
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 /*! Determine if this is really an executable file or not. */
00141 
00142 int THD_is_executable( char *pathname )  /* 26 Jun 2001 */
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    /* 15 Jul 2002: also check if file is owned & executable by user */
00151 
00152    ii = ( getuid() == buf.st_uid       &&
00153           (buf.st_mode & S_IXUSR) != 0   ) ;
00154    return ii ;
00155 }
00156 
00157 /*--------------------------------------------------------------*/
00158 /*! Determine if two filenames are really the same thing. */
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 /*! Find a 'trailing name in a pathname.
00174 
00175    For example, for fname = "/bob/cox/is/the/author/of/AFNI",
00176      - the lev=0 trailing name is "AFNI",
00177      - the lev=1 trailing name is "of/AFNI",
00178      - the lev=2 trailing name is "author/of/AFNI", and so on.
00179    That is, "lev" is the number of directory names above the
00180    last name to keep.  The pointer returned is to some place
00181    in the middle of fname; that is, this is not a malloc()-ed
00182    string, so don't try to free() it!.
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-- ;  /* skip trailing slash */
00196 
00197    /* fpos   = index of latest character I've accepted,
00198       fpos-1 = index of next character to examine,
00199       flev   = number of directory levels found so far */
00200 
00201    while( fpos > 0 ){
00202 
00203       if( fname[fpos-1] == '/' ){
00204          flev++ ; if( flev >  lev ) break ;  /* reached the lev we like */
00205       }
00206       fpos-- ;  /* scan backwards */
00207    }
00208 
00209    return (fname+fpos) ;
00210 }
00211 
00212 /*----------------------------------------------------------------------*/
00213 /*! Check if a filename is OK - that is, has no crummy characters.
00214 
00215   The filename can have a '/' in it.  To insist that there be not '/',
00216   use THD_filename_pure().
00217   The list of crummy characters can be inferred from the source code.
00218 */
00219 
00220 int THD_filename_ok( char *name )  /* 24 Apr 1997 */
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 /*! Check if a filename is pure - no crummy characters, no '/'. */
00244 
00245 int THD_filename_pure( char *name )  /* 28 Feb 2001 */
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)  /* Mac or BSD */
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)                     /* 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)   /* Sun or 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 /*! Get free space (in megabytes) on a disk partition.
00285     Return value is -1 if can't be determined.
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 }
 

Powered by Plone

This site conforms to the following standards: