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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5.  
  6. #include "libfdisk.h"
  7.  
  8. /* Constraints used to give allocation routines hints about what */
  9. /* to do with a given partition                                  */
  10.  
  11. /* store current setttings of constraint in vars if active, otherwise */
  12. /* use the specified defaults                                         */
  13. int fdiskQueryConstraint( Constraint *c, unsigned int *cur,
  14.             unsigned int *l,  unsigned int *m,
  15.             unsigned int dl,  unsigned int dm ) {
  16.     
  17.     if (c->active) {
  18.     *l = c->min;
  19.     *m = c->max;
  20.     } else {
  21.     *l = dl;
  22.     *m = dm;
  23.     }
  24.  
  25.     *cur = c->current;
  26.     return FDISK_SUCCESS;
  27. }
  28.  
  29. /* store current setttings of constraint in vars if active, otherwise */
  30. /* use the specified defaults                                         */
  31. int fdiskGetConstraint( Constraint *c, unsigned int *cur,
  32.             unsigned int *l,  unsigned int *m,
  33.             unsigned int *active ) {
  34.     
  35.     *l      = c->min;
  36.     *m      = c->max;
  37.     *cur    = c->current;
  38.     *active = c->active;
  39.     return FDISK_SUCCESS;
  40. }
  41.  
  42. /* get min/max of constraint */
  43. int fdiskGetMinMaxConstraint(Constraint *c,unsigned int *l,unsigned int *m){
  44.     *l      = c->min;
  45.     *m      = c->max;
  46.     return FDISK_SUCCESS;
  47. }
  48.  
  49. /* get current value of constraint */
  50. int fdiskGetCurrentConstraint( Constraint *c, unsigned int *cur ){
  51.     *cur    = c->current;
  52.     return FDISK_SUCCESS;
  53. }
  54.  
  55. /* set current value constraint in cur */
  56. int fdiskSetCurrentConstraint( Constraint *c, unsigned int cur ){
  57.     c->current = cur;
  58.     return FDISK_SUCCESS;
  59. }
  60.  
  61. /* set a constraint that is fixed in value and has no range */
  62. int fdiskSetFixedConstraint( Constraint *c, unsigned int value ) {
  63.     c->current = c->min = c->max = value;
  64.     c->active = 1;
  65.     return FDISK_SUCCESS;
  66. }
  67.  
  68. int fdiskEnableConstraint( Constraint *c ) {
  69.     c->active  = 1;
  70.     return FDISK_SUCCESS;
  71. }
  72.  
  73. int fdiskDisableConstraint( Constraint *c ) {
  74.     c->active = 0;
  75.     return FDISK_SUCCESS;
  76. }
  77.  
  78. int fdiskClearConstraint( Constraint *c ) {
  79.     memset( c, 0, sizeof(Constraint) );
  80.     return FDISK_SUCCESS;
  81. }
  82.  
  83. int fdiskSetConstraint( Constraint *c, unsigned int cur,
  84.             unsigned int min, unsigned int max,
  85.             unsigned int active) {
  86.     c->current = cur;
  87.     c->min     = min;
  88.     c->max     = max;
  89.     c->active  = active;
  90.     return FDISK_SUCCESS;
  91. }
  92.  
  93. int fdiskResetConstraint( Constraint *c ) {
  94.     memset( c, 0, sizeof(Constraint) );
  95.     return FDISK_SUCCESS;
  96. }
  97.  
  98. /* return true/false if constraint is active */
  99. int fdiskConstraintIsActive( Constraint *c ) {
  100.     return c->active;
  101. }
  102.  
  103. /* another type of constraint is a DriveSet. It is a set of numbers from */
  104. /* which is normally used to specify which drives a partition can go on  */
  105. /* if the entry corresponding to drive num is active (equal to 0) we can */
  106. /* consider the drive. If it is equal to 1, the drive is masked off      */
  107.  
  108. int fdiskActivateAllDriveSet( DriveSet *ds ) {
  109.     unsigned int i;
  110.  
  111.     for (i=0; i<=MAX_DRIVESET_NUM; i++)
  112.     ds->active[i] = 0;
  113.     return FDISK_SUCCESS;
  114. }
  115.  
  116. int fdiskDeactivateAllDriveSet( DriveSet *ds ) {
  117.     unsigned int i;
  118.  
  119.     for (i=0; i<=MAX_DRIVESET_NUM; i++)
  120.     ds->active[i] = 1;
  121.     return FDISK_SUCCESS;
  122. }
  123.  
  124. int fdiskActivateDriveSet( DriveSet *ds, unsigned int i ) {
  125.     ds->active[i] = 0;
  126.     return FDISK_SUCCESS;
  127. }
  128.  
  129. int fdiskDeactivateDriveSet( DriveSet *ds, unsigned int i ) {
  130.     ds->active[i] = 1;
  131.     return FDISK_SUCCESS;
  132. }
  133.  
  134. int fdiskThisDriveSetIsActive( DriveSet *ds, unsigned int i ) {
  135.     return !ds->active[i];
  136. }
  137.  
  138. /* tells us if the driveset is active (acting as a constraint)            */
  139. /* if all bits are unset then there is no constraint, as all drives are   */
  140. /* valid places to put the partition                                      */
  141. /* NOTE that just because a drive is 'active' doesn't mean partition      */
  142. /* can go there! Just means it can't (that drive number may not exist)    */
  143. int fdiskDriveSetIsActive( DriveSet *ds ) {
  144.     unsigned int k;
  145.  
  146.     for (k=0; k <= MAX_DRIVESET_NUM; k++)
  147.     if (ds->active[k])
  148.         return 1;
  149.  
  150.     return 0;
  151. }
  152.  
  153. /* get first entry in DriveSet, use fdiskWalkDriveSet to get other entries */
  154. int fdiskInitWalkDriveSet( DriveSet *ds, unsigned int *i ) {
  155.     unsigned int k;
  156.  
  157.     for (k=0; k <=MAX_DRIVESET_NUM; k++)
  158.     if (!ds->active[k])
  159.         break;
  160.  
  161.     if (k <= MAX_DRIVESET_NUM) {
  162.     *i = k;
  163.     return FDISK_SUCCESS;
  164.     } else {
  165.     return FDISK_ERR_BADNUM;
  166.     }
  167. }
  168.  
  169. /* start at first set entry starting AFTER that passed in i */
  170. int fdiskWalkDriveSet( DriveSet *ds, unsigned int *i ) {
  171.     unsigned int k;
  172.  
  173.     if (*i == MAX_DRIVESET_NUM)
  174.     return FDISK_ERR_BADNUM;
  175.     
  176.     for (k=*i+1; k <=MAX_DRIVESET_NUM; k++)
  177.     if (!ds->active[k])
  178.         break;
  179.  
  180.     if (k <= MAX_DRIVESET_NUM) {
  181.     *i = k;
  182.     return FDISK_SUCCESS;
  183.     } else {
  184.     return FDISK_ERR_BADNUM;
  185.     }
  186. }
  187.  
  188. int fdiskGetCurrentDriveSet( DriveSet *ds, unsigned int *i ) {
  189.     *i = ds->current;
  190.     return FDISK_SUCCESS;
  191. }
  192.  
  193. int fdiskSetCurrentDriveSet( DriveSet *ds, unsigned int i ) {
  194.     ds->current = i;
  195.     return FDISK_SUCCESS;
  196. }
  197.     
  198.