home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / EXPATH.C < prev    next >
C/C++ Source or Header  |  1993-08-08  |  7KB  |  194 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    e x p a t h . c                                                 */
  3. /*                                                                    */
  4. /*    Path expansion functions for UUPC/extended                      */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*                          RCS Information                           */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /*
  12.  *    $Id: expath.c 1.6 1993/08/08 17:39:09 ahd Exp $
  13.  *
  14.  *    Revision history:
  15.  *    $Log: expath.c $
  16.  *     Revision 1.6  1993/08/08  17:39:09  ahd
  17.  *     Denormalize path for opening on selected networks
  18.  *
  19.  */
  20.  
  21. /*--------------------------------------------------------------------*/
  22. /*                    MS-DOS and OS/2 header files                    */
  23. /*--------------------------------------------------------------------*/
  24.  
  25. #include <ctype.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30.  
  31. #ifndef __GNUC__
  32. #include <direct.h>
  33. #endif
  34.  
  35. /*--------------------------------------------------------------------*/
  36. /*                     UUPC/extended header files                     */
  37. /*--------------------------------------------------------------------*/
  38.  
  39. #include "lib.h"
  40. #include "expath.h"
  41. #include "hlib.h"
  42. #include "hostable.h"
  43. #include "security.h"
  44. #include "usertabl.h"
  45. #include "pushpop.h"
  46.  
  47. /*--------------------------------------------------------------------*/
  48. /*   e x p a n d _ p a t  h                                           */
  49. /*                                                                    */
  50. /*   Expands ~, ~/ and relative paths                                 */
  51. /*--------------------------------------------------------------------*/
  52.  
  53. char *expand_path(char *input,         /* Input/output path name     */
  54.                   const char *cur_dir, /* Default directory path     */
  55.                   const char *home,    /* Default home directory     */
  56.                   const char *ftype )  /* Default extension          */
  57. {
  58.    char        *p, *fname;
  59.    char        save[FILENAME_MAX];
  60.    char        path[FILENAME_MAX];
  61.    struct UserTable *userp;
  62.  
  63. /*--------------------------------------------------------------------*/
  64. /*                   Convert backslashes to slashes                   */
  65. /*--------------------------------------------------------------------*/
  66.  
  67.    p  = strcpy(path, input);
  68.    while ((p = strchr(p,'\\')) != NULL)
  69.       *p++ = '/';
  70.  
  71. /*--------------------------------------------------------------------*/
  72. /*                 Add optional extension, if needed                  */
  73. /*--------------------------------------------------------------------*/
  74.  
  75.    if ( ftype != NULL )
  76.    {
  77.       p = strrchr(path,'/');  /* Get the last slash in name          */
  78.  
  79.       if ( p == NULL )        /* No slash?                           */
  80.          p = path;            /* Okay, look at entire name           */
  81.  
  82.       if (( strchr( p , '.') == NULL ) && (*p != '~'))
  83.                               /* Does name have a period?            */
  84.          strcat( strcat(p, ".") ,ftype );
  85.                               /* No --> Add extension                */
  86.    } /* if ( ftype != NULL ) */
  87.  
  88. /*--------------------------------------------------------------------*/
  89. /*               If a fully qualified path name, return               */
  90. /*--------------------------------------------------------------------*/
  91.  
  92.    if ((*path == '/') || (isalpha( *path ) && (path[1] == ':')))
  93.    {
  94.       boolean push = ((cur_dir != NULL ) && ( path[1] != ':' ));
  95.       strcpy( save, path );
  96.  
  97.       if (push)
  98.          PushDir( cur_dir );
  99.  
  100.       p = _fullpath( path, save, sizeof save );
  101.  
  102.       if (push)
  103.          PopDir();
  104.  
  105.       while ((p = strchr(p,'\\')) != NULL)
  106.          *p++ = '/';
  107.  
  108.       printmsg(5,"expand_path: cwd = %s, input = %s, output = %s",
  109.                   E_cwd, input, path );
  110.       return strcpy( input, path );
  111.  
  112.    } /* if */
  113.  
  114. /*--------------------------------------------------------------------*/
  115. /*            Try to translate the file as a home directory path      */
  116. /*--------------------------------------------------------------------*/
  117.  
  118.    p = path;                  /* Copy entire path                    */
  119.    strcpy(save, p);
  120.    if (save[0] == '~')
  121.    {
  122.       if (save[1] == '/')
  123.       {
  124.          strcpy(path, home);  /* Use home dir for this user          */
  125.          fname = save + 2;    /* Step past directory for simple name */
  126.       }
  127.       else if ( save[1] == '\0')
  128.       {
  129.          strcpy(path, home);  /* Use home dir for this user          */
  130.          fname = save + 1;    /* Step past directory for simple name */
  131.       }
  132.       else {
  133.  
  134.          if ((fname = strchr(save + 1, '/')) == NULL)
  135.             fname = save + strlen(save);  // That's all, folks!
  136.          else
  137.             *fname++ = '\0';           /* End string, step past it */
  138.  
  139. /*--------------------------------------------------------------------*/
  140. /*                Look in /etc/passwd for the user id                 */
  141. /*--------------------------------------------------------------------*/
  142.  
  143.          userp = checkuser(save + 1);  /* Locate user id in table  */
  144.  
  145.          if ( userp == BADUSER )    /* Invalid user id?         */
  146.          {                          /* Yes --> Dump in trash    */
  147.             printmsg(0,"expand_path: User \"%s\" is invalid", save + 1);
  148.             return NULL;
  149.          } /* if */
  150.  
  151.          strcpy(path, userp->homedir);
  152.  
  153.       } /* else */
  154.    } /* if (save[0] == '~') */
  155.  
  156. /*--------------------------------------------------------------------*/
  157. /*    No user id appears in the path; just append the input data      */
  158. /*    to the current directory to convert the relative path to an     */
  159. /*    absolute path                                                   */
  160. /*--------------------------------------------------------------------*/
  161.  
  162.    else {
  163.          fname = save;              // Save entire file name
  164.  
  165.          if ( cur_dir == NULL )
  166.             getcwd( path, FILENAME_MAX);
  167.          else
  168.             strcpy( path, cur_dir );
  169.    } /* else */
  170.  
  171. /*--------------------------------------------------------------------*/
  172. /*             Normalize the path, and then add the name              */
  173. /*--------------------------------------------------------------------*/
  174.  
  175.    while ((p = strchr(p,'\\')) != NULL)
  176.       *p++ = '/';
  177.  
  178.    if ( path[ strlen( path ) - 1 ] != '/' )
  179.       strcat( path, "/");
  180.  
  181.    strlwr( path );            /* Can lower case path, but not the
  182.                                  name because name may be UNIX!      */
  183.    strcat( path, fname );
  184.  
  185. /*--------------------------------------------------------------------*/
  186. /*                       Return data to caller                        */
  187. /*--------------------------------------------------------------------*/
  188.  
  189.    printmsg(5,"expand_path: cwd = %s, input = %s, output = %s",
  190.                E_cwd, input, path );
  191.    return strcpy( input, path );
  192.  
  193. } /* expand_path */
  194.