From 1509a6fe36875e4e2df58e311fd129895cbb042b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Fri, 19 Jun 2015 22:38:34 +0000 Subject: [PATCH] [ROSAPPS]: Merge the two existing CATs into one and improve it (see r66942, r66989 and r66990). svn path=/trunk/; revision=68198 --- rosapps/applications/cmdutils/CMakeLists.txt | 1 + .../utils => cmdutils}/cat/CMakeLists.txt | 2 +- rosapps/applications/cmdutils/cat/cat.c | 131 ++++++++++++++++++ rosapps/applications/sysutils/CMakeLists.txt | 1 - .../applications/sysutils/tcat/CMakeLists.txt | 5 - rosapps/applications/sysutils/tcat/cat.c | 52 ------- .../sysutils/utils/CMakeLists.txt | 1 - rosapps/applications/sysutils/utils/cat/cat.c | 26 ---- 8 files changed, 133 insertions(+), 86 deletions(-) rename rosapps/applications/{sysutils/utils => cmdutils}/cat/CMakeLists.txt (69%) create mode 100644 rosapps/applications/cmdutils/cat/cat.c delete mode 100644 rosapps/applications/sysutils/tcat/CMakeLists.txt delete mode 100644 rosapps/applications/sysutils/tcat/cat.c delete mode 100644 rosapps/applications/sysutils/utils/cat/cat.c diff --git a/rosapps/applications/cmdutils/CMakeLists.txt b/rosapps/applications/cmdutils/CMakeLists.txt index 31d5ec71206..b30ee491457 100644 --- a/rosapps/applications/cmdutils/CMakeLists.txt +++ b/rosapps/applications/cmdutils/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(appwiz) +add_subdirectory(cat) add_subdirectory(tee) add_subdirectory(touch) add_subdirectory(uptime) diff --git a/rosapps/applications/sysutils/utils/cat/CMakeLists.txt b/rosapps/applications/cmdutils/cat/CMakeLists.txt similarity index 69% rename from rosapps/applications/sysutils/utils/cat/CMakeLists.txt rename to rosapps/applications/cmdutils/cat/CMakeLists.txt index 26d22fdfb48..f53eec008af 100644 --- a/rosapps/applications/sysutils/utils/cat/CMakeLists.txt +++ b/rosapps/applications/cmdutils/cat/CMakeLists.txt @@ -1,5 +1,5 @@ add_executable(cat cat.c) set_module_type(cat win32cui) -add_importlibs(cat ntdll user32 msvcrt kernel32) +add_importlibs(cat msvcrt kernel32) add_cd_file(TARGET cat DESTINATION reactos/bin FOR all) diff --git a/rosapps/applications/cmdutils/cat/cat.c b/rosapps/applications/cmdutils/cat/cat.c new file mode 100644 index 00000000000..75d66c2d79a --- /dev/null +++ b/rosapps/applications/cmdutils/cat/cat.c @@ -0,0 +1,131 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS conCATenation tool + * FILE: cmdutils/cat/cat.c + * PURPOSE: Concatenates STDIN or an arbitrary number of files to STDOUT + * PROGRAMMERS: David Welch + * Semyon Novikov (tappak) + * Hermès Bélusca - Maïto + */ + +#include + +#ifdef _WIN32 +#include // Required for _stricmp() +#include // Required for _setmode flags +#include // Required for _setmode() +#else +#include // Required for strcasecmp() +#define O_TEXT 0x4000 +#define O_BINARY 0x8000 +#define _setmode(fd, mode) // This function is useless in *nix world +#define _stricmp strcasecmp +#endif + +#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0])) + +void help(void) +{ + fprintf(stdout, + "\n" + "ReactOS File Concatenation Tool\n" + "\n" + "Usage: cat [options] [file [...]]\n" + "options - Currently ignored\n"); +} + +int main(int argc, char* argv[]) +{ + int i; + FILE* in; + unsigned char buff[512]; + size_t cnt, readcnt; + + if (argc >= 2) + { + if (_stricmp(argv[1], "-h" ) == 0 || + _stricmp(argv[1], "--help") == 0 || + _stricmp(argv[1], "/?" ) == 0 || + _stricmp(argv[1], "/help" ) == 0) + { + help(); + return 0; + } + } + + /* Set STDOUT to binary */ + _setmode(_fileno(stdout), O_BINARY); + + /* Special case where we run 'cat' without any argument: we use STDIN */ + if (argc <= 1) + { + unsigned int ch; + + /* Set STDIN to binary */ + _setmode(_fileno(stdin), O_BINARY); + +#if 0 // Version using feof() + ch = fgetc(stdin); + while (!feof(stdin)) + { + putchar(ch); + ch = fgetc(stdin); + } +#else + while ((ch = fgetc(stdin)) != EOF) + { + putchar(ch); + } +#endif + + return 0; + } + + /* We have files: read them and output them to STDOUT */ + for (i = 1; i < argc; i++) + { + /* Open the file in binary read mode */ + in = fopen(argv[i], "rb"); + if (in == NULL) + { + fprintf(stderr, "Failed to open file '%s'\n", argv[i]); + return -1; + } + + /* Dump the file to STDOUT */ + cnt = 0; readcnt = 0; + while (readcnt == cnt) + { + /* Read data from the input file */ + cnt = ARRAYSIZE(buff); + readcnt = fread(&buff, sizeof(buff[0]), cnt, in); + if (readcnt != cnt) + { + /* + * The real number of read bytes differs from the number of bytes + * we wanted to read, so either a reading error occurred, or EOF + * was reached while reading. Bail out if it is a reading error. + */ + if (!feof(in)) + { + fprintf(stderr, "Error while reading file '%s'\n", argv[i]); + fclose(in); + return -1; + } + } + + /* Nothing to be read anymore, so we can gracefully break */ + if (readcnt == 0) break; + + /* Write data to STDOUT */ + fwrite(&buff, sizeof(buff[0]), readcnt, stdout); + } + + /* Finally close the file */ + fclose(in); + } + + return 0; +} + +/* EOF */ diff --git a/rosapps/applications/sysutils/CMakeLists.txt b/rosapps/applications/sysutils/CMakeLists.txt index 4b3b2eb0801..c694a1b1930 100644 --- a/rosapps/applications/sysutils/CMakeLists.txt +++ b/rosapps/applications/sysutils/CMakeLists.txt @@ -11,6 +11,5 @@ add_subdirectory(regexpl) add_subdirectory(rosddt) add_subdirectory(screenshot) add_subdirectory(systeminfo) -add_subdirectory(tcat) add_subdirectory(tlist) add_subdirectory(utils) diff --git a/rosapps/applications/sysutils/tcat/CMakeLists.txt b/rosapps/applications/sysutils/tcat/CMakeLists.txt deleted file mode 100644 index 573baf695dc..00000000000 --- a/rosapps/applications/sysutils/tcat/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ - -add_executable(tcat cat.c) -set_module_type(tcat win32cui) -add_importlibs(tcat user32 msvcrt kernel32 ntdll) -add_cd_file(TARGET tcat DESTINATION reactos/system32 FOR all) diff --git a/rosapps/applications/sysutils/tcat/cat.c b/rosapps/applications/sysutils/tcat/cat.c deleted file mode 100644 index 3ad7130ad89..00000000000 --- a/rosapps/applications/sysutils/tcat/cat.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * FILE : cat.c - * NATIVE NAME: tcat "tappak's cat" :) - * AUTHOR : Semyon Novikov (tappak) - * PROJECT : ReactOS Operating System - * DESCRIPTION: file concatenation tool - * DATE : 2004-01-21 - * LICENSE : GPL - */ - -#include -#include -#define F_O_ERR "can not open file" - -void help(void) -{ - puts("File concatenation tool"); - puts("Usage: cat [file]"); -} - -int main(int argc, char *argv[]) -{ - FILE *srcf; - char *keys[]={"--help","/help"}; - int i=0,ret=0; - switch(argc) - { - case 1:puts("Usage: cat [file]");break; - case 2: - if ((!strcmp(argv[1],keys[0]))||(!strcmp(argv[1],keys[1]))) - help(); - else - { - if((srcf=fopen(argv[1],"r"))!=NULL) - { - while(i!=EOF) - { i=fgetc(srcf); - putchar(i); - } - fclose(srcf); - } - else - { - printf("%s %s %s\n",argv[0],F_O_ERR,argv[1]); - ret=-1; - } - } - break; - } - return ret; -} - diff --git a/rosapps/applications/sysutils/utils/CMakeLists.txt b/rosapps/applications/sysutils/utils/CMakeLists.txt index 928d21ec511..e7dcc393722 100644 --- a/rosapps/applications/sysutils/utils/CMakeLists.txt +++ b/rosapps/applications/sysutils/utils/CMakeLists.txt @@ -1,5 +1,4 @@ add_subdirectory(binpatch) -add_subdirectory(cat) add_subdirectory(driver) add_subdirectory(infinst) add_subdirectory(nts2w32err) diff --git a/rosapps/applications/sysutils/utils/cat/cat.c b/rosapps/applications/sysutils/utils/cat/cat.c deleted file mode 100644 index 97fbf38387b..00000000000 --- a/rosapps/applications/sysutils/utils/cat/cat.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include - -int main(int argc, char* argv[]) -{ - int i; - FILE* in; - char ch; - - for (i=1; i