1
0

added more code

This commit is contained in:
Andrew W
2022-08-28 16:12:16 -05:00
parent 5a2894ed1b
commit 7dabaef6f6
2345 changed files with 1343530 additions and 0 deletions

BIN
CPE435/Lab3/Lab03.pdf Normal file

Binary file not shown.

BIN
CPE435/Lab3/Study_Lab03.pdf Normal file

Binary file not shown.

Binary file not shown.

26
CPE435/Lab3/a.txt Normal file
View File

@ -0,0 +1,26 @@
total 1.2M
drwx------ 2 anw0044 student 4.0K Feb 8 20:59 .
drwx------ 7 anw0044 student 4.0K Feb 1 16:17 ..
-rw------- 1 anw0044 student 0 Feb 8 21:00 a.txt
-rw------- 1 anw0044 student 189 Feb 8 20:17 a.txt
-rwx------ 1 anw0044 student 8.3K Feb 6 21:55 demo1
-rw------- 1 anw0044 student 1.6K Feb 7 19:31 demo1.c
-rwx------ 1 anw0044 student 9.8K Feb 8 09:12 demo2
-rw------- 1 anw0044 student 1.3K Feb 8 09:12 demo2.c
-rwx------ 1 anw0044 student 8.6K Feb 6 21:56 demo3
-rw------- 1 anw0044 student 1.7K Feb 7 19:31 demo3.c
-rwx------ 1 anw0044 student 8.4K Feb 7 01:09 demo4
-rw------- 1 anw0044 student 1.7K Feb 8 01:15 demo4.c
-rwx------ 1 anw0044 student 8.5K Feb 6 21:56 demo5
-rw------- 1 anw0044 student 1.3K Feb 7 19:31 demo5.c
-rwx------ 1 anw0044 student 8.4K Feb 8 02:17 example2
-rw------- 1 anw0044 student 271 Jan 27 16:19 example2.c
-rwx------ 1 anw0044 student 8.5K Feb 8 02:17 example3
-rw------- 1 anw0044 student 536 Jan 27 16:19 example3.c
-rw------- 1 anw0044 student 121K Jan 27 16:19 Lab03.pdf
-rwx------ 1 anw0044 student 17K Feb 8 20:59 shell
-rw------- 1 anw0044 student 7.4K Feb 8 20:58 shell.c
-rw------- 1 anw0044 student 417K Jan 27 16:19 Study_Lab03.pdf
-rw------- 1 anw0044 student 482K Jan 27 16:19 Study_Lab03.pptx
-rw------- 1 anw0044 student 94 Feb 8 09:12 test.txt

BIN
CPE435/Lab3/demo1 Executable file

Binary file not shown.

44
CPE435/Lab3/demo1.c Normal file
View File

@ -0,0 +1,44 @@
/*
Written By: Prawar Poudel
This program is intended to showcase the use of strtok() function
Please study about the strtok function first and compare the three outputs that you will receive here
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char* argv[])
{
printf("Demo Number 1\n");
printf("================\n");
char myString[100] = "i,want to break,this string using, both,comma and space";
//following is the temporary string that I want to keep my char[] read from breaking the above char[] myString
//following breaks based on space character
char *temp;
temp = strtok(myString," "); //include <space> inside ""
while(temp!=NULL)
{
printf("%s\n",temp);
temp = strtok(NULL," "); //include <space> inside ""
}
//following breaks based on comma character
printf("\n\nDemo Number 2\n");
printf("================\n");
strcpy(myString,"i,want to break,this string using, both,comma and space");
temp = strtok(myString,","); //include , inside ""
while(temp!=NULL)
{
printf("%s\n",temp);
temp = strtok(NULL,","); //include , inside ""
}
//following breaks based on space or comma character
printf("\n\nDemo Number 3\n");
printf("================\n");
strcpy(myString,"i,want to break,this string using, both,comma and space");
temp = strtok(myString,", "); //include both space and , inside "" while(temp!=NULL)
while(temp!=NULL)
{
printf("%s\n",temp);
temp = strtok(NULL,", "); //include both space and , inside ""
}
return 0;
}

BIN
CPE435/Lab3/demo2 Executable file

Binary file not shown.

33
CPE435/Lab3/demo2.c Normal file
View File

@ -0,0 +1,33 @@
/*
Written By: Prawar Poudel
This program is supposed to demonstrate the execution of dup2() function
Please read the manual page first before jumping to run this program
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <fcntl.h>
int main()
{
printf("You would expect this to go to your stdout, and it does\n");
//we will create a file using open function
char myFileName[] = "test.txt";
//lets open the file by the name of test.txt
int myDescriptor = open(myFileName,O_CREAT|O_RDWR|O_TRUNC,0644);
int id;
//creating a child that redirects the stdout to test.txt
// you can use similar functionality for '>' operator
if((id=fork())==0)
{
//lets call dup2 so that out stdout (second argument) is now copied to (points to) test.txt (first argument)
// what this essentially means is that anything that you send to stdout will be sent to myDescriptor
dup2(myDescriptor,1); //1 is stdout, 0 is stdin and 2 is stderr
printf("You would expect this to go to your stdout, but since we called dup2, this will go to test.txt");
close(myDescriptor);
exit(0);
}else
wait(0);
printf("This is also printed to the console\n");
return 0;
}

BIN
CPE435/Lab3/demo3 Executable file

Binary file not shown.

49
CPE435/Lab3/demo3.c Normal file
View File

@ -0,0 +1,49 @@
/*
Written By: Prawar Poudel
This program demonstrates the use of pipe() function in C
Please man pipe and have understanding before going through this code
Pipe passes information from one process to another, similar to water-pipes there is a read-end and a write-end of pipe
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/wait.h>
int main()
{
int myPipingDescriptors[2];
if(pipe(myPipingDescriptors)==-1)
{
printf("Error in calling the piping function\n");
exit(0);
}
//at this point two pipe ends are created
// one is the read end and other is write end
// [0] will be the read end, [1] will be the write end
//now lets fork two process where one will make use of the read end and other will make
// use of write end
// they can communicate this way through the pipe
int id;
if((id=fork())==0)
{
dup2(myPipingDescriptors[1],1); //second argument 1 is stdout
close(myPipingDescriptors[0]); //read end is unused to lets close it
//this following statement will not be printed since we have copied the stdout to write end of pipe
printf("I am child, and sending this message.\n");
exit(0);
}else if (id>0)
{
wait(0);
char myRead[100];
//basically what's written to the write-end of pipe stays there until we read the read-end of pipe
read(myPipingDescriptors[0],myRead,37);
printf("I am parent. I read following statement\n\t%s\n",myRead); close(myPipingDescriptors[1]);
}else
{
printf("Failed to fork so terminating the process\n");
exit(-1);
}
close(myPipingDescriptors[0]);
close(myPipingDescriptors[1]);
return 0;
}

BIN
CPE435/Lab3/demo4 Executable file

Binary file not shown.

46
CPE435/Lab3/demo4.c Normal file
View File

@ -0,0 +1,46 @@
/*
Written By: Prawar Poudel
execvp runs a program that you pass as argument
Please study about the execvp function before going to run this program
After you understand the things, please run and watch them
Please make sure that execvp has the right executable provided
*/
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/wait.h>
int main()
{
char myArgument[100];
//you can change the content of myArgument using any user typed input using gets()
strcpy(myArgument,"date");
//execvp expects the arguments to be provided as char[][]
//so please make sure you understand strtok before coming here
//we will use strtok() to break the sequence of command and argument in myArgument to convert to char[][]
char* myBrokenArgs[10]; //this will hold the values after we tokenize
printf("Starting tokenization...\n");
myBrokenArgs[0] = strtok(myArgument," ");
int counter = 0;
while(myBrokenArgs[counter]!=NULL)
{
counter+=1;
myBrokenArgs[counter] = strtok(NULL," ");
}
myBrokenArgs[counter] = NULL;
printf("\ttokenization complete....\n\nNow executing using execvp\n");
printf("Following will be the output of execvp\n");
printf("=======================================\n");
int id;
//I will spawn a child that will run my execvp command
if((id=fork())==0)
execvp(myBrokenArgs[0],myBrokenArgs);
else if(id<0)
printf("Failed to make child...\n");
else
{
//parent shall wait until the child is killed
wait(0);
printf("=======================================\n"); printf("Completed execution\n");
}
return 0;
}

BIN
CPE435/Lab3/demo5 Executable file

Binary file not shown.

57
CPE435/Lab3/demo5.c Normal file
View File

@ -0,0 +1,57 @@
/*
program runs "ls | sort" command using 2 child processes
dup2 is used to duplicate an old file descriptor into a new one
normal file descriptor table
0 [ standard input ]
1 [ standard output ]
2 [ standard error ]
pipe is used to communicate between child processes
unused ends of the pipe should be closed if unused
in that process
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int fds[2];
pipe(fds);
// Child 1 will duplicate downstream into stdin
if(fork() == 0)
{
dup2(fds[0], 0); // normally 0 is for stdin
// will now read to fds[0]
// end of pipe
close(fds[1]); // close other end of pipe
execlp("sort", "sort", 0);
//execlp("wc", "wc", "-l", 0); // Note the first argument is the command
// After it are the arguments including
// original command
}
// Child2 duplicates upstream into stdout
else if (fork() == 0)
{
dup2(fds[1], 1); // normally 1 is for stdout
// will now write to fds[1]
// end of pipe
close(fds[0]); // close other end of pipe
execlp("ls", "ls", 0);
}
// Parent
else
{
close(fds[0]);
close(fds[1]);
wait(0);
wait(0);
}
}

BIN
CPE435/Lab3/example2 Executable file

Binary file not shown.

14
CPE435/Lab3/example2.c Normal file
View File

@ -0,0 +1,14 @@
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/wait.h>
int main()
{
char *cmd[] = {"who", "ls", "date"};
int i;
printf("0=who, 1=ls, 2=date :");
scanf("%d", &i);
execlp(cmd[i], cmd[i], 0);
printf("command not found\n"); /*exec failed*/
}

BIN
CPE435/Lab3/example3 Executable file

Binary file not shown.

28
CPE435/Lab3/example3.c Normal file
View File

@ -0,0 +1,28 @@
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
char *cmd[] = {"who", "ls", "date", "notacommand"};
int i;
while(1)
{
printf("0=who, 1=ls, 2=date, 3=notacommand :");
scanf("%d", &i);
if(fork() == 0)
{
execlp(cmd[i], cmd[i], 0); // If execlp runs correctly, control
// is transfer to the new program
printf("command not found\n"); // exec failed
exit(1);
}
else
{
// Parent waits for child process to complete
wait(0);
}
}
}

BIN
CPE435/Lab3/shell Executable file

Binary file not shown.

278
CPE435/Lab3/shell.c Normal file
View File

@ -0,0 +1,278 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
char* substr(const char *src, int m, int n);
void pipeFunc(char *command1, char *command2);
int main()
{
while (1)
{
printf("My Shell\n");
printf(">> ");
//following is the temporary string that I want to keep my char[] read from breaking the above char[] myString
//following breaks based on space character
char commandStr[100], commandStrFull[100];
char *temp;
char *temp2;
char temp3[100];
//strcpy(commandStr, "exit");
fgets(commandStr, 100, stdin);
//strcpy(commandStr, "ls -l > a.txt");
strcpy(commandStrFull,commandStr);
char *filename;
int flag = 0;
// exit program if command is exit
if(strcmp(commandStr, "exit\n")==0)
{
exit(-1);
}
int redir=0;
int redirIndex;
int pipeNum=0;
int single=0;
char* redirCheckTwo=NULL;
char* command2;
char* command1;
char* pipeCheck = strchr(commandStr, '|');
char* redirCheck = strchr(commandStr, '>');
if(pipeCheck!=NULL)
{
pipeNum=1;
redirCheckTwo = strchr(commandStr, '>');
command1 = strrchr(commandStr, '|');
redirIndex = strcspn(commandStr, ">");
}
else single=1;
if (redirCheckTwo!=NULL)
{
command1 = strrchr(commandStr, '|');
redir=1;
}
if(redirCheck!=NULL) redir=1;
//commandStr[strcspn(commandStr, "\n")] = 0;
// pipe or redirect
if (pipeNum==1 || redir==1)
{
temp = strtok(commandStr," "); //include , inside ""
while(temp!=NULL)
{
//printf("%s\n",temp);
if (strcmp(temp,">")==0 && redir==1)
{
filename = strtok(NULL, " ");
//printf("Filename: %s\n", filename);
}
if (strcmp(temp,"|")==0 && pipeCheck!=NULL)
{
command2 = strtok(NULL," >");
//puts(strchr(temp, '|'));
flag=1;
}
if (pipeNum==1 || redir==1)
{
temp = strtok(NULL," "); //include , inside ""
}
else temp2 = strtok(NULL," "); //include , inside ""
}
}
// single command is run
if(single==1 && pipeNum==0 && redir==0){
commandStr[strcspn(commandStr, "\n")] = 0;
//strcpy(temp,commandStr);
//execvp expects the arguments to be provided as char[][]
//so please make sure you understand strtok before coming here
//we will use strtok() to break the sequence of command and argument in myArgument to convert to char[][]
char* myBrokenArgs[10]; //this will hold the values after we tokenize
//printf("Starting tokenization...\n");
myBrokenArgs[0] = strtok(commandStr," ");
int counter = 0;
while(myBrokenArgs[counter]!=NULL)
{
counter+=1;
myBrokenArgs[counter] = strtok(NULL," ");
}
myBrokenArgs[counter] = NULL;
//printf("\ttokenization complete....\n\nNow executing using execvp\n");
//printf("Following will be the output of execvp\n");
//printf("=======================================\n");
if ((strcmp("ls",myBrokenArgs[0]) ==0) && single==1)
{
int id;
//I will spawn a child that will run my execvp command
if((id=fork())==0)
execvp(myBrokenArgs[0],myBrokenArgs);
else if(id<0)
printf("Failed to make child...\n");
else
{
//parent shall wait until the child is killed
wait(0);
printf("=======================================\n");
printf("Completed execution\n");
}
}
else if (strcmp("date",myBrokenArgs[0]) ==0)
{
int id;
//I will spawn a child that will run my execvp command
if((id=fork())==0)
execvp(myBrokenArgs[0],myBrokenArgs);
else if(id<0)
printf("Failed to make child...\n");
else
{
//parent shall wait until the child is killed
wait(0);
printf("=======================================\n");
printf("Completed execution\n");
}
}
else if ((strcmp("ps",myBrokenArgs[0]) ==0))
{
int id;
//I will spawn a child that will run my execvp command
if((id=fork())==0)
execvp(myBrokenArgs[0],myBrokenArgs);
else if(id<0)
printf("Failed to make child...\n");
else
{
//parent shall wait until the child is killed
wait(0);
printf("=======================================\n");
printf("Completed execution\n");
}
}
}
// pipe is used
if (pipeNum==1 && redirCheck==NULL && single==0)
{
strcpy(commandStrFull, "ls -lah | wc -l");
int size = strcspn(commandStrFull, "|");
int end = strcspn(commandStrFull, "\0");
int beg = strcspn(commandStrFull, "|");
char* command2 = substr(commandStrFull, beg+2, end);
end = strcspn(commandStrFull, "|");
char* command1 = substr(commandStrFull, 0, end-1);
char* command1Args[10]; //this will hold the values after we tokenize
//printf("Starting tokenization...\n");
command1Args[0] = strtok(command1," ");
int counter = 0;
while(command1Args[counter]!=NULL)
{
counter+=1;
command1Args[counter] = strtok(NULL,"\0");
}
command1Args[counter] = NULL;
char* command2Args[10]; //this will hold the values after we tokenize
//printf("Starting tokenization...\n");
command2Args[0] = strtok(command2," ");
counter = 0;
while(command2Args[counter]!=NULL)
{
counter+=1;
command2Args[counter] = strtok(NULL," ");
}
command2Args[counter] = NULL;
int fds[2];
pipe(fds);
// Child 1 will duplicate downstream into stdin
if(fork() == 0)
{
dup2(fds[0], 0); // normally 0 is for stdin
// will now read to fds[0]
// end of pipe
close(fds[1]); // close other end of pipe
execlp(command2Args[0] ,command2Args[0], command2Args, 0);
//execlp("wc", "wc", "-l", 0); // Note the first argument is the command
// After it are the arguments including
// original command
}
// Child2 duplicates upstream into stdout
else if (fork() == 0)
{
dup2(fds[1], 1); // normally 1 is for stdout
// will now write to fds[1]
// end of pipe
close(fds[0]); // close other end of pipe
execlp(command1Args[0],command1Args[0], command1Args, 0);
}
// Parent
else
{
close(fds[0]);
close(fds[1]);
wait(0);
wait(0);
}
}
if (pipeNum==0 && redir==1)
{
int end = strcspn(commandStrFull, ">");
int beg = strcspn(commandStrFull, "|");
char* command1 = substr(commandStrFull, 0, end-1);
char* myBrokenArgs[10]; //this will hold the values after we tokenize
printf("Starting tokenization...\n");
myBrokenArgs[0] = strtok(command1," ");
int counter = 0;
while(myBrokenArgs[counter]!=NULL)
{
counter+=1;
myBrokenArgs[counter] = strtok(NULL," ");
}
myBrokenArgs[counter] = NULL;
int myDescriptor = open(filename,O_CREAT|O_RDWR|O_TRUNC,0644);
int id;
//creating a child that redirects the stdout to test.txt
// you can use similar functionality for '>' operator
if((id=fork())==0)
{
//lets call dup2 so that out stdout (second argument) is now copied to (points to) test.txt (first argument)
// what this essentially means is that anything that you send to stdout will be sent to myDescriptor
dup2(myDescriptor,1); //1 is stdout, 0 is stdin and 2 is stderr
execvp(myBrokenArgs[0],myBrokenArgs);
close(myDescriptor);
exit(0);
}else
wait(0);
printf("Completed execution.\n");
}
}
return 0;
}
void pipeFunc(char *command1, char *command2)
{
}
char* substr(const char *src, int m, int n)
{
// get the length of the destination string
int len = n - m;
// allocate (len + 1) chars for destination (+1 for extra null character)
char *dest = (char*)malloc(sizeof(char) * (len + 1));
// start with m'th char and copy `len` chars into the destination
strncpy(dest, (src + m), len);
// return the destination string
return dest;
}

1
CPE435/Lab3/test.txt Normal file
View File

@ -0,0 +1 @@
You would expect this to go to your stdout, but since we called dup2, this will go to test.txt