-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotes.c
More file actions
149 lines (114 loc) · 2.39 KB
/
notes.c
File metadata and controls
149 lines (114 loc) · 2.39 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
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<dirent.h>
#include "config.h"
void syntaxError(){
printf("invalid syntax, use --set --delete --read --list (-s -d -r -l)\n");
exit(1);
}
void setNote(char name[]){
FILE *fptr;
char dir[] = DIRC;
struct stat st = {0};
if (stat(dir, &st) == -1) {
mkdir(dir, 0700);
}
strcat(dir, name);
fptr = fopen(dir, "w");
if (fptr == NULL){
printf("failed to open file %s\n",dir);
exit(2);
}
char info[200];
char date[15];
printf("about this note: ");
scanf("%199[^\n]", info);
printf("date due: ");
scanf("%s",date);
fprintf(fptr, "%s\n%s\n", date, info);
fclose(fptr);
}
void deleteNote(char name[]){
char dir[] = DIRC;
strcat(dir, name);
int ret = remove(dir);
if (ret != 0){
printf("failed to delete file %s\n",dir);
exit(3);
}
exit(0);
}
void readNote(char name[]){
FILE *fptr;
char dir[] = DIRC;
strcat(dir, name);
fptr = fopen(dir, "r");
if (fptr == NULL){
printf("failed to open file %s%s\n",dir,name);
exit(2);
}
char c;
while((c=fgetc(fptr))!=EOF){
printf("%c",c);
}
fclose(fptr);
exit(0);
}
void listNotes(){
DIR *d;
struct dirent *dir;
d = opendir(DIRC);
if (d){
int i = 0;
while ((dir = readdir(d)) != NULL){
char *fileName = dir->d_name;
if (strcmp(fileName,".") && strcmp(fileName,"..")){
if (i == 0){
printf("| NAME | DUE |\n");
}
i++;
char dir[] = DIRC;
strcat(dir, fileName);
FILE *fptr;
fptr = fopen(dir, "r");
if (fptr == NULL){
printf("failed to read files\n");
exit(4);
}
char dueDate[15];
fscanf(fptr, "%s", dueDate);
printf("| %s | %s |\n", fileName, dueDate);
}
}
closedir(d);
}
exit(0);
}
int main(int argc, char *argv[]){
if (argc <= 1){
syntaxError();
}
if (!strcmp(argv[1], "-s") || !strcmp(argv[1],"--set")){
if (argc >= 3){
setNote(argv[2]);
}
else{ syntaxError(); }
}else if (!strcmp(argv[1], "-d") || !strcmp(argv[1],"--delete")){
if (argc >= 3){
deleteNote(argv[2]);
}
else{ syntaxError(); }
}else if (!strcmp(argv[1], "-r") || !strcmp(argv[1],"--read")){
if (argc >= 3){
readNote(argv[2]);
}
else{ syntaxError(); }
}else if (!strcmp(argv[1], "-l") || !strcmp(argv[1],"--list")){
listNotes();
}else{ syntaxError(); }
return 0;
}