4
0
mirror of https://github.com/RT-Thread/rt-thread.git synced 2025-01-18 12:53:31 +08:00

use RT_NULL instead of NULL in DFS, and format the code in DFS

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2213 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
dzzxzz@gmail.com 2012-07-10 09:25:19 +00:00
parent 9d4072ce59
commit fdc7dfc768
2 changed files with 92 additions and 52 deletions

View File

@ -1,7 +1,7 @@
/* /*
* File : dfs.c * File : dfs.c
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -38,6 +38,7 @@ struct dfs_fd fd_table[DFS_FD_MAX];
/** /**
* @addtogroup DFS * @addtogroup DFS
*/ */
/*@{*/ /*@{*/
/** /**
@ -204,6 +205,7 @@ int fd_is_open(const char *pathname)
{ {
/* can't find mounted file system */ /* can't find mounted file system */
rt_free(fullpath); rt_free(fullpath);
return -1; return -1;
} }
@ -225,6 +227,7 @@ int fd_is_open(const char *pathname)
/* found file in file descriptor table */ /* found file in file descriptor table */
rt_free(fullpath); rt_free(fullpath);
dfs_unlock(); dfs_unlock();
return 0; return 0;
} }
} }
@ -256,6 +259,7 @@ const char *dfs_subdir(const char *directory, const char *filename)
{ {
dir --; dir --;
} }
return dir; return dir;
} }
@ -276,12 +280,13 @@ char *dfs_normalize_path(const char *directory, const char *filename)
RT_ASSERT(filename != RT_NULL); RT_ASSERT(filename != RT_NULL);
#ifdef DFS_USING_WORKDIR #ifdef DFS_USING_WORKDIR
if (directory == NULL) /* shall use working directory */ if (directory == RT_NULL) /* shall use working directory */
directory = &working_directory[0]; directory = &working_directory[0];
#else #else
if ((directory == NULL) && (filename[0] != '/')) if ((directory == RT_NULL) && (filename[0] != '/'))
{ {
rt_kprintf(NO_WORKING_DIR); rt_kprintf(NO_WORKING_DIR);
return RT_NULL; return RT_NULL;
} }
#endif #endif
@ -307,61 +312,61 @@ char *dfs_normalize_path(const char *directory, const char *filename)
{ {
char c = *src; char c = *src;
if (c == '.') if (c == '.')
{ {
if (!src[1]) src ++; /* '.' and ends */ if (!src[1]) src ++; /* '.' and ends */
else if (src[1] == '/') else if (src[1] == '/')
{ {
/* './' case */ /* './' case */
src += 2; src += 2;
while ((*src == '/') && (*src != '\0')) while ((*src == '/') && (*src != '\0'))
src ++; src ++;
continue; continue;
} }
else if (src[1] == '.') else if (src[1] == '.')
{ {
if (!src[2]) if (!src[2])
{ {
/* '..' and ends case */ /* '..' and ends case */
src += 2; src += 2;
goto up_one; goto up_one;
} }
else if (src[2] == '/') else if (src[2] == '/')
{ {
/* '../' case */ /* '../' case */
src += 3; src += 3;
while ((*src == '/') && (*src != '\0')) while ((*src == '/') && (*src != '\0'))
src ++; src ++;
goto up_one; goto up_one;
} }
} }
} }
/* copy up the next '/' and erase all '/' */ /* copy up the next '/' and erase all '/' */
while ((c = *src++) != '\0' && c != '/') while ((c = *src++) != '\0' && c != '/')
*dst ++ = c; *dst ++ = c;
if (c == '/') if (c == '/')
{ {
*dst ++ = '/'; *dst ++ = '/';
while (c == '/') while (c == '/')
c = *src++; c = *src++;
src --; src --;
} }
else if (!c) else if (!c)
break; break;
continue; continue;
up_one: up_one:
dst --; dst --;
if (dst < dst0) if (dst < dst0)
{ {
rt_free(fullpath); rt_free(fullpath);
return NULL; return RT_NULL;
} }
while (dst0 < dst && dst[-1] != '/') while (dst0 < dst && dst[-1] != '/')
dst --; dst --;

View File

@ -42,7 +42,7 @@ int open(const char *file, int flags, int mode)
rt_set_errno(-DFS_STATUS_ENOMEM); rt_set_errno(-DFS_STATUS_ENOMEM);
return -1; return -1;
} }
d = fd_get(fd); d = fd_get(fd);
result = dfs_file_open(d, file, flags); result = dfs_file_open(d, file, flags);
if (result < 0) if (result < 0)
@ -58,6 +58,7 @@ int open(const char *file, int flags, int mode)
/* release the ref-count of fd */ /* release the ref-count of fd */
fd_put(d); fd_put(d);
return fd; return fd;
} }
@ -87,10 +88,12 @@ int close(int fd)
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
fd_put(d); fd_put(d);
return 0; return 0;
} }
@ -110,7 +113,7 @@ int read(int fd, void *buf, size_t len)
struct dfs_fd *d; struct dfs_fd *d;
/* get the fd */ /* get the fd */
d = fd_get(fd); d = fd_get(fd);
if (d == RT_NULL) if (d == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
@ -128,6 +131,7 @@ int read(int fd, void *buf, size_t len)
/* release the ref-count of fd */ /* release the ref-count of fd */
fd_put(d); fd_put(d);
return result; return result;
} }
@ -147,10 +151,11 @@ int write(int fd, const void *buf, size_t len)
struct dfs_fd *d; struct dfs_fd *d;
/* get the fd */ /* get the fd */
d = fd_get(fd); d = fd_get(fd);
if (d == RT_NULL) if (d == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return -1; return -1;
} }
@ -165,6 +170,7 @@ int write(int fd, const void *buf, size_t len)
/* release the ref-count of fd */ /* release the ref-count of fd */
fd_put(d); fd_put(d);
return result; return result;
} }
@ -206,12 +212,12 @@ off_t lseek(int fd, off_t offset, int whence)
default: default:
rt_set_errno(-DFS_STATUS_EINVAL); rt_set_errno(-DFS_STATUS_EINVAL);
return -1; return -1;
} }
if (offset < 0) if (offset < 0)
{ {
rt_set_errno(-DFS_STATUS_EINVAL); rt_set_errno(-DFS_STATUS_EINVAL);
return -1; return -1;
} }
result = dfs_file_lseek(d, offset); result = dfs_file_lseek(d, offset);
@ -219,11 +225,13 @@ off_t lseek(int fd, off_t offset, int whence)
{ {
fd_put(d); fd_put(d);
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
/* release the ref-count of fd */ /* release the ref-count of fd */
fd_put(d); fd_put(d);
return offset; return offset;
} }
@ -246,8 +254,10 @@ int rename(const char *old, const char *new)
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
return 0; return 0;
} }
@ -267,8 +277,10 @@ int unlink(const char *pathname)
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
return 0; return 0;
} }
@ -288,8 +300,10 @@ int stat(const char *file, struct stat *buf)
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
return result; return result;
} }
@ -308,6 +322,7 @@ int fstat(int fildes, struct stat *buf)
if (d == RT_NULL) if (d == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return -1; return -1;
} }
@ -348,6 +363,7 @@ int statfs(const char *path, struct statfs *buf)
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
@ -372,6 +388,7 @@ int mkdir(const char *path, mode_t mode)
if (fd == -1) if (fd == -1)
{ {
rt_set_errno(-DFS_STATUS_ENOMEM); rt_set_errno(-DFS_STATUS_ENOMEM);
return -1; return -1;
} }
@ -383,11 +400,13 @@ int mkdir(const char *path, mode_t mode)
{ {
fd_put(d); fd_put(d);
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
dfs_file_close(d); dfs_file_close(d);
fd_put(d); fd_put(d);
return 0; return 0;
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
@ -400,7 +419,7 @@ FINSH_FUNCTION_EXPORT(mkdir, create a directory);
* *
* @param pathname the path name to be removed. * @param pathname the path name to be removed.
* *
* @return 0 on sucessfull, others on failed. * @return 0 on successful, others on failed.
*/ */
int rmdir(const char *pathname) int rmdir(const char *pathname)
{ {
@ -410,6 +429,7 @@ int rmdir(const char *pathname)
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
@ -436,6 +456,7 @@ DIR *opendir(const char *name)
if (fd == -1) if (fd == -1)
{ {
rt_set_errno(-DFS_STATUS_ENOMEM); rt_set_errno(-DFS_STATUS_ENOMEM);
return RT_NULL; return RT_NULL;
} }
d = fd_get(fd); d = fd_get(fd);
@ -452,10 +473,11 @@ DIR *opendir(const char *name)
} }
else else
{ {
rt_memset(t, 0, sizeof(DIR)); rt_memset(t, 0, sizeof(DIR));
t->fd = fd; t->fd = fd;
} }
fd_put(d); fd_put(d);
return t; return t;
} }
@ -485,6 +507,7 @@ struct dirent *readdir(DIR *d)
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return RT_NULL; return RT_NULL;
} }
@ -505,6 +528,7 @@ struct dirent *readdir(DIR *d)
} }
fd_put(fd); fd_put(fd);
return (struct dirent *)(d->buf+d->cur); return (struct dirent *)(d->buf+d->cur);
} }
@ -525,6 +549,7 @@ long telldir(DIR *d)
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return 0; return 0;
} }
@ -549,6 +574,7 @@ void seekdir(DIR *d, off_t offset)
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return ; return ;
} }
@ -571,6 +597,7 @@ void rewinddir(DIR *d)
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return ; return ;
} }
@ -597,6 +624,7 @@ int closedir(DIR *d)
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return -1; return -1;
} }
@ -609,9 +637,11 @@ int closedir(DIR *d)
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
else return 0; else
return 0;
} }
#ifdef DFS_USING_WORKDIR #ifdef DFS_USING_WORKDIR
@ -632,12 +662,14 @@ int chdir(const char *path)
dfs_lock(); dfs_lock();
rt_kprintf("%s\n", working_directory); rt_kprintf("%s\n", working_directory);
dfs_unlock(); dfs_unlock();
return 0; return 0;
} }
if (rt_strlen(path) > DFS_PATH_MAX) if (rt_strlen(path) > DFS_PATH_MAX)
{ {
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; return -1;
} }
@ -645,6 +677,7 @@ int chdir(const char *path)
if (fullpath == RT_NULL) if (fullpath == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; /* build path failed */ return -1; /* build path failed */
} }
@ -655,6 +688,7 @@ int chdir(const char *path)
rt_free(fullpath); rt_free(fullpath);
/* this is a not exist directory */ /* this is a not exist directory */
dfs_unlock(); dfs_unlock();
return -1; return -1;
} }
@ -692,6 +726,7 @@ char *getcwd(char *buf, size_t size)
#else #else
rt_kprintf("WARNING: not support working directory\n"); rt_kprintf("WARNING: not support working directory\n");
#endif #endif
return buf; return buf;
} }