top shell project
top shell project
c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include "main.h"
/**
*main - our shell's main program
*Return: returns 1 on success
*/
int main(int ac, char **argv)
{
int h = 1, status, i = 0, checker;
char *cmd, *cmdp, *buffer = NULL, **tokenz, *copy = malloc(50);
pid_t pid;
if (ac == 0)
return (1);
while (h == 1)
{
buffer = prompt(buffer);
if (*buffer == '\n')
continue;
while (buffer[i] != '\0')
{
copy[i] = buffer[i];
i++;
}
checker = get_builtin_func(copy)(copy);
if (checker == 1)
{
continue;
}
if (checker == 2)
break;
tokenz = parse_line(buffer);
cmd = strtok(buffer, " ");
cmdp = cmd;
if (*cmd != '/')
{
cmdp = _strcat(cmd);
}
pid = fork();
if (pid == 0)
{
if (execve(cmdp, tokenz, environ) == -1)
perror(argv[0]);
exit(0);
}
else if (pid < 0)
perror(argv[0]);
else
wait(&status);
}
free(copy);
free(buffer);
return (1);
}
Prompt.c
#include <stdio.h>
#include <unistd.h>
#include "main.h"
/**
*prompt - a function that displays prompt and gets input from stdin
*@buffer: string input from stdin
*Return: returns string read from command line
*/
char *prompt(char *buffer)
{
int i, l = 0, p = 1;
size_t bufsize = 0;
p = isatty(STDIN_FILENO);
if (p == 1)
{
write(1, " ($) ", 5);
}
getline(&buffer, &bufsize, stdin);
/*_getline(&buffer, &bufsize);*/
if (*buffer == '\n')
return (buffer);
for (i = 0; buffer[i] != '\0'; i++)
{
l++;
}
l = l - 1;
if (buffer[l] == '\n' || buffer[l] == EOF)
{
*(buffer + l) = '\0';
}
return (buffer);
}
Play_on_strings.c
#include <stdio.h>
#include "main.h"
/**
*_strcmp - compares a string
*@s1: character pointer
*@s2: character pointer
*Return: returns an integer
*/
int _strcmp(char *s1, char *s2)
{
int l = 0, i, r, a, b, j;
for (j = 0; s1[j] != '\0'; j++)
{
l++;
}
for (i = 0; i < l; i++)
{
if (*(s1 + i) != *(s2 + i))
break;
}
a = s1[i];
b = s2[i];
r = a - b;
return (r);
}
/**
*
*
*/
char *_strcpy(char *dest, char *src)
{
int i = 0, l = 0;
while (src[i] !='\0')
{
l++;
i++;
}
for (i = 0; i < l; i++)
{
*(dest + i) = *(src + i);
}
return (dest);
}
/**
*
*
*/
char *_strcat(char *src)
{
int i = 0, l1 = 0, l2 = 0;
char *e;
while (src[i] != '\0')
{
l1++;
i++;
}
e = malloc(20);
_strcpy(e, "/bin/");
i = 0;
while (e[i] != '\0')
{
l2++;
i++;
}
for (i = 0; i < l1; i++)
{
*(e + l2 + i) = *(src + i);
}
return (e);
}
/**
*_strncmp - compares two strings.
*@s1: first string
*@s2: second string
*@n: number of bytes to be compared
*Return: returns 0 on success, 1 for fail
*/
int _strncmp(char *s1, char *s2, int n)
{
int j;
for (j = 0; j < n; j++)
{
if (*(s1 + j) != *(s2 + j))
break;
}
if (j == n)
{
return (0);
}
else
return (1);
}
/**
*
*
*
*/
int _strlen(char *s)
{
int l = 0, i = 0;
while (s[i] != '\0')
{
l++;
i++;
}
return (l);
}
Parse_line.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
/**
*
*/
char **parse_line(char *buffer)
{
int i = 0;
char *tok;
char **tokenp = malloc(100 * sizeof(char*));
if (tokenp == NULL)
{
exit(EXIT_FAILURE);
}
tok = strtok(buffer, " ");
while (tok != NULL)
{
tokenp[i] = tok;
tok = strtok(NULL, " ");
i = i + 1;
}
tokenp[i] = NULL;
return (tokenp);
}
Main.h
#ifndef MAIN_H
#define MAIN_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <errno.h>
#include <stddef.h>
#include <sys/stat.h>
#include <signal.h>
typedef struct cl
{
char *cmd;
int (*f)(char *s);
} cmd_l;
Envman.c
#include <stdio.h>
#include "main.h"
/**
*
*
*/
int _setenv(char *name, char *value, int overwrite)
{
int i = 0, l = 0, c;
if (overwrite == 0)
return (0);
while (name[i] != '\0')
{
l++;
i++;
}
for (i = 0; environ[i] != NULL; i++)
{
c = _strncmp(name, environ[i], l);
if (c == 0)
break;
}
if (environ[i] != NULL)
{
environ[i] = value;
}
/*i = 0;
while (environ[i] != NULL)
{
printf("%s\n", environ[i]);
i++;
}*/
return (1);
}
/**
*
*
*
*/
int _unsetenv(char *name)
{
int i = 0, l = 0, c;
while (name[i] != '\0')
{
l++;
i++;
}
for (i = 0; environ[i] != NULL; i++)
{
c = _strncmp(name, environ[i], l);
if (c == 0)
break;
}
if (environ[i] != NULL)
{
environ[i] = environ[i + 1];
}
/*i = 0;
while (environ[i] != NULL)
{
printf("%s\n", environ[i]);
i++;
}*/
return (0);
}
Builtin_cmd.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "main.h"
extern char **environ;
/**
*
*
*/
int for_cd(char *s)
{
/*char array[100];*/
s = strtok(NULL, " ");
if (s == NULL)
{
chdir("/root");
/*printf("%s\n", getcwd(array, 100));*/
return(1);
}
/*printf("%s\n", getcwd(array, 100));*/
if (chdir(s) != 0)
{
perror("Error");
}
chdir(s);
/*printf("%s\n", getcwd(array, 100));*/
return (1);
}
/**
*
*
*/
int for_unsetenv(char *s)
{
char *p;
int i = 0, l;
l = _strlen(s);
p = malloc(l + 1);
while (s[i] != '\0')
{
p[i] = s[i];
i++;
}
p = strtok(p, " ");
p = strtok(NULL, " ");
_unsetenv(p);
return (1);
}
/**
*
*/
int for_setenv(char *s)
{
char *p, *v;
int i = 0, l;
l = _strlen(s);
p = malloc(l + 1);
v = malloc(l + 1);
while (s[i] != '\0')
{
p[i] = s[i];
i++;
}
p = strtok(p, " ");
p = strtok(NULL, " ");
v = strtok(NULL, " ");
_setenv(p, v, 1);
return (1);
}
/**
*
*
*/
int for_alias(char *s)
{
s = strtok(s, " ");
printf("prints alias");
return (1);
}
/**
*
*
*/
int for_echo(char *s)
{
int l;
s = strtok(s, " ");
s = strtok(NULL, " ");
l = _strlen(s);
write(1, s, l);
write(1, "\n", 1);
return (1);
}
/**
*
*
*/
int for_env(char *s)
{
int i, l = 0;
s = strtok(s, " ");
i = 0;
while (environ[i] != NULL)
{
l = strlen(environ[i]);
write(1, environ[i], l);
write(1, "\n", 1);
i++;
}
return (1);
}
/**
*
*
*/
int for_not_builtin(char *s)
{
s = strtok(s, " ");
return (0);
}
/**
*
*
*/
int for_exit(__attribute__((unused))char *s)
{
/*s = strtok(s, " ");*/
return (2);
}
Builtin.c
#include <stdio.h>
#include "main.h"
/**
*
*
*/
int (*get_builtin_func(char *s))(char *s)
{
cmd_l cd[] = {
{"cd", for_cd},
{"unsetenv", for_unsetenv},
{"setenv", for_setenv},
{"alias", for_alias},
{"env", for_env},
{"echo", for_echo},
{"exit", for_exit},
{NULL, NULL}
};
int i = 0, k, l;
char *c, *h;
l = _strlen(s);
c = malloc(l + 1);
/*printf("%s", s);*/
h = malloc(l + 1);
while (s[i] != '\0')
{
h[i] = s[i];
i++;
}
c = strtok(h, " ");
for (i = 0; i <= 6; i++)
{
k = _strcmp(cd[i].cmd, c);
if (k == 0)
{
/*free(c);
free(h);*/
return (cd[i].f);
}
}
return (for_not_builtin);
}
_strtok.c
#include <stdio.h>
#include "main.h"
/**
*_strtok -
*
*
*/
char *_strtok(char *str, const char *delim)
{
int i, j = 0, l = 0;
static char *copy, *cop;
if (str == NULL)
{
copy = _strtok(cop, delim);
return (copy);
}
for (j = 0; str[j] != '\0'; j++)
{
l++;
}
copy = malloc(l);
cop = malloc(l);
for (i = 0; str[i] != '\0'; i++)
{
copy[i] = str[i];
}
if (i == 0)
return (NULL);
for (i = 0; copy[i] != '\0'; i++)
{
if (copy[i] == ' ')
break;
}
copy[i] = '\0';
i++;
j = 0;
while (str[i] != '\0')
{
cop[j] = str[i];
i++;
j++;
}
cop[j] = '\0';
return (copy);
}
_getline.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**
*
*
*
*/
size_t _getline(char *string, size_t *n)
{
int a;
string = malloc(sizeof(char) * (*n));
a = read(0, string, 100);
if (a < 0)
{
free(string);
return (0);
}
string[a] = '\0';
/*(*string + a) = '\0';*/
/*printf("%s", string);*/
/*close(0);*/
return (a);
}