Jump to content

Need help with this C code - segmentation fault 11??!

JoeyStorm

Hey everyone,

Working on a C program that takes a MSDOS filesystem image as input and outputs the file attributes of that filesystem. I keep getting a segmentation fault 11 code everytime I try to run my program and don't know why; been stuck for a couple of days. Below is the code along with a link underneath if you would like to try it out and download the filesystem image (it's not too big). Any help that would point me in the right direction would be much appreciated! Thanks

filesystem image: https://docs.google.com/file/d/0B6yxCOyY-HVlSzhFWlJ0SHB6MmM/edit?usp=sharing

#include 


#include 





typedef struct {
   unsigned char first_byte;
   unsigned char start_chs[3];
   unsigned char partition_type;
   unsigned char end_chs[3];
   unsigned long start_sector;
   unsigned long length_sectors;
} __attribute((packed)) PartitionTable;





typedef struct {
   unsigned char jmp[3];
   char oem[8];
   unsigned short sector_size;
   unsigned char sectors_per_cluster;
   unsigned short reserved_sectors;
   unsigned char number_of_fats;
   unsigned short root_dir_entries;
   unsigned short total_sectors_short; // if zero, later field is used
   unsigned char media_descriptor;
   unsigned short fat_size_sectors;
   unsigned short sectors_per_track;
   unsigned short number_of_heads;
   unsigned long hidden_sectors;
   unsigned long total_sectors_long;
   unsigned char drive_number;
   unsigned char current_head;
   unsigned char boot_signature;
   unsigned long volume_id;


   char volume_label[11];
   char fs_type[8];
   char boot_code[448];


   unsigned short boot_sector_signature;


} __attribute((packed)) Fat12BootSector;





int main() {


   FILE * in = fopen("fat_volume.dat", "rb");
   int i;
   PartitionTable pt[4];
   Fat12BootSector bs;


   fseek(in, 0x1BE, SEEK_SET); // go to partition table start
   fread(pt, sizeof(PartitionTable), 4, in); // read all four entries


   for(i=0; i<4; i++) {        
       if(pt[i].partition_type == 4 || pt[i].partition_type == 6 ||
          pt[i].partition_type == 14) {
           printf("FAT12 filesystem found from partition %d\n", i);
           break;
       }
   }


   if(i == 4) {
       printf("No FAT12 filesystem found, exiting...\n");
       return -1;
   }


   fseek(in, 512 * pt[i].start_sector, SEEK_SET);
   fread(&bs, sizeof(Fat12BootSector), 1, in);


   printf("  Jump code: %02X:%02X:%02X\n", bs.jmp[0], bs.jmp[1], bs.jmp[2]);
   printf("  OEM code: [%.8s]\n", bs.oem);
   printf("  sector_size: %d\n", bs.sector_size);
   printf("  sectors_per_cluster: %d\n", bs.sectors_per_cluster);
   printf("  reserved_sectors: %d\n", bs.reserved_sectors);
   printf("  number_of_fats: %d\n", bs.number_of_fats);
   printf("  root_dir_entries: %d\n", bs.root_dir_entries);
   printf("  total_sectors_short: %d\n", bs.total_sectors_short);
   printf("  media_descriptor: 0x%02X\n", bs.media_descriptor);
   printf("  fat_size_sectors: %d\n", bs.fat_size_sectors);
   printf("  sectors_per_track: %d\n", bs.sectors_per_track);
   printf("  number_of_heads: %d\n", bs.number_of_heads);
   printf("  hidden_sectors: %d\n", bs.hidden_sectors);
   printf("  total_sectors_long: %d\n", bs.total_sectors_long);
   printf("  drive_number: 0x%02X\n", bs.drive_number);
   printf("  current_head: 0x%02X\n", bs.current_head);
   printf("  boot_signature: 0x%02X\n", bs.boot_signature);
   printf("  volume_id: 0x%08X\n", bs.volume_id);
   printf("  Volume label: [%.11s]\n", bs.volume_label);
   printf("  Filesystem type: [%.8s]\n", bs.fs_type);
   printf("  Boot sector signature: 0x%04X\n", bs.boot_sector_signature);  


   fclose(in);


   return 0;


}
Link to comment
Share on other sites

Link to post
Share on other sites

I'm pretty sure it has something to do with not allocating space in the memory for your variables.

I have quickly looked through your code, and nowhere i see a malloc or calloc command.

It's been a while since i have last worked with C, so i could be wrong :P

Link to comment
Share on other sites

Link to post
Share on other sites

1. You don't check whether fopen or fread are successful. You should always do that.

2. The types short and long etc. do not have a exactly specified bit-width. Use the typedefs from instead. (uint32_t etc.)

3. At what point does the program actually segfault?

Link to comment
Share on other sites

Link to post
Share on other sites

it may also help for you to learn some hungarian_notation :\

your code is very illegible

and you code is annoyingly wrong....

you have not checked to see if the file exists !!! line 1 in main()

plus a good use of catch statements would be better coding ;)

if (fexists(fname))

{

FILE * in = fopen(fname, "rb");

}

else

{

printf("filename does not exist\n");

exit(0);

}

// do something with fname :)

as for the error....

You can use

#include // For printf()

#include // For struct stat and stat()

struct stat stResult;

if(stat("Filename.txt", &stResult) == 0)

{

// We have File Attributes

// stResult.st_size is the size in bytes (I believe)

// It also contains some other information you can lookup if you feel like it

printf("Filesize: %i", stResult.st_size);

}

else

{

// We couldn't open the file

printf("Couldn't get file attributes OR filename does not exist...");

Link to comment
Share on other sites

Link to post
Share on other sites

dugdiamond You do realize that "hungarian notation" is basically seen as the worst nightmare of programming nowadays even by Microsoft itself, and they basically just still use it to keep the WinAPI streamlined? O_o

Link to comment
Share on other sites

Link to post
Share on other sites

dugdiamond You do realize that "hungarian notation" is basically seen as the worst nightmare of programming nowadays even by Microsoft itself' date=' and they basically just still use it to keep the WinAPI streamlined? O_o[/quote']

lol - don't apply to me for a job ;)

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, I wouldn't worry about that, no need for me to go back to VC6 and GCC 3.x ;)

Link to comment
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...

Hungarian notation is very useful, if you use the original hungarian notation (apps hungarian notation), where you describe what the context of the variable or function.

It is useless to type the variable type before a variable (systems hungarian notation). Your IDE can tell you what type it is.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×