|
Packit |
fd8b60 |
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
Packit |
fd8b60 |
/* util/support/path.c - Portable path manipulation functions */
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Copyright (C) 2011 by the Massachusetts Institute of Technology.
|
|
Packit |
fd8b60 |
* All rights reserved.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* Export of this software from the United States of America may
|
|
Packit |
fd8b60 |
* require a specific license from the United States Government.
|
|
Packit |
fd8b60 |
* It is the responsibility of any person or organization contemplating
|
|
Packit |
fd8b60 |
* export to obtain such a license before exporting.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
|
|
Packit |
fd8b60 |
* distribute this software and its documentation for any purpose and
|
|
Packit |
fd8b60 |
* without fee is hereby granted, provided that the above copyright
|
|
Packit |
fd8b60 |
* notice appear in all copies and that both that copyright notice and
|
|
Packit |
fd8b60 |
* this permission notice appear in supporting documentation, and that
|
|
Packit |
fd8b60 |
* the name of M.I.T. not be used in advertising or publicity pertaining
|
|
Packit |
fd8b60 |
* to distribution of the software without specific, written prior
|
|
Packit |
fd8b60 |
* permission. Furthermore if you modify this software you must label
|
|
Packit |
fd8b60 |
* your software as modified software and not distribute it in such a
|
|
Packit |
fd8b60 |
* fashion that it might be confused with the original M.I.T. software.
|
|
Packit |
fd8b60 |
* M.I.T. makes no representations about the suitability of
|
|
Packit |
fd8b60 |
* this software for any purpose. It is provided "as is" without express
|
|
Packit |
fd8b60 |
* or implied warranty.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#include <k5-platform.h>
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* For testing purposes, use a different symbol for Windows path semantics. */
|
|
Packit |
fd8b60 |
#ifdef _WIN32
|
|
Packit |
fd8b60 |
#define WINDOWS_PATHS
|
|
Packit |
fd8b60 |
#endif
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* This file implements a limited set of portable path manipulation functions.
|
|
Packit |
fd8b60 |
* When in doubt about edge cases, we follow the Python os.path semantics.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#ifdef WINDOWS_PATHS
|
|
Packit |
fd8b60 |
#define SEP '\\'
|
|
Packit |
fd8b60 |
#define IS_SEPARATOR(c) ((c) == '\\' || (c) == '/')
|
|
Packit |
fd8b60 |
#else
|
|
Packit |
fd8b60 |
#define SEP '/'
|
|
Packit |
fd8b60 |
#define IS_SEPARATOR(c) ((c) == '/')
|
|
Packit |
fd8b60 |
#endif
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Find the rightmost path separator in path, or NULL if there is none. */
|
|
Packit |
fd8b60 |
static inline const char *
|
|
Packit |
fd8b60 |
find_sep(const char *path)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
#ifdef WINDOWS_PATHS
|
|
Packit |
fd8b60 |
const char *slash, *backslash;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
slash = strrchr(path, '/');
|
|
Packit |
fd8b60 |
backslash = strrchr(path, '\\');
|
|
Packit |
fd8b60 |
if (slash != NULL && backslash != NULL)
|
|
Packit |
fd8b60 |
return (slash > backslash) ? slash : backslash;
|
|
Packit |
fd8b60 |
else
|
|
Packit |
fd8b60 |
return (slash != NULL) ? slash : backslash;
|
|
Packit |
fd8b60 |
#else
|
|
Packit |
fd8b60 |
return strrchr(path, '/');
|
|
Packit |
fd8b60 |
#endif
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* XXX drive letter prefixes */
|
|
Packit |
fd8b60 |
long
|
|
Packit |
fd8b60 |
k5_path_split(const char *path, char **parent_out, char **basename_out)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
const char *pathstart, *sep, *pend, *bstart;
|
|
Packit |
fd8b60 |
char *parent = NULL, *basename = NULL;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (parent_out != NULL)
|
|
Packit |
fd8b60 |
*parent_out = NULL;
|
|
Packit |
fd8b60 |
if (basename_out != NULL)
|
|
Packit |
fd8b60 |
*basename_out = NULL;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
pathstart = path;
|
|
Packit |
fd8b60 |
#ifdef WINDOWS_PATHS
|
|
Packit |
fd8b60 |
if (*path != '\0' && path[1] == ':')
|
|
Packit |
fd8b60 |
pathstart = path + 2;
|
|
Packit |
fd8b60 |
#endif
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
sep = find_sep(pathstart);
|
|
Packit |
fd8b60 |
if (sep != NULL) {
|
|
Packit |
fd8b60 |
bstart = sep + 1;
|
|
Packit |
fd8b60 |
/* Strip off excess separators before the one we found. */
|
|
Packit |
fd8b60 |
pend = sep;
|
|
Packit |
fd8b60 |
while (pend > pathstart && IS_SEPARATOR(pend[-1]))
|
|
Packit |
fd8b60 |
pend--;
|
|
Packit |
fd8b60 |
/* But if we hit the start, keep the whole separator sequence. */
|
|
Packit |
fd8b60 |
if (pend == pathstart)
|
|
Packit |
fd8b60 |
pend = sep + 1;
|
|
Packit |
fd8b60 |
} else {
|
|
Packit |
fd8b60 |
bstart = pathstart;
|
|
Packit |
fd8b60 |
pend = pathstart;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (parent_out) {
|
|
Packit |
fd8b60 |
parent = malloc(pend - path + 1);
|
|
Packit |
fd8b60 |
if (parent == NULL)
|
|
Packit |
fd8b60 |
return ENOMEM;
|
|
Packit |
fd8b60 |
memcpy(parent, path, pend - path);
|
|
Packit |
fd8b60 |
parent[pend - path] = '\0';
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
if (basename_out) {
|
|
Packit |
fd8b60 |
basename = strdup(bstart);
|
|
Packit |
fd8b60 |
if (basename == NULL) {
|
|
Packit |
fd8b60 |
free(parent);
|
|
Packit |
fd8b60 |
return ENOMEM;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (parent_out)
|
|
Packit |
fd8b60 |
*parent_out = parent;
|
|
Packit |
fd8b60 |
if (basename_out)
|
|
Packit |
fd8b60 |
*basename_out = basename;
|
|
Packit |
fd8b60 |
return 0;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
long
|
|
Packit |
fd8b60 |
k5_path_join(const char *path1, const char *path2, char **path_out)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
char *path, c;
|
|
Packit |
fd8b60 |
int ret;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
*path_out = NULL;
|
|
Packit |
fd8b60 |
if (k5_path_isabs(path2) || *path1 == '\0') {
|
|
Packit |
fd8b60 |
/* Discard path1 and return a copy of path2. */
|
|
Packit |
fd8b60 |
path = strdup(path2);
|
|
Packit |
fd8b60 |
if (path == NULL)
|
|
Packit |
fd8b60 |
return ENOMEM;
|
|
Packit |
fd8b60 |
} else {
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Compose path1 and path2, adding a separator if path1 is non-empty
|
|
Packit |
fd8b60 |
* there's no separator between them already. (*path2 can be a
|
|
Packit |
fd8b60 |
* separator in the weird case where it starts with /: or \: on
|
|
Packit |
fd8b60 |
* Windows, and Python doesn't insert a separator in this case.)
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
c = path1[strlen(path1) - 1];
|
|
Packit |
fd8b60 |
if (IS_SEPARATOR(c) || IS_SEPARATOR(*path2))
|
|
Packit |
fd8b60 |
ret = asprintf(&path, "%s%s", path1, path2);
|
|
Packit |
fd8b60 |
else
|
|
Packit |
fd8b60 |
ret = asprintf(&path, "%s%c%s", path1, SEP, path2);
|
|
Packit |
fd8b60 |
if (ret < 0)
|
|
Packit |
fd8b60 |
return ENOMEM;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
*path_out = path;
|
|
Packit |
fd8b60 |
return 0;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
int
|
|
Packit |
fd8b60 |
k5_path_isabs(const char *path)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
#ifdef WINDOWS_PATHS
|
|
Packit |
fd8b60 |
if (*path != '\0' && path[1] == ':')
|
|
Packit |
fd8b60 |
path += 2;
|
|
Packit |
fd8b60 |
return (*path == '/' || *path == '\\');
|
|
Packit |
fd8b60 |
#else
|
|
Packit |
fd8b60 |
return (*path == '/');
|
|
Packit |
fd8b60 |
#endif
|
|
Packit |
fd8b60 |
}
|