~% cd Desktop/V3 V3% gcc -o extract_file extract_file.c V3%./extract_file disk1.img CONFIG.SYS File not found in root directory. V3%./extract_file DISK1.img CONFIG.SYS File not found in root directory. DISK1 V3 % 1s disk1.img V3% extract_file extract_file.c > Name < > DISK1 Date Modified » a Size .fseventsd ATTRIB.EXE AUTOEXEC.BAT CHKDSK.EXE COMMAND.COM CONFIG.SYS COUNTRY.SYS COUNTRY.TX Yesterday at 7:37 PM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 37 37 DEBUG.EXE May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 1 5 13B 2 DEFRAG.EXE May 31, 1994 at 6:22 AM DEFRAG.HL May 31, 1994 at 6:22 AM DOSSETUP.INI May 31, 1994 at 6:22 AM DRVSPACE.BIN May 31, 1994 at 6:22 AM EDIT.COM EGA.CP_ EGA2.CP_ May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 413 b 1 EGA3.CP May 31, 1994 at 6:22 AM 1 EMM386.EX_ May 31, 1994 at 6:22 AM 5 EXPAND.EXE FDISK.EXE FORMAT.COM IO.SYS May 31, 1994 at 6:22 AM 1 May 31, 1994 at 6:22 AM 2 May 31, 1994 at 6:22 AM 2 < > V3 Name DISK1 disk1.img extract_file extract_file.c QBASIC.EXE iCloud Drive > I Desktop ISO.CP_ KEYB.COM KEYBOARD.SYS KEYBRD2.SY_ MEM.EX_ MSCDEX.EXE MSDOS.SYS NETWORKS.TXT May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 1 May 31, 1994 at 6:22 AM 3 May 31, 1994 at 6:22 AM 1 May 31, 1994 at 6:22 AM 2 May 31, 1994 at 6:22 AM 2 May 31, 1994 at 6:22 AM 3 May 31, 1994 at 6:22 AM NLSFUNC.EXE PACKING.LST May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 19 V3 > DISK1
~% cd Desktop/V3 V3% gcc -o extract_file extract_file.c V3%./extract_file disk1.img CONFIG.SYS File not found in root directory. V3%./extract_file DISK1.img CONFIG.SYS File not found in root directory. DISK1 V3 % 1s disk1.img V3% extract_file extract_file.c > Name < > DISK1 Date Modified » a Size .fseventsd ATTRIB.EXE AUTOEXEC.BAT CHKDSK.EXE COMMAND.COM CONFIG.SYS COUNTRY.SYS COUNTRY.TX Yesterday at 7:37 PM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 37 37 DEBUG.EXE May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 1 5 13B 2 DEFRAG.EXE May 31, 1994 at 6:22 AM DEFRAG.HL May 31, 1994 at 6:22 AM DOSSETUP.INI May 31, 1994 at 6:22 AM DRVSPACE.BIN May 31, 1994 at 6:22 AM EDIT.COM EGA.CP_ EGA2.CP_ May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 413 b 1 EGA3.CP May 31, 1994 at 6:22 AM 1 EMM386.EX_ May 31, 1994 at 6:22 AM 5 EXPAND.EXE FDISK.EXE FORMAT.COM IO.SYS May 31, 1994 at 6:22 AM 1 May 31, 1994 at 6:22 AM 2 May 31, 1994 at 6:22 AM 2 < > V3 Name DISK1 disk1.img extract_file extract_file.c QBASIC.EXE iCloud Drive > I Desktop ISO.CP_ KEYB.COM KEYBOARD.SYS KEYBRD2.SY_ MEM.EX_ MSCDEX.EXE MSDOS.SYS NETWORKS.TXT May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 1 May 31, 1994 at 6:22 AM 3 May 31, 1994 at 6:22 AM 1 May 31, 1994 at 6:22 AM 2 May 31, 1994 at 6:22 AM 2 May 31, 1994 at 6:22 AM 3 May 31, 1994 at 6:22 AM NLSFUNC.EXE PACKING.LST May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM May 31, 1994 at 6:22 AM 19 V3 > DISK1
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
code is still not working!! please help me ! idk why it wont work. I was following directions as given.
#include
#include
#include
#include
#include
#pragma pack(push, 1)
typedef struct {
int8_tjump_to_bootstrap[3];
int8_toem_id[8];
uint16_tbytes_per_sector;
uint8_tsectors_per_cluster;
uint16_treserved_sectors;
uint8_tfat_copies;
uint16_troot_dir_entries;
uint16_ttotal_sectors;
uint8_tmedia_descriptor_type;
uint16_tsectors_per_fat;
uint16_tsectors_per_track;
uint16_theads;
uint32_thidden_sectors;
uint32_ttotal_sectors2;
uint8_tdrive_index;
uint8_t_stuff;
uint8_tsignature;
uint32_tid;
int8_tlabel[11];
int8_ttype[8];
int8_t_more_stuff[448];
uint16_tsig;
} boot_t;
typedef struct {
int8_tfilename[8];
int8_textension[3];
int8_tattributes;
int8_t_reserved[10];
uint16_tupdate_time;
uint16_tupdate_date;
uint16_tstarting_cluster;
uint32_tfile_size;
} dir_entry_t;
#pragma pack(pop)
void readSector(FILE *disk, uint32_t sectorNumber, uint8_t *buffer, uint16_t bytesPerSector) {
if (disk==NULL) {
fprintf(stderr, "Invalid file pointer.\n");
return;
}
if (buffer==NULL) {
fprintf(stderr, "Invalid buffer pointer.\n");
return;
}
if (fseek(disk, sectorNumber*bytesPerSector, SEEK_SET) !=0) {
perror("Error seeking file");
return;
}
size_tbytesRead=fread(buffer, 1, bytesPerSector, disk);
if (bytesRead!=bytesPerSector) {
if (feof(disk)) {
fprintf(stderr, "End of file reached unexpectedly.\n");
} else if (ferror(disk)) {
perror("Error reading file");
}
return;
}
}
void readMBR(FILE *disk, boot_t *bootSector) {
uint8_tbuffer[512];
readSector(disk, 0, buffer, 512);
memcpy(bootSector, buffer, sizeof(boot_t));
}
dir_entry_t* getRootDirEntries(FILE *disk, boot_t *bootSector) {
uint16_trootDirSectors= ((bootSector->root_dir_entries*32) + (bootSector->bytes_per_sector-1)) /bootSector->bytes_per_sector;
uint8_t*buffer= (uint8_t*)malloc(rootDirSectors*bootSector->bytes_per_sector);
readSector(disk, bootSector->reserved_sectors+ (bootSector->fat_copies*bootSector->sectors_per_fat), buffer, rootDirSectors*bootSector->bytes_per_sector);
return (dir_entry_t*)buffer;
}
dir_entry_t* findRootDirEntry(const char *filename, dir_entry_t *entries, uint16_t numEntries) {
for (inti=0; ireserved_sectors+ (bootSector->fat_copies*bootSector->sectors_per_fat) +
((bootSector->root_dir_entries * 32 + bootSector->bytes_per_sector - 1) / bootSector->bytes_per_sector);
uint32_tfirstSectorOfCluster= ((clusterNumber-2) *bootSector->sectors_per_cluster) +firstDataSector;
for (inti=0; isectors_per_cluster; i++) {
readSector(disk, firstSectorOfCluster+i, buffer+ (i*bootSector->bytes_per_sector), bootSector->bytes_per_sector);
}
}
void extractFile(FILE *disk, dir_entry_t *entry, boot_t *bootSector) {
uint8_t*buffer= (uint8_t*)malloc(entry->file_size);
uint32_tcluster=entry->starting_cluster;
uint32_tbytesRead=0;
while (bytesReadfile_size) {
readCluster(disk, cluster, buffer+bytesRead, bootSector);
bytesRead+=bootSector->bytes_per_sector*bootSector->sectors_per_cluster;
// In a real implementation, follow the FAT to find the next cluster
cluster++;
}
FILE*output=fopen("output_file", "wb");
fwrite(buffer, 1, entry->file_size, output);
fclose(output);
free(buffer);
}
int main(int argc, char *argv[]) {
if (argc!=3) {
fprintf(stderr, "Usage: %s \n", argv[0]);
return1;
}
constchar*diskImage=argv[1];
constchar*filename=argv[2];
FILE*disk=fopen(diskImage, "rb");
if (disk==NULL) {
perror("Error opening disk image");
return1;
}
boot_tbootSector;
readMBR(disk, &bootSector);
dir_entry_t*entries=getRootDirEntries(disk, &bootSector);
dir_entry_t*entry=findRootDirEntry(filename, entries, bootSector.root_dir_entries);
if (entry) {
extractFile(disk, entry, &bootSector);
printf("File extracted successfully.\n");
} else {
fprintf(stderr, "File not found in root directory.\n");
}
free(entries);
fclose(disk);
return0;
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education