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");
}
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.