Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #define MAX_ARGS 20
- #define BUFSIZE 1024
- int get_args(char* cmdline, char* args[])
- {
- int i = 0;
- /* if no args */
- if((args[0] = strtok(cmdline, "nt ")) == NULL)
- return 0;
- while((args[++i] = strtok(NULL, "nt ")) != NULL) {
- if(i >= MAX_ARGS) {
- printf("Too many arguments!n");
- exit(1);
- }
- }
- /* the last one is always NULL */
- return i;
- }
- void execute(char* cmdline)
- {
- int pid, async, oneapp;
- char* args[MAX_ARGS];
- char* args2[] = {"-l", NULL};
- int nargs = get_args(cmdline, args);
- if(nargs <= 0) return;
- if(!strcmp(args[0], "quit") || !strcmp(args[0], "exit")) {
- exit(0);
- }
- printf("before the ifn");
- printf("%sn",args[nargs - 2]);
- int i = 0;
- while (args[i] != ">" && i < nargs - 1) {
- printf("%sn",args[i]);
- i++;
- }
- // Presence of ">" token in args
- // causes errors in execvp() because ">" is not
- // a built-in Unix command, so remove it from args
- args[i - 1] = NULL;
- printf("Escaped the whilen");
- // File descriptor array for the pipe
- int fd[2];
- // PID for the forked process
- pid_t fpid1;
- // Open the pipe
- pipe(fd);
- // Here we fork
- fpid1 = fork();
- if (fpid1 < 0)
- {
- // The case where the fork fails
- perror("Fork failed!n");
- exit(-1);
- }
- else if (fpid1 == 0)
- {
- //dup2(fd[1], STDOUT_FILENO);
- close(fd[1]);
- //close(fd[0]);
- // File pointer for the file that'll be written to
- FILE * file;
- // freopen() redirects stdin to args[nargs - 1],
- // which contains the name of the file we're writing to
- file = freopen(args[nargs - 1], "w+", stdin);
- // If we include this line, the functionality works
- //execvp(args[0],args);
- // We're done writing to the file, so close it
- fclose(file);
- // We're done using the pipe, so close it (unnecessary?)
- //close(fd[1]);
- }
- else
- {
- // Wait for the child process to terminate
- wait(0);
- printf("This is the parentn");
- // Connect write end of pipe (fd[1]) to standard output
- dup2(fd[1], STDOUT_FILENO);
- // We don't need the read end, so close it
- close(fd[0]);
- // args[0] contains the command "ls", which is
- // what we want to execute
- execvp(args[0], args);
- // This is just a test line I was using before to check
- // whether anything was being written to stdout at all
- printf("Exec was heren");
- }
- // This is here to make sure program execution
- // doesn't continue into the original code, which
- // currently causes errors due to incomplete functionality
- exit(0);
- /* check if async call */
- printf("Async call partn");
- if(!strcmp(args[nargs-1], "&")) { async = 1; args[--nargs] = 0; }
- else async = 0;
- pid = fork();
- if(pid == 0) { /* child process */
- execvp(args[0], args);
- /* return only when exec fails */
- perror("exec failed");
- exit(-1);
- } else if(pid > 0) { /* parent process */
- if(!async) waitpid(pid, NULL, 0);
- else printf("this is an async calln");
- } else { /* error occurred */
- perror("fork failed");
- exit(1);
- }
- }
- int main (int argc, char* argv [])
- {
- char cmdline[BUFSIZE];
- for(;;) {
- printf("COP4338$ ");
- if(fgets(cmdline, BUFSIZE, stdin) == NULL) {
- perror("fgets failed");
- exit(1);
- }
- execute(cmdline) ;
- }
- return 0;
- }
- else if (fpid1 == 0)
- {
- printf("This is the child.n");
- //dup2(fd[1], STDOUT_FILENO);
- close(fd[1]);
- //close(fd[0]);
- // File pointer for the file that'll be written to
- FILE * file;
- // freopen() redirects stdin to args[nargs - 1],
- // which contains the name of the file we're writing to
- file = freopen(args[nargs - 1], "w+", stdout);
- // If we include this line, the functionality works
- //execvp(args[0],args);
- // We're done writing to the file, so close it
- fclose(file);
- // We're done using the pipe, so close it (unnecessary?)
- //close(fd[1]);
- }
- else
- {
- // Wait for the child process to terminate
- wait(0);
- printf("This is the parentn");
- // Connect write end of pipe (fd[1]) to standard output
- dup2(fd[1], STDOUT_FILENO);
- // We don't need the read end, so close it
- close(fd[0]);
- // args[0] contains the command "ls", which is
- // what we want to execute
- execvp(args[0], args);
- // This is just a test line I was using before to check
- // whether anything was being written to stdout at all
- printf("Exec was heren");
- }
- file = freopen(args[nargs - 1], "w+", stdout)
- // freopen() redirects stdin to args[nargs - 1],
- // which contains the name of the file we're writing to
- file = freopen(args[nargs - 1], "w+", stdin);
- // If we include this line, the functionality works
- execvp(args[0],args);
Add Comment
Please, Sign In to add comment