BRL-CAD
getcwd.c
Go to the documentation of this file.
1 /* G E T C W D . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libbu/getcwd.c
21  *
22  * Routine(s) for getting the current working directory full pathname.
23  *
24  */
25 
26 #include "common.h"
27 
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #ifdef HAVE_DIRECT_H
32 # include <direct.h>
33 #endif
34 #ifdef HAVE_SYS_PARAM_H /* for MAXPATHLEN */
35 # include <sys/param.h>
36 #endif
37 #include <string.h>
38 
39 #include "bu/file.h"
40 #include "bu/log.h"
41 #include "bu/malloc.h"
42 #include "bu/str.h"
43 
44 char *
45 bu_getcwd(char *buf, size_t size)
46 {
47  char *cwd = NULL;
48  char *pwd = NULL;
49  char cbuf[MAXPATHLEN] = {0};
50  size_t sz = size;
51 
52  /* NULL buf means allocate */
53  if (!buf) {
54  sz = MAXPATHLEN;
55  buf = (char *)bu_calloc(1, sz, "alloc bu_getcwd");
56  }
57 
58  /* FIRST: try getcwd */
59 #ifdef HAVE_GETCWD
60  cwd = getcwd(cbuf, MAXPATHLEN);
61  if (cwd
62  && strlen(cwd) > 0
63  && bu_file_exists(cwd, NULL))
64  {
65 #if defined(HAVE_WORKING_REALPATH_FUNCTION)
66  /* FIXME: shouldn't have gotten here with -std=c99 (HAVE_REALPATH test not working right?) */
67  char rbuf[MAXPATHLEN] = {0};
68  char *rwd = bu_realpath(cbuf, rbuf);
69  if (rwd
70  && strlen(rwd) > 0
71  && bu_file_exists(rwd, NULL))
72  {
73  BU_ASSERT(sz > strlen(rwd)+1);
74  bu_strlcpy(buf, rwd, strlen(rwd)+1);
75  return buf;
76  }
77 #endif /* HAVE_WORKING_REALPATH_FUNCTION */
78  BU_ASSERT(sz > strlen(cwd)+1);
79  bu_strlcpy(buf, cwd, strlen(cwd)+1);
80  return buf;
81  }
82 #else
83  /* quellage */
84  cwd = memset(cbuf, 0, MAXPATHLEN);
85 #endif /* HAVE_GETCWD */
86 
87 
88  /* SECOND: try environment */
89  pwd = getenv("PWD");
90  if (pwd
91  && strlen(pwd) > 0
92  && bu_file_exists(pwd, NULL))
93  {
94 #if defined(HAVE_WORKING_REALPATH_FUNCTION)
95  char rbuf[MAXPATHLEN] = {0};
96  char *rwd = realpath(pwd, rbuf);
97  if (rwd
98  && strlen(rwd) > 0
99  && bu_file_exists(rwd, NULL))
100  {
101  BU_ASSERT(sz > strlen(rwd)+1);
102  bu_strlcpy(buf, rwd, strlen(rwd)+1);
103  return buf;
104  }
105 #endif /* HAVE_WORKING_REALPATH_FUNCTION */
106  BU_ASSERT(sz > strlen(pwd)+1);
107  bu_strlcpy(buf, pwd, strlen(pwd)+1);
108  return buf;
109  }
110 
111  /* LAST: punt (but do not return NULL) */
112  BU_ASSERT(sz > strlen(".")+1);
113  bu_strlcpy(buf, ".", strlen(".")+1);
114  return buf;
115 }
116 
117 
118 /*
119  * Local Variables:
120  * tab-width: 8
121  * mode: C
122  * indent-tabs-mode: t
123  * c-file-style: "stroustrup"
124  * End:
125  * ex: shiftwidth=4 tabstop=8
126  */
Header file for the BRL-CAD common definitions.
#define BU_ASSERT(_equation)
Definition: defines.h:216
void * memset(void *s, int c, size_t n)
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
char * bu_getcwd(char *buf, size_t size)
Definition: getcwd.c:45
char * bu_realpath(const char *path, char *resolved_path)
Definition: realpath.c:33
int bu_file_exists(const char *path, int *fd)
Definition: file.c:57
#define MAXPATHLEN
Definition: defines.h:113