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 ':': 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"; }
Posting this question again because the first time it was answered the problem remained. The problem is the -m argument is impossible to set to true with the current getopt loop
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
