home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES1.ZIP / LIB / expath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-06  |  8.3 KB  |  223 lines

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