home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / CLIBSRC2.ZIP / GETCWD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.4 KB  |  80 lines

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