home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / getcwd.c < prev    next >
C/C++ Source or Header  |  1993-05-25  |  1KB  |  72 lines

  1. #include <compiler.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>    /* both of these added for malloc() */
  4. #include <limits.h>
  5. #include <errno.h>
  6. #include <osbind.h>
  7. #include <mintbind.h>
  8. #include "lib.h"
  9.  
  10. /*******************************************************************
  11. getcwd: returns current working directory. By ERS.
  12. This routine is in the public domain.
  13. ********************************************************************/
  14.  
  15. extern int __mint;
  16. extern char _rootdir;    /* in main.c: user's preferred root directory */
  17.  
  18. char *getcwd __PROTO((char *, int));
  19.  
  20. char *getcwd(buf, size)
  21. char *buf; int size;
  22. {
  23.     char _path[PATH_MAX];
  24.     char *path;
  25.     char drv;
  26.     int buf_malloced = 0;
  27.     int r;
  28.  
  29.     if (!buf) {
  30.         if ((buf = (char *) malloc((size_t)size)) == 0)
  31.             return NULL;
  32.         buf_malloced = 1;
  33.     }
  34.  
  35.     drv = Dgetdrv() + 'a';
  36.     _path[0] = drv;
  37.     _path[1] = ':';
  38.     _path[2] = '\0';
  39.     path = _path + 2;
  40.     if (__mint >= 96) {
  41.         if ((r = (int) Dgetcwd(path, 0, size - 2)) != 0) {
  42.             if (buf_malloced)
  43.                 free(buf);
  44.             errno = -r;
  45.             return NULL;
  46.         }
  47.     } else {
  48.         (void)Dgetpath(path, 0);
  49.     }
  50.  
  51.     if (_rootdir && drv == _rootdir) {
  52.         if (!*path) {
  53.             path[0] = '\\';
  54.             path[1] = '\0';
  55.         }
  56.         _dos2unx((char *)path, buf);
  57.         return buf;
  58.     }
  59.     _dos2unx(_path, buf);    /* convert DOS filename to unix */
  60.     return buf;
  61. }
  62.  
  63. /*
  64.  * char *getwd(char *buf)
  65.  *    return cwd in buf
  66.  */
  67. char *getwd(buf)
  68. char *buf;
  69. {
  70.     return getcwd(buf, PATH_MAX);
  71. }
  72.