Question: g. touch -m t1 -a t2 file1 file2 file3 . (4 marks) i. Implement a small getopt loop in your function to implement this command.

g. touch -m t1 -a t2 file1 file2 file3 . (4 marks)

i. Implement a small getopt loop in your function to implement this command. Set the default times for the modify and access flags to the current time seconds since the epoch. Use the getopt loop to capture -m t1 and -a t2, if they are present where t1 and t2 are large integers representing

ii. Verify that each file exists.

1. If it does use utimes to set both times

2. If it doesnt,, use creat to create it, verify that you were successful, use futimes to set both times and then close the file.

3. Verify using the stat command that you were successful.

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: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: %c ",flag,optopt); break; case '?': fprintf(stderr,"%c is an illegal flag ",optopt); break; } }

// Print the flags and times for testing printf("Flags: -m %d -a %d ", t1, t2); printf("mFlag: %d aFlag: %d ", flags.mFlag, flags.aFlag);

return "Command 'touch' was received"; }

The command will not recognise the first argument touch -m 21 -a 32 will not correctly set the flag. Also how do you capture and use the file name provided to the shell?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!