From: John Hodge Date: Mon, 20 Aug 2012 04:26:16 +0000 (+0800) Subject: DiskTool - Added 'cat' command X-Git-Tag: rel0.15~706^2~36 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=054944599253e2ffff5c7153f2cee1bb275efcfd;p=tpg%2Facess2.git DiskTool - Added 'cat' command --- diff --git a/Tools/DiskTool/src/actions.c b/Tools/DiskTool/src/actions.c index e63ae612..665a5957 100644 --- a/Tools/DiskTool/src/actions.c +++ b/Tools/DiskTool/src/actions.c @@ -151,6 +151,29 @@ int DiskTool_ListDirectory(const char *Directory) return 0; } +int DiskTool_Cat(const char *File) +{ + int src = DiskTool_int_TranslateOpen(File, VFS_OPENFLAG_READ); + if( src == -1 ) { + Log_Error("DiskTool", "Unable to open %s for reading", File); + return -1; + } + + char buf[1024]; + size_t len, total = 0; + while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) { + _fwrite_stdout(len, buf); + total += len; + } + _fwrite_stdout(len, buf); + total += len; + + Log_Notice("DiskTool", "%i bytes from %s", total, File); + + VFS_Close(src); + return 0; +} + int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest) { return VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512; diff --git a/Tools/DiskTool/src/include/disktool_common.h b/Tools/DiskTool/src/include/disktool_common.h index bd30b01d..38703c21 100644 --- a/Tools/DiskTool/src/include/disktool_common.h +++ b/Tools/DiskTool/src/include/disktool_common.h @@ -14,8 +14,11 @@ extern int DiskTool_RegisterLVM(const char *Identifier, const char *Path); extern int DiskTool_MountImage(const char *Identifier, const char *Path); extern int DiskTool_Copy(const char *Source, const char *Destination); extern int DiskTool_ListDirectory(const char *Directory); +extern int DiskTool_Cat(const char *File); extern size_t DiskTool_int_TranslatePath(char *Buffer, const char *Path); +extern size_t _fwrite_stdout(size_t bytes, const void *data); + #endif diff --git a/Tools/DiskTool/src/main.c b/Tools/DiskTool/src/main.c index 687bb3bc..2de26a6d 100644 --- a/Tools/DiskTool/src/main.c +++ b/Tools/DiskTool/src/main.c @@ -32,7 +32,7 @@ int main(int argc, char *argv[]) if( strcmp("mountlvm", argv[i]) == 0 || strcmp("lvm", argv[i]) == 0 ) { if( argc - i < 3 ) { - fprintf(stderr, "mountlvm takes 2 arguments (iamge and ident)\n"); + fprintf(stderr, "lvm takes 2 arguments (iamge and ident)\n"); exit(-1); } @@ -47,7 +47,7 @@ int main(int argc, char *argv[]) if( strcmp("ls", argv[i]) == 0 ) { if( argc - i < 2 ) { - fprintf(stderr, "ls 1 argument (path)\n"); + fprintf(stderr, "ls takes 1 argument (path)\n"); break; } @@ -68,6 +68,19 @@ int main(int argc, char *argv[]) i += 2; continue ; } + + if( strcmp("cat", argv[i]) == 0 ) { + + if( argc - 1 < 2 ) { + fprintf(stderr, "cat takes 1 argument (path)\n"); + break; + } + + DiskTool_Cat(argv[i+1]); + + i += 1; + continue; + } fprintf(stderr, "Unknown command '%s'\n", argv[i]); } @@ -121,3 +134,7 @@ uint64_t DivMod64U(uint64_t value, uint64_t divisor, uint64_t *remainder) return value / divisor; } +size_t _fwrite_stdout(size_t bytes, const void *data) +{ + return fwrite(data, bytes, 1, stdout); +}