BRL-CAD
db_path.c
Go to the documentation of this file.
1 /* D B _ P A T H . 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 
21 #include "common.h"
22 #include <string.h>
23 
24 #ifdef HAVE_SYS_PARAM_H
25 # include <sys/param.h>
26 #endif
27 
28 #include "raytrace.h"
29 
30 
31 /* Most of db_normalize is a subset of NetBSD's realpath function:
32  *
33  * Copyright (c) 1989, 1991, 1993, 1995
34  * The Regents of the University of California. All rights reserved.
35  *
36  * This code is derived from software contributed to Berkeley by
37  * Jan-Simon Pendry.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  * notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  * notice, this list of conditions and the following disclaimer in the
46  * documentation and/or other materials provided with the distribution.
47  * 3. Neither the name of the University nor the names of its contributors
48  * may be used to endorse or promote products derived from this software
49  * without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  */
63 const char *
64 db_normalize(const char *path)
65 {
66  static char resolved[MAXPATHLEN] = {0};
67  const char *q;
68  char *p;
69  if (!path) return (NULL);
70 
71  /*
72  * `p' is where we'll put a new component with prepending
73  * a delimiter.
74  */
75  p = resolved;
76 
77  if (*path == 0) return (NULL);
78 
79 loop:
80  /* Skip any slash. */
81  while (*path == '/')
82  path++;
83 
84  if (*path == 0) {
85  if (p == resolved)
86  *p++ = '/';
87  *p = 0;
88  return (resolved);
89  }
90 
91  /* Find the end of this component. */
92  q = path;
93  do
94  q++;
95  while (*q != '/' && *q != 0);
96 
97  /* Test . or .. */
98  if (path[0] == '.') {
99  if (q - path == 1) {
100  path = q;
101  goto loop;
102  }
103  if (path[1] == '.' && q - path == 2) {
104  /* Trim the last component. */
105  if (p != resolved)
106  while (*--p != '/')
107  ;
108  path = q;
109  goto loop;
110  }
111  }
112 
113  /* Append this component. */
114  if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) {
115  if (p == resolved)
116  *p++ = '/';
117  *p = 0;
118  return (NULL);
119  }
120  p[0] = '/';
121  memcpy(&p[1], path,
122  /* LINTED We know q > path. */
123  q - path);
124  p[1 + q - path] = 0;
125 
126  /* Advance both resolved and unresolved path. */
127  p += 1 + q - path;
128  path = q;
129  goto loop;
130 }
131 
132 
133 /*
134  * Local Variables:
135  * tab-width: 8
136  * mode: C
137  * indent-tabs-mode: t
138  * c-file-style: "stroustrup"
139  * End:
140  * ex: shiftwidth=4 tabstop=8
141  */
const char * db_normalize(const char *path)
Definition: db_path.c:64
Header file for the BRL-CAD common definitions.
#define MAXPATHLEN
Definition: defines.h:113