home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Python / getcwd.c < prev    next >
C/C++ Source or Header  |  1994-01-02  |  3KB  |  107 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Two PD getcwd() implementations.
  26.    Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */
  27.  
  28. #include <stdio.h>
  29. #include <errno.h>
  30.  
  31. #ifdef HAVE_GETWD
  32.  
  33. /* Version for BSD systems -- use getwd() */
  34.  
  35. #ifdef HAVE_SYS_PARAM_H
  36. #include <sys/param.h>
  37. #endif
  38.  
  39. #ifndef MAXPATHLEN
  40. #define MAXPATHLEN 1024
  41. #endif
  42.  
  43. extern char *getwd();
  44.  
  45. char *
  46. getcwd(buf, size)
  47.     char *buf;
  48.     int size;
  49. {
  50.     char localbuf[MAXPATHLEN+1];
  51.     char *ret;
  52.     
  53.     if (size <= 0) {
  54.         errno = EINVAL;
  55.         return NULL;
  56.     }
  57.     ret = getwd(localbuf);
  58.     if (ret != NULL && strlen(localbuf) >= size) {
  59.         errno = ERANGE;
  60.         return NULL;
  61.     }
  62.     if (ret == NULL) {
  63.         errno = EACCES; /* Most likely error */
  64.         return NULL;
  65.     }
  66.     strncpy(buf, localbuf, size);
  67.     return buf;
  68. }
  69.  
  70. #else /* !HAVE_GETWD */
  71.  
  72. /* Version for really old UNIX systems -- use pipe from pwd */
  73.  
  74. #ifndef PWD_CMD
  75. #define PWD_CMD "/bin/pwd"
  76. #endif
  77.  
  78. char *
  79. getcwd(buf, size)
  80.     char *buf;
  81.     int size;
  82. {
  83.     FILE *fp;
  84.     char *p;
  85.     int sts;
  86.     if (size <= 0) {
  87.         errno = EINVAL;
  88.         return NULL;
  89.     }
  90.     if ((fp = popen(PWD_CMD, "r")) == NULL)
  91.         return NULL;
  92.     if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) {
  93.         errno = EACCES; /* Most likely error */
  94.         return NULL;
  95.     }
  96.     for (p = buf; *p != '\n'; p++) {
  97.         if (*p == '\0') {
  98.             errno = ERANGE;
  99.             return NULL;
  100.         }
  101.     }
  102.     *p = '\0';
  103.     return buf;
  104. }
  105.  
  106. #endif /* !HAVE_GETWD */
  107.