-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (82 loc) · 2.07 KB
/
main.cpp
File metadata and controls
95 lines (82 loc) · 2.07 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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <cstdlib>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
// Perform "binary deletion" on a file
// this will overwrite the entire file with zero bytes
// and then attempt to remove the file
int doDeletion(char* fileName, bool quiet) {
if (strcmp(fileName, "") == 0) {
printf("File name is empty string %s\n", fileName);
return 1;
}
// Attempt to open the file in read/write binary mode
FILE* fp = fopen(fileName, "rb+");
if (fp) {
// Gather file size
fseek(fp, 0, SEEK_END);
long fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (!quiet) {
printf("File: %s", fileName);
printf("\tSize: %d bytes\n", fileSize);
}
// Write zero byte for every byte
char* data = (char*)malloc(fileSize);
fwrite(data, 1, fileSize, fp);
free(data);
// Close file handler
fclose(fp);
// Now remove the file
int del = remove(fileName);
if (del) {
printf("Unable to delete the file: %s\n", fileName);
return 1;
}
} else {
printf("Unable to open the file: %s\n", fileName);
return 1;
}
return 0;
}
// Entry point, expects arguments or line
// delimeted list of file paths from STD-IN
int main(int argc, char** argv) {
bool quiet = false;
// Read from STD-IN
if (argc == 1) {
char ch;
size_t i = 0;
char fileName[PATH_MAX];
// Read all bytes using new lines as delimeters for file names
// once a filename has been accumulated, perform the operation
while (read(STDIN_FILENO, &ch, 1) > 0) {
if (ch == '\n') {
if (i > 0) {
fileName[i] = '\0';
i = 0;
int result = doDeletion(fileName, quiet);
if (result > 0) {
return result;
}
}
} else {
fileName[i] = ch;
i++;
}
}
} else {
// Expect list of file names from argv
for (int i = 1; i < argc; i++) {
int result = doDeletion(argv[i], quiet);
if (result > 0) {
return result;
}
}
}
return 0;
}