home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / realpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-29  |  4.4 KB  |  151 lines

  1. /*
  2.  * Portable implementation of realpath() written by Chris Myers
  3.  * <chris@wugate.wustl.edu>.
  4.  *
  5.  * Copyright (c) 1990, 1991 Washington University in Saint Louis, MO
  6.  * All rights reserved.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by Washington University,
  19.  *    Saint Louis, Missouri and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. char copyright[] =
  39. "@(#) Copyright (c) 1990, 1991 Washington University in St. Louis, MO.\n\
  40.  All rights reserved.\n";
  41. #endif /* not lint */
  42.  
  43. #include <stdio.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <sys/param.h>
  47. #include <strings.h>
  48.  
  49. char *
  50. realpath(pathname, result)
  51. char    *pathname, *result;
  52.  
  53. {
  54. struct    stat    sbuf;
  55. char    curpath[MAXPATHLEN],
  56.     workpath[MAXPATHLEN],
  57.     linkpath[MAXPATHLEN],
  58.         namebuf[MAXPATHLEN],
  59.     *where,
  60.     *ptr,
  61.     *last;
  62. int    len;
  63.  
  64.    strcpy(curpath, pathname);
  65.  
  66.    if (*pathname != '/') {
  67.       if (!getwd(workpath)) {
  68.          strcpy(result, ".");
  69.          return(NULL);
  70.       }
  71.    } else *workpath = NULL;
  72.  
  73.    /* curpath is the path we're still resolving      */
  74.    /* linkpath is the path a symbolic link points to */
  75.    /* workpath is the path we've resolved            */
  76.  
  77. loop:
  78.    where = curpath;
  79.    while (*where != NULL) {
  80.       if (!strcmp(where, ".")) {
  81.          where++;
  82.          continue;
  83.       }
  84.  
  85.       /* deal with "./" */
  86.       if (!strncmp(where, "./", 2)) {
  87.          where += 2;
  88.          continue;
  89.       }
  90.  
  91.       /* deal with "../" */
  92.       if (!strncmp(where, "../", 3)) {
  93.          where += 3;
  94.          ptr = last = workpath;
  95.          while (*ptr) {
  96.             if (*ptr == '/') last = ptr;
  97.             ptr++;
  98.          }
  99.          *last = NULL;
  100.          continue;
  101.       }
  102.  
  103.       ptr = strchr(where, '/');
  104.       if (!ptr)
  105.          ptr = where + strlen(where) - 1;
  106.       else
  107.          *ptr = NULL;
  108.  
  109.       strcpy(namebuf, workpath);
  110.       for (last = namebuf; *last; last++) continue;
  111.       if (*--last != '/') strcat(namebuf, "/");
  112.       strcat(namebuf, where);
  113.  
  114.       where = ++ptr;
  115.       if (lstat(namebuf, &sbuf) == -1) {
  116.          strcpy(result, namebuf);
  117.          return(NULL);
  118.       }
  119.  
  120.       if ((sbuf.st_mode & S_IFLNK) == S_IFLNK) {
  121.          len = readlink(namebuf, linkpath, MAXPATHLEN);
  122.          if (len == 0) {
  123.             strcpy(result, namebuf);
  124.             return(NULL);
  125.          }
  126.          *(linkpath + len) = NULL; /* readlink doesn't null-terminate result */
  127.          if (*linkpath == '/') *workpath = NULL;
  128.          if (*where) {
  129.             strcat(linkpath, "/");
  130.             strcat(linkpath, where);
  131.          }
  132.          strcpy(curpath, linkpath);
  133.          goto loop;
  134.       }
  135.  
  136.       if ((sbuf.st_mode & S_IFDIR) == S_IFDIR) {
  137.          strcpy(workpath, namebuf);
  138.          continue;
  139.       }
  140.  
  141.       if (*where) {
  142.          strcpy(result, namebuf);
  143.          return(NULL);  /* path/notadir/morepath */
  144.       } else
  145.          strcpy(workpath, namebuf);
  146.    }
  147.    strcpy(result, workpath);
  148.    return(result);
  149.  
  150. }
  151.