Uploaded by Alazhar Alabri

user guide 129050

advertisement
Sultan Qaboos University
College of Science Department of Computer Science
COMP4506- SYSTEMS & NETWORK PROGRAMMING
Fall23-Assignment 3
Demodir System Program User Guide
Submitted to: Dr Ahmed Al Kindi
Name
ID
Al Azhar Jalal Saud Al Abri
129050
1|Page
Contents
Introduction............................................................................................................................................... 3
1. Building the Demodir Tree ..................................................................................................................... 3
2. Deleting the Demodir Tree ..................................................................................................................... 3
3. Moving Subdirectories ........................................................................................................................... 3
4. Displaying the Absolute Path of Demodir ............................................................................................... 4
5. Displaying Demodir Content .................................................................................................................. 4
6. Exiting the Program ............................................................................................................................... 4
The implementation: ................................................................................................................................. 4
2|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:
-
3|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>
#endif
void printCentered(const char *text) {
4|Page
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];
snprintf(command, sizeof(command), "rm -rf %s/demodir", absolutePath);
if (system(command) != 0) {
5|Page
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(" ");
}
printf("%ld %-10s %ld %s %s\n", (long)entryStat.st_ino,
6|Page
(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.
printf("
5.
demodir.\n");
7|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");
Display, in column-wise format, the content of the entire
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;
}
8|Page
Download