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

@ -20,3 +20,4 @@ Work is still in porgress, buf when you will see a "finished" topic assigned to
* Environmental variables
* `Ctrl+Z` running programm with `fd`
* 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
#define _SHELL_H
#define _GNU_SOURCE
#include <features.h>
#include <stdio.h>
#include <stdlib.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]);
}
sz = get_dir_list(opts, curr_pos, 0);
free(curr_pos);
}
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);
free_tree(child_dirs_root);
free_str_arr(folders);
return *size;
}

View File

@ -1,6 +1,7 @@
#include "../include/shell.h"
#include "../include/input.h"
#include "../include/output.h"
#include "../include/utils.h"
/* Global definitions */
char *builtin[] = {
@ -33,7 +34,8 @@ void process_command()
status = execute(args);
free(line);
free(args);
free(prompt);
free_str_arr(args);
} while (status);
}
@ -47,11 +49,11 @@ int process_line(char *line, char ***args)
{
int buff_size = ARG_SIZE, pos = 0;
*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)
{
(*args)[pos] = tok;
(*args)[pos] = strdup(tok);
pos++;
if (pos > buff_size)
@ -66,6 +68,8 @@ int process_line(char *line, char ***args)
}
}
free(free_rest);
(*args)[pos] = NULL;
return pos;
}
@ -156,7 +160,13 @@ int sh_exit(char **args)
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);
if (getuid() == 0)

View File

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