0% found this document useful (0 votes)
5 views

c compiler

Uploaded by

soc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

c compiler

Uploaded by

soc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
char *readcmd(char *command);
char **parse_line(char *buffer);
int check_builtin(char **tokenz);
void for_env(void);
int _strlen(char *s);
int _strcmp(char *s1, char *s2);
extern char **environ;
/**
*main - simple shell
*Return: returns 1 on success
*/
int main(void)

{
int h = 1, status, p, c;
char *buffer = NULL;
char **tokenz;
pid_t pid;
while (h == 1)
{
//p = isatty(STDIN_FILENO);
buffer = readcmd(buffer);
//printf("%s", buffer);
tokenz = parse_line(buffer);
c = check_builtin(tokenz);
if (c == 0)
break;
if (c == 1)
{
free(buffer);
free(tokenz);
continue;
}
pid = fork();
if (pid == 0)
{
if (execve(tokenz[0], tokenz, environ) == -1)
perror("Error");
exit(0);
}
else if (pid < 0)
perror("Error");
else
wait(&status);
free(buffer);
free(tokenz);
if (p != 1)
h--;
}
return (1);
}

char *readcmd(char *command)

{
int l, p;
size_t bufsize = 0;
p = isatty(STDIN_FILENO);
if (p == 1)
{
write(1, " ($) ", 5);
}
getline(&command, &bufsize, stdin);
/**== -1)
*{
*if (feof(stdin))
*{
*exit(EXIT_SUCCESS);
*}
*else
*{
*perror("readline");
*exit(EXIT_FAILURE);
*}
*}
*/
l = _strlen(command);
l = l - 1;
if (command[l] == '\n' || command[l] == EOF)
{
*(command + l) = '\0';
}
//printf("%s", command);
return (command);
}

char **parse_line(char *buffer)

{
int i = 0, size = 64;
char *tok;
char **tokenp = malloc(size * sizeof(char *));
if (tokenp == NULL)
{
exit(EXIT_FAILURE);
}
printf("%s", buffer);
tok = strtok(buffer, " ");
printf("%s", tok);
while (tok != NULL)
{
tokenp[i] = tok;
tok = strtok(NULL, " ");
i = i + 1;
}
tokenp[i] = NULL;
printf("%s", tokenp[0]);
return (tokenp);
}

int check_builtin(char **tokenz)

{
int i = 0, k, l;
char *builtins[] = {"echo", "exit", "env", NULL};
for (i = 0; i <= 2; i++)
{
k = _strcmp(tokenz[0], builtins[i]);
if (k == 0)
{
break;
}
}
if (i == 3)
return (2);
if (i == 0)
{
l = _strlen(tokenz[1]);
write(1, tokenz[1], l);
write(1, "\n", 1);
return (1);
}
if (i == 1)
return (0);
if (i == 2)
{
for_env();
return (1);
}
return (1);
}

void for_env()

{
int i, l;
i = 0;
while (environ[i] != NULL)
{
l = _strlen(environ[i]);
//printf("%s", environ[i]);
write(1, environ[i], l);
write(1, "\n", 1);
i++;
}
}

int _strlen(char *s)

{
int l = 0, i = 0;
while (s[i] != '\0')
{
l++;
i++;
}
return (l);
}

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);
}

You might also like