home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / GETDCWD.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  124 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** GETDCWD.C - returns the current working directory for a specific drive
  5. **
  6. ** public domain by Bob Jarvis, modified by Bob Stout
  7. ** OS/2 compatibility mods by Ed Blackman
  8. */
  9.  
  10. #include "extkword.h"
  11.  
  12. #if defined(__OS2__)
  13.  #define INCL_DOSFILEMGR    /* DosQueryCurrentDisk/Dir   */
  14.  #define INCL_DOSMISC       /* DosError, DosQuerySysInfo */
  15.  #include <os2.h>
  16.  #define GetDrive(d) os2_getdrive(&d)
  17.  
  18.  void os2_getdrive(unsigned int *drive)
  19.  {
  20.        ULONG dummy;
  21.  
  22.        DosQueryCurrentDisk((ULONG *)&drive, &dummy);
  23.  }
  24.  
  25. #elif defined(__ZTC__)
  26.  #define GetDrive(d) dos_getdrive(&d)
  27. #elif defined(__TURBOC__)
  28.  #define GetDrive(d) ((d) = getdisk() + 1)
  29. #else /* assume MSC */
  30.  #define GetDrive(d) _dos_getdrive(&d)
  31. #endif
  32.  
  33. #ifndef __OS2__
  34.  #include <dos.h>
  35. #endif      /* !__OS2__ */
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <ctype.h>
  39. #include "dosfiles.h"
  40.  
  41. char *getdcwd(unsigned int drive)   /* 0 = current, 1 = A, 2 = B, etc */
  42. {
  43.       char *retptr, FAR *fretptr;
  44.       unsigned long pathMaxLen = FILENAME_MAX + 4;
  45.  
  46. #ifdef __OS2__
  47.       APIRET rc;
  48. #else
  49.       union REGS regs;
  50.       struct SREGS sregs;
  51. #endif
  52.  
  53.  
  54. #ifdef __OS2__
  55. /*
  56. ** it's bad practice in OS/2 to use hard coded values for things like
  57. ** the maximum length of paths and such.  Query the system instead.
  58. */
  59.       rc = DosQuerySysInfo(QSV_MAX_PATH_LENGTH, QSV_MAX_PATH_LENGTH,
  60.             &pathMaxLen, sizeof(pathMaxLen));
  61.       if(0 != rc)
  62.             return NULL;
  63. #endif
  64.  
  65.       retptr = calloc((size_t)pathMaxLen, sizeof(char));
  66.       if(retptr == NULL)
  67.             return NULL;
  68.       else  fretptr = (char FAR *)retptr;
  69.  
  70.       if(drive == 0)    /* figure out which drive is current */
  71.             GetDrive(drive);
  72.  
  73.       *retptr = (char)((drive-1) + 'A');
  74.       *(retptr+1) = ':';
  75.       *(retptr+2) = '\\';
  76.  
  77. #ifdef __OS2__
  78.       pathMaxLen -= 3;
  79.       DosError(FERR_DISABLEHARDERR); /* disable "drive not ready" popups */
  80.  
  81.       rc = DosQueryCurrentDir(drive, retptr + 3, &pathMaxLen);
  82.  
  83.       DosError(FERR_ENABLEHARDERR);  /* re-enable popups */
  84. #else
  85.       segread(&sregs);
  86.       regs.h.ah = 0x47;
  87.       regs.h.dl = (unsigned char)drive;
  88.       sregs.ds  = FP_SEG(fretptr);
  89.       regs.x.si = FP_OFF(fretptr) + 3;
  90.  
  91.       intdosx(®s, ®s, &sregs);
  92. #endif
  93.  
  94. #ifdef __OS2__
  95.       if (0 != rc)             /* drive number invalid or other error */
  96. #else
  97.       if (15 == regs.x.ax)     /* drive number invalid */
  98. #endif
  99.       {
  100.             free(retptr);
  101.             return NULL;
  102.       }
  103.       else  return retptr;
  104. }
  105.  
  106. #ifdef TEST
  107.  
  108. main(int argc, char *argv[])
  109. {
  110.       char *curpath;
  111.       unsigned int n;
  112.  
  113.       if(argc > 1)
  114.             n = (tolower(*argv[1]) - 'a') + 1;
  115.       else  n = 0;
  116.       
  117.       printf("curpath = '%s'\n", curpath = getdcwd(n));
  118.       if (curpath)
  119.             free(curpath);
  120.       return 0;
  121. }
  122.  
  123. #endif /* TEST */
  124.