home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / tmp9 / p_dir.c < prev    next >
C/C++ Source or Header  |  2000-06-11  |  6KB  |  191 lines

  1. /*****************************************************************************/
  2. /*             Copyright (c) 1994 by Jyrki Salmi <jytasa@jyu.fi>             */
  3. /*        You may modify, recompile and distribute this file freely.         */
  4. /*****************************************************************************/
  5.  
  6. /*
  7.    A bit cleaner directory handling routines than the OS/2's DosFindFirst and
  8.    DosFindNext. Note that dir_find_close() should be called when done with
  9.    the directory listing.
  10. */
  11.  
  12. #include "ckcdeb.h"
  13. #ifndef NOXFER
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17.  
  18. #ifdef OS2
  19. #ifdef NT
  20. #include <windows.h>
  21. #define APIRET DWORD
  22. #else
  23. #define INCL_DOSFILEMGR
  24. #include <os2.h>
  25. #undef COMMENT
  26. #endif
  27. #include "ckocon.h"
  28. #endif /* OS2 */
  29.  
  30. #include "p_type.h"
  31. #include "p_dir.h"
  32.  
  33.  
  34. /* Finds a first file */
  35. /* returns 0 if successful */
  36. U32
  37. #ifdef CK_ANSIC
  38. dir_find_first(U8 *mask,
  39.                U32 search_attribute,
  40.                DIR_ENTRY *dir_entry)
  41. #else
  42. dir_find_first(mask,search_attribute,dir_entry)
  43.     U8 *mask;
  44.     U32 search_attribute;
  45.     DIR_ENTRY *dir_entry;
  46. #endif
  47. {
  48.  
  49.     U32 find_count = 1;
  50.     APIRET rc = 0;
  51.  
  52. #ifdef OS2
  53. #ifdef NT
  54.     FILETIME * writetime;
  55.     static WORD date, time;
  56.     if ((dir_entry->find_buf = malloc(sizeof(WIN32_FIND_DATA))) == NULL) {
  57.         fprintf(stderr, "Failed to allocate memory\n");
  58.         exit(1);
  59.     }
  60.     dir_entry->search_attr = search_attribute ;
  61.     if ( !(dir_entry->handle = FindFirstFile( mask, dir_entry->find_buf )) )
  62.       rc = GetLastError();
  63.     else {
  64.         dir_entry->name = ((WIN32_FIND_DATA *)dir_entry->find_buf)->cFileName;
  65.         dir_entry->size =
  66.           (U32 *)&((WIN32_FIND_DATA *)dir_entry->find_buf)->nFileSizeLow;
  67.         writetime =
  68.           &(((WIN32_FIND_DATA *)dir_entry->find_buf)->ftLastWriteTime);
  69.         FileTimeToDosDateTime( writetime, &date, &time);
  70.         dir_entry->date = (U16 *)&date;
  71.         dir_entry->time = (U16 *)&time;
  72.         dir_entry->attribute =
  73.           &(((WIN32_FIND_DATA *)dir_entry->find_buf)->dwFileAttributes) ;
  74.         while (rc == 0 &&
  75.                dir_entry->search_attr  == DIR_FIND_DIRECTORY &&
  76.                !(*dir_entry->attribute & FILE_ATTRIBUTE_DIRECTORY)
  77.                ) {
  78.             rc = dir_find_next( dir_entry ) ;
  79.         }
  80.     }
  81. #else /* NT */
  82.     if ((dir_entry->find_buf = malloc(sizeof(FILEFINDBUF3))) == NULL) {
  83.         fprintf(stderr, "Failed to allocate memory\n");
  84.         exit(1);
  85.     }
  86.     dir_entry->handle = HDIR_CREATE; /* HDIR_SYSTEM; */
  87.     rc = DosFindFirst(mask,
  88.                       &dir_entry->handle,
  89.                       search_attribute,
  90.                       dir_entry->find_buf,
  91.                       sizeof(FILEFINDBUF3),
  92.                       &find_count,
  93.                       FIL_STANDARD);
  94.     dir_entry->name = ((FILEFINDBUF3 *)dir_entry->find_buf)->achName;
  95.     dir_entry->size = (U32 *)&((FILEFINDBUF3 *)dir_entry->find_buf)->cbFile;
  96.     dir_entry->date =
  97.       (U16 *)&((FILEFINDBUF3 *)dir_entry->find_buf)->fdateLastWrite;
  98.     dir_entry->time =
  99.       (U16 *)&((FILEFINDBUF3 *)dir_entry->find_buf)->ftimeLastWrite;
  100.     dir_entry->attribute =
  101.       &((FILEFINDBUF3 *)dir_entry->find_buf)->attrFile;
  102. #endif /* NT */
  103. #else /* OS2 */
  104.     THIS_HAS_TO_BE_FILLED_IN_FOR_UNIX;
  105.     /* In Kermit we call zxpand(name) for this */
  106.     /* But then what? */
  107. #endif /* OS2 */
  108.     return(rc);
  109. }
  110.  
  111. /* Finds the next file */
  112.  
  113. U32
  114. #ifdef CK_ANSIC
  115. dir_find_next(DIR_ENTRY *dir_entry)
  116. #else
  117. dir_find_next(dir_entry) DIR_ENTRY * dir_entry ;
  118. #endif
  119. {
  120.     U32 find_count = 1;
  121.     APIRET rc=0;
  122.  
  123. #ifdef OS2
  124. #ifdef NT
  125.     FILETIME * writetime;
  126.     static WORD date, time;
  127.     if ( !FindFirstFile( dir_entry->handle, dir_entry->find_buf ) )
  128.       rc = GetLastError();
  129.     else {
  130.         dir_entry->name = ((WIN32_FIND_DATA *)dir_entry->find_buf)->cFileName;
  131.         dir_entry->size =
  132.           (U32 *)&((WIN32_FIND_DATA *)dir_entry->find_buf)->nFileSizeLow;
  133.  
  134.         writetime = &((WIN32_FIND_DATA *)dir_entry->find_buf)->ftLastWriteTime;
  135.         FileTimeToDosDateTime( writetime, &date, &time ) ;
  136.         dir_entry->date = (U16 *)&date ;
  137.         dir_entry->time = (U16 *)&time ;
  138.         dir_entry->attribute =
  139.           &((WIN32_FIND_DATA *)dir_entry->find_buf)->dwFileAttributes ;
  140.  
  141.         while (rc == 0 &&
  142.                dir_entry->search_attr  == DIR_FIND_DIRECTORY &&
  143.                !(*dir_entry->attribute & FILE_ATTRIBUTE_DIRECTORY)
  144.                ) {
  145.             rc = dir_find_next( dir_entry ) ;
  146.         }
  147.     }
  148. #else /* NT */
  149.     rc = DosFindNext(dir_entry->handle,
  150.                      dir_entry->find_buf,
  151.                      sizeof(FILEFINDBUF3),
  152.                      &find_count);
  153.     dir_entry->name = ((FILEFINDBUF3 *)dir_entry->find_buf)->achName;
  154.     dir_entry->size = (U32 *)&((FILEFINDBUF3 *)dir_entry->find_buf)->cbFile;
  155.     dir_entry->date =
  156.       (U16 *)&((FILEFINDBUF3 *)dir_entry->find_buf)->fdateLastWrite;
  157.     dir_entry->time =
  158.       (U16 *)&((FILEFINDBUF3 *)dir_entry->find_buf)->ftimeLastWrite;
  159.     dir_entry->attribute = &((FILEFINDBUF3 *)dir_entry->find_buf)->attrFile;
  160. #endif /* NT */
  161. #else /* OS2 */
  162.     THIS_HAS_TO_BE_FILLED_IN_FOR_UNIX;
  163.     /* In Kermit we call znext(name), but then what? */
  164. #endif /* OS2 */
  165.     return(rc);
  166. }
  167.  
  168. /* Ends the directory handling started by dir_find_first() */
  169.  
  170. U32
  171. #ifdef CK_ANSIC
  172. dir_find_close(DIR_ENTRY *dir_entry)
  173. #else
  174. dir_find_close(dir_entry) DIR_ENTRY * dir_entry ;
  175. #endif
  176. {
  177.     APIRET rc=0;
  178. #ifdef OS2
  179. #ifdef NT
  180.     if ( !FindClose( dir_entry->handle) )
  181.       rc = GetLastError() ;
  182. #else
  183.     rc = DosFindClose(dir_entry->handle);
  184. #endif
  185. #else /* OS2 */
  186.     THIS_HAS_TO_BE_FILLED_IN_FOR_UNIX;
  187.     /* Not needed in Kermit */
  188. #endif /* OS2 */
  189.     return(rc);
  190. }
  191. #endif /* NOXFER */