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

This assignment will help you to understand Linux operating system file structur

ID: 3917825 • Letter: T

Question

This assignment will help you to understand Linux operating system file structure and basic file interface techniques. The systems administrator at your company has asked you to write a file utility program that gives useful information about the file system on the company server. Your program (written in C or C++) should include as a minimum:

A menu-driven or GUI interface (your choice)

Thorough commenting

Error handling

The ability to select a file or files to view

The ability to select specific information about a file(s) to display

The ability to change the permissions on a file or files

The ability to copy a file to a new directory

Explanation / Answer

Its partially done, but i guess you can extend it to complete it easily. Hope it helps!

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


#define MAX_FILE_NAME_LEN 100

int main()
{
    int file_count= 0;
    int option=0;
    struct dirent *de = NULL; //pointer for directory entry    


    printf("Hello welcome to the File Utility ");
    printf("Enter the number for one of the below options to proceed: 1. Select File(s) 2.Exit ");
    scanf("%d",&option);

    //exiting utility if Exit option chosen    
    if(option == 2)
        return 0;

    //getting pointer of DIR tyoe for cwd    
    DIR *dr = opendir(".");

    if(dr == NULL)
    {
        printf("Error in opening current working directory ");
        return -1;
    }
    
    printf("The files present in current directory are: ");
    while ((de = readdir(dr)) != NULL)
    {
            printf("%s ,", de->d_name);
        file_count++;
     }
    closedir(dr);    

    //creating array to take input of name of files
    char file_input[file_count*MAX_FILE_NAME_LEN + file_count];

    //creating array of pointers to handle number of files    
    char* files[file_count];

    printf(" Enter the name of the file to select. In case of multiple files, enter file names separated by ','. ");
    scanf("%s",file_input);
    printf("Files selected by you are: ");

    char *temp = strtok(file_input,",");
    int file_selected = 0;
    while(temp != NULL)
    {    
        printf("%s ",temp);
        files[file_selected]=temp;
        file_selected++;
        temp=strtok(NULL,",");
    }
    
    //options for further functions    
    printf("Enter the number for one of the below options to proceed: 1. View Information of file 2.Change Permission 3. Copy file to different directory ");
    scanf("%d", &option);
    char command[1000]="";
    char path[1000];

    //switch case to handle with options
    switch(option)
    {    
        case 1:
            for(int i=0;i<file_selected;i++)
            {
                strcat(command,"stat ");
                strcat(command,files[i]);
                system(command);
            }
            break;
        case 2:
            //can use same system command to create and execute chmod command for permission changes, hope that helps!
        case 3:
            printf("Please, enter the absolute path alongwith file name where you want to copy the selected files ");
            scanf("%s",path);
            strcat(command,"sudo cp ");
            for(int i=0;i<file_selected;i++)
            {
                strcat(command,files[i]);
                strcat(command," ");
            }
            strcat(command,path);
            system(command);
            break;
        default:
            printf("Invalid selection, exiting utility ");
            return 0;
    }
    


}