-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.c
More file actions
109 lines (87 loc) · 2.29 KB
/
func.c
File metadata and controls
109 lines (87 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <limits.h>
#define PATH_MAX 4096
// Create a new directory
int createDirectory() {
char dirname[50];
printf("Enter the name of the directory to create: ");
scanf("%49s", dirname);
// Use mkdir system call
if (mkdir(dirname, 0755) == -1) {
perror("Error creating directory");
return -1;
}
printf("Directory created successfully!\n");
return 0;
}
// Delete a directory
int deleteDirectory() {
char dirname[50];
printf("Enter the name of the directory to delete: ");
scanf("%49s", dirname);
// Use rmdir system call (directory must be empty)
if (rmdir(dirname) == -1) {
perror("Error deleting directory");
return -1;
}
printf("Directory deleted successfully!\n");
return 0;
}
// Rename a directory
int renameDirectory() {
char oldname[50], newname[50];
printf("Enter the current name of the directory: ");
scanf("%49s", oldname);
printf("Enter the new name for the directory: ");
scanf("%49s", newname);
// Use rename system call
if (rename(oldname, newname) == -1) {
perror("Error renaming directory");
return -1;
}
printf("Directory renamed successfully!\n");
return 0;
}
// Change current working directory
int changeDirectory() {
char path[100];
printf("Enter the path to change to: ");
scanf("%99s", path);
// Use chdir system call
if (chdir(path) == -1) {
perror("Error changing directory");
return -1;
}
printf("Changed directory to %s successfully!\n", path);
return 0;
}
// Print the current working directory
int printCurrentDirectory() {
char cwd[PATH_MAX];
// Use getcwd system call
if (getcwd(cwd, sizeof(cwd)) == NULL) {
perror("Error getting current directory");
return -1;
}
printf("Current directory: %s\n", cwd);
return 0;
}
// Display help for commands
void displayHelp() {
FILE *file = fopen("help.txt", "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[1024];
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
}