-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.h
More file actions
161 lines (136 loc) · 3.46 KB
/
func.h
File metadata and controls
161 lines (136 loc) · 3.46 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <limits.h>
#include <time.h>
#include <sys/stat.h>
#ifndef FUNC_H
#define FUNC_H
int createDirectory();
int deleteDirectory();
int renameDirectory();
int changeDirectory();
int printCurrentDirectory();
void displayHelp();
#endif
#ifndef DT_DIR
#define DT_DIR 4
#endif
// Function to create a new file
// Returns 0 if the file is created successfully
// Returns -1 if file creation fails
// Returns 2 if the file already exists
int createFile()
{
printf("\nEnter name for the file: ");
char filename[100];
fgets(filename, sizeof(filename), stdin);
// Remove the newline character from the filename
filename[strcspn(filename, "\n")] = 0;
// Check if the file already exists
if (access(filename, F_OK) != -1)
{
return 2; // Return 2 if the file already exists
}
// Try to create the file
FILE *file = fopen(filename, "w");
if (file == NULL)
{
return -1; // Return -1 if file creation fails
}
else
{
fclose(file);
return 0; // Return 0 if the file is created successfully
}
}
int deleteFile()
{
char filename[100];
printf("\nEnter name of file to delete: ");
fgets(filename, sizeof(filename), stdin);
filename[strcspn(filename, "\n")] = 0;
// Attempt to delete the file
if (remove(filename) == 0)
{
return 0; // Successful deletion
}
else
{
return -1; // Deletion failed
}
}
int renameFile()
{
char oldname[100], newname[100];
printf("\nEnter current filename: ");
fgets(oldname, sizeof(oldname), stdin);
oldname[strcspn(oldname, "\n")] = 0;
printf("Enter new filename: ");
fgets(newname, sizeof(newname), stdin);
newname[strcspn(newname, "\n")] = 0;
// Attempt to rename the file
if (rename(oldname, newname) == 0)
{
return 0; // Successful rename
}
else
{
return -1; // Rename failed
}
}
int listFilesAndDirectories()
{
DIR *dir;
struct dirent *entry;
// Open the current directory
dir = opendir(".");
if (dir == NULL)
{
printf("Error opening directory!\n");
return -1;
}
printf("\nFiles and Directories:\n");
printf("--------------------\n");
// Read and print directory contents
while ((entry = readdir(dir)) != NULL)
{
// Skip current and parent directory entries
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
{
// Determine type of entry
if (entry->d_type == DT_DIR)
{
printf("[DIR] %s\n", entry->d_name);
}
else
{
printf("[FILE] %s\n", entry->d_name);
}
}
}
// Close the directory
closedir(dir);
return 0;
}
//display metadata of the file
int fileMetadata() {
struct stat fileStat;
printf("Enter the filename:");
char filename[100];
scanf("%s",filename);
if (stat(filename, &fileStat) < 0) {
return -1;
}
printf("File: %s\n", filename);
printf("Size: %lld bytes\n", (long long)fileStat.st_size); // Use %lld for off_t
printf("Last accessed: %s", ctime(&fileStat.st_atime));
printf("Last modified: %s", ctime(&fileStat.st_mtime));
printf("Last status change: %s\n", ctime(&fileStat.st_ctime));
return 0;
}