From b7716cf7b11fc2dab69b2ba3f8e545903dc48096 Mon Sep 17 00:00:00 2001 From: Wend4r Date: Mon, 12 Oct 2020 22:10:00 +0300 Subject: [PATCH] Push 1.0 --- .gitignore | 1 + README.md | 11 ++++++ build.sh | 7 ++++ main.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ vc140.pdb | Bin 0 -> 36864 bytes 5 files changed, 124 insertions(+) create mode 100644 .gitignore create mode 100644 build.sh create mode 100644 main.c create mode 100644 vc140.pdb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/README.md b/README.md index 56c8bd4..3f17220 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ # bzip2_exec C program for translating each file in a directory to bz2. + +Requirements: +------------ +For build +``` +sudo apt install clang +``` +For working +``` +sudo apt install bzip2 +``` \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..cbf1981 --- /dev/null +++ b/build.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +if ! [ -e ./build/ ]; then + mkdir ./build/; +fi + +clang main.c -o ./build/bz2_exec && echo "Build successful"; diff --git a/main.c b/main.c new file mode 100644 index 0000000..923c312 --- /dev/null +++ b/main.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include + +static const char g_sProgramName[] = "bzip2_exec", + g_sProgramDescription[] = "converts each file to bz2 in directories", + g_sProgramVersion[] = "1.0", + g_sProgramBuildDate[] = __DATE__; + +int PrintHelpInformation(const char *sExecFile) +{ + return printf("%s, a %s. Version %s, %s\n\n\tusage: %s [bzip2 args, by default -9] [directory to work, by default current directory]\n\n", g_sProgramName, g_sProgramDescription, g_sProgramVersion, g_sProgramBuildDate, sExecFile); +} + +int ReadDir(const char *sDirPath, const char *sIgnore, const char *sCompression) +{ + DIR *pDir = opendir(sDirPath); + + if(pDir) + { + struct dirent *pEnt; + + while((pEnt = readdir(pDir))) + { + char iElementType = pEnt->d_type; + + const char *sElementName = pEnt->d_name; + + char sNewDir[1024]; + + int iLength = strlen(sDirPath); + + if(iLength) + { + sprintf(sNewDir, sDirPath[iLength - 1] == '/' ? "%s%s" : "%s/%s", sDirPath, sElementName); + } + else + { + sprintf(sNewDir, "./%s", sElementName); + } + + if(iElementType & (1 << 2)) // Is dir. + { + if(strcmp(sElementName, ".") && strcmp(sElementName, "..")) + { + ReadDir(sNewDir, "", sCompression); + } + } + else if(iElementType & (1 << 3) && strcmp(sElementName, sIgnore)) // Is file. + { + /* I was too lazy to do it at a low level BZip2 API :P */ + + char sSysCommand[256]; + + sprintf(sSysCommand, "/bin/bzip2 %s \"%s\"\n", sCompression, sNewDir); + + if(!system(sSysCommand)) + { + printf("%s -> %s.bz2\n", sElementName, sElementName); + } + } + } + + closedir(pDir); + } + else + { + /* Could not open directory. */ + + char sErrorMessage[256]; + + sprintf(sErrorMessage, "Failed the open \"%s\"\n", sDirPath); + perror(sErrorMessage); + + return 1; + } + + return 0; +} + +int main(int iArgs, const char **psArgs) +{ + bool bIsArgsLessThanTwo = iArgs > 1; + + int iResult; // aka EAX + + if(bIsArgsLessThanTwo && (!strcmp(psArgs[1], "-h") || !strcmp(psArgs[1], "-V") || !strcmp(psArgs[1], "--help") || !strcmp(psArgs[1], "--version"))) + { + iResult = PrintHelpInformation(psArgs[0]); + } + else + { + const char *sIgnore = strrchr(psArgs[0], '/'); + + sIgnore = sIgnore ? &sIgnore[1] : psArgs[0]; + + printf("Ignore \"%s\"\n", sIgnore); + + iResult = ReadDir(iArgs > 2 && psArgs[2] ? psArgs[2] : "./", sIgnore, bIsArgsLessThanTwo && psArgs[1] ? psArgs[1] : "-9"); + } + + return iResult; +} \ No newline at end of file diff --git a/vc140.pdb b/vc140.pdb new file mode 100644 index 0000000000000000000000000000000000000000..555ec2c4eecebb74fc3373aed3f4e02f61bbded0 GIT binary patch literal 36864 zcmeI)u}Z^07y#fzZAC?J6sPQk*dnMyw~CV;qz~XA2u=m{5d>#Np}T{xAkI!cgM*H~ zgz-)hLMe3h>i5H+Kglikf7>OO^iPk^hUdeRi=wyLYqg60V87TY+wIlmjlDq@VlB(& zLRkt~gp9HHQyd8pAV7cs0RjXF5FkK+0D(jV-YP+W009C72oNAZfB*pk1PIK!!1}B= zs6PS(2oNAZfB*pk1PBlyK%iQA>Eyd{uR_@8|DB;Uv#MGMwW{XBQ4k