2006-12-06 17:25:32 -06:00
|
|
|
/*
|
|
|
|
* realpath.c -- canonicalize pathname by removing symlinks
|
|
|
|
* Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Library Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2006-12-06 17:26:12 -06:00
|
|
|
#define resolve_symlinks
|
2006-12-06 17:25:48 -06:00
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
/*
|
2006-12-06 17:25:34 -06:00
|
|
|
* This routine is part of libc. We include it nevertheless,
|
|
|
|
* since the libc version has some security flaws.
|
2006-12-06 17:25:32 -06:00
|
|
|
*/
|
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
#include <limits.h> /* for PATH_MAX */
|
2006-12-06 17:25:48 -06:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 8192
|
|
|
|
#endif
|
2006-12-06 17:25:32 -06:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2006-12-06 17:25:41 -06:00
|
|
|
#include "realpath.h"
|
|
|
|
#include "sundries.h" /* for xstrdup */
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
#define MAX_READLINKS 32
|
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
char *
|
|
|
|
myrealpath(const char *path, char *resolved_path, int maxreslth) {
|
2006-12-06 17:25:32 -06:00
|
|
|
int readlinks = 0;
|
2006-12-06 17:25:48 -06:00
|
|
|
char *npath;
|
|
|
|
char link_path[PATH_MAX+1];
|
|
|
|
int n;
|
2006-12-06 17:26:19 -06:00
|
|
|
char *buf = NULL;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
npath = resolved_path;
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
/* If it's a relative pathname use getcwd for starters. */
|
2006-12-06 17:25:32 -06:00
|
|
|
if (*path != '/') {
|
2006-12-06 17:25:43 -06:00
|
|
|
if (!getcwd(npath, maxreslth-2))
|
|
|
|
return NULL;
|
2006-12-06 17:25:41 -06:00
|
|
|
npath += strlen(npath);
|
|
|
|
if (npath[-1] != '/')
|
|
|
|
*npath++ = '/';
|
|
|
|
} else {
|
|
|
|
*npath++ = '/';
|
2006-12-06 17:25:32 -06:00
|
|
|
path++;
|
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
/* Expand each slash-separated pathname component. */
|
|
|
|
while (*path != '\0') {
|
2006-12-06 17:25:41 -06:00
|
|
|
/* Ignore stray "/" */
|
2006-12-06 17:25:32 -06:00
|
|
|
if (*path == '/') {
|
|
|
|
path++;
|
|
|
|
continue;
|
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
if (*path == '.' && (path[1] == '\0' || path[1] == '/')) {
|
|
|
|
/* Ignore "." */
|
|
|
|
path++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*path == '.' && path[1] == '.' &&
|
|
|
|
(path[2] == '\0' || path[2] == '/')) {
|
|
|
|
/* Backup for ".." */
|
|
|
|
path += 2;
|
|
|
|
while (npath > resolved_path+1 &&
|
|
|
|
(--npath)[-1] != '/')
|
|
|
|
;
|
|
|
|
continue;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
/* Safely copy the next pathname component. */
|
|
|
|
while (*path != '\0' && *path != '/') {
|
2006-12-06 17:25:41 -06:00
|
|
|
if (npath-resolved_path > maxreslth-2) {
|
2006-12-06 17:25:32 -06:00
|
|
|
errno = ENAMETOOLONG;
|
2006-12-06 17:26:19 -06:00
|
|
|
goto err;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
*npath++ = *path++;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
/* Protect against infinite loops. */
|
|
|
|
if (readlinks++ > MAX_READLINKS) {
|
|
|
|
errno = ELOOP;
|
2006-12-06 17:26:19 -06:00
|
|
|
goto err;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2006-12-06 17:25:48 -06:00
|
|
|
/* See if last pathname component is a symlink. */
|
2006-12-06 17:25:41 -06:00
|
|
|
*npath = '\0';
|
|
|
|
n = readlink(resolved_path, link_path, PATH_MAX);
|
2006-12-06 17:25:32 -06:00
|
|
|
if (n < 0) {
|
|
|
|
/* EINVAL means the file exists but isn't a symlink. */
|
|
|
|
if (errno != EINVAL)
|
2006-12-06 17:26:19 -06:00
|
|
|
goto err;
|
2006-12-06 17:25:41 -06:00
|
|
|
} else {
|
2006-12-06 17:25:48 -06:00
|
|
|
#ifdef resolve_symlinks /* Richard Gooch dislikes sl resolution */
|
2006-12-06 17:26:19 -06:00
|
|
|
int m;
|
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
/* Note: readlink doesn't add the null byte. */
|
|
|
|
link_path[n] = '\0';
|
|
|
|
if (*link_path == '/')
|
|
|
|
/* Start over for an absolute symlink. */
|
2006-12-06 17:25:41 -06:00
|
|
|
npath = resolved_path;
|
2006-12-06 17:25:32 -06:00
|
|
|
else
|
|
|
|
/* Otherwise back up over this component. */
|
2006-12-06 17:25:41 -06:00
|
|
|
while (*(--npath) != '/')
|
2006-12-06 17:25:32 -06:00
|
|
|
;
|
2006-12-06 17:25:43 -06:00
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
/* Insert symlink contents into path. */
|
2006-12-06 17:25:43 -06:00
|
|
|
m = strlen(path);
|
2006-12-06 17:26:19 -06:00
|
|
|
if (buf)
|
|
|
|
free(buf);
|
2006-12-06 17:25:43 -06:00
|
|
|
buf = xmalloc(m + n + 1);
|
|
|
|
memcpy(buf, link_path, n);
|
|
|
|
memcpy(buf + n, path, m + 1);
|
|
|
|
path = buf;
|
2006-12-06 17:25:48 -06:00
|
|
|
#endif
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
*npath++ = '/';
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
/* Delete trailing slash but don't whomp a lone slash. */
|
2006-12-06 17:25:41 -06:00
|
|
|
if (npath != resolved_path+1 && npath[-1] == '/')
|
|
|
|
npath--;
|
2006-12-06 17:25:32 -06:00
|
|
|
/* Make sure it's null terminated. */
|
2006-12-06 17:25:41 -06:00
|
|
|
*npath = '\0';
|
2006-12-06 17:26:19 -06:00
|
|
|
|
|
|
|
if (buf)
|
|
|
|
free(buf);
|
2006-12-06 17:25:32 -06:00
|
|
|
return resolved_path;
|
2006-12-06 17:26:19 -06:00
|
|
|
|
|
|
|
err:
|
|
|
|
if (buf)
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|