From c1324010e5f70ddf2f8fca2ac8f265ac2daf150b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Sun, 29 Mar 2015 00:13:25 +0000 Subject: [PATCH] [ROSAPPS][HOST-TOOLS]: - Take the best of the two CAT rosapps and turn them into a host-tool. Will be used later on in the build process. - Few comment cleaning in bin2c tool. svn path=/trunk/; revision=66942 --- reactos/CMakeLists.txt | 4 +- reactos/tools/CMakeLists.txt | 1 + reactos/tools/bin2c.c | 26 ++-- reactos/tools/cat.c | 120 ++++++++++++++++++ rosapps/applications/sysutils/CMakeLists.txt | 1 - rosapps/applications/sysutils/tcat/cat.c | 52 -------- .../applications/sysutils/tcat/tcat.rbuild | 6 - .../sysutils/utils/CMakeLists.txt | 1 - .../sysutils/utils/cat/CMakeLists.txt | 5 - rosapps/applications/sysutils/utils/cat/cat.c | 26 ---- 10 files changed, 132 insertions(+), 110 deletions(-) create mode 100644 reactos/tools/cat.c delete mode 100644 rosapps/applications/sysutils/tcat/cat.c delete mode 100644 rosapps/applications/sysutils/tcat/tcat.rbuild delete mode 100644 rosapps/applications/sysutils/utils/cat/CMakeLists.txt delete mode 100644 rosapps/applications/sysutils/utils/cat/cat.c diff --git a/reactos/CMakeLists.txt b/reactos/CMakeLists.txt index 75e03aab284..956b22ffa72 100644 --- a/reactos/CMakeLists.txt +++ b/reactos/CMakeLists.txt @@ -85,9 +85,9 @@ if(NOT CMAKE_CROSSCOMPILING) add_subdirectory(lib) if(NOT MSVC) - export(TARGETS bin2c widl gendib cabman cdmake mkhive obj2bin spec2def geninc rsym mkshelllink utf16le FILE ${CMAKE_BINARY_DIR}/ImportExecutables.cmake NAMESPACE native- ) + export(TARGETS bin2c cat widl gendib cabman cdmake mkhive obj2bin spec2def geninc rsym mkshelllink utf16le FILE ${CMAKE_BINARY_DIR}/ImportExecutables.cmake NAMESPACE native- ) else() - export(TARGETS bin2c widl gendib cabman cdmake mkhive obj2bin spec2def geninc mkshelllink utf16le FILE ${CMAKE_BINARY_DIR}/ImportExecutables.cmake NAMESPACE native- ) + export(TARGETS bin2c cat widl gendib cabman cdmake mkhive obj2bin spec2def geninc mkshelllink utf16le FILE ${CMAKE_BINARY_DIR}/ImportExecutables.cmake NAMESPACE native- ) endif() else() diff --git a/reactos/tools/CMakeLists.txt b/reactos/tools/CMakeLists.txt index 7c2981610ce..245e893ec89 100644 --- a/reactos/tools/CMakeLists.txt +++ b/reactos/tools/CMakeLists.txt @@ -6,6 +6,7 @@ if(MSVC) endif() add_executable(bin2c bin2c.c) +add_executable(cat cat.c) add_executable(gendib gendib/gendib.c) add_executable(geninc geninc/geninc.c) add_executable(mkshelllink mkshelllink/mkshelllink.c) diff --git a/reactos/tools/bin2c.c b/reactos/tools/bin2c.c index 61b78ef5271..3fab46f0064 100644 --- a/reactos/tools/bin2c.c +++ b/reactos/tools/bin2c.c @@ -16,18 +16,14 @@ int main(int argc, char *argv[]) unsigned char ch; unsigned char cnt; - /* - * Validate the arguments. - */ + /* Validate the arguments */ if (argc < 5) { printf("Usage: bin2c infile.bin outfile.c outfile.h array_name [array_attribute [header_for_attribute]]\n"); return -1; } - /* - * Open the input and the output files. - */ + /* Open the input and output files */ inFile = fopen(argv[1], "rb"); if (!inFile) { @@ -50,9 +46,7 @@ int main(int argc, char *argv[]) return -1; } - /* - * Generate the header file and close it. - */ + /* Generate the header file and close it */ fprintf(outHFile, "/* This file is autogenerated, do not edit. */\n\n"); fprintf(outHFile, "#ifndef CHAR\n" "#define CHAR char\n" @@ -60,21 +54,19 @@ int main(int argc, char *argv[]) fprintf(outHFile, "extern CHAR %s[];\n", argv[4]); fclose(outHFile); - /* - * Generate the source file and close it. - */ + /* Generate the source file and close it */ fprintf(outCFile, "/* This file is autogenerated, do not edit. */\n\n"); if (argc >= 7) { - /* There is a header to be included for defining the array attribute. */ + /* Include needed header for defining the array attribute */ fprintf(outCFile, "#include \"%s\"\n", argv[6]); } fprintf(outCFile, "#include \"%s\"\n\n", argv[3]); - /* Generate the array. */ + /* Generate the data array */ if (argc >= 6) { - /* There is an array attribute. */ + /* Add the array attribute */ fprintf(outCFile, "%s ", argv[5]); } fprintf(outCFile, "CHAR %s[] =\n{", argv[4]); @@ -92,12 +84,12 @@ int main(int argc, char *argv[]) ++cnt; ch = fgetc(inFile); } - /* Put a final NULL terminator. */ + /* Put a final NULL terminator */ fprintf(outCFile, "\n 0x00"); fprintf(outCFile, "\n};\n"); fclose(outCFile); - /* Close the input file. */ + /* Close the input file */ fclose(inFile); return 0; diff --git a/reactos/tools/cat.c b/reactos/tools/cat.c new file mode 100644 index 00000000000..2bedf0a44b0 --- /dev/null +++ b/reactos/tools/cat.c @@ -0,0 +1,120 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS conCATenation tool + * FILE: tools/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 +#include +#include + +#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0])) + +void help(void) +{ + printf("\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) + { + printf("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)) + { + printf("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 f93dfdd66ae..23789988392 100644 --- a/rosapps/applications/sysutils/CMakeLists.txt +++ b/rosapps/applications/sysutils/CMakeLists.txt @@ -12,6 +12,5 @@ add_subdirectory(kill) #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/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/tcat/tcat.rbuild b/rosapps/applications/sysutils/tcat/tcat.rbuild deleted file mode 100644 index f1bd63a26cd..00000000000 --- a/rosapps/applications/sysutils/tcat/tcat.rbuild +++ /dev/null @@ -1,6 +0,0 @@ - - ntdll - user32 - - cat.c - diff --git a/rosapps/applications/sysutils/utils/CMakeLists.txt b/rosapps/applications/sysutils/utils/CMakeLists.txt index d8d6917b97e..0df11975115 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/CMakeLists.txt b/rosapps/applications/sysutils/utils/cat/CMakeLists.txt deleted file mode 100644 index 26d22fdfb48..00000000000 --- a/rosapps/applications/sysutils/utils/cat/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ - -add_executable(cat cat.c) -set_module_type(cat win32cui) -add_importlibs(cat ntdll user32 msvcrt kernel32) -add_cd_file(TARGET cat DESTINATION reactos/bin FOR all) 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