mirror of
https://github.com/bigsketti/cshell.git
synced 2025-06-07 05:14:47 +00:00
Fixed current directory shown as full path
This commit is contained in:
parent
f4553629bd
commit
7b0b62248b
@ -4,7 +4,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
enum TokenType {
|
typedef enum TokenType {
|
||||||
INTEGER,
|
INTEGER,
|
||||||
CHAR,
|
CHAR,
|
||||||
TRUE,
|
TRUE,
|
||||||
@ -19,12 +19,12 @@ enum TokenType {
|
|||||||
DOUBLE_QUOTES,
|
DOUBLE_QUOTES,
|
||||||
COMMA,
|
COMMA,
|
||||||
END_OF_FILE,
|
END_OF_FILE,
|
||||||
};
|
} Token_type;
|
||||||
|
|
||||||
struct Token {
|
typedef struct Token {
|
||||||
enum TokenType type;
|
enum TokenType type;
|
||||||
char value[100];
|
char value[100];
|
||||||
};
|
} Token;
|
||||||
|
|
||||||
void advancePosition(int *pos, char *currentChar, char *input[]) {
|
void advancePosition(int *pos, char *currentChar, char *input[]) {
|
||||||
if(pos < sizeof(input)) {
|
if(pos < sizeof(input)) {
|
||||||
|
30
main.c
30
main.c
@ -8,6 +8,18 @@
|
|||||||
#define INPUT_SIZE 1024
|
#define INPUT_SIZE 1024
|
||||||
#define VERSION 0.1f
|
#define VERSION 0.1f
|
||||||
|
|
||||||
|
char *trimCWD(char *cwd) {
|
||||||
|
char *token = strtok(cwd, "/");
|
||||||
|
char *trimmedCWD = NULL;
|
||||||
|
|
||||||
|
while (token != NULL) {
|
||||||
|
trimmedCWD = token;
|
||||||
|
token = strtok(NULL, "/");
|
||||||
|
}
|
||||||
|
|
||||||
|
return trimmedCWD;
|
||||||
|
}
|
||||||
|
|
||||||
void execute(char **args) {
|
void execute(char **args) {
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
@ -53,10 +65,11 @@ int main() {
|
|||||||
char *args[100];
|
char *args[100];
|
||||||
char user[100] = "defaultusr";
|
char user[100] = "defaultusr";
|
||||||
char *cwd = getCWD();
|
char *cwd = getCWD();
|
||||||
|
char *trimmedCWD = trimCWD(cwd);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (cwd != NULL) {
|
if (cwd != NULL) {
|
||||||
printf("[cShell-%s] %s $> ", user, cwd);
|
printf("[cShell-%s] /%s $> ", user, trimmedCWD);
|
||||||
} else {
|
} else {
|
||||||
printf("[cShell-%s] $> ", user);
|
printf("[cShell-%s] $> ", user);
|
||||||
}
|
}
|
||||||
@ -75,16 +88,16 @@ int main() {
|
|||||||
// also modify permissions for admin users and regular users
|
// also modify permissions for admin users and regular users
|
||||||
// This will have to come after i finish the JSON parser
|
// This will have to come after i finish the JSON parser
|
||||||
|
|
||||||
// TODO: add mkdir, rmdir, mkfile, and rmfile commands built in
|
// TODO: refactor this to a switch case one day im bored
|
||||||
|
// these else ifs are getting ugly and its just gonna get worse
|
||||||
if (strcmp("exit", input) == 0) {
|
if (strcmp("exit", args[0]) == 0) {
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
||||||
} else if (strcmp("help", input) == 0) {
|
} else if (strcmp("help", args[0]) == 0) {
|
||||||
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");
|
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;
|
continue;
|
||||||
|
|
||||||
} else if (strcmp("version", input) == 0) {
|
} else if (strcmp("version", args[0]) == 0) {
|
||||||
printf("cShell version: %f\n", VERSION);
|
printf("cShell version: %f\n", VERSION);
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -97,6 +110,7 @@ int main() {
|
|||||||
} else {
|
} else {
|
||||||
free(cwd);
|
free(cwd);
|
||||||
cwd = getCWD();
|
cwd = getCWD();
|
||||||
|
trimmedCWD = trimCWD(cwd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@ -125,8 +139,10 @@ int main() {
|
|||||||
perror("Error removing directory");
|
perror("Error removing directory");
|
||||||
} else { printf("success\n"); }
|
} else { printf("success\n"); }
|
||||||
continue;
|
continue;
|
||||||
}
|
} else if (strcmp("ls", args[0])) {
|
||||||
|
// TODO: finish this hoe
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// execute the external command
|
// execute the external command
|
||||||
execute(args);
|
execute(args);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user