|
1 | | -/************************************* |
2 | | - Terminal Notes Utility written in C |
3 | | - ************************************* |
4 | | - * Simple: Create and manage notes from the terminal |
5 | | - ************************************* |
6 | | - * Developed and engineered by |
7 | | - * Felipe Alfonso Gonzalez <f.alfonso@res-ear.ch> |
8 | | - * Computer Science Engineer |
9 | | - * Chile |
10 | | - ************************************* |
11 | | - * To create the term_notes binary: |
12 | | - * |
13 | | - * 1. Extract the source code if needed: |
14 | | - * tar -xf term_notes.tar.gz |
15 | | - * |
16 | | - * 2. Compile the source code: |
17 | | - * gcc -o term_notes term_notes.c |
18 | | - * |
19 | | - * 3. Elevate to superuser (if necessary): |
20 | | - * sudo su |
21 | | - * |
22 | | - * 4. Move the binary to the system bin directory: |
23 | | - * mv term_notes /usr/local/bin/ |
24 | | - * |
25 | | - * 5. Exit superuser mode: |
26 | | - * exit |
27 | | - * |
28 | | - * 6. You can now run term_notes from the terminal: |
29 | | - * term_notes |
30 | | - * |
31 | | - ************************************* |
32 | | - * Please note that term_notes requires the following dependencies: |
33 | | - * - Nano: A text editor (sudo apt-get install nano) |
34 | | - * - Vim: A text editor (sudo apt-get install vim) |
35 | | - * |
36 | | - * If any of these dependencies are not installed, please install them |
37 | | - * using the provided commands. |
38 | | - * |
39 | | - * For more information, please refer to the documentation. |
40 | | - ************************************* |
41 | | - */ |
42 | | - |
43 | 1 | #include <stdio.h> |
44 | 2 | #include <stdlib.h> |
45 | 3 | #include <string.h> |
46 | 4 | #include <unistd.h> |
| 5 | +#include <ctype.h> |
47 | 6 |
|
48 | 7 | #define MAX_NOTES 100 |
49 | 8 |
|
@@ -76,7 +35,11 @@ void addNote() { |
76 | 35 |
|
77 | 36 | printf("Choose the editor (1. Nano, 2. Vim): "); |
78 | 37 | int editorChoice; |
79 | | - scanf("%d", &editorChoice); |
| 38 | + while (scanf("%d", &editorChoice) != 1) { |
| 39 | + while (getchar() != '\n'); |
| 40 | + printf("Invalid editor choice. Please enter a number.\n"); |
| 41 | + printf("Choose the editor (1. Nano, 2. Vim): "); |
| 42 | + } |
80 | 43 |
|
81 | 44 | char tempFileName[20]; |
82 | 45 | sprintf(tempFileName, "temp_note_%d.txt", getpid()); |
@@ -152,7 +115,11 @@ void editNote() { |
152 | 115 |
|
153 | 116 | int choice; |
154 | 117 | printf("Enter the note number to edit: "); |
155 | | - scanf("%d", &choice); |
| 118 | + while (scanf("%d", &choice) != 1) { |
| 119 | + while (getchar() != '\n'); |
| 120 | + printf("Invalid note number. Please enter a number.\n"); |
| 121 | + printf("Enter the note number to edit: "); |
| 122 | + } |
156 | 123 |
|
157 | 124 | if (choice < 1 || choice > count) { |
158 | 125 | printf("Invalid note number.\n"); |
@@ -186,7 +153,11 @@ void editNote() { |
186 | 153 |
|
187 | 154 | printf("Choose the editor (1. Nano, 2. Vim): "); |
188 | 155 | int editorChoice; |
189 | | - scanf("%d", &editorChoice); |
| 156 | + while (scanf("%d", &editorChoice) != 1) { |
| 157 | + while (getchar() != '\n'); |
| 158 | + printf("Invalid editor choice. Please enter a number.\n"); |
| 159 | + printf("Choose the editor (1. Nano, 2. Vim): "); |
| 160 | + } |
190 | 161 |
|
191 | 162 | char tempFileName[20]; |
192 | 163 | sprintf(tempFileName, "temp_note_%d.txt", getpid()); |
@@ -253,7 +224,11 @@ void deleteNote() { |
253 | 224 |
|
254 | 225 | int id; |
255 | 226 | printf("Enter the note ID to delete: "); |
256 | | - scanf("%d", &id); |
| 227 | + while (scanf("%d", &id) != 1) { |
| 228 | + while (getchar() != '\n'); |
| 229 | + printf("Invalid note ID. Please enter a number.\n"); |
| 230 | + printf("Enter the note ID to delete: "); |
| 231 | + } |
257 | 232 |
|
258 | 233 | FILE *file = fopen("notes.txt", "r"); |
259 | 234 | if (file != NULL) { |
@@ -291,9 +266,13 @@ void deleteAllNotes() { |
291 | 266 |
|
292 | 267 | printf("Are you sure you want to delete all notes? (Y/N): "); |
293 | 268 | char confirm; |
294 | | - scanf(" %c", &confirm); |
| 269 | + while (scanf(" %c", &confirm) != 1 || (tolower(confirm) != 'y' && tolower(confirm) != 'n')) { |
| 270 | + while (getchar() != '\n'); |
| 271 | + printf("Invalid choice. Please enter 'Y' or 'N'.\n"); |
| 272 | + printf("Are you sure you want to delete all notes? (Y/N): "); |
| 273 | + } |
295 | 274 |
|
296 | | - if (confirm == 'Y' || confirm == 'y') { |
| 275 | + if (tolower(confirm) == 'y') { |
297 | 276 | FILE *file = fopen("notes.txt", "w"); |
298 | 277 | if (file != NULL) { |
299 | 278 | fclose(file); |
@@ -324,7 +303,11 @@ void showNote() { |
324 | 303 |
|
325 | 304 | int id; |
326 | 305 | printf("Enter the note ID to show: "); |
327 | | - scanf("%d", &id); |
| 306 | + while (scanf("%d", &id) != 1) { |
| 307 | + while (getchar() != '\n'); |
| 308 | + printf("Invalid note ID. Please enter a number.\n"); |
| 309 | + printf("Enter the note ID to show: "); |
| 310 | + } |
328 | 311 |
|
329 | 312 | FILE *file = fopen("notes.txt", "r"); |
330 | 313 | if (file != NULL) { |
@@ -363,7 +346,11 @@ void notesMenu() { |
363 | 346 | do { |
364 | 347 | showNotesMenu(); |
365 | 348 | printf("Enter an option: "); |
366 | | - scanf("%d", &option); |
| 349 | + while (scanf("%d", &option) != 1) { |
| 350 | + while (getchar() != '\n'); |
| 351 | + printf("Invalid option. Please enter a number.\n"); |
| 352 | + printf("Enter an option: "); |
| 353 | + } |
367 | 354 |
|
368 | 355 | switch (option) { |
369 | 356 | case 1: |
@@ -402,7 +389,11 @@ void mainMenu() { |
402 | 389 | printf("0. Exit\n"); |
403 | 390 | printf("******************\n"); |
404 | 391 | printf("Enter an option: "); |
405 | | - scanf("%d", &option); |
| 392 | + while (scanf("%d", &option) != 1) { |
| 393 | + while (getchar() != '\n'); |
| 394 | + printf("Invalid option. Please enter a number.\n"); |
| 395 | + printf("Enter an option: "); |
| 396 | + } |
406 | 397 |
|
407 | 398 | switch (option) { |
408 | 399 | case 1: |
|
0 commit comments