home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume17 / tcl-editor / part02 / findfiles.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-18  |  2.5 KB  |  117 lines

  1. /* $Header: /nfs/unmvax/faculty/crowley/x/pt/RCS/findfiles.c,v 1.5 1992/03/04 17:07:18 crowley Exp crowley $ */
  2.  
  3. #include <sys/types.h>
  4. #include "pt.h"
  5. #ifdef SYSV
  6. #include <dirent.h>
  7. #else
  8. #include <sys/dir.h>
  9. #endif
  10. #include <ctype.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13.  
  14. /* scratch file name */
  15. char scratchFileName[FILENAMESIZE];
  16.  
  17. char *
  18. makeFullPathname(origName)
  19.     char *origName;
  20. {
  21.     extern char msgBuffer[];
  22.     extern char scratchFileName[];
  23.  
  24.     int n;
  25.     register char *p;
  26.     char *fromPtr, *toPtr;
  27.  
  28.     /* first figure out what we have to do */
  29.     if( origName[0] != '/' ) {
  30.         /* must prepend the current drive and directory */
  31.         (void)getcwd(scratchFileName, FILENAMESIZE);
  32.         n = strlen(scratchFileName);
  33.         if( scratchFileName[n-1] != '/' ) {
  34.             scratchFileName[n++] = '/';
  35.             scratchFileName[n] = '\0';
  36.         }
  37.     } else
  38.         scratchFileName[0] = '\0';
  39.     strncat(scratchFileName, origName, FILENAMESIZE);
  40.     
  41.     /* now eliminate any ".." components */
  42.     p = scratchFileName;
  43.     while( *p != '\0' ) {
  44.         /* look for a "/../" */
  45.         if( *p=='.' && *(p+1)=='.' && *(p-1)=='/' && *(p+2)=='/' ) {
  46.             /* find the previous path component */
  47.             n = 2;
  48.             while( 1 ) {
  49.                 if( (p-n) < scratchFileName )
  50.                     /* string is "component/../ ..." */
  51.                     break;
  52.                 if( *(p-n) == '/' )
  53.                     break;
  54.                 ++n;
  55.             }
  56.             /* eliminate the "component/../" by copying the */
  57.             /* rest of the  string up n character positions */
  58.             fromPtr = p + 3;
  59.             /* *(p-n) is the last character to keep so: */
  60.             toPtr = p - n + 1;
  61.             /* move p to continue the scan at the beginning */
  62.             /* of the moved part of the string */
  63.             p = toPtr;
  64.             while( 1 ) {
  65.                 if( (*toPtr++ = *fromPtr++) == '\0' )
  66.                     break;
  67.             }
  68.         } else
  69.             ++p;
  70.     }
  71.  
  72.     return scratchFileName;
  73. }
  74.  
  75. int
  76. striccmp( a, b )
  77.     char *a, *b;
  78. {
  79.     char cha, chb;
  80.  
  81.     while( 1 ) {
  82.         chb = *b++;
  83.         if( (cha = *a++) == '\0' )
  84.             break;
  85.         /* not end of string for 'a' */
  86.         if( cha != chb ) {
  87.             if( isupper(cha) )
  88.                 cha = tolower(cha);
  89.             if( isupper(chb) )
  90.                 chb = tolower(chb);
  91.             if( cha != chb )    
  92.                 break;
  93.             /* either chb is a letter hence not end of string */
  94.             /* for 'b' or if cha != chb we break */
  95.         } /* else cha == chb hence not end of string for 'b' either */
  96.     }
  97.     return cha - chb;
  98. }
  99.  
  100. struct window *
  101. findFilenameWindow(filename)
  102.     char *filename;
  103. {
  104.     extern struct window *windowList;
  105.     extern struct openFile *files;
  106.  
  107.     register struct window *w;
  108.  
  109.     w = windowList;
  110.     while( w != NULL ) {
  111.         if( striccmp(files[w->fileId].origName, filename) == 0 )
  112.             return w;
  113.         w = w->nextWindow;
  114.     }
  115.     return NULL;
  116. }
  117.