Skip to content

Commit

Permalink
prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
sameepkat committed Jul 29, 2024
0 parents commit b239030
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main
shell
Empty file added README.md
Empty file.
53 changes: 53 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "main.h"

int main(int argc, char *argv[])
{
char *token;
char **array;
int i;
(void)argc, (void)argv;
char *buf = NULL;
size_t count = 0;
ssize_t nread;
pid_t child_pid;
int status;

while (1) {
write(STDOUT_FILENO,"Dashed$ ",9);
nread = getline(&buf, &count, stdin);
if(nread == -1)
{
perror("Exiting shell");
exit(1);
}
token = strtok(buf, "\n");
array = malloc(sizeof(char *) * 1024);
i = 0;
while(token)
{
array[i] = token;
token = strtok(NULL, " \n");
i++;
}
array[i] = NULL;
child_pid = fork();
if(child_pid == -1)
{
perror("Failed to create.");
exit(41);
}
if(child_pid == 0)
{
if(execve(array[0], array, NULL) == -1)
{
perror("Couldn't execute");
exit(97);
}
}
else {
wait(&status);
}
}
free(buf);
return (0);
}
12 changes: 12 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef MAIN_H
#define MAIN_H

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>

#endif /* MAIN_H */

0 comments on commit b239030

Please sign in to comment.