mirror of
https://github.com/bigsketti/cshell.git
synced 2025-06-06 12:54:46 +00:00
added support for create/remove files and directories.
This commit is contained in:
parent
43ed358e3a
commit
f4553629bd
47
handleJSON.c
Normal file
47
handleJSON.c
Normal file
@ -0,0 +1,47 @@
|
||||
// This file exists because I hate myself too much to use cJSON
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
enum TokenType {
|
||||
INTEGER,
|
||||
CHAR,
|
||||
TRUE,
|
||||
FALSE,
|
||||
IDENTIFIER,
|
||||
VALUE,
|
||||
COLON,
|
||||
R_CURLY_BRACE,
|
||||
L_CURLY_BRACE,
|
||||
R_BRACKET,
|
||||
L_BRACKET,
|
||||
DOUBLE_QUOTES,
|
||||
COMMA,
|
||||
END_OF_FILE,
|
||||
};
|
||||
|
||||
struct Token {
|
||||
enum TokenType type;
|
||||
char value[100];
|
||||
};
|
||||
|
||||
void advancePosition(int *pos, char *currentChar, char *input[]) {
|
||||
if(pos < sizeof(input)) {
|
||||
currentChar = input[*pos];
|
||||
printf("Advancing to %s at position %s", currentChar, *pos);
|
||||
} else {
|
||||
currentChar = "\0";
|
||||
printf("End of Input");
|
||||
}
|
||||
}
|
||||
|
||||
void skipWhiteSpace(int *pos, char currentChar) {
|
||||
if (currentChar != "\0" && is_space(currentChar) == true) {
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
|
||||
void tokenizeJSON() {
|
||||
|
||||
}
|
56
main.c
56
main.c
@ -3,10 +3,10 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define INPUT_SIZE 1024
|
||||
|
||||
const double VERSION = 0.1;
|
||||
#define VERSION 0.1f
|
||||
|
||||
void execute(char **args) {
|
||||
pid_t pid = fork();
|
||||
@ -67,31 +67,67 @@ int main() {
|
||||
|
||||
input[strcspn(input, "\n")] = '\0';
|
||||
|
||||
parse_input(input, args);
|
||||
|
||||
// built in command checks
|
||||
|
||||
// TODO: add commands to handle creating and deleting users
|
||||
// also modify permissions for admin users and regular users
|
||||
// This will have to come after i finish the JSON parser
|
||||
|
||||
// TODO: add mkdir, rmdir, mkfile, and rmfile commands built in
|
||||
|
||||
if (strcmp("exit", input) == 0) {
|
||||
exit(0);
|
||||
|
||||
} else if (strcmp("help", input) == 0) {
|
||||
printf("Welcome to cShell, my simple terminal written in C\nBuilt-in commands:\n\texit\n\thelp\n\tcd\n\tversion\n");
|
||||
printf("Welcome to cShell, my simple terminal written in C\nBuilt-in commands:\n\texit\n\thelp\n\tcd [dir name]\n\tversion\n\tmkdir [dir name]\n\trmdir [dir name]\n\tmkfile [file name]\n\trmfile [file name]\n\n");
|
||||
continue;
|
||||
|
||||
} else if (strcmp("version", input) == 0) {
|
||||
printf("cShell version: %f\n", VERSION);
|
||||
continue;
|
||||
}
|
||||
|
||||
parse_input(input, args);
|
||||
|
||||
// handle 'cd' command
|
||||
if (strcmp("cd", args[0]) == 0) {
|
||||
} else if (strcmp("cd", args[0]) == 0) {
|
||||
if (args[1] == NULL) {
|
||||
fprintf(stderr, "Missing argument for command \"cd\"\n");
|
||||
} else {
|
||||
if (chdir(args[1]) != 0) {
|
||||
perror("chdir failed");
|
||||
perror("chdir failed\n");
|
||||
} else {
|
||||
free(cwd);
|
||||
cwd = getCWD();
|
||||
}
|
||||
char *cwd = getCWD();
|
||||
}
|
||||
continue;
|
||||
|
||||
} else if (strcmp("mkfile", args[0]) == 0) {
|
||||
FILE *file = fopen(args[1], "w");
|
||||
if (file == NULL) {
|
||||
perror("Error making file");
|
||||
} else { printf("success\n"); }
|
||||
continue;
|
||||
|
||||
} else if (strcmp("mkdir", args[0]) == 0) {
|
||||
if (mkdir(args[1], 0777) != 0) {
|
||||
perror("Error making directory");
|
||||
} else { printf("success\n"); }
|
||||
continue;
|
||||
|
||||
} else if (strcmp("rmfile", args[0]) == 0) {
|
||||
if (remove(args[1]) != 0) {
|
||||
perror("Error removing file");
|
||||
} else { printf("success\n"); }
|
||||
continue;
|
||||
|
||||
} else if (strcmp("rmdir", args[0]) == 0) {
|
||||
if (rmdir(args[1]) != 0) {
|
||||
perror("Error removing directory");
|
||||
} else { printf("success\n"); }
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// execute the external command
|
||||
execute(args);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user