mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-05 18:58:30 +00:00
Fix basename() and dirname()
This commit is contained in:
parent
9b11206ae3
commit
04d39d47f1
41 changed files with 489 additions and 207 deletions
|
@ -17,19 +17,42 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/isslash.internal.h"
|
||||
#include "libc/str/path.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* Returns pointer to last filename component in path.
|
||||
* Returns pointer to last filename component in path, e.g.
|
||||
*
|
||||
* path │ dirname() │ basename()
|
||||
* ─────────────────────────────────
|
||||
* . │ . │ .
|
||||
* .. │ . │ ..
|
||||
* / │ / │ /
|
||||
* usr │ . │ usr
|
||||
* /usr/ │ / │ usr
|
||||
* /usr/lib │ /usr │ lib
|
||||
*
|
||||
* Both / and \ are are considered valid component separators on all
|
||||
* platforms. Trailing slashes are ignored. We don't grant special
|
||||
* consideration to things like foo/., c:/, \\?\Volume, etc.
|
||||
*
|
||||
* @param path is NUL-terminated UTF-8 path
|
||||
* @return pointer inside path or path itself
|
||||
* @param path is UTF-8 and may be mutated, but not expanded in length
|
||||
* @return pointer to path, or inside path, or to a special r/o string
|
||||
* @see dirname()
|
||||
* @see SUSv2
|
||||
*/
|
||||
textstartup char *basename(const char *path) {
|
||||
return basename_n(path, strlen(path));
|
||||
char *basename(char *path) {
|
||||
size_t i;
|
||||
if (path && *path) {
|
||||
i = strlen(path) - 1;
|
||||
for (; i && _isdirsep(path[i]); i--) {
|
||||
path[i] = 0;
|
||||
}
|
||||
for (; i && !_isdirsep(path[i - 1]);) {
|
||||
i--;
|
||||
}
|
||||
return path + i;
|
||||
} else {
|
||||
return ".";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/isslash.internal.h"
|
||||
|
||||
/**
|
||||
* Returns pointer to last filename component in path.
|
||||
*
|
||||
* Both / and \ are are considered valid component separators on all
|
||||
* platforms. Trailing slashes are ignored. We don't grant special
|
||||
* consideration to things like foo/., c:/, \\?\Volume, etc.
|
||||
*
|
||||
* @param path is UTF-8 path
|
||||
* @param size is byte length of path
|
||||
* @return pointer inside path or path itself
|
||||
*/
|
||||
textstartup char *basename_n(const char *path, size_t size) {
|
||||
size_t i, l;
|
||||
if (size) {
|
||||
if (isslash(path[size - 1])) {
|
||||
l = size - 1;
|
||||
while (l && isslash(path[l - 1])) --l;
|
||||
if (!l) return (/*unconst*/ char *)&path[size - 1];
|
||||
size = l;
|
||||
}
|
||||
for (i = size; i > 0; --i) {
|
||||
if (isslash(path[i - 1])) {
|
||||
return (/*unconst*/ char *)&path[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return (/*unconst*/ char *)path;
|
||||
}
|
|
@ -42,13 +42,13 @@ size_t wcsxfrm(wchar_t *, const wchar_t *, size_t);
|
|||
│ cosmopolitan § conversion » time ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
int64_t DosDateTimeToUnix(unsigned, unsigned) dontthrow;
|
||||
struct timeval WindowsTimeToTimeVal(int64_t) dontthrow;
|
||||
struct timespec WindowsTimeToTimeSpec(int64_t) dontthrow;
|
||||
int64_t TimeSpecToWindowsTime(struct timespec) dontthrow;
|
||||
int64_t TimeValToWindowsTime(struct timeval) dontthrow;
|
||||
struct timeval WindowsDurationToTimeVal(int64_t) dontthrow;
|
||||
struct timespec WindowsDurationToTimeSpec(int64_t) dontthrow;
|
||||
int64_t DosDateTimeToUnix(unsigned, unsigned) libcesque nosideeffect;
|
||||
struct timeval WindowsTimeToTimeVal(int64_t) libcesque nosideeffect;
|
||||
struct timespec WindowsTimeToTimeSpec(int64_t) libcesque nosideeffect;
|
||||
int64_t TimeSpecToWindowsTime(struct timespec) libcesque nosideeffect;
|
||||
int64_t TimeValToWindowsTime(struct timeval) libcesque nosideeffect;
|
||||
struct timeval WindowsDurationToTimeVal(int64_t) libcesque nosideeffect;
|
||||
struct timespec WindowsDurationToTimeSpec(int64_t) libcesque nosideeffect;
|
||||
|
||||
static inline struct NtFileTime MakeFileTime(int64_t x) {
|
||||
return (struct NtFileTime){(uint32_t)x, (uint32_t)(x >> 32)};
|
||||
|
@ -69,9 +69,7 @@ static inline int64_t ReadFileTime(struct NtFileTime t) {
|
|||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
char *dirname(char *);
|
||||
char *basename(const char *) nosideeffect;
|
||||
char *basename_n(const char *, size_t) nosideeffect;
|
||||
bool isabspath(const char *) paramsnonnull() nosideeffect;
|
||||
char *basename(char *);
|
||||
char *stripext(char *);
|
||||
char *stripexts(char *);
|
||||
|
||||
|
|
|
@ -17,34 +17,42 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/str/path.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
#define ISSLASH(c) (c == '/' || c == '\\')
|
||||
#define ISDELIM(c) (ISSLASH(c) || c == '.')
|
||||
|
||||
/**
|
||||
* Returns directory portion of path.
|
||||
* Returns directory portion of path, e.g.
|
||||
*
|
||||
* This returns "." if path doesn't have slashes. If path is empty then
|
||||
* this returns empty string.
|
||||
* path │ dirname() │ basename()
|
||||
* ─────────────────────────────────
|
||||
* . │ . │ .
|
||||
* .. │ . │ ..
|
||||
* / │ / │ /
|
||||
* usr │ . │ usr
|
||||
* /usr/ │ / │ usr
|
||||
* /usr/lib │ /usr │ lib
|
||||
*
|
||||
* @param s is mutated and must not be NULL
|
||||
* @param path is UTF-8 and may be mutated, but not expanded in length
|
||||
* @return pointer to path, or inside path, or to a special r/o string
|
||||
* @see basename()
|
||||
* @see SUSv2
|
||||
*/
|
||||
char *dirname(char *s) {
|
||||
size_t i, n;
|
||||
if (!(n = strlen(s))) return s;
|
||||
while (n && ISDELIM(s[n - 1])) --n;
|
||||
if (n) {
|
||||
while (n && !ISSLASH(s[n - 1])) --n;
|
||||
if (n) {
|
||||
while (n && ISDELIM(s[n - 1])) --n;
|
||||
if (!n) ++n;
|
||||
} else {
|
||||
s[n++] = '.';
|
||||
char *dirname(char *path) {
|
||||
size_t i;
|
||||
if (path && *path) {
|
||||
i = strlen(path) - 1;
|
||||
for (; _isdirsep(path[i]); i--) {
|
||||
if (!i) return "/";
|
||||
}
|
||||
for (; !_isdirsep(path[i]); i--) {
|
||||
if (!i) return ".";
|
||||
}
|
||||
for (; _isdirsep(path[i]); i--) {
|
||||
if (!i) return "/";
|
||||
}
|
||||
path[i + 1] = 0;
|
||||
return path;
|
||||
} else {
|
||||
++n;
|
||||
return ".";
|
||||
}
|
||||
s[n] = '\0';
|
||||
return s;
|
||||
}
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/isslash.internal.h"
|
||||
|
||||
/**
|
||||
* Returns true if pathname could be absolute on any known platform.
|
||||
*
|
||||
* The ones we know about are System V (/foo/bar), DOS (C:\foo\bar),
|
||||
* Windows NT (\\.\C:\foo\bar), Google Cloud (gs://bucket/foo/bar), etc.
|
||||
*/
|
||||
bool isabspath(const char *path) {
|
||||
if (isslash(*path)) return true;
|
||||
for (; *path; ++path) {
|
||||
if (isslash(*path)) return false;
|
||||
if (*path == ':') {
|
||||
++path;
|
||||
if (isslash(*path)) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_FMT_ISSLASH_H_
|
||||
#define COSMOPOLITAN_LIBC_FMT_ISSLASH_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
||||
forceinline bool isslash(int c) {
|
||||
return c == '/' || c == '\\';
|
||||
}
|
||||
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_FMT_ISSLASH_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue