CS 332/532 – 1G- Systems Programming HW 3 Deadline: 11/01/2020 Sunday 11:59pm Objectives To implement a search program in C program using system calls for files and directories. Description Find is a...

1 answer below »


CS 332/532 – 1G- Systems Programming



HW 3



Deadline:
11/01/2020 Sunday 11:59pm



Objectives


To implement a
search
program in C program using system calls for files and


directories.



Description


Find is a popular UNIX command that traverses a file hierarchy and performs various


functions on each file in the hierarchy. The goal of this project is to implement a


program similar called
search
that supports the following functionality:


1. The program should take the directory name from where to start the file traversal as a


command-line argument and print the file hierarchy starting with the directory that is


provided by the command-line argument.


2. If the program is executed without any arguments, the program should print the file


hierarchy starting with the current directory where the program is executed. If there are no


directories in the current directory only files are listed one per line.


3. If there are other directories in the current directory then the directory name is first


displayed on a separate line and then the files in that directory are listed one-per-line with


one-tab indentation.


4. If a file is a symbolic link then the program should display the symbolic link name and in


parentheses the file name the link points to.


5. The program should also support four command-line options:


1. -S


This should list all files in the file hierarchy and print the file size next to the filename in


parenthesis.


2. -s


This should list all files in the file hierarchy with file size greater than or equal to the


value specified.


3. -f


This should list all files in the file hierarchy whose file name or directory name contains


the substring specified in the string pattern option.


4. e "" For each file that matches the search criteria the UNIX command specified with arguments must be executed.


6. The program should support not only each of these options separately but also any


combination of these options. For example: -S, -s 1024, -f jpg, -S -s 1024, -S -f jpg, -s


1024 -f jpg, -S -s 1024 -f jpg, -S -f jpg -s 1024.


7. If both -s and -f options are specified then the program should list only those files that


match both criteria. The order of the options should not matter.




Guidelines and Hints


1. The program must use function pointers similar to Figure 4.22 in the text book to


implement the functionality described above. You can use the logic and structure from


Figure 4.22 as the starting point to implement this program (make sure to go over the


program in Figure 4.22 and understand all the steps performed). However, please note that


your final program must compile and execute without any dependencies on the source


code provided by the text book. You can find a simple example on how to use function


pointers in the funcptr.c file


2. You can use the
getopt
function to process the command-line options. See
man 3



getopt
for more details and an example on how to use
getopt
function.


3. You should use a Makefile to compile and build this project and make sure to submit the


Makefile along with the rest of the source code.


4. You should upload all the source code, Makefile, and a README.txt file to Canvas. Please


do not upload any object files or executable files.



Program Documentation and Testing


1. Use appropriate names for variables and functions.


2. Use a Makefile to compile your program.


3. Include meaningful comments to indicate various operations performed by the program.


4. Programs must include the following header information within comments:


/*


Name:


BlazerId:


Project #:


To compile:


To run:


*/


5. Test your program with the sample test cases provided as well as your own test cases.


6. You can include any comments you may have about testing in the README.txt file.



Examples




























































Command








Description






$ ./search



List all files in the current directory where the program is executed.



$ ./search ../programs



List all files in the directory ../programs (relative to the current directory)



$./search /home/UAB/puri/CS332/programs



List all files in the directory /home/UAB/puri/CS332/programs (absolute path)



$./search -S ../programs



List all files in the directory ../programs along with the file size



$./search -s 1024



List all files with size >= 1024 bytes in the current directory



$./search -s 1024 ../programs



List all files with size >= 1024 bytes in the ../programs (relative to the current directory)



$./search -f jpg



List all files that have the substring “jpg” in their filename or directory name in the current directory



$./search -f jpg -s 1024



List all files that have the substring “jpg” in their filename or directory name with size >= 1024 in the current directory



$./search -s 1024 -f jpg



List all files with size >= 1024 and have the substring “jpg” in their filename or directory name in the current directory



$./search -S -s 1024 -f jpg



List all files with size >= 1024 and have the substring “jpg” in their filename or directory name in the current directory and include the file size next to the filename



$./search -s 1024 -e "ls -l"



List all files with size >= 1024 bytes in the current directory and execute the command "ls -l" on each file (ignore directories)



$./search -s 1024 -e "wc -l"



List all files that have the substring “jpg” in their filename or directory name with size >= 1024 in the current directory and execute the command "wc -l" on each file (ignore directories)







Sample Input and Output:


If you have the following directory structure as shown by the output of "ls -lR"


command:







Submission Guidelines


• Use best software engineering practices to design and implement this homework.


The next homework will extend the functionality provided by this program.


• Use functions and organize these functions in multiple files if necessary.


• Use a Makefile to compile and build the multiple files.


• Document you code and include instructions for compiling and executing the


program in the README.txt file.


• Test your program and describe how you tested this program in the README.txt file.


• To submit this homework :


o upload all the source files and documentation to Canvas.


Answered Same DayOct 28, 2021

Answer To: CS 332/532 – 1G- Systems Programming HW 3 Deadline: 11/01/2020 Sunday 11:59pm Objectives To...

Ria answered on Nov 02 2021
142 Votes
/* To implement a search program in C program using system calls for files and directories */
#include
#
include
#include
int main (int argc, char *argv[])
{
    // ./search
    if (argc == 1) { system ("sudo find"); }
    else
    {
        int i=1;
        // ./search -S
        if (strcmp(argv[i], "-S") == 0)
        {
            i++;
            if (i < argc)
            {
                // ./search -S -s
                if (strcmp(argv[i], "-s") == 0)
                {
                    i++;
                    if (i+1 < argc)
                    {
                        // ./search -S -s -f
                        if (strcmp (argv[i+1], "-f") == 0)
                        {
                            char com[100] = "sudo find / -name '*.";
                            strcat (com, argv[i+2]);
                            strcat (com, "' -type f -size ");
                            strcat (com, argv[i]);
                            strcat (com, " -exec ls -lh {} \\; | awk '{ print $9 \" {\" $5 \"}\" }'");
                            system (com);
                        }
                    }
                    else
                    {
                        char com[100] = "sudo find / -name '*' -type f -size ";
                        strcat (com, argv[i]);
                        strcat (com, " -exec ls -lh {} \\; | awk '{ print $9 \" {\" $5 \"}\" }'");
                        system (com);
                    }
                }
                // ./search -S -f
                else if (strcmp(argv[i], "-f") ==...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here