Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d14c1d8
everything
matxa Nov 25, 2019
ba15f92
Merge pull request #8 from fvesp18/matxa
fvesp18 Nov 25, 2019
bd1612f
man page
fvesp18 Nov 25, 2019
4d9fadd
excess file
fvesp18 Nov 26, 2019
2c2448f
excess file
fvesp18 Nov 26, 2019
e712d49
delete empty execute.c
fvesp18 Nov 26, 2019
b53fcfa
removes printf debug statements
fvesp18 Nov 26, 2019
f435db6
Merge branch 'master' of https://github.com/fvesp18/simple_shell
fvesp18 Nov 26, 2019
a942afa
Update edit manually, see branch matxa for history.
fvesp18 Nov 26, 2019
0bc2c9c
update header, declare environ
fvesp18 Nov 26, 2019
304885c
Add strchr implementation
fvesp18 Nov 26, 2019
ed01792
delete excess commit at end of file
fvesp18 Nov 26, 2019
f037556
Shorten code to make betty compliant
fvesp18 Nov 26, 2019
d3a7daa
change foo** bar to foo **bar
fvesp18 Nov 26, 2019
a470759
delete excess code at end
fvesp18 Nov 26, 2019
681db53
remove extern var, declare in header
fvesp18 Nov 26, 2019
88ba93e
delete excess code at end of file
fvesp18 Nov 26, 2019
cd35de8
change from (foo*) to (foo *)
fvesp18 Nov 26, 2019
d513ef0
add description for user_input
fvesp18 Nov 26, 2019
7393f70
initial filter
fvesp18 Nov 26, 2019
c564d4d
filter function
fvesp18 Nov 26, 2019
564a2ef
change run to execute
fvesp18 Nov 26, 2019
d318b13
add prototype
fvesp18 Nov 26, 2019
0e57af6
delete trailing
fvesp18 Nov 26, 2019
3b1f080
Add authors
fvesp18 Nov 26, 2019
b94b95f
Edit manpage
fvesp18 Nov 26, 2019
d83bfba
edit header
fvesp18 Nov 26, 2019
3ab8148
fix trailing space, line 36
fvesp18 Nov 26, 2019
779a5c5
README
fvesp18 Nov 26, 2019
4230278
Add image
fvesp18 Nov 26, 2019
f4b6569
add exit
fvesp18 Nov 26, 2019
a64ba82
rm exit func
fvesp18 Nov 26, 2019
5a838f4
update
fvesp18 Nov 27, 2019
2959d61
update
fvesp18 Nov 27, 2019
1c1a494
update
fvesp18 Nov 27, 2019
b1be6b9
making env work
Nov 27, 2019
ceac085
last update from matxa
Nov 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions 2-str_concat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "simpleshell.h"
/**
* str_concat - concatenates two strings
* @s1: string one
* @s2: string two
* Return: ptr
*/
char *str_concat(char *s1, char *s2)
{
/* declare your variables */
int len1 = 0, len2 = 0, count = 0, begs = 0;
char *cat;

/* check for empty strings */
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";

/* find length of strings */
while (s1[len1] != '\0')
len1++;
while (s2[len2] != '\0')
len2++;

begs = len1 + len2 + 1;
/* allocate memory for concatenated string */
cat = malloc(begs * sizeof(char));
/* malloc fail safe */
if (cat == NULL)
return (NULL);

len2 = 0;

/* duplicate s1 to cat */
while (count < begs)
{
if (count < len1)
cat[count] = s1[count];
else
{
cat[count] = s2[len2];
len2++;
}
count++;
}
cat[count] = '\0';
return (cat);
}
21 changes: 21 additions & 0 deletions 3-strcmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "simpleshell.h"

/**
*_strcmp - shows the pointer of the var
*@s2: var for p
*@s1: p
*Return: equal
*/

int _strcmp(char *s1, char *s2)
{
int i = 0;
int equal = 0;

for (i = 0; (*(s1 + i) || *(s2 + i)) && !equal; i++)
{
if (*(s1 + i) != *(s2 + i))
equal = *(s1 + i) - *(s2 + i);
}
return (equal);
}
107 changes: 106 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,106 @@
# simple_shell
![simple_shell](https://i.imgur.com/1qxIw3q.png)

# Shell Project `(simple Shell)`

By Marcelo R-M, and Flavio

** This project is our implementation of a simple shell. **

## Topics:

- ### How the shell works?

1. ** The shell prompts for user input **

Using the `write()` function to display (stdout(1)) `$ `

2. Get user input.

Using the `char *user_input(void);` function, which uses `getline()`function to get and store user input into a buffer, which getline mallocs for, if getline succeds, the function returns the buffer that holds the user input.

3. Parse the input.

Using the `char **tokens(char *buffer);` function, tokanizes the user input and saves each token into a double pointer erray(**hold_tokens). The double pointer erray is then returned.

4. Run the command.

1. Using the `int _execute(char **args);` function, checks if the user input is a builtin or not(this is called a filler). For example if the command `exit` is the user input. the function will make sure the command is in the array list of builtin commands."exit" or "env".

2. If it's not then then the `int run(char **args);` function is returned.

3. Using the `int run(char **args);` function, creates a child Proccess, In the child process we check if the user input is a path command (`/bin/ls`) or a regular command (`ls`). If it is a regular command (`ls`) we tokanize the path variable whos value is equall to PATH env variable, and cp = strcat("/", ls); = `/ls`, we then append `cp` to every path's token deliminated by `:`. This is saved into a variable called new. And then using `access` we check if the `new` variable is correct(`ls` is in the dir) or if we can execute `ls`. If so then `execve` the new varible. But if the user input is is a path then we skip all the above steps and just assigned the path to `execve`. AND finally if neither is true then `perror`.(print error).

5. Starts again at number 1 until terminated(exit, or with kill signals).

- ### The PATH(env var)

** PATH ** is an environmental ** variable in Linux ** and other Unix-like operating systems that tells the shell which directories to search for executable files. Understanding the path, is really important to buiding a shell. Its important to know how to access the PATH (working with environmental variables).



- ### Builtins

A builtin command is a Linux/Unix command which is "* built into a shell interpreter * such as sh, ksh, bash, etc.



```c
#include "simpleshell.h"

/**
* main - main shell function, where the magic happens
* takes in no arg
* Return: return (0) on success
*/

int main(void)
{
/* the dollar :)*/
char *prompt = "$ ";

char *buffer;
char **cmd;

while (1)
{
write(1, prompt, (sizeof(char) * 2));
buffer = user_input();
cmd = tokens(buffer);
_execute(cmd);

/* free(buffer);*/
/* free(cmd);*/
}
}
```





| Function `(name)` | File `(name)` |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| `char *user_input(void);` | [user_input.c](https://github.com/fvesp18/simple_shell/blob/master/user_input.c) |
| `char **tokens(char *buffer);` | [tokens.c](https://github.com/fvesp18/simple_shell/blob/master/tokens.c) |
| `int main(void);` | [shell_main.c](https://github.com/fvesp18/simple_shell/blob/master/shell_main.c) |
| `char *_getenv(char *var_name);` | [shell_getenv.c](https://github.com/fvesp18/simple_shell/blob/master/shell_getenv.c) |
| `int run(char **args);` | [run.c](https://github.com/fvesp18/simple_shell/blob/master/run.c) |
| `void *_realloc(void *ptr, size_t old_size, size_t new_size);` | [100-realloc.c](https://github.com/fvesp18/simple_shell/blob/master/100-realloc.c) |
| `int _execute(char **args);`<br />`int _exit(char **argv)` | [execute.c](https://github.com/fvesp18/simple_shell/blob/master/execute.c) |
| ** String functions **<br />`int _strlen(char *s);`<br />`char *_strchr(char *s, char c);`<br />`char *_strcat(char *dest, char *src);`<br />`int _strcmp(char *s1, char *s2);`<br />`char *str_concat(char *s1, char *s2);` | <br />[_strlen.c](https://github.com/fvesp18/simple_shell/blob/master/_strlen.c)<br />[_strchr.c](https://github.com/fvesp18/simple_shell/blob/master/_strchr.c)<br />[_strcat.c](https://github.com/fvesp18/simple_shell/blob/master/_strcat.c)<br />[_strcmp.c](https://github.com/fvesp18/simple_shell/blob/master/3-strcmp.c)<br />[2-str_concat.c](https://github.com/fvesp18/simple_shell/blob/master/2-str_concat.c) |
| `man_1_simple_shell` | [man_1_simple_shell](https://github.com/fvesp18/simple_shell/blob/master/man_1_simple_shell) |



## what works:

- PATH being passed as a command ex.(`/bin/ls`).
- single commands ex.(`ls`)
- muiltiple commands : parsed user input : (`ls -l`)

## Bugs:

- Exit - doesn't work first time it's typed in.
- exit and env don't work togther, either or works.(only one can be typed in at the time the shell is running)
- memory leak
- Etc...
22 changes: 22 additions & 0 deletions _strcat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "simpleshell.h"
/**
* _strcat - will concatenate two strings
* @dest: destination string
* @src: src string
* Return: 0
*/
char *_strcat(char *dest, char *src)
{
int srci = 0;
int countdest = _strlen(dest);

while (src[srci] != '\0')
{
dest[countdest + srci] = src[srci];
srci++;
}

dest[countdest + srci] = '\0';

return (dest);
}
18 changes: 18 additions & 0 deletions _strchr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "simpleshell.h"

/**
**_strchr - finds and return char
*@s: pointer
*@c: int var
*Return: (s)
*/

char *_strchr(char *s, char c)
{
while (*s++)
{
if (*s == c)
return (s);
}
return (0);
}
18 changes: 18 additions & 0 deletions _strlen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "simpleshell.h"
/**
* _strlen - print length of string
* @s: string
* Return: void
*/
int _strlen(char *s)
{
int count = 0;

while (s[count] != '\0')
{

count++;

}
return (count);
}
30 changes: 0 additions & 30 deletions builtin_func.c

This file was deleted.

64 changes: 59 additions & 5 deletions execute.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,67 @@
#include "simpleshell.h"

/**
* Work on forking - fork(), wait(), exexve()
*
*
*
* _printenv - prints environmental variables
* Return: (N/A)
*/

void _printenv(void)
{
int i;

for (i = 0; environ[i] != NULL; i++)
{
write(1, environ[i], _strlen(environ[i]));
write(1, "\n", (sizeof(char) * 2));
}

}

/**
* shell_exit - exits the shell
* @argv: user input
* Return: exit (EXIT_SUCCESS)
*/

int execute()
int shell_exit(char **argv)
{
free(argv);
exit(EXIT_SUCCESS);
}

/**
* _execute - filters if builtin or not
* @args: user input
* Return: run(cmd)
*/

int _execute(char **args)
{
int type;
char *cmd[4] = {"", "env", "exit", NULL};

type = 0;
if (args[type] == NULL)
return (1);
/* filter */
while (cmd[type] != NULL)
{
if (_strcmp(cmd[type], args[0]) == 0)
break;
type++;
}
/* call to cmds */
switch (type)
{
case (1):
_printenv();
break;
case (2):
shell_exit(args);
break;
default:
run(args);
break;
}
return (type);
}
48 changes: 0 additions & 48 deletions get_line.c

This file was deleted.

Binary file added hsh
Binary file not shown.
Loading