home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / SRC / E2GLOB.C < prev    next >
C/C++ Source or Header  |  1996-01-09  |  4KB  |  150 lines

  1. /***************************************************************************
  2.  * e2glob - glob() interface routines for the ext2 tools
  3.  *
  4.  * Copyright (C) 1995 Claus Tondering, ct@login.dknet.dk
  5.  * This file may be redistributed under the terms of the GNU Public License.
  6.  ***************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <sys/types.h>
  13.  
  14. #include "ext2_fs.h"
  15. #include "ext2fs/ext2fs.h"
  16. #include "ldisk.h"
  17. #include "istat.h"
  18. #include "e2err.h"
  19. #include "globlib/glob.h"
  20.  
  21. #define NAMELEN 80
  22.  
  23. struct fileinfo {
  24.     u_char    name[NAMELEN];
  25. };
  26.  
  27. struct dirst {
  28.     struct fileinfo *list;
  29.     int listix;
  30.     int maxlist;
  31. };
  32.  
  33. static ext2_filsys fs;
  34.  
  35. /**********************************************************************
  36.  * myproc is a callback routine which is called once for each entry
  37.  * in the directory
  38.  **********************************************************************/
  39.  
  40. static int myproc(struct ext2_dir_entry *dirent,
  41.                   int    offset,
  42.                   int    blocksize,
  43.                   char    *buf,
  44.                   void    *private)
  45. {
  46.     struct dirst *di = (struct dirst*)private;
  47.  
  48.     if (!di->list) {
  49.         di->list = malloc(100*sizeof(struct fileinfo));
  50.         if (!di->list)
  51.             return E2E_BADMEM;
  52.         di->maxlist = 100;
  53.     }
  54.  
  55.     strncpy(di->list[di->listix].name,dirent->name,dirent->name_len);
  56.     di->list[di->listix].name[dirent->name_len] = 0;
  57.  
  58.     di->listix++;
  59.     if (di->listix==di->maxlist) {
  60.         di->list = realloc(di->list, (di->maxlist+100) * sizeof(struct fileinfo));
  61.         if (!di->list) {
  62.             fprintf(stderr,"Cannot allocate memory\n");
  63.             return DIRENT_ABORT;
  64.         }
  65.         di->maxlist += 100;
  66.     }
  67.     return 0;
  68. }
  69.  
  70. /**********************************************************************
  71.  * opendir_hook, readdir_hook, and closedir_hook are routines to
  72.  * open, read, and close an ext2 directory.
  73.  * 
  74.  * opendir_hook allocates and returns a struct dirst pointer, which is
  75.  * used by the two other routines
  76.  **********************************************************************/
  77.  
  78. static
  79. __ptr_t opendir_hook (const char *directory)
  80. {
  81.     ino_t ino;
  82.     struct ext2_inode e2ino;
  83.     int err;
  84.     struct dirst *di;
  85.  
  86.     /* Lookup specified name */
  87.     err = ext2fs_namei(fs, 2, cwdino, directory, &ino);
  88.     if (err)
  89.         e2_err2("Cannot find file",directory,err);
  90.  
  91.     /* Read specified inode */
  92.     err = ext2fs_read_inode(fs, ino, &e2ino);
  93.     if (err)
  94.         e2_err("Cannot read inode information", err);
  95.  
  96.     /* Is it a directory? */
  97.     if (!S_ISDIR(e2ino.i_mode))
  98.         return 0;
  99.     
  100.     /* Gather file list */
  101.     di = (struct dirst*)malloc(sizeof(struct dirst));
  102.     if (!di) {
  103.         fprintf(stderr,"Cannot allocate memory\n");
  104.         return 0;
  105.     }
  106.     di->listix = 0;
  107.     di->maxlist = 0;
  108.     di->list = NULL;
  109.  
  110.     if (ext2fs_dir_iterate(fs, ino, 0, 0, myproc, di))
  111.         return 0;
  112.     else {
  113.         di->maxlist = di->listix;
  114.         di->listix = 0;
  115.         return (__ptr_t)di;
  116.     }
  117. }
  118.  
  119. static
  120. const char * readdir_hook (__ptr_t stream)
  121. {
  122.     struct dirst *di = (struct dirst*)stream;
  123.     
  124.     if (di->listix==di->maxlist)
  125.         return 0;
  126.     else
  127.         return di->list[di->listix++].name;
  128. }
  129.  
  130. void closedir_hook(__ptr_t stream)
  131. {
  132.     struct dirst *di = (struct dirst*)stream;
  133.     free(di->list);
  134.     free(di);
  135. }
  136.  
  137. /**********************************************************************
  138.  * e2glob_init sets up the directory open, read, and close routines
  139.  * for glob().
  140.  **********************************************************************/
  141.  
  142. void
  143. e2glob_init(ext2_filsys efs)
  144. {
  145.     fs = efs;
  146.     __glob_opendir_hook=opendir_hook;
  147.     __glob_readdir_hook=readdir_hook;
  148.     __glob_closedir_hook=closedir_hook;
  149. }
  150.