* Adding libgen.h as a public header
* Implementing dirname and basename I removed dirname from glibc/misc and reimplemented in order to (hopefully) keep thing tidy. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28888 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2009, Haiku Inc. All Rights Reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
#ifndef _LIBGEN_H
|
||||||
|
#define _LIBGEN_H
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
|
__BEGIN_DECLS
|
||||||
|
|
||||||
|
char *basename(char *);
|
||||||
|
char *dirname(char *);
|
||||||
|
|
||||||
|
__END_DECLS
|
||||||
|
|
||||||
|
#endif /* _LIBGEN_H */
|
||||||
@@ -19,6 +19,7 @@ MergeObject posix_main.o :
|
|||||||
fnmatch.c
|
fnmatch.c
|
||||||
glob.c
|
glob.c
|
||||||
inttypes.c
|
inttypes.c
|
||||||
|
libgen.cpp
|
||||||
poll.c
|
poll.c
|
||||||
$(PWD_BACKEND)
|
$(PWD_BACKEND)
|
||||||
scheduler.cpp
|
scheduler.cpp
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ SubDirSysHdrs $(HAIKU_TOP) src system libroot posix glibc ;
|
|||||||
SubDirCcFlags -D_GNU_SOURCE -DUSE_IN_LIBIO ;
|
SubDirCcFlags -D_GNU_SOURCE -DUSE_IN_LIBIO ;
|
||||||
|
|
||||||
MergeObject posix_gnu_misc.o :
|
MergeObject posix_gnu_misc.o :
|
||||||
dirname.c
|
|
||||||
insremque.c
|
insremque.c
|
||||||
lsearch.c
|
lsearch.c
|
||||||
tsearch.c
|
tsearch.c
|
||||||
|
|||||||
@@ -1,81 +0,0 @@
|
|||||||
/* dirname - return directory part of PATH.
|
|
||||||
Copyright (C) 1996, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|
||||||
This file is part of the GNU C Library.
|
|
||||||
Contributed by Ulrich Drepper <[email protected]>, 1996.
|
|
||||||
|
|
||||||
The GNU C Library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
The GNU C Library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with the GNU C Library; if not, write to the Free
|
|
||||||
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
||||||
02111-1307 USA. */
|
|
||||||
|
|
||||||
#include <libgen.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
|
||||||
char *
|
|
||||||
dirname (char *path)
|
|
||||||
{
|
|
||||||
static const char dot[] = ".";
|
|
||||||
char *last_slash;
|
|
||||||
|
|
||||||
/* Find last '/'. */
|
|
||||||
last_slash = path != NULL ? strrchr (path, '/') : NULL;
|
|
||||||
|
|
||||||
if (last_slash != NULL && last_slash != path && last_slash[1] == '\0')
|
|
||||||
{
|
|
||||||
/* Determine whether all remaining characters are slashes. */
|
|
||||||
char *runp;
|
|
||||||
|
|
||||||
for (runp = last_slash; runp != path; --runp)
|
|
||||||
if (runp[-1] != '/')
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* The '/' is the last character, we have to look further. */
|
|
||||||
if (runp != path)
|
|
||||||
last_slash = __memrchr (path, '/', runp - path);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (last_slash != NULL)
|
|
||||||
{
|
|
||||||
/* Determine whether all remaining characters are slashes. */
|
|
||||||
char *runp;
|
|
||||||
|
|
||||||
for (runp = last_slash; runp != path; --runp)
|
|
||||||
if (runp[-1] != '/')
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* Terminate the path. */
|
|
||||||
if (runp == path)
|
|
||||||
{
|
|
||||||
/* The last slash is the first character in the string. We have to
|
|
||||||
return "/". As a special case we have to return "//" if there
|
|
||||||
are exactly two slashes at the beginning of the string. See
|
|
||||||
XBD 4.10 Path Name Resolution for more information. */
|
|
||||||
if (last_slash == path + 1)
|
|
||||||
++last_slash;
|
|
||||||
else
|
|
||||||
last_slash = path + 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
last_slash = runp;
|
|
||||||
|
|
||||||
last_slash[0] = '\0';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
/* This assignment is ill-designed but the XPG specs require to
|
|
||||||
return a string containing "." in any case no directory part is
|
|
||||||
found and so a static and constant string is required. */
|
|
||||||
path = (char *) dot;
|
|
||||||
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
/* Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
|
|
||||||
This file is part of the GNU C Library.
|
|
||||||
|
|
||||||
The GNU C Library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
The GNU C Library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with the GNU C Library; if not, write to the Free
|
|
||||||
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
||||||
02111-1307 USA. */
|
|
||||||
|
|
||||||
#ifndef _LIBGEN_H
|
|
||||||
#define _LIBGEN_H 1
|
|
||||||
|
|
||||||
#include <features.h>
|
|
||||||
|
|
||||||
__BEGIN_DECLS
|
|
||||||
|
|
||||||
/* Return directory part of PATH or "." if none is available. */
|
|
||||||
extern char *dirname (char *__path) __THROW;
|
|
||||||
|
|
||||||
/* Return final component of PATH.
|
|
||||||
|
|
||||||
This is the weird XPG version of this function. It sometimes will
|
|
||||||
modify its argument. Therefore we normally use the GNU version (in
|
|
||||||
<string.h>) and only if this header is included make the XPG
|
|
||||||
version available under the real name. */
|
|
||||||
extern char *__xpg_basename (char *__path) __THROW;
|
|
||||||
#define basename __xpg_basename
|
|
||||||
|
|
||||||
__END_DECLS
|
|
||||||
|
|
||||||
#endif /* libgen.h */
|
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2009, Haiku Inc. All rights reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* Salvatore Benedetto <[email protected]>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <libgen.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
char*
|
||||||
|
basename(char *filepath)
|
||||||
|
{
|
||||||
|
if (filepath == NULL || filepath[0] == '\0')
|
||||||
|
return (char *)".";
|
||||||
|
|
||||||
|
size_t length = strlen(filepath);
|
||||||
|
/* Remove trailing slashes if any */
|
||||||
|
while (filepath[--length] == '/' && length)
|
||||||
|
filepath[length] = '\0';
|
||||||
|
|
||||||
|
char *last = strrchr(filepath, '/');
|
||||||
|
/* If no slash were found return the whole string */
|
||||||
|
if (last == NULL)
|
||||||
|
return filepath;
|
||||||
|
|
||||||
|
/* If the next char is the end it means we got only "/"
|
||||||
|
* and we don't have to truncate */
|
||||||
|
if (*(last + 1) != '\0')
|
||||||
|
++last;
|
||||||
|
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char*
|
||||||
|
dirname(char *filepath)
|
||||||
|
{
|
||||||
|
if (filepath == NULL || filepath[0] == '\0')
|
||||||
|
return (char *)".";
|
||||||
|
|
||||||
|
size_t length = strlen(filepath);
|
||||||
|
/* Remove trailing slashes if any */
|
||||||
|
while (filepath[--length] == '/' && length)
|
||||||
|
filepath[length] = '\0';
|
||||||
|
|
||||||
|
char *last = strrchr(filepath, '/');
|
||||||
|
/* If no slash were found return a dot */
|
||||||
|
if (last == NULL)
|
||||||
|
return (char *)".";
|
||||||
|
|
||||||
|
/* In case we got just "/" don't truncate it */
|
||||||
|
if (last == filepath)
|
||||||
|
last++;
|
||||||
|
|
||||||
|
*last = '\0';
|
||||||
|
|
||||||
|
return filepath;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user