home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / util / gnu / groff_src.lha / groff-1.10src / libgroff / getcwd.c < prev    next >
C/C++ Source or Header  |  1992-05-15  |  656b  |  39 lines

  1. /* Partial emulation of getcwd in terms of getwd. */
  2.  
  3. #include <sys/param.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #ifndef errno
  7. extern int errno;
  8. #endif
  9.  
  10. char *getwd();
  11.  
  12. char *getcwd(buf, size)
  13.      char *buf;
  14.      int size;            /* POSIX says this should be size_t */
  15. {
  16.   if (size <= 0) {
  17.     errno = EINVAL;
  18.     return 0;
  19.   }
  20.   else {
  21.     char mybuf[MAXPATHLEN];
  22.     int saved_errno = errno;
  23.  
  24.     errno = 0;
  25.     if (!getwd(mybuf)) {
  26.       if (errno == 0)
  27.     ;       /* what to do? */
  28.       return 0;
  29.     }
  30.     errno = saved_errno;
  31.     if (strlen(mybuf) + 1 > size) {
  32.       errno = ERANGE;
  33.       return 0;
  34.     }
  35.     strcpy(buf, mybuf);
  36.     return buf;
  37.   }
  38. }
  39.