restruct & mem fixes
This commit is contained in:
105
cmd/dfs.c
105
cmd/dfs.c
@@ -11,20 +11,13 @@
|
||||
#include <readline/history.h>
|
||||
|
||||
#include "dfs.h"
|
||||
#include "util.h"
|
||||
|
||||
#define clear() printf("\033[H\033[J")
|
||||
|
||||
static char *prompt = NULL;
|
||||
char *prompt = NULL;
|
||||
static size_t commandRun = 0;
|
||||
|
||||
union pipe {
|
||||
int fileDesc[2];
|
||||
struct {
|
||||
int read;
|
||||
int write;
|
||||
};
|
||||
};
|
||||
|
||||
static char *builtinFunctions[] = {
|
||||
"exit",
|
||||
"author",
|
||||
@@ -45,7 +38,6 @@ int builtinExit(char **args) {
|
||||
#else
|
||||
clear_history();
|
||||
#endif
|
||||
free(args);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -56,7 +48,6 @@ int builtinAuthor(char **args) {
|
||||
"Ask for the source code!\n"
|
||||
"\n";
|
||||
printf("%s", message);
|
||||
free(args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -102,12 +93,10 @@ int builtinCD(char **dir) {
|
||||
}
|
||||
free(changeDir);
|
||||
changeDir = NULL;
|
||||
free(dir);
|
||||
return status;
|
||||
}
|
||||
|
||||
int builtinNMN(char **args) {
|
||||
free(args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -142,46 +131,55 @@ int runCommand(char *command) {
|
||||
return status;
|
||||
}
|
||||
|
||||
char **splitCMD(const char *line) {
|
||||
char **splitCMD(const char *line, size_t *length) {
|
||||
size_t size = 8;
|
||||
size_t pos = 0;
|
||||
char **args = malloc(size * sizeof(char*));
|
||||
char *arg = NULL;
|
||||
|
||||
char *lineTest = NULL;
|
||||
lineTest = strdup(line);
|
||||
|
||||
if(line == NULL) {
|
||||
*args=NULL;
|
||||
return args;
|
||||
}
|
||||
|
||||
char *lineTest = strdup(line);
|
||||
|
||||
arg = strtok(lineTest, " \t\r\n\a");
|
||||
while (arg != NULL) {
|
||||
args[pos] = arg;
|
||||
if (pos >= size) {
|
||||
size *= 2;
|
||||
args = realloc(args, size * sizeof(char*));
|
||||
}
|
||||
|
||||
*length += 1;
|
||||
args[pos] = strdup(arg);
|
||||
pos++;
|
||||
|
||||
if (pos >= size) {
|
||||
size += 1;
|
||||
args = realloc(args, size * sizeof(char*));
|
||||
|
||||
}
|
||||
arg = strtok(NULL, " \t\r\n\a");
|
||||
}
|
||||
|
||||
free(arg);
|
||||
free(lineTest);
|
||||
args[pos] = NULL;
|
||||
return args;
|
||||
}
|
||||
|
||||
int execCommand(char *command) {
|
||||
char **commandARGS = splitCMD(command);
|
||||
size_t commandLen = 0;
|
||||
char **commandARGS = splitCMD(command, &commandLen);
|
||||
for (size_t i = 0; i < (sizeof(builtinFunctions) / sizeof(char *)); i++) {
|
||||
if (strcmp(commandARGS[0], builtinFunctions[i]) == 0) {
|
||||
return (*builtinFunc[i])(commandARGS);
|
||||
(*builtinFunc[i])(commandARGS);
|
||||
for (size_t i = 0; i < commandLen; i++) {
|
||||
free(commandARGS[i]);
|
||||
}
|
||||
free(commandARGS);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < commandLen; i++) {
|
||||
free(commandARGS[i]);
|
||||
}
|
||||
|
||||
free(commandARGS);
|
||||
return runCommand(command);
|
||||
}
|
||||
@@ -215,59 +213,6 @@ void initShell() {
|
||||
return;
|
||||
}
|
||||
|
||||
char *getPrompt() {
|
||||
pid_t pid;
|
||||
union pipe input, output;
|
||||
FILE *outputFileD;
|
||||
size_t len = 0;
|
||||
int status = 0;
|
||||
|
||||
static size_t promptCap = 0;
|
||||
|
||||
pipe(input.fileDesc);
|
||||
pipe(output.fileDesc);
|
||||
|
||||
pid = fork();
|
||||
if (pid == 0) { // Child process
|
||||
close(input.write);
|
||||
close(output.read);
|
||||
|
||||
dup2(input.read, STDIN_FILENO);
|
||||
dup2(output.write, STDOUT_FILENO);
|
||||
close(STDERR_FILENO); // Do not print errors to the screen
|
||||
|
||||
execlp("bash", "bash", "-i", "-c", "\
|
||||
ver=$(bash --version | head -n 1 | awk \'{print $4}\' | grep -o \".\\..\");\
|
||||
check_ver=$(echo -e \"${ver}\n4.3\" | sort -V | head -n 1);\
|
||||
if [[ \"${ver}\" == \"${check_ver}\" ]]; then\
|
||||
echo \"[${USER}@${HOSTNAME} ${PWD##*/}]$ \";\
|
||||
else\
|
||||
echo \"${PS1@P}\";\
|
||||
fi\
|
||||
", NULL);
|
||||
} else if (pid < 0) { // Error forking
|
||||
printf("Error forking\n");
|
||||
} else { // Parent process
|
||||
close(input.read);
|
||||
close(output.write);
|
||||
|
||||
outputFileD = fdopen(output.read, "r");
|
||||
|
||||
len = getline(&prompt, &promptCap, outputFileD);
|
||||
|
||||
if (prompt[len - 1] == '\n') {
|
||||
prompt[len - 1] = '\0';
|
||||
}
|
||||
|
||||
fclose(outputFileD);
|
||||
do {
|
||||
waitpid(pid, &status, WUNTRACED);
|
||||
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||
}
|
||||
|
||||
return prompt;
|
||||
}
|
||||
|
||||
int randomNum(int limit) {
|
||||
int num = 0;
|
||||
if (limit != 0) {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
char *getPrompt();
|
||||
|
||||
68
src/util.c
68
src/util.c
@@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static char *prompt = NULL;
|
||||
|
||||
union pipe {
|
||||
int fileDesc[2];
|
||||
struct {
|
||||
int read;
|
||||
int write;
|
||||
};
|
||||
};
|
||||
|
||||
char *getPrompt() {
|
||||
pid_t pid;
|
||||
union pipe input, output;
|
||||
FILE *outputFileD;
|
||||
size_t len = 0;
|
||||
int status = 0;
|
||||
|
||||
static size_t promptCap = 0;
|
||||
|
||||
pipe(input.fileDesc);
|
||||
pipe(output.fileDesc);
|
||||
|
||||
pid = fork();
|
||||
if (pid == 0) { // Child process
|
||||
close(input.write);
|
||||
close(output.read);
|
||||
|
||||
dup2(input.read, STDIN_FILENO);
|
||||
dup2(output.write, STDOUT_FILENO);
|
||||
close(STDERR_FILENO); // Do not print errors to the screen
|
||||
|
||||
execlp("bash", "bash", "-i", "-c", "\
|
||||
ver=$(bash --version | head -n 1 | awk \'{print $4}\' | grep -o \".\\..\");\
|
||||
check_ver=$(echo -e \"${ver}\n4.3\" | sort -V | head -n 1);\
|
||||
if [[ \"${ver}\" == \"${check_ver}\" ]]; then\
|
||||
echo \"[${USER}@${HOSTNAME} ${PWD##*/}]$ \";\
|
||||
else\
|
||||
echo \"${PS1@P}\";\
|
||||
fi\
|
||||
", NULL);
|
||||
} else if (pid < 0) { // Error forking
|
||||
printf("Error forking\n");
|
||||
} else { // Parent process
|
||||
close(input.read);
|
||||
close(output.write);
|
||||
|
||||
outputFileD = fdopen(output.read, "r");
|
||||
|
||||
len = getline(&prompt, &promptCap, outputFileD);
|
||||
|
||||
if (prompt[len - 1] == '\n') {
|
||||
prompt[len - 1] = '\0';
|
||||
}
|
||||
|
||||
fclose(outputFileD);
|
||||
do {
|
||||
waitpid(pid, &status, WUNTRACED);
|
||||
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||
}
|
||||
|
||||
return prompt;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user