shell: mufs: add utilities to append, cat, rm, mv and stat files

tags/0.1.0
S.J.R. van Schaik 8 years ago
parent ab233bfd33
commit 286c5ebefe
  1. 106
      source/shell/mufs.c

@ -18,6 +18,11 @@ static void do_mufs_mount(const char *s);
static void do_mufs_format(const char *s);
static void do_mufs_mkdir(const char *s);
static void do_mufs_rmdir(const char *s);
static void do_mufs_stat(const char *stat);
static void do_mufs_cat(const char *s);
static void do_mufs_append(const char *s);
static void do_mufs_mv(const char *s);
static void do_mufs_rm(const char *s);
static void do_mufs_ls(const char *s);
static struct cmd mufs_cmds[] = {
@ -26,6 +31,11 @@ static struct cmd mufs_cmds[] = {
{ "mkdir", do_mufs_mkdir },
{ "rmdir", do_mufs_rmdir },
{ "ls", do_mufs_ls },
{ "stat", do_mufs_stat },
{ "cat", do_mufs_cat },
{ "append", do_mufs_append },
{ "mv", do_mufs_mv, },
{ "rm", do_mufs_rm, },
{ NULL, NULL },
};
@ -81,6 +91,102 @@ static void do_mufs_rmdir(const char *s)
}
}
static void do_mufs_stat(const char *s)
{
struct mufs_stat stat;
if (mufs_stat(&mufs, s, &stat) < 0) {
fprintf(stderr, "error: unable to stat the file\n");
return;
}
switch (stat.type) {
case MUFS_DIR: printf(" type: directory\n"); break;
case MUFS_FILE: printf(" type: file\n"); break;
default: return;
}
printf(" file size: %u bytes\n", stat.file_size);
}
static void do_mufs_cat(const char *s)
{
char data[256];
struct mufs_file *file;
if (!(file = mufs_open(&mufs, s, MUFS_READ))) {
fprintf(stderr, "error: unable to open the file\n");
return;
}
while (mufs_read(file, data, sizeof data) != 0) {
fwrite(data, sizeof *data, sizeof data, stdout);
}
mufs_close(file);
}
static void do_mufs_append(const char *s)
{
struct mufs_file *file;
char *path, *line;
if (!(path = malloc(strlen(s) + 2)))
return;
strcpy(path, s);
if (!(line = strchr(path, ' '))) {
fprintf(stderr, "usage: append <path> <line>\n");
goto err_free_path;
}
*line++ = '\0';
if (!(file = mufs_open(&mufs, path, MUFS_WRITE | MUFS_APPEND))) {
fprintf(stderr, "error: unable to open the file\n");
goto err_free_path;
}
strcpy(line + strlen(line), "\n");
mufs_write(file, line, strlen(line));
mufs_close(file);
err_free_path:
free(path);
}
static void do_mufs_mv(const char *s)
{
char *old, *new;
if (!(old = strdup(s)))
return;
if (!(new = strchr(old, ' '))) {
fprintf(stderr, "usage: mv <old> <new>\n");
goto err_free_old;
}
*new++ = '\0';
if (mufs_rename(&mufs, old, new) < 0) {
fprintf(stderr, "error: unable to move the file\n");
goto err_free_old;
}
err_free_old:
free(old);
}
static void do_mufs_rm(const char *s)
{
if (mufs_unlink(&mufs, s) < 0) {
fprintf(stderr, "error: unable to remove the file\n");
return;
}
}
static void do_mufs_ls(const char *s)
{
struct mufs_dirent ent;

Loading…
Cancel
Save