34#ifndef _CONSOLE_FILECOMMANDS_HXX_
35#define _CONSOLE_FILECOMMANDS_HXX_
67 const char *argv[],
void *context)
71 fprintf(fp,
"concatenate files and print on the standard output\n");
75 for (
int i = 1; i < argc; ++i)
77 int fd = ::open(argv[i], O_RDONLY | O_NONBLOCK);
80 fprintf(fp,
"%s: %s: No such file or directory\n",
88 result = ::read(fd, buf,
sizeof(buf));
91 fprintf(fp,
"%.*s", result, buf);
115 fprintf(fp,
"display a line of text\n");
121 fprintf(fp,
"%s\n", argv[1]);
126 if (!strcmp(argv[2],
">"))
128 int fd = ::open(argv[3], O_WRONLY | O_CREAT);
131 fprintf(fp,
"%s: No such file or directory\n", argv[3]);
135 ssize_t wr_size = strlen(argv[1]);
136 ssize_t result = ::write(fd, argv[1], wr_size);
154 const char *argv[],
void *context)
158 fprintf(fp,
"remove files or directories\n");
167 for (
int i = 1; i < argc; ++i)
169 if (::unlink(argv[i]) == 0)
176 "%s: cannot remove '%s': No such file or directory\n",
191 const char *argv[],
void *context)
195 fprintf(fp,
"list directory contents\n");
205 if (::stat(argv[1], &stat) != 0)
210 "%s: cannot access '%s': No such file or directory\n",
216 if (S_ISDIR(stat.st_mode))
232 else if (S_ISREG(stat.st_mode) || S_ISLNK(stat.st_mode))
244 static void ls_printline(FILE *fp,
const char *name,
struct stat *stat)
247 type = S_ISDIR(stat->st_mode) ?
'd' :
'-';
248 type = S_ISLNK(stat->st_mode) ?
'l' : type;
250 fprintf(fp,
"%c%c%c%c%c%c%c%c%c%c %5ld %s\n", type,
251 stat->st_mode & S_IRUSR ?
'r' :
'-',
252 stat->st_mode & S_IWUSR ?
'w' :
'-',
253 stat->st_mode & S_IXUSR ?
'x' :
'-',
254 stat->st_mode & S_IRGRP ?
'r' :
'-',
255 stat->st_mode & S_IWGRP ?
'w' :
'-',
256 stat->st_mode & S_IXGRP ?
'x' :
'-',
257 stat->st_mode & S_IROTH ?
'r' :
'-',
258 stat->st_mode & S_IWOTH ?
'w' :
'-',
259 stat->st_mode & S_IXOTH ?
'x' :
'-',
260 stat->st_size, name);
DIR * opendir(const char *name)
Open a directory.
struct dirent * readdir(DIR *dirp)
Read the next entry in a directory.
int closedir(DIR *dirp)
Close a directory.
This class provides a console available from stdin/stdout as well as via telnet.
void add_command(const char *name, Callback callback, void *context=NULL)
Add a new command to the console.
CommandStatus
Enumeration of recognized command callback results.
@ COMMAND_ERROR
Command had some kind of error.
@ COMMAND_OK
Command executed successfully.
Container for all the file system operations.
static Console::CommandStatus rm_command(FILE *fp, int argc, const char *argv[], void *context)
Remove files or directories.
static void ls_printline(FILE *fp, const char *name, struct stat *stat)
Helper method to print a single "ls" line.
static Console::CommandStatus cat_command(FILE *fp, int argc, const char *argv[], void *context)
Concatinate files.
static Console::CommandStatus echo_command(FILE *fp, int argc, const char *argv[], void *context)
Echo string.
static Console::CommandStatus ls_command(FILE *fp, int argc, const char *argv[], void *context)
List directory contents.
FileCommands(Console *console)
Constructor.
uintptr_t DIR
DIR typedef.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Directory entry structure.
char d_name[]
filename string of entry