diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-02-09 19:36:03 -0600 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-02-09 19:36:03 -0600 |
| commit | f1f01cbbf9681ee311a69db40e832c630950cb53 (patch) | |
| tree | c2f1f743856ec7601635e74b0bdd5d1f633c80ae /src/lsh_main_func.c | |
| parent | 5f08fd6e7794dcdfb74c93af16dfbc903458b0ea (diff) | |
environment vairables. set and get
Diffstat (limited to 'src/lsh_main_func.c')
| -rw-r--r-- | src/lsh_main_func.c | 144 |
1 files changed, 77 insertions, 67 deletions
diff --git a/src/lsh_main_func.c b/src/lsh_main_func.c index af20b1b..f2a19a2 100644 --- a/src/lsh_main_func.c +++ b/src/lsh_main_func.c @@ -10,97 +10,107 @@ #define LSH_RL_BUFSIZE 1024 char *lsh_read_line(void) { - int bufsize = LSH_RL_BUFSIZE; - int position = 0; - char *buffer = malloc(sizeof(char) * bufsize); - int c; + int bufsize = LSH_RL_BUFSIZE; + int position = 0; + char *buffer = malloc(sizeof(char) * bufsize); + int c; - if (!buffer) { - fprintf(stderr, "lsh: allocation error\n"); - exit(EXIT_FAILURE); - } + if (!buffer) { + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } - while (1) { - // Read a character - c = getchar(); + while (1) { + // Read a character + c = getchar(); - // If we hit EOF, replace it with a null character and return. - if (c == EOF || c == '\n') { - buffer[position] = '\0'; - return buffer; - } else { - buffer[position] = c; + // If we hit EOF, replace it with a null character and return. + if (c == EOF || c == '\n') { + buffer[position] = '\0'; + return buffer; + } else { + buffer[position] = c; + } + position++; + + // If we have exceeded the buffer, reallocate. + if (position >= bufsize) { + bufsize += LSH_RL_BUFSIZE; + buffer = realloc(buffer, bufsize); + if (!buffer) { + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } + } } - position++; +} - // If we have exceeded the buffer, reallocate. - if (position >= bufsize) { - bufsize += LSH_RL_BUFSIZE; - buffer = realloc(buffer, bufsize); - if (!buffer) { - fprintf(stderr, "lsh: allocation error\n"); - exit(EXIT_FAILURE); - } +char *handle_vars(char *token){ + if (token[0] == '$'){ + token=&token[1]; + token=getenv(token); } - } + return token; } #define LSH_TOK_BUFSIZE 64 #define LSH_TOK_DELIM " \t\r\n\a" char **lsh_split_line(char *line) { - int bufsize = LSH_TOK_BUFSIZE, position = 0; - char **tokens = malloc(bufsize * sizeof(char*)); - char *token; + int bufsize = LSH_TOK_BUFSIZE, position = 0; + char **tokens = malloc(bufsize * sizeof(char*)); + char *token; - if (!tokens) { - fprintf(stderr, "lsh: allocation error\n"); - exit(EXIT_FAILURE); - } - - token = strtok(line, LSH_TOK_DELIM); - while (token != NULL) { - tokens[position] = token; - position++; - - if (position >= bufsize) { - bufsize += LSH_TOK_BUFSIZE; - tokens = realloc(tokens, bufsize * sizeof(char*)); - if (!tokens) { + if (!tokens) { fprintf(stderr, "lsh: allocation error\n"); exit(EXIT_FAILURE); - } } - token = strtok(NULL, LSH_TOK_DELIM); - } - tokens[position] = NULL; - return tokens; + token = strtok(line, LSH_TOK_DELIM); + while (token != NULL) { + token = handle_vars(token); + tokens[position] = token; + position++; + + + if (position >= bufsize) { + bufsize += LSH_TOK_BUFSIZE; + tokens = realloc(tokens, bufsize * sizeof(char*)); + if (!tokens) { + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } + } + + token = strtok(NULL, LSH_TOK_DELIM); + } + tokens[position] = NULL; + return tokens; } int lsh_launch(char **args) { - pid_t pid, wpid; - int status; + pid_t pid, wpid; + int status; - pid = fork(); - if (pid == 0) { - // Child process - if (execvp(args[0], args) == -1) { - perror("lsh"); + pid = fork(); + if (pid == 0) { + // Child process + if (execvp(args[0], args) == -1) { + perror("lsh"); + } + exit(EXIT_FAILURE); + } else if (pid < 0) { + // Error forking + perror("lsh"); + } else { + // Parent process + do { + wpid = waitpid(pid, &status, WUNTRACED); + } while (!WIFEXITED(status) && !WIFSIGNALED(status)); } - exit(EXIT_FAILURE); - } else if (pid < 0) { - // Error forking - perror("lsh"); - } else { - // Parent process - do { - wpid = waitpid(pid, &status, WUNTRACED); - } while (!WIFEXITED(status) && !WIFSIGNALED(status)); - } - return 1; + return 1; } #endif |
