Added path display in prompt, and yeah, solved some memory errors and added some new (I hate valgrind)

This commit is contained in:
Dmitriy Shishkov 2020-07-11 17:57:58 +05:00
parent 3354a97867
commit 922424abd4
5 changed files with 25 additions and 7 deletions

View File

@ -19,4 +19,5 @@ Work is still in porgress, buf when you will see a "finished" topic assigned to
* Replace linux `echo` command with builtin one with support of environmental variables * Replace linux `echo` command with builtin one with support of environmental variables
* Environmental variables * Environmental variables
* `Ctrl+Z` running programm with `fd` * `Ctrl+Z` running programm with `fd`
* Getting commands path from system `PATH` environment variable * Getting commands path from system `PATH` environment variable
* Create global struture for shell to save data between commands

View File

@ -1,6 +1,9 @@
#ifndef _SHELL_H #ifndef _SHELL_H
#define _SHELL_H #define _SHELL_H
#define _GNU_SOURCE
#include <features.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@ -90,6 +90,8 @@ size_t get_complete_options(char ***opts, char *line, char **to_complete)
curr_pos = strcat(curr_pos, folders[i]); curr_pos = strcat(curr_pos, folders[i]);
} }
sz = get_dir_list(opts, curr_pos, 0); sz = get_dir_list(opts, curr_pos, 0);
free(curr_pos);
} }
else else
{ {
@ -132,6 +134,7 @@ size_t filter_options(char ***comp_list, size_t *size, char *filter_string)
*size = list_strings_containing(child_dirs_root, last_option, comp_list); *size = list_strings_containing(child_dirs_root, last_option, comp_list);
free_tree(child_dirs_root); free_tree(child_dirs_root);
free_str_arr(folders);
return *size; return *size;
} }

View File

@ -1,6 +1,7 @@
#include "../include/shell.h" #include "../include/shell.h"
#include "../include/input.h" #include "../include/input.h"
#include "../include/output.h" #include "../include/output.h"
#include "../include/utils.h"
/* Global definitions */ /* Global definitions */
char *builtin[] = { char *builtin[] = {
@ -33,7 +34,8 @@ void process_command()
status = execute(args); status = execute(args);
free(line); free(line);
free(args); free(prompt);
free_str_arr(args);
} while (status); } while (status);
} }
@ -47,11 +49,11 @@ int process_line(char *line, char ***args)
{ {
int buff_size = ARG_SIZE, pos = 0; int buff_size = ARG_SIZE, pos = 0;
*args = malloc(buff_size * sizeof(char *)); *args = malloc(buff_size * sizeof(char *));
char *tok = NULL, *rest = strdup(line); char *tok = NULL, *rest = strdup(line), *free_rest = rest;
while ((tok = strtok_r(rest, " ", &rest)) != NULL) while ((tok = strtok_r(rest, " ", &rest)) != NULL)
{ {
(*args)[pos] = tok; (*args)[pos] = strdup(tok);
pos++; pos++;
if (pos > buff_size) if (pos > buff_size)
@ -66,6 +68,8 @@ int process_line(char *line, char ***args)
} }
} }
free(free_rest);
(*args)[pos] = NULL; (*args)[pos] = NULL;
return pos; return pos;
} }
@ -156,7 +160,13 @@ int sh_exit(char **args)
char *compose_prompt() char *compose_prompt()
{ {
char *prompt = strdup(""); char *prompt = strdup("\n");
char *full_path = get_current_dir_name();
prompt = realloc(prompt, strlen(prompt) + strlen(full_path) + 2);
prompt = strcat(prompt, full_path);
free(full_path);
prompt = realloc(prompt, strlen(prompt) + 4); prompt = realloc(prompt, strlen(prompt) + 4);
if (getuid() == 0) if (getuid() == 0)

View File

@ -50,6 +50,7 @@ int sep_string(char *line, char ***toks, char *sep)
{ {
free(*toks); free(*toks);
char *tmp_line = strdup(line); char *tmp_line = strdup(line);
char *free_tmp_line = tmp_line;
int n = 0; int n = 0;
*toks = malloc(sizeof(char *) * n); *toks = malloc(sizeof(char *) * n);
@ -61,7 +62,7 @@ int sep_string(char *line, char ***toks, char *sep)
(*toks)[n - 1] = strdup(tmp); (*toks)[n - 1] = strdup(tmp);
} }
free(tmp_line); free(free_tmp_line);
return n; return n;
} }
@ -83,7 +84,7 @@ char *trim_string(char **str)
void free_str_arr(char **arr) void free_str_arr(char **arr)
{ {
if (arr) if (arr[0] != NULL)
for (int i = 0; i < sizeof(arr) / sizeof(char *); i++) for (int i = 0; i < sizeof(arr) / sizeof(char *); i++)
free(arr[i]); free(arr[i]);
free(arr); free(arr);