From: <ba...@us...> - 2006-09-17 23:44:15
|
Revision: 358 http://svn.sourceforge.net/cadcdev/?rev=358&view=rev Author: bardtx Date: 2006-09-17 16:44:08 -0700 (Sun, 17 Sep 2006) Log Message: ----------- kos: Patch #1552702 from SourceForge: A patch that includes realpath() to gain support for "." and ".." in pathnames. Also includes some small changes to map standard functions to KOS functions. Modified Paths: -------------- kos/include/kos/limits.h kos/kernel/libc/koslib/Makefile Added Paths: ----------- kos/include/limits.h kos/kernel/libc/koslib/chdir.c kos/kernel/libc/koslib/creat.c kos/kernel/libc/koslib/getcwd.c kos/kernel/libc/koslib/mkdir.c kos/kernel/libc/koslib/realpath.c kos/kernel/libc/koslib/rename.c kos/kernel/libc/koslib/rmdir.c kos/kernel/libc/koslib/sleep.c Modified: kos/include/kos/limits.h =================================================================== --- kos/include/kos/limits.h 2006-09-17 23:33:39 UTC (rev 357) +++ kos/include/kos/limits.h 2006-09-17 23:44:08 UTC (rev 358) @@ -11,5 +11,6 @@ #define __KOS_LIMITS_H #define MAX_FN_LEN 256 /* Max filename length */ +#define PATH_MAX 4095 /* Max path length */ #endif /* __KOS_LIMITS_H */ Added: kos/include/limits.h =================================================================== --- kos/include/limits.h (rev 0) +++ kos/include/limits.h 2006-09-17 23:44:08 UTC (rev 358) @@ -0,0 +1,13 @@ +/* KallistiOS ##version## + + limits.h + Copyright (C)2006 Dan Potter + +*/ + +#ifndef __LIMITS_H +#define __LIMITS_H + +#include <kos/limits.h> + +#endif /* __LIMITS_H */ Modified: kos/kernel/libc/koslib/Makefile =================================================================== --- kos/kernel/libc/koslib/Makefile 2006-09-17 23:33:39 UTC (rev 357) +++ kos/kernel/libc/koslib/Makefile 2006-09-17 23:44:08 UTC (rev 358) @@ -10,6 +10,7 @@ OBJS = abort.o byteorder.o memset2.o memset4.o memcpy2.o memcpy4.o \ assert.o dbglog.o malloc.o crtbegin.o crtend.o atexit.o \ opendir.o readdir.o closedir.o rewinddir.o scandir.o seekdir.o \ - telldir.o usleep.o inet_addr.o + telldir.o usleep.o inet_addr.o realpath.o getcwd.o chdir.o mkdir.o \ + creat.o sleep.o rmdir.o rename.o include $(KOS_BASE)/Makefile.prefab Added: kos/kernel/libc/koslib/chdir.c =================================================================== --- kos/kernel/libc/koslib/chdir.c (rev 0) +++ kos/kernel/libc/koslib/chdir.c 2006-09-17 23:44:08 UTC (rev 358) @@ -0,0 +1,12 @@ +/* KallistiOS ##version## + + chdir.c + Copyright (C)2005 Walter van Niftrik +*/ + +#include <kos/fs.h> + +int chdir(const char *path) +{ + return fs_chdir(path); +} Added: kos/kernel/libc/koslib/creat.c =================================================================== --- kos/kernel/libc/koslib/creat.c (rev 0) +++ kos/kernel/libc/koslib/creat.c 2006-09-17 23:44:08 UTC (rev 358) @@ -0,0 +1,12 @@ +/* KallistiOS ##version## + + creat.c + Copyright (C)2005 Walter van Niftrik +*/ + +#include <fcntl.h> + +int creat(const char *pathname, mode_t mode) +{ + return open(pathname, O_CREAT|O_WRONLY|O_TRUNC, mode); +} Added: kos/kernel/libc/koslib/getcwd.c =================================================================== --- kos/kernel/libc/koslib/getcwd.c (rev 0) +++ kos/kernel/libc/koslib/getcwd.c 2006-09-17 23:44:08 UTC (rev 358) @@ -0,0 +1,23 @@ +/* KallistiOS ##version## + + getcwd.c + Copyright (C)2005 Walter van Niftrik +*/ + +#include <string.h> +#include <errno.h> + +#include <kos/fs.h> + +char *getcwd(char *buf, size_t size) +{ + const char *wd = fs_getwd(); + + if (strlen(wd) + 1 > size) { + errno = ERANGE; + return NULL; + } + + strcpy(buf, wd); + return buf; +} Added: kos/kernel/libc/koslib/mkdir.c =================================================================== --- kos/kernel/libc/koslib/mkdir.c (rev 0) +++ kos/kernel/libc/koslib/mkdir.c 2006-09-17 23:44:08 UTC (rev 358) @@ -0,0 +1,12 @@ +/* KallistiOS ##version## + + mkdir.c + Copyright (C)2005 Walter van Niftrik +*/ + +#include <kos/fs.h> + +int mkdir(const char *pathname, mode_t mode) +{ + return fs_mkdir(pathname); +} Added: kos/kernel/libc/koslib/realpath.c =================================================================== --- kos/kernel/libc/koslib/realpath.c (rev 0) +++ kos/kernel/libc/koslib/realpath.c 2006-09-17 23:44:08 UTC (rev 358) @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2003 Constantin S. Svintsoff <ko...@ic...> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * 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. + * 3. The names of the authors may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * 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 <sys/cdefs.h> + +#include <sys/param.h> +#include <sys/stat.h> + +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <limits.h> + +/* + * char *realpath(const char *path, char resolved[PATH_MAX]); + * + * Find the real name of path, by removing all ".", ".." and symlink + * components. Returns (resolved) on success, or (NULL) on failure, + * in which case the path which caused trouble is left in (resolved). + */ +char * +realpath(const char *path, char resolved[PATH_MAX]) +{ + char *p, *q, *s; + size_t left_len, resolved_len; + int serrno; + char left[PATH_MAX], next_token[PATH_MAX]; + + serrno = errno; + if (path[0] == '/') { + resolved[0] = '/'; + resolved[1] = '\0'; + if (path[1] == '\0') + return (resolved); + resolved_len = 1; + left_len = strlcpy(left, path + 1, sizeof(left)); + } else { + if (getcwd(resolved, PATH_MAX) == NULL) { + strlcpy(resolved, ".", PATH_MAX); + return (NULL); + } + resolved_len = strlen(resolved); + left_len = strlcpy(left, path, sizeof(left)); + } + if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { + errno = ENAMETOOLONG; + return (NULL); + } + + /* + * Iterate over path components in `left'. + */ + while (left_len != 0) { + /* + * Extract the next path component and adjust `left' + * and its length. + */ + p = strchr(left, '/'); + s = p ? p : left + left_len; + if (s - left >= sizeof(next_token)) { + errno = ENAMETOOLONG; + return (NULL); + } + memcpy(next_token, left, s - left); + next_token[s - left] = '\0'; + left_len -= s - left; + if (p != NULL) + memmove(left, s + 1, left_len + 1); + if (resolved[resolved_len - 1] != '/') { + if (resolved_len + 1 >= PATH_MAX) { + errno = ENAMETOOLONG; + return (NULL); + } + resolved[resolved_len++] = '/'; + resolved[resolved_len] = '\0'; + } + if (next_token[0] == '\0') + continue; + else if (strcmp(next_token, ".") == 0) + continue; + else if (strcmp(next_token, "..") == 0) { + /* + * Strip the last path component except when we have + * single "/" + */ + if (resolved_len > 1) { + resolved[resolved_len - 1] = '\0'; + q = strrchr(resolved, '/') + 1; + *q = '\0'; + resolved_len = q - resolved; + } + continue; + } + + /* + * Append the next path component and lstat() it. If + * lstat() fails we still can return successfully if + * there are no more path components left. + */ + resolved_len = strlcat(resolved, next_token, PATH_MAX); + if (resolved_len >= PATH_MAX) { + errno = ENAMETOOLONG; + return (NULL); + } + } + + /* + * Remove trailing slash except when the resolved pathname + * is a single "/". + */ + if (resolved_len > 1 && resolved[resolved_len - 1] == '/') + resolved[resolved_len - 1] = '\0'; + return (resolved); +} Added: kos/kernel/libc/koslib/rename.c =================================================================== --- kos/kernel/libc/koslib/rename.c (rev 0) +++ kos/kernel/libc/koslib/rename.c 2006-09-17 23:44:08 UTC (rev 358) @@ -0,0 +1,12 @@ +/* KallistiOS ##version## + + rename.c + Copyright (C)2005 Walter van Niftrik +*/ + +#include <kos/fs.h> + +int rename(const char *oldpath, const char *newpath) +{ + return fs_rename(oldpath, newpath); +} Added: kos/kernel/libc/koslib/rmdir.c =================================================================== --- kos/kernel/libc/koslib/rmdir.c (rev 0) +++ kos/kernel/libc/koslib/rmdir.c 2006-09-17 23:44:08 UTC (rev 358) @@ -0,0 +1,12 @@ +/* KallistiOS ##version## + + rmdir.c + Copyright (C)2005 Walter van Niftrik +*/ + +#include <kos/fs.h> + +int rmdir(const char *pathname) +{ + return fs_rmdir(pathname); +} Added: kos/kernel/libc/koslib/sleep.c =================================================================== --- kos/kernel/libc/koslib/sleep.c (rev 0) +++ kos/kernel/libc/koslib/sleep.c 2006-09-17 23:44:08 UTC (rev 358) @@ -0,0 +1,14 @@ +/* KallistiOS ##version## + + sleep.c + Copyright (C)2005 Walter van Niftrik +*/ + +#include <kos/thread.h> + +unsigned int sleep(unsigned int seconds) +{ + thd_sleep(seconds * 1000); + + return 0; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |