Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Submit your solution in a single, plain-text source file with the .c extension.

ID: 3784036 • Letter: S

Question

Submit your solution in a single, plain-text source file with the .c extension.

For this assignment, you will create a system command that will directory entries by inode number.


NAME   
rbi - removes a directory entry by inode number.   

SYNOPSIS   
rbi INODENUMBER

DESCRIPTION
Removes a file or directory from the current directory matching the supplied inode number. Displays an appropriate error message on failure. You may assume the user always provides an integer for the inode number.   

SAMPLE USAGE
rbi 23593064 -- directory entry with matching inode number is removed.
rbi -- synopsis displayed

Hints:

You'll need system calls covered in the previous chapter.

ls -i will show you the inode numbers of each file in the current directory.

Not much room for partial credit here. It either works or it doesn't.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<unistd.h>

main(int argc,char *argv[])
{
if((argc == 3) && (strcmp("rbi", argv[1]) == 0)) {
  
char *inodeNum = argv[2];
char cmd[20] = "find . -inum " ;
char rem[20] = " -delete" ;
char buffer[30] ;
sprintf( buffer, "%s", inodeNum);
strcat(cmd,buffer);
strcat(cmd,rem);
system(cmd);
}
else {
printf("Usage: file.c rbi inodeNumber ");
}
}