codec cleanup
[platform/upstream/boost-jam.git] / pwd.c
1 /* Copyright Vladimir Prus 2002, Rene Rivera 2005. Distributed under the Boost */
2 /* Software License, Version 1.0. (See accompanying */
3 /* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) */
4
5 #include "jam.h"
6 #include "lists.h"
7 #include "newstr.h"
8 #include "pathsys.h"
9 #include "mem.h"
10
11 #include <limits.h>
12 #include <errno.h>
13
14 /* MinGW on windows declares PATH_MAX in limits.h */
15 #if defined(NT) && ! defined(__GNUC__)
16 #include <direct.h>
17 #define PATH_MAX _MAX_PATH
18 #else
19 #include <unistd.h>
20 #if defined(__COMO__)
21      #include <linux/limits.h>
22 #endif
23 #endif
24
25 #ifndef PATH_MAX
26         #define PATH_MAX 1024
27 #endif
28
29 /* The current directory can't change in bjam, so optimize this to cache
30 ** the result.
31 */
32 static char * pwd_result = NULL;
33
34
35 LIST*
36 pwd(void)
37 {
38     if (!pwd_result)
39     {
40                 int buffer_size = PATH_MAX;
41                 char * result_buffer = 0;
42                 do
43                 {
44                         char * buffer = BJAM_MALLOC_RAW(buffer_size);
45                         result_buffer = getcwd(buffer,buffer_size);
46                         if (result_buffer)
47                         {
48                                 #ifdef NT
49                                 pwd_result = short_path_to_long_path(result_buffer);
50                                 #else
51                                 pwd_result = newstr(result_buffer);
52                                 #endif
53                         }
54                         buffer_size *= 2;
55                         BJAM_FREE_RAW(buffer);
56                 }
57                 while (!pwd_result && errno == ERANGE);
58                 
59                 if (!pwd_result)
60                 {
61             perror("can not get current directory");
62             return L0;
63         }
64     }
65     return list_new(L0, pwd_result);
66 }