-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.c
executable file
·445 lines (407 loc) · 12.8 KB
/
note.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#ifndef _WIN32
#include <unistd.h>
#endif
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "./include/alpha2int.h"
// fix for Windows version of mkdir() function
#if (defined(_WIN32) || defined(__WIN32__))
#define mkdir(A, B) mkdir(A)
#endif
typedef unsigned int uint;
typedef unsigned long ulong;
// Global data
// "comm" is used to convert subcommand arguments into integers that can be
// used with a switch statement
enum comm {
helpComm=536744u, lsComm=620u, addComm=4225u, editComm=664709u,
rmComm=434u, cpComm=515u, mvComm=717u,
mkdirComm=19173741u, rmdirComm=19173810u
};
// Function prototype declarations
char** sortStrArray(char** inArr, uint numOfStrings);
void printHelp();
uint showNote(char* notePath);
uint lsNote(int argc, char** argv, char* notesDirPath);
uint addNote(int argc, char** argv, char* notesDirPath);
uint editNote(int argc, char** argv, char* notesDirPath);
uint rmNote(int argc, char** argv, char* notesDirPath);
uint cpNote(int argc, char** argv, char* notesDirPath, uint subComm);
uint mkNoteDir(int argc, char** argv, char* notesDirPath);
uint rmNoteDir(int argc, char** argv, char* notesDirPath);
int main(int argc, char** argv) {
uint subComm = helpComm;
char* homeDirPath = NULL;
char* notesDirPath = NULL;
char* filePath = NULL;
struct stat statBuf;
#ifndef _WIN32
homeDirPath = getenv("HOME");
if (homeDirPath == NULL) {
puts("ERROR: environment variable 'HOME' not set\n");
return 1;
}
#endif
#ifdef _WIN32
homeDirPath = getenv("HOME");
if (homeDirPath == NULL) {
homeDirPath = getenv("USERPROFILE");
}
if (homeDirPath == NULL) {
puts("ERROR: environment variables 'HOME' and 'USERPROFILE' not set");
return 1;
}
#endif
notesDirPath = malloc((strlen(homeDirPath)+18)*sizeof(char));
strcpy(notesDirPath, homeDirPath);
// Convert Windows backslashes to normal forward slashes
for (uint i = 0; i <= strlen(notesDirPath); i++) {
if (notesDirPath[i] == '\\') {
notesDirPath[i] = '/';
}
}
strcat(notesDirPath, "/documents/notes");
if ( stat(notesDirPath, &statBuf) != 0 ) {
if ( mkdir(notesDirPath, 775) != 0 ) {
fprintf(stderr, "ERROR: directory '%s' does not exist and cannot be created\n", notesDirPath);
}
}
// Read first argument as subcommand
if ( argc > 1 ) {
subComm = alpha2int(argv[1]);
}
else if ( argc > 4 ) {
fputs("ERROR: too many arguments\n", stderr);
}
switch (subComm) {
case helpComm:
printHelp();
break;
case lsComm:
lsNote(argc, argv, notesDirPath);
break;
case addComm:
addNote(argc, argv, notesDirPath);
break;
case editComm:
editNote(argc, argv, notesDirPath);
break;
case rmComm:
rmNote(argc, argv, notesDirPath);
break;
case cpComm:
cpNote(argc, argv, notesDirPath, subComm);
break;
case mvComm:
cpNote(argc, argv, notesDirPath, subComm); // cpNote() will remove the source note if the subcommand is "mv"
break;
case mkdirComm:
mkNoteDir(argc, argv, notesDirPath);
break;
case rmdirComm:
rmNoteDir(argc, argv, notesDirPath);
break;
default:
// First argument isn't a subcommand. Is it a file?
filePath = malloc((strlen(notesDirPath)+strlen(argv[1])+6)*sizeof(char));
sprintf(filePath, "%s/%s.txt", notesDirPath, argv[1]);
if ( stat(filePath, &statBuf) == 0 ) {
showNote(filePath);
}
else printHelp();
break;
}
return 0;
}
// Bubble sort
char** sortStrArray(char** inArr, uint numOfStrings) {
char** outArr = malloc(numOfStrings*sizeof(char*));
for (uint i = 0; i < numOfStrings; i++) {
outArr[i] = malloc((strlen(inArr[i])+1)*sizeof(char));
strcpy(outArr[i], inArr[i]);
}
char* tempStr = NULL;
uint keepSorting = 1;
while (keepSorting == 1) {
keepSorting = 0;
for (uint i = 0; i < (numOfStrings-1); i++) {
if ( strcmp(outArr[i], outArr[i+1]) > 1 ) {
keepSorting = 1;
tempStr = outArr[i+1];
outArr[i+1] = outArr[i];
outArr[i] = tempStr;
}
}
}
return outArr;
}
void printHelp() {
puts(
"usage: note NOTE|SUBCOMMAND [ARG1 [ARG2]]\n"
" Display NOTE or do SUBCOMMAND\n"
"subcommands:\n"
" help Display this help text\n"
" ls List notes and subdirs in note directory. If ARG1 is a\n"
" subdir list notes/subdirs inside it.\n"
" add Append the string ARG2 to a new line at the end of ARG1.txt\n"
" edit Open ARG1.txt with $EDITOR\n"
" rm Remove ARG1.txt\n"
" cp Copy ARG1.txt to ARG2.txt\n"
" mv Move or rename ARG1.txt to ARG2.txt\n"
" mkdir Create subdir named ARG1\n"
" rmdir Remove subdir named ARG1");
return;
}
uint showNote(char* notePath) {
char* noteContent = NULL;
FILE* noteFile = fopen(notePath, "r");
ulong length = 0;
if (noteFile) {
fseek(noteFile, 0, SEEK_END);
length = ftell(noteFile);
fseek(noteFile, 0, SEEK_SET);
noteContent = malloc(length+1);
for ( uint i = 0; i < length; i++ ) {
noteContent[i] = fgetc(noteFile);
if (noteContent[i] == '\r') {
length--;
noteContent[i] = fgetc(noteFile);
}
}
noteContent[length] = '\0';
fclose (noteFile);
}
fputs(noteContent, stdout);
return 0;
}
uint lsNote(int argc, char** argv, char* notesDirPath) {
DIR* lsDir = NULL;
char** fileNames = NULL;
char* statFilePath = NULL;
char* lsDirPath = NULL;
char fileExt[5] = {0,0,0,0,0};
struct dirent* entry = NULL;
struct stat statBuf;
uint isTxtFile = 0;
uint numOfFiles = 0;
// Check if argument to ls subcommand is a directory
if ( argc == 3 ) {
lsDirPath = malloc((strlen(notesDirPath)+strlen(argv[2])+2)*sizeof(char));
sprintf(lsDirPath, "%s/%s", notesDirPath, argv[2]);
if ( stat(lsDirPath, &statBuf) != 0 ) {
fprintf(stderr, "ERROR: Cannot open directory '%s'\n", lsDirPath);
return 2;
}
}
else if ( argc > 3 ) {
fputs("ERROR: Too many arguments to subcommand 'ls'\n", stderr);
return 3;
}
else {
lsDirPath = malloc((strlen(notesDirPath)+1)*sizeof(char));
strcpy(lsDirPath, notesDirPath);
}
// Open the directory to list
lsDir = opendir(lsDirPath);
if ( lsDir == NULL ) {
fprintf(stderr, "ERROR: Cannot open directory '%s'\n", lsDirPath);
return 2;
}
// Count files so we can allocate enough pointers for the filenames
while ((entry = readdir(lsDir)) != NULL) {
numOfFiles++;
}
fileNames = malloc(numOfFiles*sizeof(char*));
// Reread the dir to record the names
rewinddir(lsDir);
uint i = 0;
while ((entry = readdir(lsDir)) != NULL) {
if (entry) {
fileNames[i]=malloc((strlen(entry->d_name)+1)*sizeof(char));
strcpy(fileNames[i], entry->d_name);
i++;
}
}
// Sort the file names alphabetically
char** sortedFileNames = sortStrArray(fileNames, numOfFiles);
// Filter the file names by type
for (uint i = 0; i < numOfFiles; i++) {
// Construct full file path for stat()
statFilePath = realloc(statFilePath, (strlen(lsDirPath)+strlen(sortedFileNames[i])+2)*sizeof(char));
sprintf(statFilePath, "%s/%s", lsDirPath, sortedFileNames[i]);
stat(statFilePath, &statBuf);
// Check file extension to identify txt files
sprintf(fileExt, "%.*s", 4, sortedFileNames[i]+strlen(sortedFileNames[i])-4);
isTxtFile = !strcmp(fileExt, ".txt");
// Append a "/" to the names of directories
if ( (statBuf.st_mode & S_IFMT) == S_IFDIR) {
sortedFileNames[i] = realloc(sortedFileNames[i], (strlen(sortedFileNames[i])+2)*sizeof(char));
strcat(sortedFileNames[i], "/");
}
// Hide extension on note names
else if (isTxtFile) {
sortedFileNames[i][strlen(sortedFileNames[i])-4]='\0';
}
// Hide files that aren't txt or dir
else {
free(sortedFileNames[i]);
sortedFileNames[i] = NULL;
}
}
// Print file names. Start at 2 to hide "." and ".."
for (uint i=2; i < numOfFiles; i++) {
if (sortedFileNames[i]) {
puts(sortedFileNames[i]);
}
}
return 0;
}
uint addNote(int argc, char** argv, char* notesDirPath) {
char* notePath = NULL;
FILE* noteFile = NULL;
if ( argc > 4 ) {
fputs("ERROR: Too many arguments to subcommand 'add'\n", stderr);
return 3;
}
else if ( argc < 4 ) {
fputs("ERROR: 'add' requires a note name argument and a string to add\n", stderr);
return 3;
}
notePath = malloc((strlen(notesDirPath)+strlen(argv[2])+6)*sizeof(char));
sprintf(notePath, "%s/%s.txt", notesDirPath, argv[2]);
noteFile = fopen(notePath, "a+");
if ( noteFile == NULL ) {
fprintf(stderr, "ERROR: failed to open file '%s'\n", notePath);
return 2;
}
fseek(noteFile, 0, SEEK_SET);
int firstChar = fgetc(noteFile); // int to fit EOF
fseek(noteFile, -1, SEEK_END);
char lastChar = fgetc(noteFile);
fseek(noteFile, 0, SEEK_END);
if ( lastChar == '\n' || firstChar == EOF ) {
fprintf(noteFile, "%s\n", argv[3]);
}
else {
fprintf(noteFile, "\n%s\n", argv[3]);
}
return 0;
}
uint editNote(int argc, char** argv, char* notesDirPath) {
if ( argc > 3 ) {
fputs("ERROR: Too many arguments to subcommand 'edit'\n", stderr);
return 3;
}
else if ( argc < 3 ) {
fputs("ERROR: 'edit' requires a note name argument\n", stderr);
return 3;
}
char* notePath = malloc((strlen(notesDirPath)+strlen(argv[2])+6)*sizeof(char));
sprintf(notePath, "%s/%s.txt", notesDirPath, argv[2]);
char* editor = getenv("EDITOR");
if (editor == NULL) {
fputs("ERROR: environment variable 'EDITOR' not set\n", stderr);
return 1;
}
char* editComm = malloc((strlen(editor)+strlen(notePath)+4)*sizeof(char));
sprintf(editComm, "%s \"%s\"", editor, notePath);
system(editComm);
return 0;
}
uint rmNote(int argc, char** argv, char* notesDirPath) {
if ( argc > 3 ) {
fputs("ERROR: Too many arguments to subcommand 'rm'\n", stderr);
return 3;
}
else if ( argc < 3 ) {
fputs("ERROR: 'rm' requires a note name argument\n", stderr);
return 3;
}
char* notePath = malloc((strlen(notesDirPath)+strlen(argv[2])+6)*sizeof(char));
sprintf(notePath, "%s/%s.txt", notesDirPath, argv[2]);
return remove(notePath);
}
uint cpNote(int argc, char** argv, char* notesDirPath, uint subComm) {
if ( argc > 4 ) {
fputs("ERROR: Too many arguments to subcommand 'mv'\n", stderr);
return 3;
}
else if ( argc < 4 ) {
fputs("ERROR: 'mv' requires two note name arguments\n", stderr);
return 3;
}
char* srcNotePath = malloc((strlen(notesDirPath)+strlen(argv[2])+6)*sizeof(char));
sprintf(srcNotePath, "%s/%s.txt", notesDirPath, argv[2]);
char* destNotePath = malloc((strlen(notesDirPath)+strlen(argv[3])+6)*sizeof(char));
sprintf(destNotePath, "%s/%s.txt", notesDirPath, argv[3]);
// char* noteContent = NULL;
// ulong length = 0;
int c = '\0'; // Character buffer to copy contents of file. int instead of char to fit EOF
FILE* srcNoteFile = NULL;
FILE* destNoteFile = NULL;
struct stat statBuf;
if ( stat(srcNotePath, &statBuf) != 0 ) {
fprintf(stderr, "ERROR: source file '%s' does not exist\n", srcNotePath);
return 3;
}
if ( stat(destNotePath, &statBuf) == 0 ) {
fprintf(stderr, "ERROR: destination file '%s' already exists\n", destNotePath);
return 3;
}
srcNoteFile = fopen(srcNotePath, "r");
if ( !srcNoteFile ) {
fprintf(stderr, "ERROR: failed to open file '%s'\n", srcNotePath);
return 2;
}
destNoteFile = fopen(destNotePath, "w");
if ( destNoteFile == NULL ) {
fputs("ERROR: failed to create destination file\n", stderr);
return 2;
}
while ( (c = fgetc(srcNoteFile)) != EOF ) {
fputc(c, destNoteFile);
}
fclose(srcNoteFile);
fclose(destNoteFile);
if ( subComm == mvComm ) remove(srcNotePath);
return 0;
}
uint mkNoteDir(int argc, char** argv, char* notesDirPath) {
if ( argc > 3 ) {
fputs("ERROR: Too many arguments to subcommand 'mkdir'\n", stderr);
return 3;
}
else if ( argc < 3 ) {
fputs("ERROR: 'mkdir' requires a new directory name argument\n", stderr);
return 3;
}
char* newDirPath = malloc((strlen(notesDirPath)+strlen(argv[2])+1)*sizeof(char));
sprintf(newDirPath, "%s/%s", notesDirPath, argv[2]);
return mkdir(newDirPath, 775);
}
uint rmNoteDir(int argc, char** argv, char* notesDirPath) {
if ( argc > 3 ) {
fputs("ERROR: Too many arguments to subcommand 'rmdir'\n", stderr);
return 3;
}
else if ( argc < 3 ) {
fputs("ERROR: 'rmdir' requires a directory name argument\n", stderr);
return 3;
}
struct stat statBuf;
char* rmDirPath = malloc((strlen(notesDirPath)+strlen(argv[2])+1)*sizeof(char));
sprintf(rmDirPath, "%s/%s", notesDirPath, argv[2]);
if ( stat(rmDirPath, &statBuf) != 0 ) {
fprintf(stderr, "ERROR: directory '%s' does not exist\n", rmDirPath);
return 3;
}
int removed = rmdir(rmDirPath);
if ( ! (removed == 0) ) {
fprintf(stderr, "ERROR: failed to remove '%s'. It may be non-empty.\n", rmDirPath);
return 2;
}
return 0;
}