home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / install / libfdisk / primary.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-17  |  4.5 KB  |  164 lines

  1. /* handles primary partitions */
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <malloc.h>
  5. #include <string.h>
  6. #include "libfdisk.h"
  7.  
  8. /* delete the primary partition into slot n of the partition table  */
  9. /* primaries are numbered 1 to 4                                    */
  10. int fdiskRemovePrimary( HardDrive *hd, unsigned int n ) {
  11.     int i;
  12.     unsigned int last;
  13.     
  14.     /* we only can have 4 primary partitions */
  15.     if (n < 1 || n > 4)
  16.     return FDISK_ERR_BADNUM;
  17.  
  18.     /* if this primary is the top extended partition, we have more to do */
  19.     if (fdiskIsExtended(hd->table[n].type.current)) {
  20.     fdiskLastPartition(hd, &last);
  21.     for (i=last; i > 4; i--)
  22.         fdiskRemoveLogical( hd, i );
  23.  
  24.     hd->pep = 0;
  25.     }
  26.     
  27.     memset(&hd->table[n], 0, sizeof(Partition));
  28.     hd->table[n].status = AVAILABLE;
  29.     
  30.     return FDISK_SUCCESS;
  31. }    
  32.  
  33.  
  34. /* create the primary partition into slot n of the partition table  */
  35. /* primaries are numbered 1 to 4                                    */
  36. /* User must set attributes using fdiskSetAttrPrimary()             */
  37. /* Just marks the partition table entry as allocated                */
  38. int fdiskCreatePrimary( HardDrive *hd, unsigned int n ) {
  39.  
  40.     /* we only can have 4 primary partitions */
  41.     if (n < 1 || n > 4)
  42.     return FDISK_ERR_BADNUM;
  43.  
  44.     /* does a partition already exist here */
  45.     if (hd->table[n].status != AVAILABLE)
  46.     return FDISK_ERR_INUSE;
  47.     
  48.     memset(&hd->table[n], 0, sizeof(Partition));
  49.     hd->table[n].status = ALLOCATED;
  50.     
  51.     return FDISK_SUCCESS;
  52. }    
  53.  
  54. /* sets attr of primary partition into slot n of the partition table  */
  55. /* primaries are numbered 1 to 4                                      */
  56. /* this routine just copies data into partition table. Up to higher   */
  57. /* level routines to check if this operation is allowable!            */
  58. int fdiskSetAttrPrimary( HardDrive *hd, unsigned int n, Partition *p) {
  59.  
  60.     /* we only can have 4 primary partitions */
  61.     if (n < 1 || n > 4)
  62.     return FDISK_ERR_BADNUM;
  63.  
  64.     if (hd->table[n].status != ALLOCATED)
  65.     return FDISK_ERR_BADNUM;
  66.  
  67.     memcpy(&hd->table[n], p, sizeof(Partition));
  68.     
  69.     return FDISK_SUCCESS;
  70. }    
  71.  
  72. /* allocates a Partition struct and returns contents of request partition */
  73. int fdiskGetAttrPrimary( HardDrive *hd, unsigned int n, Partition **p ) {
  74.  
  75.     /* we only can have 4 primary partitions */
  76.     if (n < 1 || n > 4)
  77.     return FDISK_ERR_BADNUM;
  78.  
  79.     if (hd->table[n].status != ALLOCATED)
  80.     return FDISK_ERR_BADNUM;
  81.     
  82.     *p = (Partition *) malloc( sizeof(Partition) );
  83.     memcpy(*p, &hd->table[n], sizeof(Partition));
  84.     
  85.     return FDISK_SUCCESS;
  86. }    
  87.  
  88. /* find the primary of the given number                                      */
  89. /* if partition doesn't exists return NULL pointer and FDISK_ERR_BADNUM      */
  90. /* Not sure this function has any use now I moved to a single partition table*/
  91. int fdiskFindPrimary( HardDrive *hd, unsigned int n, Partition **p ) {
  92.     if (hd->table[n].status == ALLOCATED) {
  93.     *p = &hd->table[n];
  94.     return FDISK_SUCCESS;
  95.     } else {
  96.     *p = NULL;
  97.     return FDISK_ERR_BADNUM;
  98.     }
  99. }
  100.  
  101. /* return value of first primary that exists */
  102. /* return FDISK_ERR_BADNUM if none do        */
  103. int fdiskFirstPrimary( HardDrive *hd, unsigned int *first ) {
  104.     int i;
  105.  
  106.     for (i=1; i<5; i++)
  107. #if 0
  108.     /* I think we want to consider Primary Partitions which have */
  109.     /* a type of extended, so this is #if'd out for now          */
  110.     if (!fdiskIsExtended(hd->table[i].type.current) && 
  111.         hd->table[i].status == ALLOCATED)
  112. #else
  113.         if (hd->table[i].status == ALLOCATED)
  114. #endif
  115.         break;
  116.  
  117.     if (i == 5)
  118.     return FDISK_ERR_BADNUM;
  119.  
  120.     *first = i;
  121.     return FDISK_SUCCESS;
  122. }
  123.  
  124. /* return value of last primary that exists */
  125. /* return FDISK_ERR_BADNUM if none do       */
  126. int fdiskLastPrimary( HardDrive *hd, unsigned int *last ) {
  127.     int i;
  128.  
  129.     for (i=5; i>=1; i--)
  130. #if 0
  131.     /* I think we want to consider Primary Partitions which have */
  132.     /* a type of extended, so this is #if'd out for now          */
  133.     if (!fdiskIsExtended(hd->table[i].type.current) && 
  134.         hd->table[i].status == ALLOCATED)
  135. #else
  136.         if (hd->table[i].status == ALLOCATED)
  137. #endif
  138.         break;
  139.  
  140.     if (i < 1)
  141.     return FDISK_ERR_BADNUM;
  142.  
  143.     *last = i;
  144.     return FDISK_SUCCESS;
  145. }
  146.  
  147.  
  148. /* return value of the first free primary slot */
  149. /* return FDISK_ERR_NOFREEPRIM if none do          */
  150. int fdiskFindFreePrimary( HardDrive *hd, unsigned int *free ) {
  151.     int i;
  152.  
  153.     for (i=1; i<5; i++)
  154.     if (hd->table[i].status == AVAILABLE)
  155.         break;
  156.  
  157.     if (i == 5)
  158.     return FDISK_ERR_NOFREEPRIM;
  159.  
  160.     *free = i;
  161.     return FDISK_SUCCESS;
  162. }
  163.  
  164.