home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / GETCWD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.4 KB  |  81 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - getcwd.c
  3.  *
  4.  * function(s)
  5.  *        getcwd - gets current working directory
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #include <dir.h>
  19. #include <stddef.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <alloc.h>
  23.  
  24.  
  25. /*---------------------------------------------------------------------*
  26.  
  27. Name        getcwd - gets current working directory
  28.  
  29. Usage        char *getcwd(char *buf, int n);
  30.  
  31. Prototype in    dir.h
  32.  
  33. Description    getcwd gets the full path name of the cwd (current
  34.         working directory, including the drive), up to n bytes
  35.         long, and stores it in buf. If the full path name length
  36.         (including the null-terminator) is longer than n, an
  37.         error occurs.
  38.  
  39.         If buf is NULL, a buffer n bytes long will be allocated
  40.         for you with malloc. You can later free the allocated buffer
  41.         by passing the getcwd return value to the function free.
  42.  
  43. Return value    getcwd returns buf; on error, it returns NULL.
  44.  
  45.         In the event of an error return, the global variable errno is
  46.         set to one of the following:
  47.  
  48.             ENODEV    No such device
  49.             ENOMEM    Not enough core
  50.             ERANGE    Result out of range
  51.  
  52. *---------------------------------------------------------------------*/
  53. char *getcwd(char *bufP, int bufL)
  54. {
  55.     char    bufI[MAXDIR + 2];
  56.  
  57.     /* Get current disk */
  58.     bufI[0] = getdisk() + 'A';
  59.     bufI[1] = ':';
  60.     bufI[2] = '\\';
  61.  
  62.     /* Get current directory in a work buffer */
  63.     if (getcurdir(0, &bufI[3]) == -1)
  64.         return    NULL;
  65.     if (strlen(bufI) >= bufL)
  66.     {
  67.         errno = ERANGE;
  68.         return    NULL;
  69.     }
  70.  
  71.     /* Allocate a buffer if bufP is NULL */
  72.     if (bufP == NULL)
  73.         if ((bufP = malloc(bufL)) == NULL)
  74.         {
  75.             errno = ENOMEM;
  76.             return    NULL;
  77.         }
  78.     strcpy(bufP, bufI);
  79.     return    bufP;
  80. }
  81.