home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / chdir.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  5KB  |  166 lines

  1. /***
  2. *chdir.c - change directory
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       This file has the _chdir() function - change current directory.
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13. #include <cruntime.h>
  14. #include <oscalls.h>
  15. #include <mtdll.h>
  16. #include <internal.h>
  17. #include <direct.h>
  18. #include <stdlib.h>
  19. #include <tchar.h>
  20.  
  21. /***
  22. *int _chdir(path) - change current directory
  23. *
  24. *Purpose:
  25. *       Changes the current working directory to that given in path.
  26. *
  27. *Entry:
  28. *       _TSCHAR *path - directory to change to
  29. *
  30. *Exit:
  31. *       returns 0 if successful,
  32. *       returns -1 and sets errno if unsuccessful
  33. *
  34. *Exceptions:
  35. *
  36. *******************************************************************************/
  37.  
  38. int __cdecl _tchdir (
  39.         const _TSCHAR *path
  40.         )
  41. {
  42.         _TSCHAR env_var[4];
  43.         _TSCHAR abspath[MAX_PATH+1];
  44.  
  45.         if ( SetCurrentDirectory((LPTSTR)path) )
  46.         {
  47.             /*
  48.              * If the new current directory path is NOT a UNC path, we must
  49.              * update the OS environment variable specifying the current
  50.              * directory for what is now current drive. To do this, get the
  51.              * full current directory, build the environment variable string
  52.              * and call SetEnvironmentVariable(). We need to do this because
  53.              * SetCurrentDirectory does not (i.e., does not update the
  54.              * current-directory-on-drive environment variables) and other
  55.              * functions (fullpath, spawn, etc) need them to be set.
  56.              *
  57.              * If associated with a 'drive', the current directory should
  58.              * have the form of the example below:
  59.              *
  60.              *  D:\nt\private\mytests
  61.              *
  62.              * so that the environment variable should be of the form:
  63.              *
  64.              *  =D:=D:\nt\private\mytests
  65.              *
  66.              */
  67.             if ( GetCurrentDirectory(MAX_PATH+1, (LPTSTR)abspath) != 0 )
  68.             {
  69.                 /*
  70.                  * check if it is a UNC name, just return if is
  71.                  */
  72.                 if ( ((abspath[0] == _T('\\')) || (abspath[0] == _T('/'))) &&
  73.                      (abspath[0] == abspath[1]) )
  74.                     return 0;
  75.  
  76.                 env_var[0] = _T('=');
  77.                 env_var[1] = (_TSCHAR) _totupper((_TUCHAR)abspath[0]);
  78.                 env_var[2] = _T(':');
  79.                 env_var[3] = _T('\0');
  80.  
  81.                 if ( SetEnvironmentVariable(env_var, abspath) )
  82.                     return 0;
  83.             }
  84.         }
  85.  
  86.         _dosmaperr(GetLastError());
  87.         return -1;
  88. }
  89.  
  90. #else  /* _MAC */
  91.  
  92.  
  93. #include <cruntime.h>
  94. #include <internal.h>
  95. #include <direct.h>
  96. #include <string.h>
  97. #include <errno.h>
  98. #include <macos\osutils.h>
  99. #include <macos\files.h>
  100. #include <macos\errors.h>
  101.  
  102. /***
  103. *int _chdir(path) - change current directory
  104. *
  105. *Purpose:
  106. *       Changes the current working directory to that given in path.
  107. *
  108. *Entry:
  109. *       char *path -    directory to change to
  110. *
  111. *Exit:
  112. *       returns 0 if successful,
  113. *       returns -1 and sets errno if unsuccessful
  114. *
  115. *Exceptions:
  116. *
  117. *******************************************************************************/
  118. #define CurDirStore (*(long *)0x398)     //directory id
  119. #define SFSaveDisk (*(short *)0x214)     //negative volume number
  120.  
  121. int __cdecl _chdir (
  122.         const char *path
  123.         )
  124. {
  125.         WDPBRec wdPB;
  126.         char st[256];
  127.         OSErr osErr;
  128.         ParamBlockRec  parm;
  129.  
  130.         if (!*path)
  131.         {
  132.                 errno = ENOENT;
  133.                 return -1;
  134.         }
  135.  
  136.         strcpy(st, path);
  137.         wdPB.ioNamePtr = _c2pstr(st);
  138.         wdPB.ioVRefNum = 0;
  139.         wdPB.ioWDDirID = 0;
  140.         osErr = PBHSetVolSync(&wdPB);
  141.  
  142.         if (osErr) {
  143.                 /* error occured -- map error code and return */
  144.                 _dosmaperr(osErr);
  145.                 return -1;
  146.         }
  147.  
  148.         osErr = PBHGetVolSync(&wdPB);
  149.  
  150.         if (osErr) {
  151.                 /* error occured -- map error code and return */
  152.                 _dosmaperr(osErr);
  153.                 return -1;
  154.         }
  155.  
  156.         CurDirStore = wdPB.ioWDDirID;
  157.         SFSaveDisk =  wdPB.ioWDVRefNum;
  158.  
  159.         memset(&parm, 0, sizeof(ParamBlockRec));
  160.         osErr = PBSetVolSync(&parm);
  161.  
  162.         return 0;
  163. }
  164.  
  165. #endif  /* _MAC */
  166.