int getPartitions(int fd, struct partition *partitions) { int count; int jump; int rc; int sector; int start = 446; int totalcount; unsigned char buf[SECTOR_SZ]; /* Read sector 0 */ sector = 0; rc = readSectors ( fd, sector, 1, buf ); if ( rc == -1 ) { perror ( "Could not read sector" ); exit(-1); } /* Read first 4 partitions from sector 0 */ for(count = 0; count < 4; count++) { jump = readPartition(1, start + count*16, buf, partitions, count, 0); } totalcount = count; /* Read extended partitions */ while (jump > 0) { sector = jump; rc = readSectors ( fd, sector, 1, buf ); if ( rc == -1 ) { perror ( "Could not read sector" ); exit(-1); } count = 0; jump = 1; while (jump == 1) { jump = readPartition(0, start + count*16, buf, partitions, totalcount, sector); if (jump == 1) { count = count + 1; totalcount = totalcount + 1; } } } return totalcount; }
static void pslPartition(char *pslFile, char *outDir) /* split PSL files into non-overlapping sets */ { struct pslInput *pi = pslInputNew(pslFile); struct pslParts parts; struct psl *newPart; ZeroVar(&parts); while ((newPart = readPartition(pi)) != NULL) { if (gDropContained) dropContained(&newPart); pslPartsAdd(&parts, newPart, outDir); } if (parts.psls != NULL) pslPartsWrite(&parts, outDir); pslInputFree(&pi); }
/*------------------------------------------------------------------------------ * Name: main * Action: Print out partition tables. *-----------------------------------------------------------------------------*/ int main () { int rc; /* Return code */ int fd; /* Device descriptor */ int sector; /* IN: sector to read */ unsigned char buf[SECTOR_SZ]; /* temporary buffer */ struct partition partitions[32]; int count; int jump; int start = 446; int totalcount; /* Open the device */ fd = open ( "disk", O_RDWR ); if ( fd == -1 ) { perror ( "Could not open device file" ); exit(-1); } /* Read the sector */ sector = atoi( "0" ); rc = readSectors ( fd, sector, 1, buf ); if ( rc == -1 ) { perror ( "Could not read sector" ); exit(-1); } /* Read the first 4 partitions in MBR and print them */ for(count = 0; count < 4; count++) { jump = readPartition(1, start + count*16, buf, partitions, count, 0); printPartition(partitions, count); } /* Update total count of entries in array */ totalcount = count; /* Traverse extended partitions, printing out entries, until we hit an empty entry */ while (jump > 0) { /* Read the sector */ sector = jump; rc = readSectors ( fd, sector, 1, buf ); if ( rc == -1 ) { perror ( "Could not read sector" ); exit(-1); } /* Reset count and jump information */ count = 0; jump = 1; /* Traverse sector, printing each entry, until we hit either an empty or extended entry */ while (jump == 1) { jump = readPartition(0, start + count*16, buf, partitions, totalcount, sector); if (jump == 1) { printPartition(partitions, totalcount); count = count + 1; totalcount = totalcount + 1; } } } close(fd); return 0; }