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

I believe I completed everything else except the forking. Could someone show me

ID: 3549830 • Letter: I

Question

                    
                

I believe I completed everything else except the forking. Could someone show me how to do that with the code below?

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>

int directories=0;
void stepThroughDirectory(char *directory_name,char *searchString)
{
    DIR *directory;
    struct dirent *entry;
    char d_name[PATH_MAX];
    int path_length;
    char path[PATH_MAX];
    directory = opendir(directory_name);

    if(directory == NULL) {
        std::cout << directory_name << "  Cannot be Opened" << std::endl;
        exit(1);
    }

    while((entry=readdir(directory)) != NULL) {
        strcpy(d_name, entry->d_name);

        if (strcmp(d_name, "..") == 0 || strcmp(d_name, ".") == 0) {
            continue;
        }
        else {
            path_length = snprintf(path, PATH_MAX, "%s/%s", directory_name, d_name);

            if (path_length >= PATH_MAX) {
                std::cout << "Path is too long" << std::endl;
                exit(2);
            }

            if(entry->d_type == DT_DIR) {
                directories++;
                stepThroughDirectory(path, searchString);
            }
            else if(entry->d_type==DT_REG) {
                std::ifstream openFile;
                openFile.open(path);
                char line[1500];
                int currentLine = 0;

                if (openFile.is_open()) {
                    while (openFile.good()) {
                        currentLine++;
                        openFile.getline(line, 1500);
                        if (strstr(line, searchString) != NULL)
                        std::cout << path << ": " << currentLine << ": " << line << std::endl;
                    }
                }
                openFile.close();    
            }
        }
    }

    if (closedir(directory)) {
        std::cout << "Unable to close " << directory_name << std::endl;
        exit(3);
    }
}

int main() {

    char *name;

    std::string str;
    std::cout << "Enter the substring that you want to search for: ";
    std::getline (std::cin,str);
    char *substring = new char[str.length() + 1];
    strcpy(substring, str.c_str());
    //delete [] cstr;

    name = get_current_dir_name();
    stepThroughDirectory(name, substring);
    free(name);
    std::cout << "Total Directories: " <<  directories << std::endl;

    return 0;
}

Explanation / Answer

Here is your program using fork() and depth limit: http://pastebin.com/kSe0F5Yi


Kindly review and rate.

Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote