Skip to content

Commit

Permalink
updated betty
Browse files Browse the repository at this point in the history
  • Loading branch information
johncoleman83 committed Apr 20, 2017
1 parent 404c8c9 commit fd11d77
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 159 deletions.
8 changes: 0 additions & 8 deletions aliasB.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,26 @@ int load_alias(arg_inventory_t *arginv)
home_node = fetch_node(arginv->envlist, "HOME");
home = home_node->val;
lenhome = _strlen(home);

file = safe_malloc(sizeof(char) * (_strlen(home) + lenname + 1));
file = _strncat(file, home, lenhome);
file = _strncat(file, name, lenname);

fd = open(file, O_RDONLY);
if (fd == -1)
{
free(file);
return (1);
}
buffer = (char *)safe_malloc(sz);

while ((count = _readline(fd, &buffer, &sz)) != 0)
{
while (buffer[count - 1] == '\n')
buffer[count - 1] = '\0';

val = buffer;

while (*val && *val != ':')
val++;

if (!*val)
break;

*(val++) = '\0';

add_node_alias(&arginv->alias, buffer, val);
}
free(file);
Expand Down
16 changes: 16 additions & 0 deletions editfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
structs.h:176: warning: No description found for parameter or member 'commands'
structs.h:176: warning: No description found for parameter or member 'st_mode'
structs.h:176: warning: No description found for parameter or member 'history'
structs.h:176: warning: No description found for parameter or member 'alias'
structs.h:176: warning: No description found for parameter or member 'tokens'
structs.h:176: warning: No description found for parameter or member 'parser'
structs.h:176: warning: No description found for parameter or member 'pipeline'
structs.h:176: warning: No description found for parameter or member 'n_bg_jobs'
structs.h:176: warning: No description found for parameter or member 'pipein'
structs.h:176: warning: No description found for parameter or member 'pipeout'
structs.h:176: warning: No description found for parameter or member 'io_redir'
structs.h:176: warning: No description found for parameter or member 'filename'
structs.h:176: warning: No description found for parameter or member 'last_exit_code'
structs.h:176: warning: No description found for parameter or member 'last_bg_pid'
structs.h:176: warning: No description found for parameter or member 'exit'
structs.h:176: warning: No description found for parameter or member 'exit_status'
25 changes: 13 additions & 12 deletions executeA.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ int exec_builtins(arg_inventory_t *arginv)
}
}
/*
if (arginv->io_redir == TOKEN_REWRITE || arginv->io_redir ==
TOKEN_APPEND || arginv->pipeout)
{
safe_dup2(old_stdout, STDOUT_FILENO);
close(old_stdout);
}
*/
* if (arginv->io_redir == TOKEN_REWRITE || arginv->io_redir ==
* TOKEN_APPEND || arginv->pipeout)
* {
* safe_dup2(old_stdout, STDOUT_FILENO);
*
* close(old_stdout);
* }
*/

return (retval);
}
Expand Down Expand Up @@ -89,10 +89,11 @@ pid_t exec_path(char *command, arg_inventory_t *arginv)
{
_environ = zelda_to_ganondorf(arginv->envlist);
/*
redirect_output(arginv, 1);
if (redirect_input(arginv))
exec_error_exit("No such file or directory\n", command, _environ, arginv);
*/
* redirect_output(arginv, 1);
* if (redirect_input(arginv))
* exec_error_exit("No such file or directory\n", command,
*_environ, arginv);
*/
if (execve(command, (char **)arginv->commands, _environ) < 0)
exec_error_exit("No Command\n", command, _environ, arginv);
}
Expand Down
88 changes: 0 additions & 88 deletions executeB.c
Original file line number Diff line number Diff line change
@@ -1,93 +1,5 @@
#include "header.h"

/**
* safe_dup2 - make a dup2 and exit if an error is found
* @new_fd: file descriptor to use
* @old_fd: file descriptor to replace
*
*/
/*
void safe_dup2(int new_fd, int old_fd)
{
if (dup2(new_fd, old_fd) < 0)
{
_perror("cannot duplicate\n");
exit(1);
}
}
*/

/**
* redirect_output - redirect stdout depending on the pipeline & redirect token
* @arginv: arguments inventory
* @close_dup: if 1 closes duplicated fd
*
* Return: old stdout file descriptor
*/
/*int redirect_output(arg_inventory_t *arginv, int close_dup)
{
int stdout_fd;
int old_stdout;
if (arginv->io_redir == TOKEN_REWRITE || arginv->io_redir == TOKEN_APPEND)
{
if (arginv->io_redir == TOKEN_REWRITE)
stdout_fd = open(arginv->filename, O_WRONLY | O_CREAT | O_TRUNC,
0666);
else
stdout_fd = open(arginv->filename, O_WRONLY | O_CREAT | O_APPEND,
0666);
old_stdout = dup(STDOUT_FILENO);
safe_dup2(stdout_fd, STDOUT_FILENO);
if (close_dup)
{
close(stdout_fd);
if (arginv->pipeout)
close(arginv->pipeout);
}
}
else if (arginv->pipeout)
{
old_stdout = dup(STDOUT_FILENO);
safe_dup2(arginv->pipeout, STDOUT_FILENO);
}
return (old_stdout);
}
*/
/**
* redirect_input - redirect stdin depending on the pipeline & redirect token
* @arginv: arguments inventory
*
*/
/*
int redirect_input(arg_inventory_t *arginv)
{
int stdin_fd;
if (arginv->io_redir == TOKEN_CAT)
{
stdin_fd = open(arginv->filename, O_RDONLY);
if (stdin_fd < 0)
return -1;
safe_dup2(stdin_fd, STDIN_FILENO);
close(stdin_fd);
if (arginv->pipein)
close(arginv->pipein);
}
else if (arginv->pipein)
{
safe_dup2(arginv->pipein, STDIN_FILENO);
}
return (0);
}
*/

/**
* is_path - checks if input command is part of directory PATH
* @command: a command
Expand Down
2 changes: 1 addition & 1 deletion file_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ssize_t read_textfile(char *filename, size_t letters)
}

/**
* append_text_to_file - appends text to the end of a file
* trunc_text_to_file - appends text to the end of a file
* @filename: name of file to have text appended to
* @text_content: text to be appended to the file
*
Expand Down
4 changes: 2 additions & 2 deletions getline.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ ssize_t _getline(char **buffer, size_t *limit)
free(*buffer);
exit(EXT_SUCCESS);
}


return (count);
}

/**
* _readline - custom getline currently reads 1 char at a time from a file descriptor
* _readline - custom getline currently reads 1 char at a time from a file
* descriptor
* @fd: file descriptor of the file to use for reading
* @buffer: address of pointer to input commands buffer
* @limit: maxsize of input character string, realloc if necessary
Expand Down
13 changes: 8 additions & 5 deletions header.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extern char **environ;
ssize_t _getline(char **buffer, size_t *limit);
arg_inventory_t *buildarginv(void);
int _filemode(int fd);
ssize_t _readline(int fd,char **buffer, size_t *limit);
ssize_t _readline(int fd, char **buffer, size_t *limit);

/* ---------------execute--------------- */
pid_t execute(arg_inventory_t *arginv);
Expand Down Expand Up @@ -92,7 +92,8 @@ int _unsetenv(arg_inventory_t *arginv);
char *_strcat(char *dest, char *src);
char *int_to_str(unsigned int n);
void replace_str(char **old_str, char *new_str, int i, int j, int flg);
char *_str_replace(char *string, unsigned int start, unsigned int end, char *rep);
char *_str_replace(char *string, unsigned int start, unsigned int end,
char *rep);

/* -----custom C std lib----- */
char _isspace(char c);
Expand Down Expand Up @@ -146,12 +147,14 @@ void free_paths(char **paths);

/* ---------------ptree--------------- */
ptree_t *ptree_new_node(ptree_t *parent);
ptree_t *ptree_new_string_node(ptree_t *parent, tokens_t *tokens, unsigned int *cur_token);
ptree_t *ptree_new_string_node(ptree_t *parent, tokens_t *tokens,
unsigned int *cur_token);
int delete_ptree(ptree_t *node);

/* ---------------parser--------------- */
int parse_error(token_t *near);
ptree_t *parse_expr(unsigned int *ntoken, tokens_t *tokens, ptree_t *lhs, int min_prec);
ptree_t *parse_expr(unsigned int *ntoken, tokens_t *tokens, ptree_t *lhs,
int min_prec);
int parse(parser_t *parser, tokens_t *tokens);
int delete_parser(parser_t *parser);
void expand_bash_vars(arg_inventory_t *arginv);
Expand All @@ -163,7 +166,7 @@ int init_pipeline_push_processes(pipeline_t *pipeline, ptree_t *tree);
int init_pipeline(pipeline_t *pipeline, ptree_t *ptree);
int worker_execute_core(arg_inventory_t *arginv);
int worker_execute(arg_inventory_t *arginv);
int delete_pipeline(pipeline_t * pipeline);
int delete_pipeline(pipeline_t *pipeline);

/* ---------------free--------------- */
int freeall(arg_inventory_t *arginv);
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int main(void)
{
if (arginv->st_mode)
write(STDOUT_FILENO, "$ ", 2);
if(!_getline(&arginv->input_commands, &arginv->buflimit))
if (!_getline(&arginv->input_commands, &arginv->buflimit))
break;
add_node_history(&arginv->history, arginv->input_commands);

Expand Down
31 changes: 5 additions & 26 deletions pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ unsigned int init_pipeline_count_processes(ptree_t *tree)
if (tree->token_id == TOKEN_STRING)
return (1);

return (init_pipeline_count_processes(tree->left) + init_pipeline_count_processes(tree->right));
return (init_pipeline_count_processes(tree->left) +
init_pipeline_count_processes(tree->right));
}

/**
* init_pipeline_push_processes - fetches everything and puts into pipeline struct
* init_pipeline_push_processes - fetches everything & puts to pipeline struct
* @pipeline: pointer to pipeline
* @tree: ptree to fetch from
*
Expand All @@ -33,56 +34,40 @@ int init_pipeline_push_processes(pipeline_t *pipeline, ptree_t *tree)

if (!tree)
return (0);

if (is_redirection(tree->token_id))
{
pipeline->processes[pipeline->processesN].ptree = tree;
pipeline->processes[pipeline->processesN].io_redir = tree->token_id;

filename = safe_malloc(strlen(tree->right->strings[0]) + 1);

/* copy filename from right child */
_strcpy(filename, (char *)tree->right->strings[0]);
pipeline->processes[pipeline->processesN].filename = filename;
pipeline->processesN++;

tree->token_id = TOKEN_STRING;
tree->strings = tree->left->strings;
tree->stringsN = tree->left->stringsN;
tree->left->strings = NULL;
tree->left->stringsN = 0;

/* clean up */
delete_ptree(tree->left);
delete_ptree(tree->right);
tree->left = NULL;
tree->right = NULL;

tree->left = NULL, tree->right = NULL;
return (0);
}

if (tree->token_id == TOKEN_STRING)
{
/* Adds string to pipeline */
pipeline->processes[pipeline->processesN].ptree = tree;
pipeline->processes[pipeline->processesN].io_redir = 0;
pipeline->processes[pipeline->processesN].filename = NULL;
pipeline->processesN++;

init_pipeline_push_processes(pipeline, tree->left);
init_pipeline_push_processes(pipeline, tree->right);

return (0);
}

if (tree->token_id == TOKEN_PIPE)
{
/* Initialize everything from left to right, order matters */
init_pipeline_push_processes(pipeline, tree->left);
init_pipeline_push_processes(pipeline, tree->right);
return (0);
}

return (0);
}

Expand All @@ -108,28 +93,22 @@ int init_pipeline(pipeline_t *pipeline, ptree_t *ptree)

/* allocate memory */
pipeline->processes = safe_malloc(mem_needed * sizeof(process_t));

if (!pipeline->processes)
{
_perror("Memory allocation failed!\n");
exit(1);
}

/* initializes background service values */
pipeline->background = 0;
pipeline->background_pid = 0;

/* initalizes actual process */
pipeline->processesN = 0;

init_pipeline_push_processes(pipeline, ptree);

if (pipeline->processesN != mem_needed)
{
_perror("uhhhhh number of commands does not match\n");
exit(1);
}

for (i = 0; i < pipeline->processesN; i++)
pipeline->processes[i].pid = 0;

Expand All @@ -144,7 +123,7 @@ int init_pipeline(pipeline_t *pipeline, ptree_t *ptree)
*/
int delete_pipeline(pipeline_t *pipeline)
{
unsigned i;
unsigned int i;

for (i = 0; i < pipeline->processesN; i++)
if (pipeline->processes[i].filename != NULL)
Expand Down
2 changes: 1 addition & 1 deletion ptree.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int delete_ptree(ptree_t *node)
if (node->strings)
{
i = 0;
while(node->strings[i])
while (node->strings[i])
{
free(node->strings[i]);
i++;
Expand Down
Loading

0 comments on commit fd11d77

Please sign in to comment.