-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
183 lines (158 loc) · 6.26 KB
/
main.c
File metadata and controls
183 lines (158 loc) · 6.26 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
char input[1024];
char current_mode[50] = "";
char *subjects[] = {"Chemistry", "Mechanics", "Electronics", "C_Programming"};
int num_subjects = 4;
while(1) {
if (strlen(current_mode) > 0) {
printf("[%s] study-sh> ", current_mode);
} else {
printf("study-sh> ");
}
if (!fgets(input, 1024, stdin)) break;
input[strcspn(input, "\n")] = 0;
if (strcmp(input, "exit") == 0 || strcmp(input, "sleep") == 0) {
printf("Shutting down workspace.\n");
break;
}
else if (strcmp(input, "build") == 0) {
printf("Initializing workspace...\n");
mkdir("Notes", 0777);
for (int i = 0; i < num_subjects; i++) {
char path[100];
sprintf(path, "Notes/%s", subjects[i]);
if (mkdir(path, 0777) == 0) {
printf("Created: %s\n", path);
} else {
printf("Exists: %s\n", path);
}
}
}
else if (strcmp(input, "help") == 0) {
printf("\n--- study-sh ---\n");
printf(" help : Show commands\n");
printf(" build : Initialize workspace folders\n");
printf(" [subject] : Enter subject mode (e.g., mechanics)\n");
printf(" add [note] : Append a note to the current subject\n");
printf(" read : Display notes for the current subject\n");
printf(" leave : Exit current subject mode\n");
printf(" search [word]: Search notes for a specific keyword\n");
printf(" clear : Clear the terminal screen\n");
printf(" cd [dir] : Change directory\n");
printf(" exit/sleep : Shut down\n");
printf("----------------\n\n");
}
else if (strcmp(input, "leave") == 0) {
strcpy(current_mode, "");
}
else if (strncmp(input, "add ", 4) == 0) {
if (strlen(current_mode) == 0) {
printf("Error: No subject mode active.\n");
} else {
char *note_text = input + 4;
char filepath[200];
sprintf(filepath, "Notes/%s/notes.txt", current_mode);
FILE *file = fopen(filepath, "a");
if (file == NULL) {
printf("Error: Could not open %s.\n", filepath);
} else {
fprintf(file, "- %s\n", note_text);
fclose(file);
printf("Note appended.\n");
}
}
}
else if (strcmp(input, "read") == 0) {
if (strlen(current_mode) == 0) {
printf("Error: No subject mode active.\n");
} else {
char filepath[200];
sprintf(filepath, "Notes/%s/notes.txt", current_mode);
FILE *file = fopen(filepath, "r");
if (file == NULL) {
printf("Empty: No notes found for %s.\n", current_mode);
} else {
printf("\n--- %s ---\n", current_mode);
char line[256];
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
}
printf("----------------\n\n");
fclose(file);
}
}
}
else if (strcmp(input, "clear") == 0) {
system("clear");
}
else if (strncmp(input, "search ", 7) == 0) {
if (strlen(current_mode) == 0) {
printf("Error: No subject mode active.\n");
} else {
char *keyword = input + 7;
char filepath[200];
sprintf(filepath, "Notes/%s/notes.txt", current_mode);
FILE *file = fopen(filepath, "r");
if (file == NULL) {
printf("Empty: No notes found for %s.\n", current_mode);
} else {
printf("\n--- Search Results for '%s' ---\n", keyword);
char line[256];
int found = 0;
while (fgets(line, sizeof(line), file) != NULL) {
if (strstr(line, keyword) != NULL) {
printf("%s", line);
found = 1;
}
}
if (found == 0) {
printf("No matches found.\n");
}
printf("----------------\n\n");
fclose(file);
}
}
}
else if (strcmp(input, "chemistry") == 0) strcpy(current_mode, "Chemistry");
else if (strcmp(input, "mechanics") == 0) strcpy(current_mode, "Mechanics");
else if (strcmp(input, "electronics") == 0) strcpy(current_mode, "Electronics");
else if (strcmp(input, "c_programming") == 0) strcpy(current_mode, "C_Programming");
// MOVED: The 'cd' command goes HERE, right before the final else
else if (strncmp(input, "cd ", 3) == 0) {
char *target_dir = input + 3;
if (chdir(target_dir) != 0) {
perror("cd failed");
}
}
// FINAL FALLBACK: Ask the OS to handle unknown commands
else {
char *args[100];
int i = 0;
args[i] = strtok(input, " ");
while (args[i] != NULL) {
i++;
args[i] = strtok(NULL, " ");
}
pid_t pid = fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
printf("Command not found: %s\n", args[0]);
}
exit(1);
}
else if (pid > 0) {
wait(NULL);
}
else {
printf("Error: Process cloning failed.\n");
}
}
}
return 0;
}