BRL-CAD
realpath.c
Go to the documentation of this file.
1 /* R E A L P A T H . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-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 
21 #include "common.h"
22 
23 #include <limits.h>
24 #include <stdlib.h>
25 
26 #include "bio.h"
27 
28 #include "bu/file.h"
29 #include "bu/malloc.h"
30 #include "bu/str.h"
31 
32 char *
33 bu_realpath(const char *path, char *resolved_path)
34 {
35  if (!resolved_path)
36  resolved_path = (char *) bu_calloc(MAXPATHLEN, sizeof(char), "resolved_path alloc");
37 
38 #if defined(HAVE_WORKING_REALPATH_FUNCTION)
39  {
40  char *dirpath = NULL;
41  dirpath = realpath(path, resolved_path);
42  if (!dirpath) {
43  /* if path lookup failed, resort to simple copy */
44  bu_strlcpy(resolved_path, path, (size_t)MAXPATHLEN);
45  }
46  }
47 #elif defined(HAVE_GETFULLPATHNAME)
48  /* Best solution currently available for Windows
49  * See https://www.securecoding.cert.org/confluence/display/seccode/FIO02-C.+Canonicalize+path+names+originating+from+untrusted+sources */
50  GetFullPathName(path, MAXPATHLEN, resolved_path, NULL);
51 #else
52  /* Last resort - if NOTHING is defined, do a simple copy */
53  bu_strlcpy(resolved_path, path, (size_t)MAXPATHLEN);
54 #endif
55 
56  return resolved_path;
57 }
58 
59 /*
60  * Local Variables:
61  * mode: C
62  * tab-width: 8
63  * indent-tabs-mode: t
64  * c-file-style: "stroustrup"
65  * End:
66  * ex: shiftwidth=4 tabstop=8
67  */
Header file for the BRL-CAD common definitions.
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_realpath(const char *path, char *resolved_path)
Definition: realpath.c:33
#define MAXPATHLEN
Definition: defines.h:113