diff --git a/headers/posix/libgen.h b/headers/posix/libgen.h new file mode 100644 index 0000000000..3f392e8f96 --- /dev/null +++ b/headers/posix/libgen.h @@ -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 + +__BEGIN_DECLS + +char *basename(char *); +char *dirname(char *); + +__END_DECLS + +#endif /* _LIBGEN_H */ diff --git a/src/system/libroot/posix/Jamfile b/src/system/libroot/posix/Jamfile index dd2083229a..789bb0655f 100644 --- a/src/system/libroot/posix/Jamfile +++ b/src/system/libroot/posix/Jamfile @@ -19,6 +19,7 @@ MergeObject posix_main.o : fnmatch.c glob.c inttypes.c + libgen.cpp poll.c $(PWD_BACKEND) scheduler.cpp diff --git a/src/system/libroot/posix/glibc/misc/Jamfile b/src/system/libroot/posix/glibc/misc/Jamfile index d984af7c9e..0dcf7afccc 100644 --- a/src/system/libroot/posix/glibc/misc/Jamfile +++ b/src/system/libroot/posix/glibc/misc/Jamfile @@ -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 diff --git a/src/system/libroot/posix/glibc/misc/dirname.c b/src/system/libroot/posix/glibc/misc/dirname.c deleted file mode 100644 index 94ab1c085f..0000000000 --- a/src/system/libroot/posix/glibc/misc/dirname.c +++ /dev/null @@ -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 , 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 -#include - - -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; -} diff --git a/src/system/libroot/posix/glibc/misc/libgen.h b/src/system/libroot/posix/glibc/misc/libgen.h deleted file mode 100644 index b25254357d..0000000000 --- a/src/system/libroot/posix/glibc/misc/libgen.h +++ /dev/null @@ -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 - -__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 - ) 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 */ diff --git a/src/system/libroot/posix/libgen.cpp b/src/system/libroot/posix/libgen.cpp new file mode 100644 index 0000000000..91fcfada59 --- /dev/null +++ b/src/system/libroot/posix/libgen.cpp @@ -0,0 +1,61 @@ +/* + * Copyright 2009, Haiku Inc. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Salvatore Benedetto + */ + +#include +#include + + +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; +}