implement cat
This commit is contained in:
parent
db31cdd70a
commit
99a46acb05
@ -87,6 +87,63 @@ static errval_t shelly_cmd_cat(char * line) {
|
|||||||
err = shelly_write_str("(@)<_____>__(_____)____/\n\r");
|
err = shelly_write_str("(@)<_____>__(_____)____/\n\r");
|
||||||
if (err_is_fail(err)) return err;
|
if (err_is_fail(err)) return err;
|
||||||
|
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
|
||||||
|
FILE *f = fopen(line, "r");
|
||||||
|
if (f == NULL) {
|
||||||
|
return FS_ERR_OPEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* obtain the file size */
|
||||||
|
res = fseek(f , 0 , SEEK_END);
|
||||||
|
if (res) {
|
||||||
|
return FS_ERR_INVALID_FH;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t filesize = ftell(f);
|
||||||
|
rewind(f);
|
||||||
|
|
||||||
|
char str_buf[filesize + 10];
|
||||||
|
snprintf(str_buf, filesize + 10, "File size is %zu\n\r", filesize);
|
||||||
|
err = shelly_write_str(str_buf);
|
||||||
|
if (err_is_fail(err)) return err;
|
||||||
|
|
||||||
|
char *file_content = calloc(filesize + 2, sizeof(char));
|
||||||
|
if (file_content == NULL) {
|
||||||
|
return LIB_ERR_MALLOC_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t read = fread(file_content, 1, filesize, f);
|
||||||
|
|
||||||
|
|
||||||
|
snprintf(str_buf, filesize + 10, "%s\n\r", file_content);
|
||||||
|
err = shelly_write_str(str_buf);
|
||||||
|
if (err_is_fail(err)) return err;
|
||||||
|
|
||||||
|
if (read != filesize) {
|
||||||
|
return FS_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
rewind(f);
|
||||||
|
|
||||||
|
size_t nchars = 0;
|
||||||
|
int c;
|
||||||
|
do {
|
||||||
|
c = fgetc(f);
|
||||||
|
nchars++;
|
||||||
|
} while (c != EOF);
|
||||||
|
|
||||||
|
if (nchars < filesize) {
|
||||||
|
return FS_ERR_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(file_content);
|
||||||
|
res = fclose(f);
|
||||||
|
if (res) {
|
||||||
|
return FS_ERR_CLOSE;
|
||||||
|
}
|
||||||
|
|
||||||
return SYS_ERR_OK;
|
return SYS_ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user