Parially port xls to Linux

上级 750ed4e1
#if defined(LINUX) #if defined(LINUX)
#include <sys/syscall.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
......
#if defined(LINUX) #if defined(LINUX)
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#define ST_SIZE(st) (st).st_size
#define ST_TYPE(st) 0
#define ST_INO(st) (st).st_ino
#define ST_ISDIR(st) S_ISDIR((st).st_mode)
#define ST_ISREG(st) S_ISREG((st).st_mode)
#define BSIZ 256
#else // assume xv6 #else // assume xv6
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "fs.h" #include "fs.h"
#define ST_SIZE(st) (st).size
#define ST_TYPE(st) (st).type
#define ST_INO(st) (st).ino
#define ST_ISDIR(st) ((st).type == T_DIR)
#define ST_ISREG(st) ((st).type == T_FILE)
#define BSIZE (DIRSIZ + 1)
#define stderr 2
#endif #endif
const char* const char*
fmtname(const char *path) fmtname(const char *path)
{ {
static char buf[DIRSIZ+1]; static char buf[BSIZ];
const char *p; const char *p;
// Find first character after last slash. // Find first character after last slash.
...@@ -18,10 +37,10 @@ fmtname(const char *path) ...@@ -18,10 +37,10 @@ fmtname(const char *path)
p++; p++;
// Return blank-padded name. // Return blank-padded name.
if(strlen(p) >= DIRSIZ) if(strlen(p) >= BSIZ-1)
return p; return p;
memmove(buf, p, strlen(p)); memmove(buf, p, strlen(p));
memset(buf+strlen(p), ' ', DIRSIZ-strlen(p)); memset(buf+strlen(p), ' ', BSIZ-1-strlen(p));
return buf; return buf;
} }
...@@ -34,25 +53,21 @@ ls(const char *path) ...@@ -34,25 +53,21 @@ ls(const char *path)
struct stat st; struct stat st;
if((fd = open(path, 0)) < 0){ if((fd = open(path, 0)) < 0){
fprintf(2, "ls: cannot open %s\n", path); fprintf(stderr, "ls: cannot open %s\n", path);
return; return;
} }
if(fstat(fd, &st) < 0){ if(fstat(fd, &st) < 0){
fprintf(2, "ls: cannot stat %s\n", path); fprintf(stderr, "ls: cannot stat %s\n", path);
close(fd); close(fd);
return; return;
} }
switch(st.type){ if (ST_ISREG(st)) {
case T_FILE: printf("%s %d %d %d\n", fmtname(path), ST_TYPE(st), ST_INO(st), ST_SIZE(st));
fprintf(1, "%s %d %d %d\n", fmtname(path), st.type, st.ino, st.size); } else if (ST_ISDIR(st)) {
break; if(strlen(path) + 1 + BSIZ > sizeof buf) {
printf("ls: path too long\n");
case T_DIR:
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
fprintf(1, "ls: path too long\n");
break;
} }
strcpy(buf, path); strcpy(buf, path);
p = buf+strlen(buf); p = buf+strlen(buf);
...@@ -60,15 +75,14 @@ ls(const char *path) ...@@ -60,15 +75,14 @@ ls(const char *path)
while(read(fd, &de, sizeof(de)) == sizeof(de)){ while(read(fd, &de, sizeof(de)) == sizeof(de)){
if(de.inum == 0) if(de.inum == 0)
continue; continue;
memmove(p, de.name, DIRSIZ); memmove(p, de.name, BSIZ-1);
p[DIRSIZ] = 0; p[BSIZ-1] = 0;
if(stat(buf, &st) < 0){ if(stat(buf, &st) < 0){
fprintf(1, "ls: cannot stat %s\n", buf); printf("ls: cannot stat %s\n", buf);
continue; continue;
} }
fprintf(1, "%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size); printf("%s %d %d %d\n", fmtname(buf), ST_TYPE(st), ST_INO(st), ST_SIZE(st));
} }
break;
} }
close(fd); close(fd);
} }
...@@ -80,9 +94,9 @@ main(int argc, char *argv[]) ...@@ -80,9 +94,9 @@ main(int argc, char *argv[])
if(argc < 2){ if(argc < 2){
ls("."); ls(".");
exit(); return 0;
} }
for(i=1; i<argc; i++) for(i=1; i<argc; i++)
ls(argv[i]); ls(argv[i]);
exit(); return 0;
} }
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论