/*********************************************************** * Name of program: * Authors: * Description: Starter code for project 4 **********************************************************/ /* These are the included libraries. You may need to add more. */ #include #include #include #include #include #include #include #include #include /* Put any symbolic constants (defines)*/ #define True 1 /* C has no booleans! */ #define False 0 #define MAX_CMD 80 /* These are globals. You set these values once in the main function when the program starts. */ uint16_t bPerSec = 0; uint8_t secPer = 0; uint16_t resSec = 0; uint8_t numFats = 0; uint32_t fatsz32 = 0; int fd; uint32_t root_addr=0; /******************************************** * This function displays the image's info *******************************************/ void print_info(){ printf("BPB_BytesPerSec is 0x%x, %i\n",bPerSec, bPerSec); printf("BPB_SecPerClus is 0x%x, %i\n",secPer, secPer); printf("BPB_RsvdSecCnt is 0x%x, %i\n",resSec, resSec); printf("BPB_NumFATs is 0x%x, %i\n",numFats, numFats); printf("BPB_FATSz32 is 0x%x, %i\n",fatsz32, fatsz32); //TODO: Figure out the root address //printf("Root addr is 0x%x\n", root_addr); printf("\n"); } /* This is the main function of your project, and it will be run * first before all other functions. */ int main(int argc, char *argv[]) { char cmd_line[MAX_CMD]; /* Parse args and open our image file */ fd = open("fat32.img", O_RDONLY); /* Check to see if it opened successfully */ if(fd == -1) { perror(argv[1]); return -1; } /* Parse boot sector and get information */ /* Seek to byte 11 in the file */ lseek(fd,11, SEEK_SET); /* Read and store BPB_BytesPerSec for 2 bytes */ /* Note: use the & when reading into numbers. You don't need to * use it if you are reading into strings. */ read(fd, &bPerSec,2); bPerSec = htole16(bPerSec); // convert to little endian order /* Read and store BPB_SecPerClus for 1 bytes */ read(fd, &secPer,1); /* Read and store BPB_RsvdSecCnt for 2 bytes */ read(fd, &resSec,2); resSec = htole16(resSec); /* Read and store BPB_NumFATS for 1 bytes */ read(fd, &numFats,1); /* Seek to byte 36 in the file */ lseek(fd, 36, SEEK_SET); /* Read and store BPB_FATSz32 for 4 bytes */ read(fd, &fatsz32,4); fatsz32 = htole32(fatsz32); /* Get root directory address */ // TODO: You will have to do some computations and additional // reads/seeks in the image file to compute the root directory // address. You will also want to seek to the root directory. //You will want to do that here. // root_addr = ?? /* Main loop. You probably want to create a helper function for each command besides quit. */ while(True) { /* Zero the command line, just in case */ bzero(cmd_line, MAX_CMD); /* Print the prompt */ printf("]"); /* Get input from the user into cmd_line */ fgets(cmd_line,MAX_CMD,stdin); /* Start comparing input */ if(strncmp(cmd_line,"info",4)==0) { print_info(); //TODO: Finish this function! } else if(strncmp(cmd_line,"volume",6)==0) { printf("Not yet implemented.\n\n"); } else if(strncmp(cmd_line,"stat",4)==0) { printf("Not yet implemented.\n\n"); } else if(strncmp(cmd_line,"cd",2)==0) { printf("Not yet implemented.\n\n"); } else if(strncmp(cmd_line,"ls",2)==0) { printf("Not yet implemented.\n\n"); } else if(strncmp(cmd_line,"deleted",7)==0) { printf("Not yet implemented.\n\n"); } else if(strncmp(cmd_line,"read",4)==0) { printf("Not yet implemented.\n\n"); } else if(strncmp(cmd_line,"quit",4)==0) { printf("Quitting.\n\n"); break; } else { printf("Unrecognized command.\n\n"); } } /* Close the file */ close(fd); return 0; /* Success */ }