ulib fstatat

上级 3d00ccd2
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#define ST_ISDIR(st) S_ISDIR((st).st_mode) #define ST_ISDIR(st) S_ISDIR((st).st_mode)
#define ST_ISREG(st) S_ISREG((st).st_mode) #define ST_ISREG(st) S_ISREG((st).st_mode)
#define BSIZ 256 #define BSIZ 256
#define xfstatat(fd, n, st) fstatat(fd, n, st, 0)
#else // assume xv6 #else // assume xv6
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
...@@ -26,12 +27,12 @@ ...@@ -26,12 +27,12 @@
#define ST_ISREG(st) ((st).type == T_FILE) #define ST_ISREG(st) ((st).type == T_FILE)
#define BSIZ (DIRSIZ + 1) #define BSIZ (DIRSIZ + 1)
#define stderr 2 #define stderr 2
#define xfstatat fstatat
#endif #endif
void void
ls(const char *path) ls(const char *path)
{ {
char buf[512], *p;
int fd; int fd;
struct stat st; struct stat st;
...@@ -49,21 +50,13 @@ ls(const char *path) ...@@ -49,21 +50,13 @@ ls(const char *path)
if (ST_ISREG(st)) { if (ST_ISREG(st)) {
printf("%u %10lu %10lu %s\n", ST_TYPE(st), ST_INO(st), ST_SIZE(st), path); printf("%u %10lu %10lu %s\n", ST_TYPE(st), ST_INO(st), ST_SIZE(st), path);
} else if (ST_ISDIR(st)) { } else if (ST_ISDIR(st)) {
if(strlen(path) + 1 + BSIZ > sizeof buf) {
printf("ls: path too long\n");
}
strcpy(buf, path);
p = buf+strlen(buf);
*p++ = '/';
dirit di(fd); dirit di(fd);
for (; !di.end(); ++di) { for (; !di.end(); ++di) {
const char *name = *di; const char *name = *di;
size_t len = strlen(name);
memmove(p, name, len); if (xfstatat(fd, name, &st) < 0){
p[len] = 0; printf("ls: cannot stat %s\n", name);
if (stat(buf, &st) < 0){
free((void*)name); free((void*)name);
printf("ls: cannot stat %s\n", buf);
continue; continue;
} }
......
...@@ -34,6 +34,7 @@ int setfs(u64 base); ...@@ -34,6 +34,7 @@ int setfs(u64 base);
// ulib.c // ulib.c
int stat(char*, struct stat*); int stat(char*, struct stat*);
int fstatat(int dirfd, const char*, struct stat*);
char* strcpy(char*, const char*); char* strcpy(char*, const char*);
void *memmove(void*, const void*, int); void *memmove(void*, const void*, int);
char* strchr(const char*, char c); char* strchr(const char*, char c);
......
...@@ -102,6 +102,20 @@ stat(char *n, struct stat *st) ...@@ -102,6 +102,20 @@ stat(char *n, struct stat *st)
} }
int int
fstatat(int dirfd, const char *n, struct stat *st)
{
int fd;
int r;
fd = openat(dirfd, n, O_RDONLY);
if(fd < 0)
return -1;
r = fstat(fd, st);
close(fd);
return r;
}
int
atoi(const char *s) atoi(const char *s)
{ {
int n; int n;
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论