c compiler
c compiler
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);
}
{
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);
}
{
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 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 l = 0, i = 0;
while (s[i] != '\0')
{
l++;
i++;
}
return (l);
}
{
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);
}