Skip to content

Commit

Permalink
Push 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Wend4r committed Oct 12, 2020
1 parent f734384 commit b7716cf
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
7 changes: 7 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

if ! [ -e ./build/ ]; then
mkdir ./build/;
fi

clang main.c -o ./build/bz2_exec && echo "Build successful";
105 changes: 105 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <dirent.h>
#include <string.h>

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;
}
Binary file added vc140.pdb
Binary file not shown.

0 comments on commit b7716cf

Please sign in to comment.