支持cat命令
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include "shell.h"
|
||||
#include "cons_cli.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
@@ -29,3 +31,29 @@ int ls(int argc, char *agrv[])
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), ls, ls, ls command);
|
||||
int cat(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
return (-1);
|
||||
}
|
||||
|
||||
FILE *fp;
|
||||
int c;
|
||||
|
||||
if ((fp = fopen(argv[1], "r")) == NULL)
|
||||
{
|
||||
return (-2);
|
||||
}
|
||||
|
||||
while ((c = fgetc(fp)) != EOF)
|
||||
{
|
||||
cons_write(&c, 1);
|
||||
}
|
||||
cons_write_str("\n");
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), cat, cat, cat command);
|
||||
|
||||
|
||||
@@ -12,21 +12,32 @@ FILE *fopen(const char *restrict filename, const char *restrict mode)
|
||||
int flags;
|
||||
|
||||
/* Check for valid initial mode character */
|
||||
if (!strchr("rwa", *mode)) {
|
||||
if (!strchr("rwa", *mode))
|
||||
{
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Compute the flags to pass to open() */
|
||||
flags = __fmodeflags(mode);
|
||||
|
||||
#ifdef NO_LITTLE_MODE
|
||||
fd = sys_open(filename, flags, 0666);
|
||||
if (fd < 0) return 0;
|
||||
#else
|
||||
fd = be_open(filename, flags, 0666);
|
||||
#endif
|
||||
if (fd < 0)
|
||||
return 0;
|
||||
if (flags & O_CLOEXEC)
|
||||
#ifdef NO_LITTLE_MODE
|
||||
__syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
|
||||
#else
|
||||
/*TODO:*/
|
||||
;
|
||||
#endif
|
||||
|
||||
f = __fdopen(fd, mode);
|
||||
if (f) return f;
|
||||
if (f)
|
||||
return f;
|
||||
|
||||
be_close(fd);
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user