* 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:
Salvatore Benedetto 2009-01-11 23:38:27 +00:00
parent 7f2d1a266e
commit b3e849c2ce
6 changed files with 79 additions and 122 deletions

17
headers/posix/libgen.h Normal file
View File

@ -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 */

View File

@ -19,6 +19,7 @@ MergeObject posix_main.o :
fnmatch.c
glob.c
inttypes.c
libgen.cpp
poll.c
$(PWD_BACKEND)
scheduler.cpp

View File

@ -13,7 +13,6 @@ SubDirSysHdrs $(HAIKU_TOP) src system libroot posix glibc ;
SubDirCcFlags -D_GNU_SOURCE -DUSE_IN_LIBIO ;
MergeObject posix_gnu_misc.o :
dirname.c
insremque.c
lsearch.c
tsearch.c

View File

@ -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 <drepper@cygnus.com>, 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;
}

View File

@ -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 */

View File

@ -0,0 +1,61 @@
/*
* Copyright 2009, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Salvatore Benedetto <salvatore.benedetto@gmail.com>
*/
#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;
}