home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / EXT2FS / BADBLOCK.C < prev    next >
C/C++ Source or Header  |  1995-05-10  |  3KB  |  153 lines

  1. /*
  2.  * badblocks.c --- routines to manipulate the bad block structure
  3.  * 
  4.  * Copyright (C) 1994 Theodore Ts'o.  This file may be redistributed
  5.  * under the terms of the GNU Public License.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <fcntl.h>
  13. #include <time.h>
  14. #include <errno.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17.  
  18. #include <linux/ext2_fs.h>
  19.  
  20. #include "ext2fs.h"
  21.  
  22. /*
  23.  * This procedure create an empty badblocks list.
  24.  */
  25. errcode_t badblocks_list_create(badblocks_list *ret, int size)
  26. {
  27.     badblocks_list    bb;
  28.  
  29.     bb = malloc(sizeof(struct struct_badblocks_list));
  30.     if (!bb)
  31.         return ENOMEM;
  32.     memset(bb, 0, sizeof(struct struct_badblocks_list));
  33.     bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
  34.     bb->size = size ? size : 10;
  35.     bb->list = malloc(bb->size * sizeof(blk_t));
  36.     if (!bb->list) {
  37.         free(bb);
  38.         return ENOMEM;
  39.     }
  40.     *ret = bb;
  41.     return 0;
  42. }
  43.  
  44. /*
  45.  * This procedure frees a badblocks list.
  46.  */
  47. void badblocks_list_free(badblocks_list bb)
  48. {
  49.     if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
  50.         return;
  51.  
  52.     if (bb->list)
  53.         free(bb->list);
  54.     bb->list = 0;
  55.     free(bb);
  56. }
  57.  
  58. /*
  59.  * This procedure adds a block to a badblocks list.
  60.  */
  61. errcode_t badblocks_list_add(badblocks_list bb, blk_t blk)
  62. {
  63.     int    i;
  64.  
  65.     EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
  66.  
  67.     for (i=0; i < bb->num; i++)
  68.         if (bb->list[i] == blk)
  69.             return 0;
  70.  
  71.     if (bb->num >= bb->size) {
  72.         bb->size += 10;
  73.         bb->list = realloc(bb->list, bb->size * sizeof(blk_t));
  74.         if (!bb->list) {
  75.             bb->size = 0;
  76.             bb->num = 0;
  77.             return ENOMEM;
  78.         }
  79.     }
  80.  
  81.     bb->list[bb->num++] = blk;
  82.     return 0;
  83. }
  84.  
  85. /*
  86.  * This procedure tests to see if a particular block is on a badblocks
  87.  * list.
  88.  */
  89. int badblocks_list_test(badblocks_list bb, blk_t blk)
  90. {
  91.     int    i;
  92.  
  93.     EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
  94.  
  95.     for (i=0; i < bb->num; i++)
  96.         if (bb->list[i] == blk)
  97.             return 1;
  98.  
  99.     return 0;
  100. }
  101.  
  102. errcode_t badblocks_list_iterate_begin(badblocks_list bb,
  103.                        badblocks_iterate *ret)
  104. {
  105.     badblocks_iterate iter;
  106.  
  107.     EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
  108.  
  109.     iter = malloc(sizeof(struct struct_badblocks_iterate));
  110.     if (!iter)
  111.         return ENOMEM;
  112.  
  113.     iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
  114.     iter->bb = bb;
  115.     iter->ptr = 0;
  116.     *ret = iter;
  117.     return 0;
  118. }
  119.  
  120. int badblocks_list_iterate(badblocks_iterate iter, blk_t *blk)
  121. {
  122.     badblocks_list    bb;
  123.  
  124.     if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
  125.         return 0;
  126.  
  127.     bb = iter->bb;
  128.  
  129.     if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
  130.         return 0;
  131.     
  132.     if (iter->ptr < bb->num) {
  133.         *blk = bb->list[iter->ptr++];
  134.         return 1;
  135.     } 
  136.     *blk = 0;
  137.     return 0;
  138. }
  139.  
  140. void badblocks_list_iterate_end(badblocks_iterate iter)
  141. {
  142.     if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
  143.         return;
  144.  
  145.     iter->bb = 0;
  146.     free(iter);
  147. }
  148.  
  149.  
  150.  
  151.  
  152.         
  153.