home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / NORMALIZ.C < prev    next >
C/C++ Source or Header  |  1993-09-20  |  6KB  |  162 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    n o r m a l i z . c                                             */
  3. /*                                                                    */
  4. /*    Normalize a path for UUPC/extended                              */
  5. /*                                                                    */
  6. /*    Copyright (c) 1992 by Kendra Electronic Wonderworks; all        */
  7. /*    rights reserved except those explicitly granted by the          */
  8. /*    UUPC/extended license.                                          */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /*--------------------------------------------------------------------*/
  12. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  13. /*       Wonderworks.                                                 */
  14. /*                                                                    */
  15. /*       All rights reserved except those explicitly granted by       */
  16. /*       the UUPC/extended license agreement.                         */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*--------------------------------------------------------------------*/
  20. /*                          RCS Information                           */
  21. /*--------------------------------------------------------------------*/
  22.  
  23. /*
  24.  *    $Id: normaliz.c 1.8 1993/09/20 04:38:11 ahd Exp $
  25.  *
  26.  *    Revision history:
  27.  *    $Log: normaliz.c $
  28.  *     Revision 1.8  1993/09/20  04:38:11  ahd
  29.  *     TCP/IP support from Dave Watt
  30.  *     't' protocol support
  31.  *     OS/2 2.x support
  32.  *
  33.  *     Revision 1.7  1993/08/08  17:39:09  ahd
  34.  *     Denormalize path for opening on selected networks
  35.  *
  36.  *     Revision 1.6  1993/07/06  10:55:20  ahd
  37.  *     Drop doubled path delimiters before calling _fullpath
  38.  *     Abort, not return NULL, if _fullpath fails
  39.  *
  40.  *     Revision 1.5  1993/06/16  04:19:31  ahd
  41.  *     Copy trailing null when copying slashes
  42.  *
  43.  *     Revision 1.4  1993/06/16  04:03:25  ahd
  44.  *     drop duplicated slashes (caused by root directory support *sigh*)
  45.  *
  46.  *     Revision 1.3  1993/06/15  12:18:06  ahd
  47.  *     Display last known directory name for debugging
  48.  *
  49.  *     Revision 1.2  1993/04/11  00:32:05  ahd
  50.  *     Global edits for year, TEXT, etc.
  51.  *
  52.  * Revision 1.1  1992/11/22  21:06:14  ahd
  53.  * Initial revision
  54.  *
  55.  */
  56.  
  57. /*--------------------------------------------------------------------*/
  58. /*                        System include files                        */
  59. /*--------------------------------------------------------------------*/
  60.  
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #include <time.h>
  65. #include <sys/types.h>
  66. #include <ctype.h>
  67.  
  68. /*--------------------------------------------------------------------*/
  69. /*                    UUPC/extended include files                     */
  70. /*--------------------------------------------------------------------*/
  71.  
  72. #include "lib.h"
  73.  
  74. currentfile();
  75.  
  76. /*--------------------------------------------------------------------*/
  77. /*    n o r m a l i z e                                               */
  78. /*                                                                    */
  79. /*    Normalize a DOS Path                                            */
  80. /*--------------------------------------------------------------------*/
  81.  
  82. char *normalize( const char *pathx )
  83. {
  84.    static char save[FILENAME_MAX];
  85.    char path[FILENAME_MAX];
  86.    int column;
  87.    char *p;
  88.  
  89. /*--------------------------------------------------------------------*/
  90. /*                      Normalize the seperators                      */
  91. /*--------------------------------------------------------------------*/
  92.  
  93.    strcpy( path, pathx );
  94.    denormalize( path );
  95.  
  96. /*--------------------------------------------------------------------*/
  97. /*                    Add leading path, if needed                     */
  98. /*--------------------------------------------------------------------*/
  99.  
  100.    if (  ( E_cwd != NULL ) &&
  101.          equaln( E_cwd, "//", 2 ) &&               // Network CWD drive
  102.        ! (isalpha( *path ) && (path[1] == ':')) && // Not explicit drive
  103.          (*path != '\\'))                          // Not explicit path
  104.    {
  105.       column = strlen( E_cwd );
  106.       memmove( path + column + 1, path, strlen(path) + 1 );
  107.                                           // Make room for path
  108.       memcpy( path, E_cwd, column );      // Insert path
  109.       path[column] = '\\';                // Add directory sep
  110.    }
  111.  
  112.    p = path + 1;                 // Allow leading double slash for
  113.                                  // Network drives
  114.  
  115.    while ((p = strstr(p,"\\\\")) != NULL)  // Drop all double slashes
  116.       memmove(p, p+1, strlen(p));          // Includes trailing NULL
  117.  
  118. /*--------------------------------------------------------------------*/
  119. /*           Drop trailing slashes, OS/2 doesn't like them            */
  120. /*--------------------------------------------------------------------*/
  121.  
  122.    column = strlen( path ) - 1;
  123.    if ( (column > 2) && (path[column] == '\\') )
  124.       path[column] = '\0';
  125.  
  126. /*--------------------------------------------------------------------*/
  127. /*                    Now actually expand the path                    */
  128. /*--------------------------------------------------------------------*/
  129.  
  130.    p = _fullpath( save, path, sizeof save );
  131.  
  132.    if ( p == NULL )
  133.    {
  134.       printerr( path );
  135.       panic();
  136.    }
  137.  
  138.    while ((p = strchr(p,'\\')) != NULL)   // Back slashes to slashes
  139.       *p++ = '/';
  140.  
  141.    if ( equaln( save + 1, "://", 3))
  142.       p = save + 2;                       // Drop drive if really network
  143.    else
  144.       p = save;                           // Else use as-is
  145.  
  146.    column = strlen( p ) - 1;
  147.    if ((column > 2) && ( p[column] == '/' )) // Zap all but root trailing
  148.        p[column] = '\0';
  149.  
  150. /*--------------------------------------------------------------------*/
  151. /*               Print the results and return to caller               */
  152. /*--------------------------------------------------------------------*/
  153.  
  154.    printmsg(5,"Normalize: cwd = %s, input = %s, output = %s",
  155.                (E_cwd == NULL) ? "?" : E_cwd,
  156.                pathx,
  157.                p );
  158.  
  159.    return p;
  160.  
  161. } /* normalize */
  162.