home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XS.ZIP / LIB / NDIR.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  6KB  |  178 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    n d i r . c                                                     */
  3. /*                                                                    */
  4. /*    Berkeley-style directory reading routine on MS-DOS by Samuel    */
  5. /*    Lam <skl@van-bc.UUCP>, June/87                                  */
  6. /*                                                                    */
  7. /*    Changes Copyright (c) 1990, 1991 by Andrew H. Derbyshire        */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. /*--------------------------------------------------------------------*/
  11. /*                   Standard library include files                   */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. #include <dos.h>
  18. #include <stdlib.h>
  19.  
  20. /*--------------------------------------------------------------------*/
  21. /*                    UUPC/extended include files                     */
  22. /*--------------------------------------------------------------------*/
  23.  
  24. #include "lib.h"
  25. #include "ndir.h"
  26. #include "getdta.h"
  27.  
  28. #ifndef __TURBOC__
  29. #include "getdta.h"           /* Custom versions of Disk Xfer Addr
  30.                                  functions for MS environment only   */
  31. #endif
  32.  
  33. /*--------------------------------------------------------------------*/
  34. /*                          Global variables                          */
  35. /*--------------------------------------------------------------------*/
  36.  
  37. currentfile();
  38.  
  39. /*--------------------------------------------------------------------*/
  40. /*    o p e n d i r x                                                 */
  41. /*                                                                    */
  42. /*    Open a directory                                                */
  43. /*--------------------------------------------------------------------*/
  44.  
  45. extern DIR *opendirx( const char *dirname, char *pattern)
  46. {
  47.    union REGS inregs, outregs;
  48.    struct SREGS segregs;
  49.    char pathname[128];
  50.    DTA far *dtasave;
  51.    DTA far *dtaptr;
  52.    char far *pathptr;
  53.    DIR *dirp;
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*                    Build pathname to be scanned                    */
  57. /*--------------------------------------------------------------------*/
  58.  
  59.    strcpy(pathname, dirname);
  60.    if ((*pattern != '/') || (dirname[ strlen(dirname) - 1] != '/'))
  61.       strcat(pathname,"/");
  62.    strcat(pathname, pattern);
  63.  
  64.    /* allocate control block */
  65.    dirp = malloc(sizeof(DIR));
  66.  
  67. /*--------------------------------------------------------------------*/
  68. /*                     Set disk transfer address                      */
  69. /*--------------------------------------------------------------------*/
  70.  
  71.    dtasave = (DTA far *)getdta();
  72.    dtaptr = (DTA far *)&(dirp->dirdta);
  73.    setdta((char far *)dtaptr);
  74.  
  75. /*--------------------------------------------------------------------*/
  76. /*                      look for the first file                       */
  77. /*--------------------------------------------------------------------*/
  78.  
  79.    inregs.h.ah = 0x4e;
  80.    pathptr = (char far *)pathname;
  81.    segregs.ds = FP_SEG(pathptr);
  82.    inregs.x.dx = FP_OFF(pathptr);
  83.    inregs.x.cx = 0;   /* attribute */
  84.    intdosx(&inregs, &outregs, &segregs);
  85.  
  86.    /* bad directory name? */
  87.    if (outregs.x.cflag && (outregs.x.ax == 2 || outregs.x.ax == 3)) {
  88.       free(dirp);
  89.       return NULL;
  90.    }
  91.  
  92.    dirp->dirfirst = outregs.x.cflag ? outregs.x.ax : 0;
  93.  
  94.    setdta((char far *)dtasave);
  95.    strcpy(dirp->dirid, "DIR");
  96.  
  97.    return dirp;
  98.  
  99. } /*opendir*/
  100.  
  101.  
  102. /*--------------------------------------------------------------------*/
  103. /*    r e a d d i r                                                   */
  104. /*                                                                    */
  105. /*    Get next entry in a directory                                   */
  106. /*--------------------------------------------------------------------*/
  107.  
  108. struct direct *readdir(DIR *dirp)
  109. {
  110.    int errcode;
  111.  
  112.    if (!equal(dirp->dirid, "DIR"))
  113.    {
  114.       printmsg(0,"Unexpected readdir call; no search in progress");
  115.       panic();
  116.    }
  117.  
  118.    if (dirp->dirfirst == -1) {
  119.       union REGS inregs, outregs;
  120.       struct SREGS segregs;
  121.       DTA far *dtaptr;
  122.       DTA far *dtasave;
  123.  
  124.      /* set DTA address to our buffer each time we're called */
  125.       dtasave = (DTA far *)getdta();
  126.       dtaptr = (DTA far *)&(dirp->dirdta);
  127.       setdta((char far *)dtaptr);
  128.  
  129.       inregs.h.ah = 0x4f;
  130.       segregs.ds = FP_SEG(dtaptr);
  131.       inregs.x.dx = FP_OFF(dtaptr);
  132.       intdosx(&inregs, &outregs, &segregs);
  133.       errcode = outregs.x.cflag ? outregs.x.ax : 0;
  134.  
  135.       setdta((char far *)dtasave);  /* Restore DTA address     */
  136.  
  137.    } else {
  138.  
  139.       errcode = dirp->dirfirst;
  140.       dirp->dirfirst = -1;
  141.  
  142.    };
  143.  
  144.    /* no more files in directory? */
  145.    if (errcode == 18)
  146.       return NULL;
  147.  
  148.    if ( errcode != 0)
  149.    {
  150.       errno = errcode;
  151.       printerr( "readdir" );
  152.       panic();
  153.    }
  154.  
  155.    dirp->dirent.d_ino = -1;   /* no inode information */
  156.    strlwr(strcpy(dirp->dirent.d_name, dirp->dirdta.filename));
  157.    dirp->dirent.d_namlen = strlen(dirp->dirent.d_name);
  158.    dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  159.       ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  160.  
  161.    return &(dirp->dirent);
  162.  
  163. } /*readdir*/
  164.  
  165. /*--------------------------------------------------------------------*/
  166. /*    c l o s e d i r                                                 */
  167. /*                                                                    */
  168. /*    Close a directory                                               */
  169. /*--------------------------------------------------------------------*/
  170.  
  171. void closedir(DIR *dirp)
  172. {
  173.  
  174.    strcpy(dirp->dirid, "XXX");
  175.    free(dirp);
  176.  
  177. } /*closedir*/
  178.