Doxygen Source Code Documentation
thd_filestuff.c File Reference
#include "mrilib.h"#include "thd.h"#include <sys/vfs.h>Go to the source code of this file.
| Defines | |
| #define | FNAME(a, b) statfs(a,b) | 
| #define | FSTYP statfs | 
| #define | BSIZE f_bsize | 
| #define | BFREE f_bavail | 
| Functions | |
| time_t | THD_file_mtime (char *pathname) | 
| int | THD_is_ondisk (char *pathname) | 
| int | THD_cwd (char *pathname) | 
| int | THD_mkdir (char *pathname) | 
| int | THD_is_file (char *pathname) | 
| int | THD_is_symlink (char *pathname) | 
| unsigned long | THD_filesize (char *pathname) | 
| int | THD_is_directory (char *pathname) | 
| int | THD_is_executable (char *pathname) | 
| int | THD_equiv_files (char *path1, char *path2) | 
| char * | THD_trailname (char *fname, int lev) | 
| int | THD_filename_ok (char *name) | 
| int | THD_filename_pure (char *name) | 
| int | THD_freemegabytes (char *pathname) | 
Define Documentation
| 
 | 
| 
 Definition at line 273 of file thd_filestuff.c. | 
| 
 | 
| 
 Definition at line 272 of file thd_filestuff.c. | 
| 
 | 
| 
 Definition at line 270 of file thd_filestuff.c. | 
| 
 | 
| 
 Definition at line 271 of file thd_filestuff.c. Referenced by THD_freemegabytes(). | 
Function Documentation
| 
 | 
| Change working directory. Definition at line 37 of file thd_filestuff.c. 
 00038 {
00039    if( pathname == NULL || *pathname == '\0' ) return 0 ;
00040    return ( chdir(pathname) == 0 ) ;
00041 }
 | 
| 
 | ||||||||||||
| Determine if two filenames are really the same thing. Definition at line 160 of file thd_filestuff.c. Referenced by AFNI_append_sessions(), AFNI_finalize_read_sess_CB(), NLFIT_get_many_MODELs(), PLUG_get_many_plugins(), THD_get_many_timeseries(), THD_getpathprogs(), and THD_normalize_flist(). 
 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 }
 | 
| 
 | 
| Return the time at which the file was last modified. Definition at line 13 of file thd_filestuff.c. Referenced by main(). 
 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 }
 | 
| 
 | 
| Check if a filename is OK - that is, has no crummy characters. The filename can have a '/' in it. To insist that there be not '/', use THD_filename_pure(). The list of crummy characters can be inferred from the source code. Definition at line 220 of file thd_filestuff.c. References name. Referenced by AFNI_finalize_saveim_CB(), AFNI_finalize_write_palette_CB(), AFNI_finalsave_layout_CB(), AFNI_process_setup(), CL_read_opts(), init_options(), ISQ_snapfile(), main(), multivector_write(), PRIC_main(), RCREND_finalize_saveim_CB(), RCREND_save_many_CB(), RCREND_save_this_CB(), REND_finalize_saveim_CB(), REND_save_many_CB(), REND_save_this_CB(), SUMA_AFNI_Extract_Colors(), SUMA_AfniPrefix(), SUMA_BrainVoyager_Read_vmr(), SUMA_OpenDX_Read_CruiseVolHead(), T3D_initialize_user_data(), TAG_write_CB(), THD_filename_pure(), THD_mkdir(), THD_warp3D(), THD_write_minc(), THD_write_nifti(), THD_zeropad(), tross_commandline(), validate_datasets(), VOLREG_main(), and WINsorize(). 
 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 }
 | 
| 
 | 
| Check if a filename is pure - no crummy characters, no '/'. Definition at line 245 of file thd_filestuff.c. References name, and THD_filename_ok(). Referenced by AFNI_process_environ(), AFNI_process_setup(), main(), PLUGIN_init(), process_NIML_Node_ROI(), RT_check_info(), RT_main(), RT_process_info(), and SUMA_AFNI_Extract_Colors(). 
 00246 {
00247    int ii ;
00248 
00249    ii = THD_filename_ok( name ) ;
00250    if( ii ) ii = (strstr(name,"/") == NULL) ;
00251    return ii ;
00252 }
 | 
| 
 | 
| Return the file length (0 if file not found). Definition at line 118 of file thd_filestuff.c. Referenced by AFNI_logger(), AFNI_suck_file(), csfft_cox(), ge4_read_header(), ge_header(), main(), mri_imcount_dicom(), mri_read3D_analyze75(), mri_read_analyze75(), mri_read_dicom(), read_ge_header(), read_URL_ftp(), read_URL_http(), suck_file(), SUMA_BinarySuck(), SUMA_BrainVoyager_Read_vmr(), SUMA_file_suck(), SUMA_suck_file(), THD_get_all_timeseries(), THD_load_datablock(), THD_open_analyze(), THD_open_ctfmri(), THD_open_ctfsam(), and THD_open_one_dataset(). 
 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 }
 | 
| 
 | 
| Get free space (in megabytes) on a disk partition. Return value is -1 if can't be determined. ---------------------------------------------------------------- Definition at line 288 of file thd_filestuff.c. Referenced by AFNI_startup_timeout_CB(). 
 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 }
 | 
| 
 | 
| Determine if this is really a directory or not. Definition at line 130 of file thd_filestuff.c. Referenced by adwarp_refashion_dataset(), AFNI_finalize_read_palette_CB(), AFNI_finalize_read_sess_CB(), AFNI_read_inputs(), AFNI_refashion_dataset(), main(), mpeg_setup(), mri_imcount_mpeg(), mri_read_mpeg(), NLFIT_get_all_MODELs(), NLFIT_get_many_MODELs(), PLUG_get_all_plugins(), PLUG_get_many_plugins(), T3D_check_data(), THD_extract_directories(), THD_get_all_filenames(), THD_get_many_timeseries(), THD_getpathprogs(), THD_init_session(), THD_mkdir(), and THD_write_datablock(). 
 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 }
 | 
| 
 | 
| Determine if this is really an executable file or not. Definition at line 142 of file thd_filestuff.c. Referenced by THD_get_all_executables(). 
 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 }
 | 
| 
 | 
| 
 | 
| Determine if this exists at all (file, directory, ...). Definition at line 25 of file thd_filestuff.c. Referenced by ISQ_snapfile(), main(), THD_mkdir(), and XSAVE_output(). 
 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 }
 | 
| 
 | 
| Determine if this is really a symbolic link or not. Definition at line 106 of file thd_filestuff.c. 
 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 }
 | 
| 
 | 
| Create a directory. Returns 1 if OK, 0 if not. Definition at line 46 of file thd_filestuff.c. References free, THD_filename_ok(), THD_is_directory(), and THD_is_ondisk(). Referenced by mri_imcount_mpeg(), and mri_read_mpeg(). 
 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 }
 | 
| 
 | ||||||||||||
| Find a 'trailing name in a pathname. For example, for fname = "/bob/cox/is/the/author/of/AFNI", 
 Definition at line 185 of file thd_filestuff.c. Referenced by AFNI_set_window_titles(), AFNI_startup_timeout_CB(), AFNI_update_surface_widgets(), AFNI_write_many_dataset_CB(), ENV_sesstrail(), MAIN_workprocess(), make_PLUGIN_dataset_link(), PLUG_get_many_plugins(), PLUG_read_plugin(), PLUTO_commandstring(), read_URL_tmpdir(), TAG_get_dset_CB(), THD_fetch_many_datasets(), THD_find_executable(), THD_get_all_timeseries(), THD_init_session(), THD_open_1D(), THD_open_3D(), THD_open_analyze(), THD_open_ctfmri(), THD_open_ctfsam(), THD_open_minc(), THD_open_mpeg(), THD_open_nifti(), and write_bucket_data(). 
 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 }
 | 
 
                             
                             
                             
                             
                             
                             
                             
                             
                             
                             
                             
                             
 
 
 
 
       
	   
	   
	   
	  