summaryrefslogtreecommitdiff
path: root/v4.0/src/CMD/FDISK/SPACE.C
blob: 21d10b5103ce6dcf44f40eb9e154845f6eaee088 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include "dos.h"
#include "fdisk.h"
#include "extern.h"
#include "subtype.h"

/*  */
char find_part_free_space(type)

char   type;

BEGIN


char        i;
char        partition_count;
char        last_found_partition;
unsigned    temp;
char        freespace_count;
char        any_partition;
unsigned    temp_size;

        /* Sort the partition table */
        sort_part_table(c(4));                                          /* AC000 */


        /* Intialize free space to zero */
        for (i = c(0); i < c(5); i++)                                   /* AC000 */
           BEGIN
            free_space[i].space = u(0);                                 /* AC000 */
            free_space[i].start = u(0);                                 /* AC000 */
            free_space[i].end = u(0);                                   /* AC000 */
            free_space[i].mbytes_unused = f(0);                         /* AC000 */  /* AN000 */
            free_space[i].percent_unused = u(0);                        /* AC000 */     /* AN000 */
           END

        /* Find space between start of disk and first partition */
        partition_count = c(0);                                         /* AC000 */

        any_partition = FALSE;
        for (i = c(0); i < c(4); i++)                                   /* AC000 */
           BEGIN
            if (part_table[cur_disk][sort[i]].sys_id != uc(0))          /* AC000 */
               BEGIN
                /* Found a partition, get the space */

                free_space[0].start = u(0);                             /* AC000 */

                /* This is a special case - the extended partition can not start */
                /* on cylinder 0 due too its archetecture. Protect against that here */
                if (type == c(EXTENDED))                                /* AC000 */
                   BEGIN
                    free_space[0].start = u(1);                         /* AC000 */
                   END

                /* free space ends before start of next valid partition */
                if (part_table[cur_disk][sort[i]].start_cyl > u(0))     /* AC000 */
                   BEGIN
                    free_space[0].end = part_table[cur_disk][sort[i]].start_cyl-1;
                   END

                free_space[0].space = part_table[cur_disk][sort[i]].start_cyl;
                free_space[0].mbytes_unused =
                    cylinders_to_mbytes(free_space[0].space,cur_disk);  /* AN004 */
                free_space[0].percent_unused = (unsigned)cylinders_to_percent(free_space[0].space,total_disk[cur_disk]); /* AN000 */

                partition_count = i;
                last_found_partition = sort[i];
                any_partition = TRUE;
                break;
               END
           END
        /* See if any partitions were there */
        if (any_partition)
           BEGIN
            /* Look for space between the rest of the partitions */
            freespace_count = c(1);                                     /* AC000 */
            for (i = partition_count+1; i < c(4); i++)                  /* AC000 */
               BEGIN
                if (part_table[cur_disk][sort[i]].sys_id != uc(0))      /* AC000 */
                   BEGIN

                    /* Check to see if more than one partition on a cylinder (i.e. XENIX bad block)  */
                    /* If so, leave the space at zero */

                    if (part_table[cur_disk][sort[i]].start_cyl != part_table[cur_disk][last_found_partition].end_cyl)

                       BEGIN
                        /* No, things are normal */
                        /* Get space between the end of the last one and the start of the next one */
                        free_space[freespace_count].space = part_table[cur_disk][sort[i]].start_cyl
                           - (part_table[cur_disk][last_found_partition].end_cyl+1);

                        temp_size = (part_table[cur_disk][sort[i]].start_cyl -
                             part_table[cur_disk][last_found_partition].end_cyl);

                        if (temp_size != u(0) )                         /* AC000 */
                           BEGIN
                            free_space[freespace_count].space = temp_size - u(1);  /* AC000 */
                           END
                       END

                    free_space[freespace_count].start = part_table[cur_disk][last_found_partition].end_cyl+1;
                    free_space[freespace_count].end = part_table[cur_disk][sort[i]].start_cyl -1;
                    free_space[freespace_count].mbytes_unused =
                         cylinders_to_mbytes(free_space[freespace_count].space,cur_disk); /* AN004 */
                    free_space[freespace_count].percent_unused = (unsigned)
                         cylinders_to_percent(free_space[freespace_count].space,total_disk[cur_disk]);  /* AN000 */



                    /* update the last found partition */
                    last_found_partition = sort[i];
                    freespace_count++;
                   END
               END
            /* Find the space between the last partition and the end of the disk */
            free_space[freespace_count].space = (total_disk[cur_disk]
                                     -  part_table[cur_disk][last_found_partition].end_cyl)-1;
            free_space[freespace_count].start = part_table[cur_disk][last_found_partition].end_cyl+1;
            free_space[freespace_count].end = total_disk[cur_disk]-1;
            free_space[freespace_count].mbytes_unused =
                 cylinders_to_mbytes(free_space[freespace_count].space,cur_disk);    /* AN004 */
            free_space[freespace_count].percent_unused =
                 cylinders_to_percent(free_space[freespace_count].space,total_disk[cur_disk]);                       /* AN000 */
            END
         else
           BEGIN
            /* No partitions found, show entire space as free */
            free_space[0].start = u(0);                                 /* AC000 */

            /* This is a special case - the extended partition can not start */
            /* on cylinder 0 due too its architecture. Protect against that here */
            if (type == c(EXTENDED))                                    /* AC000 */
               BEGIN
                free_space[0].start = u(1);                             /* AC000 */
               END
            free_space[0].end = total_disk[cur_disk]-1;
            free_space[0].space = (free_space[0].end - free_space[0].start)+1;
            free_space[0].mbytes_unused =
                 cylinders_to_mbytes(free_space[0].space,cur_disk);    /* AN004 */
            free_space[0].percent_unused =
                 cylinders_to_percent(free_space[0].space,total_disk[cur_disk]);                       /* AN000 */
           END



         /* Find largest free space, and verify the golden tracks while we are at it */
         do
            BEGIN
             temp = u(0);                                               /* AC000 */

             /* Zip thru the table */
             for (i = c(0); i < c(5); i++)                              /* AC000 */
                BEGIN
                 /* Is this one bigger ? */
                 if (free_space[i].space > temp)
                    BEGIN
                     temp = free_space[i].space;
                     last_found_partition = i;

                    END
                END

             /* If there is any free space, go verify it */
             temp = u(0);
             if (free_space[last_found_partition].space != u(0))        /* AC000 */
               BEGIN

                /* Go verify the tracks */
                temp = verify_tracks(last_found_partition,c(PRIMARY));  /* AC000 */
               END
             /* Move up to next golden track */
             free_space[last_found_partition].start = free_space[last_found_partition].start+temp;
             free_space[last_found_partition].space = free_space[last_found_partition].space-temp;
             free_space[last_found_partition].mbytes_unused =
                  cylinders_to_mbytes(free_space[last_found_partition].space,cur_disk);    /* AN004 */
             free_space[last_found_partition].percent_unused = (unsigned)
                  cylinders_to_percent(free_space[last_found_partition].space,total_disk[cur_disk]);                      /* AN000 */
             END

            /* Repeat the loop if the start was moved due to bad tracks */
            /* Unless we're past the end of the free space */
            while ((temp != u(0)) && (free_space[last_found_partition].space != u(0)));    /* AC000 */

        /* Return with the pointer to the largest free space */
        return(last_found_partition);
END



/*  */
void sort_part_table(size)

char size;

BEGIN

char  changed;
char  temp;
char   i;

        /* Init the sorting parameters */

        for (i=c(0); i < size; i++)                                     /* AC000 */
           BEGIN
            sort[i] = i;
           END

        /* Do a bubble sort */
        changed = TRUE;

        /* Sort until we don't do a swap */
        while (changed)

           BEGIN
            changed = FALSE;
            for (i=c(1); i < size; i++)                                 /* AC000 */
               BEGIN

                /* Does the partition entry start before the previous one, or */
                /* is it empty (0 ENTRY). If empty, it automatically gets shoved */
                /* to the front, if the previous entry isn't also empty */

                if ((part_table[cur_disk][sort[i]].end_cyl < part_table[cur_disk][sort[i-1]].end_cyl)
                   || ((part_table[cur_disk][sort[i]].num_sec == ul(0))
                   &&  (part_table[cur_disk][sort[i-1]].num_sec != ul(0))))  /* AC000 */

                   BEGIN
                    /* Swap the order indicators */
                    temp = sort[i-1];
                    sort[i-1] = sort[i];
                    sort[i] = temp;

                 /* printf("\nI-1 =%d\n",part_table[cur_disk][sort[i-1]].start_cyl);*/
                 /* printf("I =%d\n",part_table[cur_disk][sort[i]].start_cyl);*/
                 /* printf("Sort[i-1] = %d\n",sort[i-1]);*/
                 /* printf("Sort[i] = %d\n",sort[i]); */
                 /* wait_for_ESC(); */


                    /* indicate we did a swap */
                    changed = TRUE;
                   END
               END
           END
        return;
END




/*  */
char find_ext_free_space()


BEGIN


char   i;
char   partition_count;
char   last_found_partition;
unsigned    temp;
char   freespace_count;
char   any_partition;
char   ext_location;

        /* Sort the partition table */
        sort_ext_table(c(23));                                          /* AC000 */


        /* Initialize free space to zero */
        for (i = c(0); i < c(24); i++)                                  /* AC000 */
           BEGIN
            free_space[i].space = u(0);                                 /* AC000 */
            free_space[i].start = u(0);
            free_space[i].end = u(0);                                   /* AC000 */
            free_space[i].mbytes_unused = f(0);                         /* AN000 */
            free_space[i].percent_unused = u(0);                        /* AN000 */
           END

        /* Find space between start of Extended partition and first volume */
        ext_location = find_partition_location(uc(EXTENDED));           /* AC000 */

        partition_count = c(0);                                         /* AC000 */

        any_partition = FALSE;
        for (i = c(0); i < c(23); i++)                                  /* AC000 */
           BEGIN
            if (ext_table[cur_disk][sort[i]].sys_id != uc(0))           /* AC000 */
               BEGIN
                /* Found a partition, get the space */
                free_space[0].space = ext_table[cur_disk][sort[i]].start_cyl - part_table[cur_disk][ext_location].start_cyl;
                free_space[0].start = part_table[cur_disk][ext_location].start_cyl;
                free_space[0].end = ext_table[cur_disk][sort[i]].start_cyl-1;
                free_space[0].mbytes_unused =
                     cylinders_to_mbytes(free_space[0].space,cur_disk); /* AN004 */
                free_space[0].percent_unused = (unsigned)cylinders_to_percent(free_space[0].space,total_disk[cur_disk]); /* AN000 */

                partition_count = i;
                last_found_partition = sort[i];
                any_partition = TRUE;
                break;
               END
           END
        /* See if any partitions were there */
        if (any_partition)
           BEGIN
            /* Look for space between the rest of the partitions */
            freespace_count = c(1);                                     /* AC000 */
            for (i = partition_count+1; i < c(23); i++)                 /* AC000 */
               BEGIN
                if (ext_table[cur_disk][sort[i]].sys_id != uc(0))       /* AC000 */
                   BEGIN

                    /* Get space between the end of the last one and the start of the next one */
                    temp = ext_table[cur_disk][sort[i]].start_cyl - (ext_table[cur_disk][last_found_partition].end_cyl+1);
                    free_space[freespace_count].space = temp;
                    free_space[freespace_count].start = ext_table[cur_disk][last_found_partition].end_cyl+1;
                    free_space[freespace_count].end = ext_table[cur_disk][sort[i]].start_cyl -1;
                    free_space[freespace_count].mbytes_unused =
                         cylinders_to_mbytes(free_space[freespace_count].space,cur_disk);       /* AN004 */
                    free_space[freespace_count].percent_unused = (unsigned)
                         cylinders_to_percent(free_space[freespace_count].space,total_disk[cur_disk]);                         /* AN000 */


                    /* update the last found partition */
                    last_found_partition = sort[i];
                    freespace_count++;
                   END
               END
            /* Find the space between the last partition and the end of the extended partition */
            temp = part_table[cur_disk][ext_location].end_cyl -  ext_table[cur_disk][last_found_partition].end_cyl;
            free_space[freespace_count].space = temp;
            free_space[freespace_count].start = ext_table[cur_disk][last_found_partition].end_cyl+1;
            free_space[freespace_count].end = part_table[cur_disk][ext_location].end_cyl;
            free_space[freespace_count].mbytes_unused =
                 cylinders_to_mbytes(free_space[freespace_count].space,cur_disk);    /* AN004 */
            free_space[freespace_count].percent_unused = (unsigned)
                 cylinders_to_percent(free_space[freespace_count].space,total_disk[cur_disk]);                      /* AN000 */

           END
        else
           BEGIN
            /* No partitions found, show entire space as free */
            free_space[0].space = (part_table[cur_disk][ext_location].end_cyl - part_table[cur_disk][ext_location].start_cyl) + 1;
            free_space[0].start = part_table[cur_disk][ext_location].start_cyl;
            free_space[0].end = part_table[cur_disk][ext_location].end_cyl;
            free_space[0].mbytes_unused =
                 cylinders_to_mbytes(free_space[0].space,cur_disk);  /* AN004 */
            free_space[0].percent_unused = (unsigned)cylinders_to_percent(free_space[0].space,total_disk[cur_disk]); /* AN000 */
           END

         /* Find largest free space */
         temp = u(0);                                                   /* AC000 */


         /* Find largest free space, and verify the golden tracks while we are at it */
         do
            BEGIN
             temp = u(0);                                               /* AC000 */

             /* Zip thru the table */
             for (i = c(0); i < c(24); i++)                             /* AC000 */
                BEGIN
                 /* Is this one bigger ? */
                 if (free_space[i].space > temp)
                    BEGIN
                     temp = free_space[i].space;
                     last_found_partition = i;
                    END
                END
             /* If there is any free space, go verify it */
             temp = u(0);
             if (free_space[last_found_partition].space != u(0))        /* AC000 */
                BEGIN

                 /* Go verify the tracks */
                 temp = verify_tracks(last_found_partition,c(EXTENDED)); /* AC000 */
                END
             /* Move up to next golden track */
             free_space[last_found_partition].start = free_space[last_found_partition].start+temp;
             free_space[last_found_partition].space = free_space[last_found_partition].space-temp;
             free_space[last_found_partition].mbytes_unused =
                  cylinders_to_mbytes(free_space[last_found_partition].space,cur_disk);    /* AN004 */
             free_space[last_found_partition].percent_unused =
                  cylinders_to_percent(free_space[last_found_partition].space,total_disk[cur_disk]);                       /* AN000 */
             END
             /* Repeat the loop if the start was moved due to bad tracks */
            /* Unless we're past the end of the free space */
            while ((temp !=u(0)) && (free_space[last_found_partition].space!= u(0)));  /* AC000 */

        /* Return with the pointer to the largest free space */
        return(last_found_partition);
END


/*  */
void sort_ext_table(size)

char size;

BEGIN

char  changed;
char  temp;
char i;

        /* Init the sorting parameters */

        for (i=c(0); i < size; i++)                                     /* AC000 */
           BEGIN
            sort[i] = i;
           END

        /* Do a bubble sort */
        changed = TRUE;

        /* Sort until we don't do a swap */
        while (changed)

           BEGIN
            changed = FALSE;
            for (i=c(1); i < size; i++)                                 /* AC000 */
               BEGIN

                if (ext_table[cur_disk][sort[i]].start_cyl < ext_table[cur_disk][sort[i-1]].start_cyl)
                   BEGIN

                    temp = sort[i-1];
                    sort[i-1] = sort[i];
                    sort[i] = temp;
                    /* indicate we did a swap */
                    changed = TRUE;
                   END
               END
           END
        return;
END