2 ** Copyright 2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
5 #include <sys/syscalls.h>
12 void (*disp_func
)(const char *, struct file_stat
*) = NULL
;
13 bool full_list
= false;
15 static void display_l(const char *filename
, struct file_stat
*stat
)
20 case STREAM_TYPE_FILE
:
23 case STREAM_TYPE_DEVICE
:
29 case STREAM_TYPE_PIPE
:
36 printf("%s %12Ld %s\n", type
, stat
->size
, filename
);
40 static void display(const char *filename
, struct file_stat
*stat
)
42 printf("%s\n", filename
);
45 static void usage(const char *progname
)
48 printf("%s [-al] [file ...]\n", progname
);
53 static int do_ls(const char *arg
)
57 struct file_stat stat
;
60 rc
= _kern_rstat(arg
, &stat
);
62 printf("_kern_rstat() returned error: %s!\n", strerror(rc
));
67 case STREAM_TYPE_DIR
: {
70 bool done_dot
, done_dotdot
;
72 fd
= _kern_opendir(arg
);
74 //printf("ls: opendir() returned error: %s!\n", strerror(fd));
78 if(strcmp(arg
, ".") != 0) {
83 done_dot
= done_dotdot
= false;
85 done_dot
= done_dotdot
= true;
92 strlcpy(filename
, ".", sizeof(filename
));
94 } else if(!done_dotdot
) {
95 strlcpy(filename
, "..", sizeof(filename
));
98 rc
= _kern_readdir(fd
, filename
, sizeof(filename
));
104 if(strcmp(arg
, ".") != 0) {
105 strlcpy(full_path
, arg
, sizeof(full_path
));
106 strlcat(full_path
, "/", sizeof(full_path
));
108 strlcat(full_path
, filename
, sizeof(full_path
));
110 rc2
= _kern_rstat(full_path
, &stat
);
112 (*disp_func
)(filename
, &stat
);
118 printf("%d files found\n", count
);
122 (*disp_func
)(arg
, &stat
);
129 int main(int argc
, char *argv
[])
133 disp_func
= &display
;
135 while((c
= getopt(argc
, argv
, "al")) >= 0) {
141 disp_func
= &display_l
;
149 // no arguments to ls. ls the current dir
152 for(; optind
< argc
; optind
++) {