#include #include #include #include #include #include "fakedisc.h" /* Reminders from fakedisc.h typedef unsigned char byte; typedef byte block[512]; int createdisc(const char *name, int number_of_blocks); int mountdisc(const char *name); void dismountdisc(int discnum); int discsize(int discnum); int writeblock(int discnum, int blocknum, void *b); int readblock(int discnum, int blocknum, void *b); */ int dnum = -1; const int maximum_name_length = 24; const int direntries_per_block = 16; const int firstdirblock=1, lastdirblock=4; struct direntry { char name[maximum_name_length]; int firstblock; int size; }; struct dirblock { direntry entry[direntries_per_block]; }; bool filenameequal(char * s, char * t) { for (int i=0; i' ' && c<='~') putchar(c); else printf("[%d]", c); } } direntry findfile(char * name) { for (int i = firstdirblock; i<=lastdirblock; i+=1) { dirblock buffer; readblock(dnum, i, & buffer); for (int j=0; j=' ' && c<='~') putchar(c); else if (c=='\n') printf("\\n\n"); else printf("[%d]", c); } void printstring(char * s) { for (int i=0; s[i]!=0; i+=1) printchar(s[i]); } void command_create(char * part[]) { bool ok = createdisc(part[1], atol(part[2])); if (ok) { dnum = mountdisc(part[1]); printf("OK, \"%s\", %d blocks\n", part[1], discsize(dnum)); } else { printf("create failed\n"); return; } dirblock buffer; for (int i=0; i= 0) dismountdisc(dnum); } void main(int argc, char * argv[]) { char line[1000]; char * part[100]; int numparts; dnum = -1; if (argc > 1) command_mount(argv[1]); while (true) { printf("> "); char * s = fgets(line, 999, stdin); if (s==NULL) break; numparts = 0; s = strtok(line, " \r\n"); while (s != NULL) { part[numparts] = s; numparts += 1; s = strtok(NULL, " \r\n"); } if (numparts == 0) continue; else if (strcmp(part[0], "?") == 0) { printf(" create name number-of-blocks\n"); printf(" dir\n"); printf(" m name\n"); printf(" make name first-block-number number-of-bytes real-source-file\n"); printf(" r block-number\n"); printf(" read name\n"); printf(" w block-number string-with-no-spaces\n"); printf(" x\n"); } else if (strcasecmp(part[0], "create") == 0 && numparts == 3) command_create(part); else if (strcasecmp(part[0], "dir") == 0 && numparts == 1) command_dir(); else if (strcasecmp(part[0], "m") == 0 && numparts == 2) command_mount(part[1]); else if (strcasecmp(part[0], "make") == 0 && numparts == 5) command_make(part); else if (strcasecmp(part[0], "r") == 0 && numparts == 2) command_readblock(part[1]); else if (strcasecmp(part[0], "read") == 0 && numparts == 2) command_read(part[1]); else if (strcasecmp(part[0], "x") == 0 && numparts == 1) { command_exit(); break; } else printf("unrecognised command \"%s\"\n", part[0]); } }