-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
199 lines (147 loc) · 3.39 KB
/
main.c
File metadata and controls
199 lines (147 loc) · 3.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
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
#include <stdio.h>
#include <jsh.h>
#include <ext.h>
#include <string.h>
#include <membox.h>
struct fs_t *fs;
#define MAX_OPEN_FILES 12
extern uint8_t *memory;
struct ext2_file *open_files[MAX_OPEN_FILES];
int place = 0;
int open_file_no = 0;
int ls(char **args){
list_files(args);
}
int get_size(char **args){
int count = 0;
while(*args != NULL){
count++;
args++;
}
return count;
}
int _display_mem(char **args){
for(int i = 0; i < 60; i++){
for(int j = 0; j < 512; j++){
if((memory[i*j + j]) == 0){
printf("0 ");
}else{
printf("%c ", (char)memory[i*j + j]);
}
}
printf("\n");
}
}
int __fwrite(char **args){
if(get_size(args) < 2){
printf("usage: fw <file_name> <word>\n");
return 0;
}
for(int i =0; i < MAX_OPEN_FILES; i++){
if(!strcmp(open_files[i]->name, args[0])){
if(!open_files[i]->open){
printf("Please open file before attempting to write to it\n");
return 0;
}
_fputs(open_files[i], args[1]);
return 0;
}
}
}
int __get_pos(char **args){
if(get_size(args) < 1){
return 0;
}
for(int i =0; i < MAX_OPEN_FILES; i++){
if(!strcmp(open_files[i]->name, args[0])){
if(!open_files[i]->open){
printf("Please open file before attempting to read its data\n");
return 0;
}
printf("%d\n", open_files[i]->pos);
return 0;
}
}
}
int __fclose(char **args){
if(get_size(args) < 1){
printf("usage: fc <file_name>\n");
return 0;
}
for(int i =0; i < MAX_OPEN_FILES; i++){
if(!strcmp(open_files[i]->name, args[0])){
fs->fclose(fs, open_files[i]);
return 0;
}
}
}
int get_place(){
for(int i =0; i < MAX_OPEN_FILES; i++){
if(open_files[i] == NULL){
return i;
}
}
return -1;
}
int fropen(char **args){
if(get_size(args) < 2){
printf("usage: fo <file_name> <read/write>\n");
return 0;
}
if(open_file_no == MAX_OPEN_FILES){
printf("too many open files\n");
return 0;
}
place = get_place();
if(args[1][0] == 'r'){
open_files[place] = fs->fopen(fs, args[0], 'r');
}else if(args[1][0] == 'w'){
open_files[place] = fs->fopen(fs, args[0], 'w');
}
if(open_files[place] != NULL){
open_file_no++;
}
return 0;
}
int __fread(char **args){
if(get_size(args) < 1){
printf("usage: fr <file_name>");
}
for(int i = 0; i < 3; i++){
if(!strcmp(open_files[i]->name, args[0])){
char *buffer = _fgets(open_files[i]);
if(buffer != NULL){
printf("%s\n", buffer);
}
return 0;
}
}
}
int __fread_char(char **args){
if(get_size(args) < 1){
printf("usage: fr <file_name>");
}
for(int i = 0; i < MAX_OPEN_FILES; i++){
if(!strcmp(open_files[i]->name, args[0])){
char buf = _fgetc(open_files[i]);
printf("%c\n", buf);
return 0;
}
}
}
int main(void){
ext2_init(&fs);
struct membox *mem = init();
fs->parent = (struct block_t*)mem;
mem->block.fs = fs;
register_command("ls", ls);
register_command("fr", __fread);
register_command("fo", fropen);
register_command("fw", __fwrite);
register_command("fc", __fclose);
register_command("dm", _display_mem);
register_command("gp", __get_pos);
register_command("frc", __fread_char);
lsh_loop();
return 0;
}