2 * This Cplant(TM) source code is the property of Sandia National
5 * This Cplant(TM) source code is copyrighted by Sandia National
8 * The redistribution of this Cplant(TM) source code is subject to the
9 * terms of the GNU Lesser General Public License
10 * (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
12 * Cplant(TM) Copyright 1998-2003 Sandia Corporation.
13 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14 * license for use of this work by or on behalf of the US Government.
15 * Export of this program may require a license from the United States
20 * #############################################################################
22 * # This Cplant(TM) source code is the property of Sandia National
25 * # This Cplant(TM) source code is copyrighted by Sandia National
28 * # The redistribution of this Cplant(TM) source code is subject to the
29 * # terms of the GNU Lesser General Public License
30 * # (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
32 * # Cplant(TM) Copyright 1998-2003 Sandia Corporation.
33 * # Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
34 * # license for use of this work by or on behalf of the US Government.
35 * # Export of this program may require a license from the United States
38 * #############################################################################
42 * This library is free software; you can redistribute it and/or
43 * modify it under the terms of the GNU Lesser General Public
44 * License as published by the Free Software Foundation; either
45 * version 2.1 of the License, or (at your option) any later version.
47 * This library is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
50 * Lesser General Public License for more details.
52 * You should have received a copy of the GNU Lesser General Public
53 * License along with this library; if not, write to the Free Software
54 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
56 * Questions or comments about this library should be sent to:
59 * Sandia National Laboratories, New Mexico
61 * Albuquerque, NM 87185-1110
67 #include <sys/types.h>
74 #include <sys/queue.h>
80 #include "sysio-symbols.h"
82 struct pnode *_sysio_cwd = NULL;
85 * Change to directory specified by the given pnode.
88 _sysio_p_chdir(struct pnode *pno)
93 * Revalidate the pnode, and ensure it's a directory
95 err = _sysio_p_validate(pno, NULL, NULL);
99 if (!(pno->p_base->pb_ino &&
100 S_ISDIR(pno->p_base->pb_ino->i_mode)))
103 * Release old if set.
109 * Finally, change to the new.
117 SYSIO_INTERFACE_NAME(chdir)(const char *path)
121 SYSIO_INTERFACE_DISPLAY_BLOCK;
123 SYSIO_INTERFACE_ENTER;
124 err = _sysio_namei(_sysio_cwd, path, 0, NULL, &pno);
126 SYSIO_INTERFACE_RETURN(-1, err);
128 err = _sysio_p_chdir(pno);
131 SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
136 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(chdir),
137 PREPEND(__, SYSIO_INTERFACE_NAME(chdir)))
141 * Return path tracked by the path ancestor chain.
143 * If the buf pointer is NULL, a buffer large enough to hold the path
144 * is allocated from the heap.
148 _sysio_p_path(struct pnode *pno, char **buf, size_t size)
158 * Walk up the tree to the root, summing the component name
159 * lengths and counting the vertices.
165 * If this is a covering path-node then the name should be
166 * the *covered* nodes name, not this one unless we are at
167 * the root of the name-space.
169 while (pno == pno->p_mount->mnt_root && pno != pno->p_parent )
170 pno = pno->p_mount->mnt_covers;
173 * Add length of this component to running sum and
174 * account for this vertex.
176 assert((len >= pno->p_base->pb_name.len &&
177 (size_t )~0 - pno->p_base->pb_name.len > len) ||
178 (size_t )~0 - len > pno->p_base->pb_name.len);
179 len += pno->p_base->pb_name.len;
183 } while (pno != pno->p_parent);
187 if (len >= size || n >= size - len)
191 * Allocate path buffer from the heap.
193 *buf = malloc(size * sizeof(char));
199 * Fill in the path buffer.
203 *cp = '\0'; /* NUL terminate */
206 * If this is a covering path-node then the name should be
207 * the *covered* nodes name, not this one unless we are at
208 * the root of the name-space.
210 while (pno == pno->p_mount->mnt_root && pno != pno->p_parent )
211 pno = pno->p_mount->mnt_covers;
214 * Add component and separator.
216 cp -= pno->p_base->pb_name.len;
217 (void )memcpy(cp, pno->p_base->pb_name.name,
218 pno->p_base->pb_name.len);
220 *--cp = PATH_SEPARATOR;
222 } while (pno != pno->p_parent);
228 SYSIO_INTERFACE_NAME(getcwd)(char *buf, size_t size)
231 SYSIO_INTERFACE_DISPLAY_BLOCK;
233 SYSIO_INTERFACE_ENTER;
234 err = _sysio_p_path(_sysio_cwd, &buf, buf ? size : 0);
235 SYSIO_INTERFACE_RETURN(err ? NULL : buf, err);
240 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(getcwd),
241 PREPEND(__, SYSIO_INTERFACE_NAME(getcwd)))
244 #if defined(PATH_MAX) && !(defined(REDSTORM))
246 SYSIO_INTERFACE_NAME(getwd)(char *buf)
254 return SYSIO_INTERFACE_NAME(getcwd)(buf, PATH_MAX);