Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
file.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 #include "tk.h"
00046 
00047 #include "all.h"
00048 
00049 #include <sys/file.h>
00050 #include <sys/stat.h>
00051 #include <sys/param.h>
00052 #include <time.h>
00053 #include <string.h>
00054 #include <dirent.h>
00055 #include <strings.h>
00056 
00057 #define MAX_FILES   1000
00058 #define MAX_NAME_LEN    256
00059 #define MAX_STRING_LEN  MAX_NAME_LEN
00060 
00061 typedef int boolean;
00062 #define TRUE 1
00063 #define FALSE 0
00064 
00065 extern char currentPath[MAXPATHLEN];
00066 
00067 char    globString[1024];
00068 
00069 static DIR *dfd;
00070 
00071 void    ResetPath(void);
00072 int ListDirectory(ClientData nulldata, Tcl_Interp *interp, int argc,
00073                   char **argv);
00074 int ChangeDirectory(ClientData nulldata, Tcl_Interp *interp, int argc,
00075                   char **argv);
00076 void    SortFiles(int numStrings, char strings[MAX_FILES][MAX_NAME_LEN],
00077                   boolean *dirList, int permute[]);
00078 
00079 static void     UpdatePath(Tcl_Interp *interp, char *directory);
00080 static boolean  MatchesGlob(char *string, char *glob);
00081 
00082 
00083 
00084 void    ResetPath()
00085 {
00086     if ( getwd(currentPath) == 0 )
00087     {
00088         fprintf(stderr, "Error getting pathname!!!\n");
00089         exit(1);
00090     }
00091 
00092     strcpy(¤tPath[strlen(currentPath)], "/");
00093 
00094     dfd = opendir(currentPath);
00095     if ( dfd == NULL )
00096     {
00097         fprintf(stderr, "can't open '%s'\n", currentPath);
00098         exit(1);
00099     }
00100 }
00101 
00102 
00103 static void     UpdatePath(Tcl_Interp *interp, char *directory)
00104 {
00105     int length;
00106     char *charPtr;
00107 
00108     length = strlen(currentPath);
00109 
00110     if ( strcmp(directory, "./") == 0 )
00111         return  ;
00112     else if ( strcmp(directory, "../") == 0 )
00113     {
00114         
00115 
00116         if ( length < 2 )
00117         {
00118             fprintf(stderr, "Error:  backing up from root directory!!!\n");
00119             exit(1);
00120         }
00121 
00122         charPtr = ¤tPath[length-2];
00123         while ( (charPtr != currentPath) && (*charPtr != '/') )
00124             charPtr--;
00125         charPtr++;      
00126         *charPtr = '\0';
00127     }
00128     else
00129     {
00130         strcpy(¤tPath[length], directory);
00131     }
00132 }
00133 
00134 
00135 int ChangeDirectory(ClientData nulldata, Tcl_Interp *interp, int argc,
00136                   char **argv)
00137 {
00138     char *directory = argv[1];
00139 
00140     UpdatePath(interp, directory);
00141 
00142     fprintf(stdout, "Opening directory: '%s'\n", currentPath);
00143 
00144     dfd = opendir(currentPath);
00145     if ( dfd == NULL )
00146     {
00147         fprintf(stderr, "can't open '%s'\n", currentPath);
00148         return TCL_OK;  
00149     }
00150 
00151     return TCL_OK;
00152 }
00153 
00154 
00155 int ListDirectory(ClientData nulldata, Tcl_Interp *interp, int argc,
00156                   char **argv)
00157 {
00158     struct dirent *dp;
00159     struct stat stbuf;
00160     char command[256];
00161     char fileName[MAX_FILES][MAX_NAME_LEN];
00162     boolean dirList[MAX_FILES];
00163     int permute[MAX_FILES];
00164     int fileCount = 0;
00165     register int index;
00166     char fullName[MAXPATHLEN];
00167     char    *restPtr;
00168 
00169     sprintf(command, "ShowCurrentDirectory %s", currentPath);
00170     Tcl_Eval(interp, command, 0, (char **) NULL);
00171 
00172     if ( dfd == NULL )
00173     {
00174         fprintf(stderr, "TRIED TO LIST NULL DIRECTORY\n");
00175 
00176         return TCL_OK;
00177     }
00178 
00179 
00180     if ( strlen(currentPath) != 1 )
00181     {
00182         sprintf(fileName[fileCount], "../");
00183         dirList[fileCount] = TRUE;
00184         fileCount++;
00185     }
00186 
00187     strcpy(fullName, currentPath);
00188     restPtr = &fullName[strlen(fullName)];
00189 
00190     while ( (dp = readdir(dfd)) != NULL )
00191     {
00192         strcpy(restPtr, dp->d_name);
00193         stat(fullName, &stbuf);
00194 
00195         if ( dp->d_name[0] != '.' )
00196         {
00197             if ( S_ISDIR(stbuf.st_mode) )
00198             {
00199                 sprintf(fileName[fileCount], "%s/", dp->d_name);
00200                 dirList[fileCount] = TRUE;
00201                 fileCount++;
00202             }
00203             else
00204             {
00205                 if ( MatchesGlob(dp->d_name, globString) )
00206                 {
00207                     strcpy(fileName[fileCount], dp->d_name);
00208                     dirList[fileCount] = FALSE;
00209                     fileCount++;
00210                 }
00211             }
00212         }
00213     }
00214 
00215     SortFiles(fileCount, fileName, dirList, permute);
00216 
00217     for ( index = 0; index < fileCount; index++ )
00218     {
00219         sprintf(command, "AddBrowseFile %s", fileName[permute[index]]);
00220         Tcl_Eval(interp, command, 0, (char **) NULL);
00221     }
00222 
00223     closedir(dfd);
00224 
00225     return TCL_OK;
00226 }
00227 
00228 
00229 void    SortFiles(int numStrings, char strings[MAX_FILES][MAX_NAME_LEN],
00230                   boolean *dirList, int permute[])
00231 {
00232     register int i, j;
00233     int temp;
00234     int numDirs;
00235     int ptr;
00236 
00237     for ( i = 0; i < numStrings; i++ )
00238         permute[i] = i;
00239 
00240     
00241     numDirs = 0;
00242     ptr = numStrings-1;
00243     while ( numDirs != ptr )
00244     {
00245         
00246         while ( (numDirs < ptr) && (dirList[permute[numDirs]]) )
00247             numDirs++;
00248 
00249         
00250         while ( (numDirs < ptr) && (! dirList[permute[ptr]]) )
00251             ptr--;
00252 
00253         if ( numDirs != ptr )
00254         {
00255             temp = permute[numDirs];
00256             permute[numDirs] = ptr;
00257             permute[ptr] = temp;
00258         }
00259     }
00260 
00261     if ( dirList[permute[numDirs]] )
00262         numDirs++;
00263 
00264     for ( i = 0; i < numDirs; i++ )
00265         for ( j = i+1; j < numDirs; j++ )
00266         {
00267             if ( strcmp(&strings[permute[j]][0], &strings[permute[i]][0]) < 0 )
00268             {
00269                 temp = permute[j];
00270                 permute[j] = permute[i];
00271                 permute[i] = temp;
00272             }
00273         }
00274 
00275     for ( i = numDirs; i < numStrings; i++ )
00276         for ( j = i+1; j < numStrings; j++ )
00277         {
00278             if ( strcmp(&strings[permute[j]][0], &strings[permute[i]][0]) < 0 )
00279             {
00280                 temp = permute[j];
00281                 permute[j] = permute[i];
00282                 permute[i] = temp;
00283             }
00284         }
00285 }
00286 
00287 
00288 int SetBrowseGlob (ClientData nulldata, Tcl_Interp *interp,
00289                    int argc, char **argv)
00290 {
00291     if (argc == 2 )
00292     {
00293         strcpy(globString, argv[1]);
00294 
00295         fprintf(stdout, "GLOB:  %s\n", globString);
00296 
00297         return TCL_OK;
00298     }
00299 
00300         Tcl_AppendResult (interp, 
00301             "wrong args: should be \"", argv[0]," string\"", (char *) NULL);
00302         return TCL_ERROR;
00303 }
00304 
00305 
00306 static boolean  MatchesGlob(char *string, char *glob)
00307 {
00308     char    *stringRight, *globRight;
00309 
00310     while ( (*glob != '\0') && (*glob != '*') )     
00311     {
00312         if ( (*string == '\0') || (*string != *glob) )
00313             return FALSE;
00314         string++;
00315         glob++;
00316     }
00317 
00318     if ( *glob == '\0' )        
00319         return TRUE;
00320 
00321     
00322     stringRight = &string[strlen(string)-1];
00323     globRight = &glob[strlen(glob)-1];
00324 
00325     while ( *globRight != '*' )
00326     {
00327         if ( (stringRight < string) || (*stringRight != *globRight) )
00328             return FALSE;
00329         globRight--;
00330         stringRight--;
00331     }
00332 
00333     return TRUE;
00334 }