forked from kcdon/File-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·132 lines (99 loc) · 3.11 KB
/
main.c
File metadata and controls
executable file
·132 lines (99 loc) · 3.11 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
#define __USE_XOPEN
#define _GNU_SOURCE
#include "dirtree.h"
#include "cmds.h"
#include "simsys.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
struct file_loaddata {
char *name;
long filesize;
time_t timestamp;
};
int main(int argc, char *argv[]) {
/* the filesystem and block sizes, in bytes */
long blk_size = 0;
long fs_size = 0;
int i;
for (i = 1; argv[i]; i++) {
if (!strcmp(argv[i], "-b")) {
/* User sets a value for the block size */
if (argv[i+1]) {
blk_size = atol(argv[i+1]);
i++;
} else {
printf("\033[1m\033[33mWarning\033[0m: Provided flag %s without value\n", argv[i]);
}
} else if (!strcmp(argv[i], "-s")) {
/* User has a custom filesystem capacity */
if (argv[i+1]) {
fs_size = atol(argv[i+1]);
i++;
} else
printf("\033[1m\033[33mWarning\033[0m: Provided flag %s without value\n", argv[i]);
}
}
if (!blk_size) {
/* Check whether or not a block size was given */
printf("\033[1m\033[33mWarning\033[0m: Block size not specified; defaulting to 512B\n");
blk_size = 512;
} else {
printf("Using block size of %ldB\n", blk_size);
}
if (!fs_size) {
/* Check whether or not a filesystem capacity was given */
printf("\033[1m\033[33mWarning\033[0m: Filesystem size not specified; defaulting to 64kB\n");
fs_size = 65536;
} else {
printf("Using filesystem size of %ldB\n", fs_size);
}
/* Initialize the filesystem */
init_filesystem(blk_size, fs_size);
while (1) {
char buff[64];
char *cmd = malloc(sizeof(char));
char **arg_vec;
int n, len;
len = 0;
printf("\033[1m\033[32m" "oslab@IIT(BHU)\033[0m:\033[1m\033[34m");
/* Show present path */
arg_vec = pathVecOfTree(getWorkDirNode());
for (n = 0; arg_vec[n]; n++)
printf("%s%s", arg_vec[n], arg_vec[n+1] ? "/" : "");
free_str_vec(arg_vec);
printf("\033[0m$ ");
fflush(stdout);
/* Read from stdin */
while ((n = read(0, buff, 64))) {
char *tmp = (char*) realloc(cmd, (len+65)*sizeof(char));
if (!tmp) {
tmp = (char*) malloc((len+65) * sizeof(char));
strcpy(tmp, cmd);
free(cmd);
}
cmd = tmp;
n = 0;
while (n < 64) {
if (buff[n] == '\n') {
cmd[len+n] = '\0';
break;
}
cmd[len+n] = buff[n];
n++;
}
len += n;
if (n < 64)
break;
}
cmd[len] = '\0';
/* Tokenize the command */
arg_vec = str_to_vec(cmd, ' ');
free(cmd);
/* Run the command */
cmd_exec(arg_vec);
free_str_vec(arg_vec);
}
}