SlideShare a Scribd company logo
1 of 8
Download to read offline
int ch=chdir(tokensleft[0]);
/*if the change of directory was successful it will print successful otherwise it will print not
successful*/
if(ch<0)
perror("chdir change of directory not successfuln");
else
printf("chdir change of directory successfuln");
return "Command 'chdir' was receivedn";
}
char * fAccess(char *cmd,char *tokensleft[])
{
int exists =0;
for(int i=0;tokensleft[i]; i++) {
exists =0;
if(access(tokensleft[i],F_OK)==0){
exists = 1;
printf("file %s existsn",tokensleft[i]);
}else{
printf("file %s does not existsn",tokensleft[i]);
}
if (exists == 1){
if(access(tokensleft[i],R_OK)==0) {
printf("file %s is readablen",tokensleft[i]);
}else{printf("file %s is not readablen",tokensleft[i]);}
if(access(tokensleft[i],W_OK)==0) {
printf("file %s is writeablen",tokensleft[i]);
}else{
printf("file %s is not writeablen",tokensleft[i]);
}
if(access(tokensleft[i],X_OK)==0) {
printf("file %s is executeablen",tokensleft[i]);
}else{
printf("file %s is not executeablen",tokensleft[i]);
}
}
}
return "Command 'acsess' was receivedn";
}
char * fChmod(char *cmd,char *tokensleft[])
{
unsigned int octalPerm;
sscanf(tokensleft[0],"%o",&octalPerm);
for(int i=1;tokensleft[i]; i++) {
if(chmod(tokensleft[i],octalPerm)==0 ){
chmod(tokensleft[i],octalPerm);
}else{
printf("Error: %s n",strerror(errno));
}
}
return "Command 'chmod' was received";
}
char * fLn(char *cmd,char *tokensleft[])
{
int opt;
int force = 0;
int symbolic = 0;
int argc =0;
for (int i = 0; tokensleft[i]; i++) {
argc++;
}
while ((opt = getopt(argc, tokensleft, "sf")) != -1) {
switch (opt) {
case 's':
symbolic = 1;
break;
case 'f':
force = 1;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-s] [-f] file1 file2n", tokensleft[0]);
}
}
if (argc - optind != 2) {
fprintf(stderr, "Usage: %s [-s] [-f] file1 file2n", tokensleft[0]);
}
const char *file1 = tokensleft[optind];
const char *file2 = tokensleft[optind+1];
if (force) {
remove(file2);
}
if (symbolic) {
link(file1, file2);
} else {
if (link(file1, file2) != 0) {
perror("Error creating hard link");
}
}
return "Command 'ln' was received";
}
char * fUnset(char *cmd, char *tokensleft[])
{
unsetenv(tokensleft[0]);
return "Command 'unset' was received";
}
char * fStat(char *cmd, char *tokensleft[])
{
struct stat info;
for(int i=0;tokensleft[i]; i++) {
stat(tokensleft[i], &info);
struct passwd *pw = getpwuid(info.st_uid);
struct group *gr = getgrgid(info.st_gid);
printf("File: %s",tokensleft[i]);
printf ("Owner: %s",pw->pw_name);
printf ("Group: %s",gr->gr_name);
}
return "Command 'stat' was received";
}
char * fPartial(char *cmd, char *tokensleft[])
{
struct passwd *pw;
setpwent();
char *result = malloc(1024);
result[0] = '0';
while ((pw = getpwent()) != NULL) {
if (strcasestr(pw->pw_name, tokensleft[0]) || strcasestr(pw->pw_gecos, tokensleft[0])) {
char *entry = malloc(strlen(pw->pw_name) + 2);
sprintf(entry, "%s,", pw->pw_name);
strcat(result, entry);
free(entry);
}
}
endpwent(); // close the password file
if (strlen(result) == 0) {
strcpy(result, "No matching users found.");
}
printf("Match Found: %s n",result);
return "Command 'partial' was received";
}
char * fshowTime(char *cmd, char *tokensleft[])
{
system(setenv("DISPLAY_FORMAT","%A, %B, %d, %y, %I:%M %p",1));
system(setenv("PARSE_FORMAT","%Y/%m/%d %H:%M:%S",1));
time_t rawtime;
struct tm *info;
char buffer[80];
time(&rawtime);
info = localtime(&rawtime);
if (!tokensleft[0]){
strftime(buffer,80,"%x - %I:%M%p", info);
printf("Current date & time : |%s|n", buffer );
}else{
}
return "Command 'showTime' was received";
}
char *commands[18]={"export","chdir","access","chmod",
"path","touch","ln","unset","stat","partial","showTime"} ;
char
*(*methods[18])()={fExport,fChdir,fAccess,fChmod,fPath,fTouch,fLn,fUnset,fStat,fPartial,fsho
wTime};
//Alternate declaration
struct CMDSTRUCT {
char *cmd;
char *(*method)();
}
cmdStruct[]={{"justin",fshowTime},{"paul",fPartial},{"sammy",fStat},{"fred",fExport},{"mary
",fChdir},{"clark",fAccess},{"sonia",fChmod},{"carlo",fPath},{"lalo",fTouch},{"samuel",fLn},
{"olga",fUnset},{NULL,NULL}} ;
char *interpret(char *cmdline)
{
char **tokens;
char *cmd;
int i;
char *result;
char sysCommand;
tokens=history_tokenize(cmdline);
if(!tokens) return "no response needed";
cmd=tokens[0];
//Detecting commands: table lookup: 2 techniques
//Using the parallel arrays to look up function calls
for(i=0;commands[i];i++)
{
if(strcasecmp(cmd,commands[i])==0) return (methods[i])(cmd,&tokens[1]);
}
for(i=0;cmdStruct[i].cmd;i++)
if(strcasecmp(cmd,cmdStruct[i].cmd)==0) return (cmdStruct[i].method)(cmd,&tokens[1]);
sysCommand=system(cmdline);
if (sysCommand==-1){
return"command status: fail";
}else if (sysCommand==0){
return"command status: sucsess";}
}
int main(int argc, char * argv[],char * envp[])
{
char cmd[100];
char *cmdLine;
char *expansion;
time_t now=time(NULL);
int nBytes; //size of msg rec'd
char cwd[PATH_MAX];
signal(SIGINT,ctrlCHandler);
read_history("shell.log");
add_history(ctime(&now));
fprintf(stdout,"Starting the shell at: %sn",ctime(&now));
while(true) {
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working dir: %sn", cwd);
} else {
perror("getcwd() error");
return 1;
}
cmdLine=readline("Enter a command: ");
if(!cmdLine) break;
history_expand(cmdLine,&expansion);
add_history(expansion);
if(strcasecmp(cmdLine,"bye")==0) break;
char *response=interpret(cmdLine);
fprintf(stdout,"%sn",response);
}
write_history("shell.log");
system("echo Your session history is; cat -n shell.log");
fprintf(stdout,"Server is now terminated n");
return 0;
} SIGHUP, Set up SIGIO as a signal to be ignored. (6 marks) a. The initial handler you should
write for each of them should be stub routines that output a message: "Signal # (SIGxxxx)
received in function _FUNCTION_. Use strsignal to output the name of the signal using dprintf
to send the output to a file. (The tail -f & command will be demonstrated to allow you to follow
text output to a file while a program is running. Take notes!) Display the name of the file, when
it was compiled and the line # of the output message. (2 marks) b. Test all of the above signal
handlers. (2 marks) i. Verify that all of your signal handlers work sending your command servers
each of the above signals from a 2nd terminal. ii. To set up a log file to record the output of
dprintf(fd, fmtstr, argsI) use one of more of the following before running your command server:
exec fd>/dev/tty #isplays the messages on the current terminal exec fd>/dev/pts/n #display on a
different terminal that you own exec fd>logfile #writes to a file tail -f logfile & #displays new
data as it is appended to logfile tail -f logfile >/dev/pts/n & #displays new data on an alternate
terminal Hand in a log file (as opposed to a screen shot) showing that you tested all of the signals
c. In a 3rd terminal attach strace to the pid of your command server. (2) i. Send each of the 4
signals to the pid of the command server. How does strace respond to each signal. Summarize
the result. ii. Send each of the 4 signals to the parent pid of the command server. Answer the
same question and hilite/describe any difference.

More Related Content

Similar to int ch=chdir(tokensleft[0]); if the change of director.pdf

So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfezonesolutions
 
IO redirection in C shellPlease implement input output redirect.pdf
IO redirection in C shellPlease implement input  output redirect.pdfIO redirection in C shellPlease implement input  output redirect.pdf
IO redirection in C shellPlease implement input output redirect.pdfforecastfashions
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Yandex
 
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdfAdrianEBJKingr
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsashukiller7
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservationSwarup Kumar Boro
 
Railway reservation
Railway reservationRailway reservation
Railway reservationSwarup Boro
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 WorldFabien Potencier
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
RAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMRAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMKrishna Raj
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxadampcarr67227
 
Simple API for XML
Simple API for XMLSimple API for XML
Simple API for XMLguest2556de
 
file handling final3333.pptx
file handling final3333.pptxfile handling final3333.pptx
file handling final3333.pptxradhushri
 

Similar to int ch=chdir(tokensleft[0]); if the change of director.pdf (20)

So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
IO redirection in C shellPlease implement input output redirect.pdf
IO redirection in C shellPlease implement input  output redirect.pdfIO redirection in C shellPlease implement input  output redirect.pdf
IO redirection in C shellPlease implement input output redirect.pdf
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
 
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
Railway reservation
Railway reservationRailway reservation
Railway reservation
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
Python build your security tools.pdf
Python build your security tools.pdfPython build your security tools.pdf
Python build your security tools.pdf
 
Anti patterns
Anti patternsAnti patterns
Anti patterns
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
RAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMRAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAM
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
 
Railwaynew
RailwaynewRailwaynew
Railwaynew
 
Simple API for XML
Simple API for XMLSimple API for XML
Simple API for XML
 
Rust-lang
Rust-langRust-lang
Rust-lang
 
File management
File managementFile management
File management
 
file handling final3333.pptx
file handling final3333.pptxfile handling final3333.pptx
file handling final3333.pptx
 
C Programming Project
C Programming ProjectC Programming Project
C Programming Project
 

More from mdualudin007

(5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
 (5 points 1. Which of the followhy statements munt be trie 2. What i.pdf (5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
(5 points 1. Which of the followhy statements munt be trie 2. What i.pdfmdualudin007
 
(4) With regard to virulence factors, what do the letters O,H and K.pdf
 (4) With regard to virulence factors, what do the letters O,H and K.pdf (4) With regard to virulence factors, what do the letters O,H and K.pdf
(4) With regard to virulence factors, what do the letters O,H and K.pdfmdualudin007
 
(4 points) Match the following data with the correct level of measure.pdf
 (4 points) Match the following data with the correct level of measure.pdf (4 points) Match the following data with the correct level of measure.pdf
(4 points) Match the following data with the correct level of measure.pdfmdualudin007
 
(3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
 (3) If events occur accordiy to a poidson porles with hate =10minnt..pdf (3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
(3) If events occur accordiy to a poidson porles with hate =10minnt..pdfmdualudin007
 
(4 marks) Suppose the production function of the fishing industry is .pdf
 (4 marks) Suppose the production function of the fishing industry is .pdf (4 marks) Suppose the production function of the fishing industry is .pdf
(4 marks) Suppose the production function of the fishing industry is .pdfmdualudin007
 
(3 points) Employee is a derived class of Person, and Worker is a der.pdf
 (3 points) Employee is a derived class of Person, and Worker is a der.pdf (3 points) Employee is a derived class of Person, and Worker is a der.pdf
(3 points) Employee is a derived class of Person, and Worker is a der.pdfmdualudin007
 
(1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
 (1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf (1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
(1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdfmdualudin007
 
() Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
 () Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf () Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
() Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdfmdualudin007
 
(1 Point) Please match the following variable properties with their d.pdf
 (1 Point) Please match the following variable properties with their d.pdf (1 Point) Please match the following variable properties with their d.pdf
(1 Point) Please match the following variable properties with their d.pdfmdualudin007
 
(1 point) If samples of size 37 are taken from a population with mean.pdf
 (1 point) If samples of size 37 are taken from a population with mean.pdf (1 point) If samples of size 37 are taken from a population with mean.pdf
(1 point) If samples of size 37 are taken from a population with mean.pdfmdualudin007
 
Bag breaks open; included as delay in the allowance factor Conveyo.pdf
  Bag breaks open; included as delay in the allowance factor  Conveyo.pdf  Bag breaks open; included as delay in the allowance factor  Conveyo.pdf
Bag breaks open; included as delay in the allowance factor Conveyo.pdfmdualudin007
 
Prove the following E(MSR)=2+12(Xix)2.pdf
  Prove the following E(MSR)=2+12(Xix)2.pdf  Prove the following E(MSR)=2+12(Xix)2.pdf
Prove the following E(MSR)=2+12(Xix)2.pdfmdualudin007
 
Computer Architecture HOW do I design a computer = Instruction .pdf
  Computer Architecture HOW do I design a computer  = Instruction .pdf  Computer Architecture HOW do I design a computer  = Instruction .pdf
Computer Architecture HOW do I design a computer = Instruction .pdfmdualudin007
 
( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
 ( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf ( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
( Ch2 ISA)3. Assume that the top of the stack in a progra.pdfmdualudin007
 
The goal of this assignment is to get familiar with pipelining in C.pdf
  The goal of this assignment is to get familiar with pipelining in C.pdf  The goal of this assignment is to get familiar with pipelining in C.pdf
The goal of this assignment is to get familiar with pipelining in C.pdfmdualudin007
 
( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
 ( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf ( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
( 20 points) Benzene is a common groundwater contaminant. Based on a .pdfmdualudin007
 
(2) Solve the following recurrence relation by using Recursion Trees .pdf
 (2) Solve the following recurrence relation by using Recursion Trees .pdf (2) Solve the following recurrence relation by using Recursion Trees .pdf
(2) Solve the following recurrence relation by using Recursion Trees .pdfmdualudin007
 
(20 pts) Let X and Y be two statistically independent random variable.pdf
 (20 pts) Let X and Y be two statistically independent random variable.pdf (20 pts) Let X and Y be two statistically independent random variable.pdf
(20 pts) Let X and Y be two statistically independent random variable.pdfmdualudin007
 
(2 pts.) Consider the following ordered list of data, x and a key.pdf
 (2 pts.) Consider the following ordered list of data,  x  and a key.pdf (2 pts.) Consider the following ordered list of data,  x  and a key.pdf
(2 pts.) Consider the following ordered list of data, x and a key.pdfmdualudin007
 
(2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
 (2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf (2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
(2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdfmdualudin007
 

More from mdualudin007 (20)

(5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
 (5 points 1. Which of the followhy statements munt be trie 2. What i.pdf (5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
(5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
 
(4) With regard to virulence factors, what do the letters O,H and K.pdf
 (4) With regard to virulence factors, what do the letters O,H and K.pdf (4) With regard to virulence factors, what do the letters O,H and K.pdf
(4) With regard to virulence factors, what do the letters O,H and K.pdf
 
(4 points) Match the following data with the correct level of measure.pdf
 (4 points) Match the following data with the correct level of measure.pdf (4 points) Match the following data with the correct level of measure.pdf
(4 points) Match the following data with the correct level of measure.pdf
 
(3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
 (3) If events occur accordiy to a poidson porles with hate =10minnt..pdf (3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
(3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
 
(4 marks) Suppose the production function of the fishing industry is .pdf
 (4 marks) Suppose the production function of the fishing industry is .pdf (4 marks) Suppose the production function of the fishing industry is .pdf
(4 marks) Suppose the production function of the fishing industry is .pdf
 
(3 points) Employee is a derived class of Person, and Worker is a der.pdf
 (3 points) Employee is a derived class of Person, and Worker is a der.pdf (3 points) Employee is a derived class of Person, and Worker is a der.pdf
(3 points) Employee is a derived class of Person, and Worker is a der.pdf
 
(1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
 (1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf (1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
(1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
 
() Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
 () Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf () Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
() Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
 
(1 Point) Please match the following variable properties with their d.pdf
 (1 Point) Please match the following variable properties with their d.pdf (1 Point) Please match the following variable properties with their d.pdf
(1 Point) Please match the following variable properties with their d.pdf
 
(1 point) If samples of size 37 are taken from a population with mean.pdf
 (1 point) If samples of size 37 are taken from a population with mean.pdf (1 point) If samples of size 37 are taken from a population with mean.pdf
(1 point) If samples of size 37 are taken from a population with mean.pdf
 
Bag breaks open; included as delay in the allowance factor Conveyo.pdf
  Bag breaks open; included as delay in the allowance factor  Conveyo.pdf  Bag breaks open; included as delay in the allowance factor  Conveyo.pdf
Bag breaks open; included as delay in the allowance factor Conveyo.pdf
 
Prove the following E(MSR)=2+12(Xix)2.pdf
  Prove the following E(MSR)=2+12(Xix)2.pdf  Prove the following E(MSR)=2+12(Xix)2.pdf
Prove the following E(MSR)=2+12(Xix)2.pdf
 
Computer Architecture HOW do I design a computer = Instruction .pdf
  Computer Architecture HOW do I design a computer  = Instruction .pdf  Computer Architecture HOW do I design a computer  = Instruction .pdf
Computer Architecture HOW do I design a computer = Instruction .pdf
 
( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
 ( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf ( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
 
The goal of this assignment is to get familiar with pipelining in C.pdf
  The goal of this assignment is to get familiar with pipelining in C.pdf  The goal of this assignment is to get familiar with pipelining in C.pdf
The goal of this assignment is to get familiar with pipelining in C.pdf
 
( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
 ( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf ( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
 
(2) Solve the following recurrence relation by using Recursion Trees .pdf
 (2) Solve the following recurrence relation by using Recursion Trees .pdf (2) Solve the following recurrence relation by using Recursion Trees .pdf
(2) Solve the following recurrence relation by using Recursion Trees .pdf
 
(20 pts) Let X and Y be two statistically independent random variable.pdf
 (20 pts) Let X and Y be two statistically independent random variable.pdf (20 pts) Let X and Y be two statistically independent random variable.pdf
(20 pts) Let X and Y be two statistically independent random variable.pdf
 
(2 pts.) Consider the following ordered list of data, x and a key.pdf
 (2 pts.) Consider the following ordered list of data,  x  and a key.pdf (2 pts.) Consider the following ordered list of data,  x  and a key.pdf
(2 pts.) Consider the following ordered list of data, x and a key.pdf
 
(2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
 (2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf (2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
(2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
 

Recently uploaded

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

int ch=chdir(tokensleft[0]); if the change of director.pdf

  • 1. int ch=chdir(tokensleft[0]); /*if the change of directory was successful it will print successful otherwise it will print not successful*/ if(ch<0) perror("chdir change of directory not successfuln"); else printf("chdir change of directory successfuln"); return "Command 'chdir' was receivedn"; } char * fAccess(char *cmd,char *tokensleft[]) { int exists =0; for(int i=0;tokensleft[i]; i++) { exists =0; if(access(tokensleft[i],F_OK)==0){ exists = 1; printf("file %s existsn",tokensleft[i]); }else{ printf("file %s does not existsn",tokensleft[i]); } if (exists == 1){ if(access(tokensleft[i],R_OK)==0) { printf("file %s is readablen",tokensleft[i]); }else{printf("file %s is not readablen",tokensleft[i]);} if(access(tokensleft[i],W_OK)==0) { printf("file %s is writeablen",tokensleft[i]); }else{ printf("file %s is not writeablen",tokensleft[i]);
  • 2. } if(access(tokensleft[i],X_OK)==0) { printf("file %s is executeablen",tokensleft[i]); }else{ printf("file %s is not executeablen",tokensleft[i]); } } } return "Command 'acsess' was receivedn"; } char * fChmod(char *cmd,char *tokensleft[]) { unsigned int octalPerm; sscanf(tokensleft[0],"%o",&octalPerm); for(int i=1;tokensleft[i]; i++) { if(chmod(tokensleft[i],octalPerm)==0 ){ chmod(tokensleft[i],octalPerm); }else{ printf("Error: %s n",strerror(errno)); } } return "Command 'chmod' was received"; } char * fLn(char *cmd,char *tokensleft[]) { int opt; int force = 0; int symbolic = 0; int argc =0;
  • 3. for (int i = 0; tokensleft[i]; i++) { argc++; } while ((opt = getopt(argc, tokensleft, "sf")) != -1) { switch (opt) { case 's': symbolic = 1; break; case 'f': force = 1; break; default: /* '?' */ fprintf(stderr, "Usage: %s [-s] [-f] file1 file2n", tokensleft[0]); } } if (argc - optind != 2) { fprintf(stderr, "Usage: %s [-s] [-f] file1 file2n", tokensleft[0]); } const char *file1 = tokensleft[optind]; const char *file2 = tokensleft[optind+1]; if (force) { remove(file2); } if (symbolic) { link(file1, file2); } else { if (link(file1, file2) != 0) { perror("Error creating hard link"); }
  • 4. } return "Command 'ln' was received"; } char * fUnset(char *cmd, char *tokensleft[]) { unsetenv(tokensleft[0]); return "Command 'unset' was received"; } char * fStat(char *cmd, char *tokensleft[]) { struct stat info; for(int i=0;tokensleft[i]; i++) { stat(tokensleft[i], &info); struct passwd *pw = getpwuid(info.st_uid); struct group *gr = getgrgid(info.st_gid); printf("File: %s",tokensleft[i]); printf ("Owner: %s",pw->pw_name); printf ("Group: %s",gr->gr_name); } return "Command 'stat' was received"; } char * fPartial(char *cmd, char *tokensleft[]) { struct passwd *pw; setpwent(); char *result = malloc(1024); result[0] = '0'; while ((pw = getpwent()) != NULL) {
  • 5. if (strcasestr(pw->pw_name, tokensleft[0]) || strcasestr(pw->pw_gecos, tokensleft[0])) { char *entry = malloc(strlen(pw->pw_name) + 2); sprintf(entry, "%s,", pw->pw_name); strcat(result, entry); free(entry); } } endpwent(); // close the password file if (strlen(result) == 0) { strcpy(result, "No matching users found."); } printf("Match Found: %s n",result); return "Command 'partial' was received"; } char * fshowTime(char *cmd, char *tokensleft[]) { system(setenv("DISPLAY_FORMAT","%A, %B, %d, %y, %I:%M %p",1)); system(setenv("PARSE_FORMAT","%Y/%m/%d %H:%M:%S",1)); time_t rawtime; struct tm *info; char buffer[80]; time(&rawtime); info = localtime(&rawtime); if (!tokensleft[0]){ strftime(buffer,80,"%x - %I:%M%p", info); printf("Current date & time : |%s|n", buffer ); }else{ } return "Command 'showTime' was received"; }
  • 6. char *commands[18]={"export","chdir","access","chmod", "path","touch","ln","unset","stat","partial","showTime"} ; char *(*methods[18])()={fExport,fChdir,fAccess,fChmod,fPath,fTouch,fLn,fUnset,fStat,fPartial,fsho wTime}; //Alternate declaration struct CMDSTRUCT { char *cmd; char *(*method)(); } cmdStruct[]={{"justin",fshowTime},{"paul",fPartial},{"sammy",fStat},{"fred",fExport},{"mary ",fChdir},{"clark",fAccess},{"sonia",fChmod},{"carlo",fPath},{"lalo",fTouch},{"samuel",fLn}, {"olga",fUnset},{NULL,NULL}} ; char *interpret(char *cmdline) { char **tokens; char *cmd; int i; char *result; char sysCommand; tokens=history_tokenize(cmdline); if(!tokens) return "no response needed"; cmd=tokens[0]; //Detecting commands: table lookup: 2 techniques //Using the parallel arrays to look up function calls for(i=0;commands[i];i++) { if(strcasecmp(cmd,commands[i])==0) return (methods[i])(cmd,&tokens[1]); } for(i=0;cmdStruct[i].cmd;i++) if(strcasecmp(cmd,cmdStruct[i].cmd)==0) return (cmdStruct[i].method)(cmd,&tokens[1]); sysCommand=system(cmdline); if (sysCommand==-1){
  • 7. return"command status: fail"; }else if (sysCommand==0){ return"command status: sucsess";} } int main(int argc, char * argv[],char * envp[]) { char cmd[100]; char *cmdLine; char *expansion; time_t now=time(NULL); int nBytes; //size of msg rec'd char cwd[PATH_MAX]; signal(SIGINT,ctrlCHandler); read_history("shell.log"); add_history(ctime(&now)); fprintf(stdout,"Starting the shell at: %sn",ctime(&now)); while(true) { if (getcwd(cwd, sizeof(cwd)) != NULL) { printf("Current working dir: %sn", cwd); } else { perror("getcwd() error"); return 1; } cmdLine=readline("Enter a command: "); if(!cmdLine) break; history_expand(cmdLine,&expansion); add_history(expansion); if(strcasecmp(cmdLine,"bye")==0) break; char *response=interpret(cmdLine); fprintf(stdout,"%sn",response); } write_history("shell.log");
  • 8. system("echo Your session history is; cat -n shell.log"); fprintf(stdout,"Server is now terminated n"); return 0; } SIGHUP, Set up SIGIO as a signal to be ignored. (6 marks) a. The initial handler you should write for each of them should be stub routines that output a message: "Signal # (SIGxxxx) received in function _FUNCTION_. Use strsignal to output the name of the signal using dprintf to send the output to a file. (The tail -f & command will be demonstrated to allow you to follow text output to a file while a program is running. Take notes!) Display the name of the file, when it was compiled and the line # of the output message. (2 marks) b. Test all of the above signal handlers. (2 marks) i. Verify that all of your signal handlers work sending your command servers each of the above signals from a 2nd terminal. ii. To set up a log file to record the output of dprintf(fd, fmtstr, argsI) use one of more of the following before running your command server: exec fd>/dev/tty #isplays the messages on the current terminal exec fd>/dev/pts/n #display on a different terminal that you own exec fd>logfile #writes to a file tail -f logfile & #displays new data as it is appended to logfile tail -f logfile >/dev/pts/n & #displays new data on an alternate terminal Hand in a log file (as opposed to a screen shot) showing that you tested all of the signals c. In a 3rd terminal attach strace to the pid of your command server. (2) i. Send each of the 4 signals to the pid of the command server. How does strace respond to each signal. Summarize the result. ii. Send each of the 4 signals to the parent pid of the command server. Answer the same question and hilite/describe any difference.