Contents Introduction............................................................................................................................................... 2 1. Building the Demodir Tree ..................................................................................................................... 2 2. Deleting the Demodir Tree ..................................................................................................................... 2 3. Moving Subdirectories ........................................................................................................................... 2 4. Displaying the Absolute Path of Demodir ............................................................................................... 3 5. Displaying Demodir Content .................................................................................................................. 3 6. Exiting the Program ............................................................................................................................... 3 The implementation: ................................................................................................................................. 3 1|Page Introduction Welcome to the Demodir System Program! This program is designed to manage and display the directory tree structure within the `demodir` directory. The following guide provides instructions on how to use the program's features. 1. Building the Demodir Tree To build the entire `demodir` tree of directories, follow these steps: - Choose option 1 from the main menu. The program will create the `demodir` tree structure in your current directory. 2. Deleting the Demodir Tree To delete the `demodir` tree and all its files, follow these steps: - Choose option 2 from the main menu. The program will delete the entire `demodir` tree. 3. Moving Subdirectories To move a subdirectory to another location within the `demodir`, follow these steps: 2|Page Choose option 3 from the main menu. Enter the source subdirectory name when prompted. Enter the destination subdirectory name when prompted. The program will move the specified subdirectory to the destination. 4. Displaying the Absolute Path of Demodir To display the absolute path of the `demodir`, follow these steps: o o Choose option 4 from the main menu. The program will print the absolute path of the `demodir`. 5. Displaying Demodir Content To display the content of the entire `demodir` tree in a similar format to 'ls -liR', follow these steps: - Choose option 5 from the main menu. The program will print the detailed content of the `demodir` tree. 6. Exiting the Program To exit the program, follow these steps: - Choose option 6 from the main menu. The program will terminate, and you will exit to the command prompt. The implementation: #include #include #include #include #include #include <stdio.h> <stdlib.h> <string.h> <dirent.h> <sys/stat.h> <sys/types.h> #ifdef _WIN32 #include <direct.h> #define getcwd _getcwd #else #include <unistd.h> 3|Page #endif void printCentered(const char *text) { int termWidth = 80; int len = strlen(text); int padding = (termWidth - len) / 2; printf("%*s%s\n", padding, "", text); } void buildDemodirTree(const char *currentDir) { char demodirPath[256]; snprintf(demodirPath, sizeof(demodirPath), "%s/demodir", currentDir); char command[256]; snprintf(command, sizeof(command), "rm -rf %s", demodirPath); system(command); snprintf(command, sizeof(command), "mkdir -p %s", demodirPath); if (system(command) != 0) { fprintf(stderr, "Error creating demodir\n"); return; } snprintf(command, sizeof(command), "mkdir %s/subdir1", demodirPath); system(command); snprintf(command, sizeof(command), "mkdir %s/subdir2", demodirPath); system(command); snprintf(command, sizeof(command), "mkdir %s/subdir2/subdir3", demodirPath); system(command); } void deleteDemodirTree(const char *demodirPath) { // Get the absolute path of demodir char absolutePath[256]; if (realpath(demodirPath, absolutePath) == NULL) { perror("Error getting absolute path"); return; } // Remove demodir if it exists char command[256]; 4|Page snprintf(command, sizeof(command), "rm -rf %s/demodir", absolutePath); if (system(command) != 0) { perror("Error deleting demodir"); } } void moveSubdirectory(const char *fromDir, const char *toDir) { // Move subdirectory from fromDir to toDir char command[256]; snprintf(command, sizeof(command), "mv %s %s", fromDir, toDir); if (system(command) != 0) { fprintf(stderr, "Error moving subdirectory\n"); } } void displayDemodirPath(const char *demodirPath) { printf("Demodir Path: %s\n", demodirPath); } void displayDemodirContent(const char *demodirPath, int depth) { // Open the directory DIR *dir = opendir(demodirPath); if (dir == NULL) { perror("Error opening demodir"); return; } // Read the directory entries struct dirent *entry; while ((entry = readdir(dir)) != NULL) { // Skip "." and ".." entries if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { // Construct the full path of the entry char entryPath[256]; snprintf(entryPath, sizeof(entryPath), "%s/%s", demodirPath, entry>d_name); // Get information about the entry struct stat entryStat; if (stat(entryPath, &entryStat) == 0) { // Print details similar to the 'ls -li' format for (int i = 0; i < depth; i++) { printf(" "); 5|Page } printf("%ld %-10s %ld %s %s\n", (long)entryStat.st_ino, (S_ISDIR(entryStat.st_mode) ? "drwxr-xr-x" : "-rw-r--r--"), entryStat.st_nlink, entryStat.st_uid == 0 ? "root" : "user", entry->d_name); if (S_ISDIR(entryStat.st_mode)) { displayDemodirContent(entryPath, depth + 1); } } } } // Close the directory closedir(dir); } int main() { char currentDir[256]; if (getcwd(currentDir, sizeof(currentDir)) == NULL) { perror("Error getting current directory"); return 1; } printCentered("SULTAN QABOOS UNIVERSITY, COLLEGE OF SCIENCE"); printCentered("DEPARTMENT OF COMPUTER SCIENCE"); printCentered("COMP 4506: SYSTEMS & NETWORK PROGRAMMING"); printCentered("Assignment # 3"); printCentered("Welcome to the Demodir System Program"); printCentered("Designed & Programed by Al Azhar Al Abri"); printCentered("_________________________________________"); printf("\n"); int choice; do { printf("\n"); printf(" 1. printf(" 2. files.\n"); printf(" 3. demodir.\n"); printf(" 4. 6|Page Build the entire demodir tree of directories.\n"); Delete the demodir tree of directories and all the Move a subdirectory to another location within Display the absolute path of demodir.\n"); printf(" 5. Display, in column-wise format, the content of the entire demodir.\n"); printf(" 6. Exit\n"); printf("What do you want to do? Kindly select your option: "); scanf("%d", &choice); switch (choice) { case 1: buildDemodirTree(currentDir); break; case 2: deleteDemodirTree(currentDir); break; case 3: { char fromDir[256], toDir[256]; printf("Enter the source subdirectory: "); scanf("%s", fromDir); printf("Enter the destination subdirectory: "); scanf("%s", toDir); moveSubdirectory(fromDir, toDir); break; } case 4: displayDemodirPath(currentDir); break; case 5: displayDemodirPath(currentDir); displayDemodirContent(currentDir, 0); // Provide the depth as 0 initially break; case 6: printf("Exiting the program.\n"); break; default: printf("Invalid choice. Please select a valid option.\n"); } } while (choice != 6); return 0; } 7|Page 8|Page