updated and simplified basename function from FreeBSD.
This commit is contained in:
parent
a6e0b2d0c3
commit
1bd5fc519f
|
@ -31,8 +31,8 @@
|
||||||
#ifndef _LIBGEN_H_
|
#ifndef _LIBGEN_H_
|
||||||
#define _LIBGEN_H_
|
#define _LIBGEN_H_
|
||||||
|
|
||||||
char *basename(const char *);
|
char *basename(char *);
|
||||||
char *basename_r(const char *, char *);
|
char *basename_r(const char *, char *);
|
||||||
char *dirname(const char *);
|
char *dirname(char *);
|
||||||
|
|
||||||
#endif /* !_LIBGEN_H_ */
|
#endif /* !_LIBGEN_H_ */
|
||||||
|
|
113
libc/basename.c
113
libc/basename.c
|
@ -1,85 +1,54 @@
|
||||||
/* $OpenBSD: basename.c,v 1.14 2005/08/08 08:05:33 espie Exp $ */
|
/*-
|
||||||
|
* Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
|
||||||
/*
|
|
||||||
* Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
|
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* modification, are permitted provided that the following conditions
|
||||||
* copyright notice and this permission notice appear in all copies.
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
//#include <sys/param.h>
|
|
||||||
|
|
||||||
#define MAXPATHLEN 1024
|
#define __DECONST(a,b) (a)(b)
|
||||||
#ifndef ENAMETOOLONG
|
|
||||||
#define ENAMETOOLONG EINVAL
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int is_sep(char c) {
|
|
||||||
return c == '/' || c == ':';
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
char *
|
||||||
basename_r(const char *path, char *bname)
|
(basename)(char *path)
|
||||||
{
|
{
|
||||||
const char *endp, *startp;
|
char *ptr;
|
||||||
size_t len;
|
|
||||||
|
|
||||||
/* Empty or NULL string gets treated as "." */
|
/*
|
||||||
if (path == NULL || *path == '\0') {
|
* If path is a null pointer or points to an empty string,
|
||||||
bname[0] = '.';
|
* basename() shall return a pointer to the string ".".
|
||||||
bname[1] = '\0';
|
*/
|
||||||
return (bname);
|
if (path == NULL || *path == '\0')
|
||||||
}
|
return (__DECONST(char *, "."));
|
||||||
|
|
||||||
/* Strip any trailing slashes */
|
/* Find end of last pathname component and null terminate it. */
|
||||||
endp = path + strlen(path) - 1;
|
ptr = path + strlen(path);
|
||||||
while (endp > path && is_sep(*endp))
|
while (ptr > path + 1 && *(ptr - 1) == '/')
|
||||||
endp--;
|
--ptr;
|
||||||
|
*ptr-- = '\0';
|
||||||
|
|
||||||
/* All slashes becomes "/" */
|
/* Find beginning of last pathname component. */
|
||||||
if (endp == path && is_sep(*endp)) {
|
while (ptr > path && *(ptr - 1) != '/')
|
||||||
bname[0] = '/';
|
--ptr;
|
||||||
bname[1] = '\0';
|
return (ptr);
|
||||||
return (bname);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Find the start of the base */
|
|
||||||
startp = endp;
|
|
||||||
while (startp > path && !is_sep(*(startp - 1)))
|
|
||||||
startp--;
|
|
||||||
|
|
||||||
len = endp - startp + 1;
|
|
||||||
if (len >= MAXPATHLEN) {
|
|
||||||
errno = ENAMETOOLONG;
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
memcpy(bname, startp, len);
|
|
||||||
bname[len] = '\0';
|
|
||||||
return (bname);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
basename(const char *path)
|
|
||||||
{
|
|
||||||
static char *bname = NULL;
|
|
||||||
|
|
||||||
if (bname == NULL) {
|
|
||||||
bname = (char *)malloc(MAXPATHLEN);
|
|
||||||
if (bname == NULL)
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
return (basename_r(path, bname));
|
|
||||||
}
|
|
Loading…
Reference in New Issue