Add a 3rd field help that contains a short help string for each of the commands you were to
implement in assignment #3. Make sure that your array(s) are big enough to handle 5 extra items
beyond your initialization. To save time only include help for exercises 4, 5, 6 and 8 in this
assignment, and use No help for the other entries.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void ctrlCHandler(int signum)
{
fprintf(stderr,"Command server terminated using C\n");
exit(1);
}
char * fExport(char *cmd, char *tokensleft[])
{
setenv(tokensleft[0],tokensleft[1],1);
return "Command 'export' was received";
}
char * fChdir(char *cmd,char *tokensleft[])
{
int ch=chdir(tokensleft[0]);
if(ch<0)
perror("chdir change of directory not successful\n");
else
printf("chdir change of directory successful\n");
return "Command 'chdir' was received\n";
}
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 exists\n",tokensleft[i]);
}else{
printf("file %s does not exists\n",tokensleft[i]);
}
if (exists == 1){
if(access(tokensleft[i],R_OK)==0) {
printf("file %s is readable\n",tokensleft[i]);
}else{printf("file %s is not readable\n",tokensleft[i]);}
if(access(tokensleft[i],W_OK)==0) {
printf("file %s is writeable\n",tokensleft[i]);
}else{
printf("file %s is not writeable\n",tokensleft[i]);
}
if(access(tokensleft[i],X_OK)==0) {
printf("file %s is executeable\n",tokensleft[i]);
}else{
printf("file %s is not executeable\n",tokensleft[i]);
}
}// exists if
} //for
return "Command 'acsess' was received\n";
}
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 * fPath(char *cmd,char *tokensleft[])
{
char *pathLink;
char actualPath[PATH_MAX+1];
char *pointer;
char *bName;
char *dName;
for(int i=0;tokensleft[i]; i++) {
pathLink = tokensleft[i];
pointer =realpath(pathLink,actualPath);
bName = basename(actualPath);
dName = dirname(tokensleft[i]);
if (pointer){
printf("The Real path of %s is: %s\n",tokensleft[i],actualPath);
printf("The Dir name path of %s is: %s\n",tokensleft[i],dName);
printf("The Base name of %s is: %s\n",tokensleft[i],bName);
}else{
printf("Error: %s \n",strerror(errno));
}
}
return "Command 'path' was received";
}
char * fTouch(char *cmd,char *tokensleft[])
{
extern int optind,optopt,opterr;
struct FLAG{
bool aFlag;
bool mFlag;
} flags = { false, false };
int t1 = time(NULL), t2 = time(NULL);
int argc = 0;
int flag;
for (int i = 0; tokensleft[i]; i++) {
argc++;
}
while ((flag = getopt(argc, tokensleft, "m.
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
Add a 3rd field help that contains a short help string for each of t.pdf
1. Add a 3rd field help that contains a short help string for each of the commands you were to
implement in assignment #3. Make sure that your array(s) are big enough to handle 5 extra items
beyond your initialization. To save time only include help for exercises 4, 5, 6 and 8 in this
assignment, and use No help for the other entries.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void ctrlCHandler(int signum)
{
fprintf(stderr,"Command server terminated using Cn");
exit(1);
2. }
char * fExport(char *cmd, char *tokensleft[])
{
setenv(tokensleft[0],tokensleft[1],1);
return "Command 'export' was received";
}
char * fChdir(char *cmd,char *tokensleft[])
{
int ch=chdir(tokensleft[0]);
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]);
}
3. 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]);
}
}// exists if
} //for
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";
4. }
char * fPath(char *cmd,char *tokensleft[])
{
char *pathLink;
char actualPath[PATH_MAX+1];
char *pointer;
char *bName;
char *dName;
for(int i=0;tokensleft[i]; i++) {
pathLink = tokensleft[i];
pointer =realpath(pathLink,actualPath);
bName = basename(actualPath);
dName = dirname(tokensleft[i]);
if (pointer){
printf("The Real path of %s is: %sn",tokensleft[i],actualPath);
printf("The Dir name path of %s is: %sn",tokensleft[i],dName);
printf("The Base name of %s is: %sn",tokensleft[i],bName);
}else{
printf("Error: %s n",strerror(errno));
}
}
return "Command 'path' was received";
}
char * fTouch(char *cmd,char *tokensleft[])
{
extern int optind,optopt,opterr;
struct FLAG{
bool aFlag;
bool mFlag;
} flags = { false, false };
int t1 = time(NULL), t2 = time(NULL);
int argc = 0;
5. int flag;
for (int i = 0; tokensleft[i]; i++) {
argc++;
}
while ((flag = getopt(argc, tokensleft, "m:a:")) != -1) {
switch (flag) {
case 'm':
flags.mFlag = true;
t1 = atoi(optarg);
break;
case 'a':
flags.aFlag = true;
t2 = atoi(optarg);
break;
case ':':
if (optopt == 'm') {
flags.mFlag = true;
}
fprintf(stderr, "%c flag is missing an argument. optopt is: %cn", flag, optopt);
break;
case '?':
fprintf(stderr, "%c is an illegal flagn", optopt);
break;
}
}
// Print the flags and times for testing
printf("Flags: -m %d -a %dn", t1, t2);
printf("mFlag: %d aFlag: %dn", flags.mFlag, flags.aFlag);
return "Command 'touch' was received";
}
char * fLn(char *cmd,char *tokensleft[])
{
int opt;
int force = 0;
int symbolic = 0;
int argc =0;
6. 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 'ln' was received";
}
char * fUnset(char *cmd, char *tokensleft[])
{
unsetenv(tokensleft[0]);
return "Command 'unset' was received";
}
char *commands[10]={"export","chdir","access","chmod", "path","touch","ln","unset"} ;
char *(*methods[10])()={fExport,fChdir,fAccess,fChmod,fPath,fTouch,fLn,fUnset};
//Alternate declaration
struct CMDSTRUCT {
char *cmd;
char *(*method)();
}
cmdStruct[]={{"fred",fExport},{"mary",fChdir},{"clark",fAccess},{"sonia",fChmod},{"carlo",f
Path},{"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); //Split cmdline into individual words.
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++)
8. {
if(strcasecmp(cmd,commands[i])==0) return (methods[i])(cmd,&tokens[1]);
}
//Using struct CMDSTRUCT as an alternative lookup method. Pick either technique, not both
//Note that its possible to create multiple aliases for the same command using either method.
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;
}
9. 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;
}