home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / CLIBSRC2.ZIP / GETDCWD.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  3.0 KB  |  104 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - _getdcwd.cas
  3.  *
  4.  * function(s)
  5.  *        _getdcwd - gets current directory for specified drive
  6.  *--------------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #pragma inline
  18.  
  19. #include <asmrules.h>
  20. #include <alloc.h>
  21. #include <dir.h>
  22. #include <dos.h>
  23. #include <errno.h>
  24. #include <string.h>
  25.  
  26.  
  27. /*---------------------------------------------------------------------*
  28.  
  29. Name            _getdcwd - gets working directory for specific drive
  30.  
  31. Usage           char *_getdcwd(int drive char *buf, int n);
  32.  
  33. Prototype in    dir.h
  34.  
  35. Description     _getdcwd gets the full path name of the working directory
  36.                 of the specified drive (including the drive name), up to n
  37.                 bytes long, and stores it in buf. If the full path name length
  38.                 (including the null-terminator) is longer than n, an
  39.                 error occurs.  The drive is 0 for the default drive, 1=A,
  40.                 2=B, etc.
  41.  
  42.                 If buf is NULL, a buffer n bytes long will be allocated
  43.                 for you with malloc. You can later free the allocated buffer
  44.                 by passing the _getdcwd return value to the function free.
  45.  
  46. Return value    _getdcwd returns buf; on error, it returns NULL.
  47.  
  48.                 In the event of an error return, the global variable errno is
  49.                 set to one of the following:
  50.  
  51.                         ENOMEM  Not enough core
  52.                         ERANGE  Result out of range
  53.  
  54. *---------------------------------------------------------------------*/
  55.  
  56. char * _FARFUNC _getdcwd(int drive, char *bufP, int bufL)
  57. {
  58.         char    bufI[MAXDIR + 3];
  59.         unsigned drv;
  60.  
  61.         /* Construct drive name */
  62.         if ((drv = drive) == 0)
  63.         {
  64.                 _AH = 0x19;             /* get current drive */
  65.                 geninterrupt(0x21);
  66.                 drv = _AL + 1;
  67.         }
  68.         bufI[0] = drv + 'A' - 1;
  69.         bufI[1] = ':';
  70.         bufI[2] = '\\';
  71.  
  72.         /* Get current directory in a work buffer */
  73. #if LDATA
  74. asm     push    ds
  75. asm     mov     ax, ss
  76. asm     mov     ds, ax
  77. #endif
  78. asm     lea     si, bufI[3]
  79. asm     mov     ah, 047h
  80. asm     mov     dl, drv
  81. asm     int     021H
  82. #if LDATA
  83. asm     pop     ds
  84. #endif
  85.         if (_FLAGS & 1)         /* carry indicates error */
  86.                 return NULL;
  87.  
  88.         if (strlen(bufI) >= bufL)
  89.         {
  90.                 errno = ERANGE;
  91.                 return  NULL;
  92.         }
  93.  
  94.         /* Allocate a buffer if bufP is NULL */
  95.         if (bufP == NULL)
  96.                 if ((bufP = malloc(bufL)) == NULL)
  97.                 {
  98.                         errno = ENOMEM;
  99.                         return  NULL;
  100.                 }
  101.         strcpy(bufP, bufI);
  102.         return  bufP;
  103. }
  104.