Path: blob/main/crypto/krb5/src/util/support/path.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/path.c - Portable path manipulation functions */2/*3* Copyright (C) 2011 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include <k5-platform.h>2728/* For testing purposes, use a different symbol for Windows path semantics. */29#ifdef _WIN3230#define WINDOWS_PATHS31#endif3233/*34* This file implements a limited set of portable path manipulation functions.35* When in doubt about edge cases, we follow the Python os.path semantics.36*/3738#ifdef WINDOWS_PATHS39#define SEP '\\'40#define IS_SEPARATOR(c) ((c) == '\\' || (c) == '/')41#else42#define SEP '/'43#define IS_SEPARATOR(c) ((c) == '/')44#endif4546/* Find the rightmost path separator in path, or NULL if there is none. */47static inline const char *48find_sep(const char *path)49{50#ifdef WINDOWS_PATHS51const char *slash, *backslash;5253slash = strrchr(path, '/');54backslash = strrchr(path, '\\');55if (slash != NULL && backslash != NULL)56return (slash > backslash) ? slash : backslash;57else58return (slash != NULL) ? slash : backslash;59#else60return strrchr(path, '/');61#endif62}6364/* XXX drive letter prefixes */65long66k5_path_split(const char *path, char **parent_out, char **basename_out)67{68const char *pathstart, *sep, *pend, *bstart;69char *parent = NULL, *basename = NULL;7071if (parent_out != NULL)72*parent_out = NULL;73if (basename_out != NULL)74*basename_out = NULL;7576pathstart = path;77#ifdef WINDOWS_PATHS78if (*path != '\0' && path[1] == ':')79pathstart = path + 2;80#endif8182sep = find_sep(pathstart);83if (sep != NULL) {84bstart = sep + 1;85/* Strip off excess separators before the one we found. */86pend = sep;87while (pend > pathstart && IS_SEPARATOR(pend[-1]))88pend--;89/* But if we hit the start, keep the whole separator sequence. */90if (pend == pathstart)91pend = sep + 1;92} else {93bstart = pathstart;94pend = pathstart;95}9697if (parent_out) {98parent = malloc(pend - path + 1);99if (parent == NULL)100return ENOMEM;101memcpy(parent, path, pend - path);102parent[pend - path] = '\0';103}104if (basename_out) {105basename = strdup(bstart);106if (basename == NULL) {107free(parent);108return ENOMEM;109}110}111112if (parent_out)113*parent_out = parent;114if (basename_out)115*basename_out = basename;116return 0;117}118119long120k5_path_join(const char *path1, const char *path2, char **path_out)121{122char *path, c;123int ret;124125*path_out = NULL;126if (k5_path_isabs(path2) || *path1 == '\0') {127/* Discard path1 and return a copy of path2. */128path = strdup(path2);129if (path == NULL)130return ENOMEM;131} else {132/*133* Compose path1 and path2, adding a separator if path1 is non-empty134* there's no separator between them already. (*path2 can be a135* separator in the weird case where it starts with /: or \: on136* Windows, and Python doesn't insert a separator in this case.)137*/138c = path1[strlen(path1) - 1];139if (IS_SEPARATOR(c) || IS_SEPARATOR(*path2))140ret = asprintf(&path, "%s%s", path1, path2);141else142ret = asprintf(&path, "%s%c%s", path1, SEP, path2);143if (ret < 0)144return ENOMEM;145}146*path_out = path;147return 0;148}149150int151k5_path_isabs(const char *path)152{153#ifdef WINDOWS_PATHS154if (*path != '\0' && path[1] == ':')155path += 2;156return (*path == '/' || *path == '\\');157#else158return (*path == '/');159#endif160}161162163