source: rtems/cpukit/libcsupport/src/getcwd.c @ 1c6926c1

5
Last change on this file since 1c6926c1 was ceaa999, checked in by Mathew Kallada <matkallada@…>, on 12/08/12 at 20:43:29

libcsupport: Doxygen Enhancement Task #3

http://www.google-melange.com/gci/task/view/google/gci2012/7992210

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/**
2 * @file
3 *
4 * @brief Get Current Working Directory
5 * @ingroup libcsupport
6 */
7
8/*
9 * Copyright (c) 1989, 1991 The Regents of the University of California.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *      This product includes software developed by the University of
23 *      California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 *    may be used to endorse or promote products derived from this software
26 *    without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 *  Copied from newlib 1.8.1.  RTEMS can not build all of the contents
41 *  of the UNIX directory but we need this routine.
42 */
43
44#if HAVE_CONFIG_H
45#include "config.h"
46#endif
47
48#ifndef HAVE_GETCWD
49
50/*
51 *  Added these when moved to RTEMS
52 */
53
54#define _stat     stat
55#define _opendir  opendir
56#define _fstat    fstat
57#define _readdir  readdir
58#define _closedir closedir
59
60#if defined(LIBC_SCCS) && !defined(lint)
61static char sccsid[] = "@(#)getcwd.c    5.11 (Berkeley) 2/24/91";
62#endif /* LIBC_SCCS and not lint */
63
64#include <sys/stat.h>
65#include <errno.h>
66#include <dirent.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#include <unistd.h>
71#include <reent.h>
72#include <_syslist.h>
73
74#define bcopy(a,b,c)    memmove (b,a,c)
75
76#define ISDOT(dp) \
77        (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
78            (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
79
80#ifndef _REENT_ONLY
81
82char *
83getcwd (
84     char *pt,
85     size_t size)
86{
87  register struct dirent *dp;
88  register DIR *dir = 0;
89  register dev_t dev;
90  register ino_t ino;
91  register int first;
92  register char *bpt, *bup;
93  struct stat s;
94  dev_t root_dev;
95  ino_t root_ino;
96  size_t ptsize, upsize;
97  int save_errno;
98  char *ept, *eup, *up;
99
100  /*
101   * If no buffer specified by the user, allocate one as necessary.
102   * If a buffer is specified, the size has to be non-zero.  The path
103   * is built from the end of the buffer backwards.
104   */
105
106  if (pt)
107    {
108      ptsize = 0;
109      if (!size)
110        {
111          errno = EINVAL;
112          return (char *) NULL;
113        }
114      ept = pt + size;
115    }
116  else
117    {
118      if (!(pt = (char *) malloc (ptsize = 1024 - 4)))
119        {
120          return (char *) NULL;
121        }
122      ept = pt + ptsize;
123    }
124  bpt = ept - 1;
125  *bpt = '\0';
126
127  /*
128   * Allocate bytes (1024 - malloc space) for the string of "../"'s.
129   * Should always be enough (it's 340 levels).  If it's not, allocate
130   * as necessary.  Special * case the first stat, it's ".", not "..".
131   */
132
133  if (!(up = (char *) malloc (upsize = 1024 - 4)))
134    {
135      goto err;
136    }
137  eup = up + MAXPATHLEN;
138  bup = up;
139  up[0] = '.';
140  up[1] = '\0';
141
142  /* Save root values, so know when to stop. */
143  if (stat ("/", &s))
144    goto err;
145  root_dev = s.st_dev;
146  root_ino = s.st_ino;
147
148  errno = 0;                    /* XXX readdir has no error return. */
149
150  for (first = 1;; first = 0)
151    {
152      /* Stat the current level. */
153      if (_stat (up, &s))
154        goto err;
155
156      /* Save current node values. */
157      ino = s.st_ino;
158      dev = s.st_dev;
159
160      /* Check for reaching root. */
161      if (root_dev == dev && root_ino == ino)
162        {
163          *--bpt = '/';
164          /*
165           * It's unclear that it's a requirement to copy the
166           * path to the beginning of the buffer, but it's always
167           * been that way and stuff would probably break.
168           */
169          (void) bcopy (bpt, pt, ept - bpt);
170          free (up);
171          return pt;
172        }
173
174      /*
175       * Build pointer to the parent directory, allocating memory
176       * as necessary.  Max length is 3 for "../", the largest
177       * possible component name, plus a trailing NULL.
178       */
179
180      if (bup + 3 + MAXNAMLEN + 1 >= eup)
181        {
182          if (!(up = (char *) realloc (up, upsize *= 2)))
183            {
184              goto err;
185            }
186          bup = up;
187          eup = up + upsize;
188        }
189      *bup++ = '.';
190      *bup++ = '.';
191      *bup = '\0';
192
193      /* Open and stat parent directory. */
194      if (!(dir = _opendir (up)) || _fstat (__dirfd (dir), &s))
195        goto err;
196
197      /* Add trailing slash for next directory. */
198      *bup++ = '/';
199
200      /*
201       * If it's a mount point, have to stat each element because
202       * the inode number in the directory is for the entry in the
203       * parent directory, not the inode number of the mounted file.
204       */
205
206      save_errno = 0;
207      if (s.st_dev == dev)
208        {
209          for (;;)
210            {
211              if (!(dp = _readdir (dir)))
212                goto notfound;
213              if (dp->d_ino == ino)
214                break;
215            }
216        }
217      else
218        for (;;)
219          {
220            if (!(dp = _readdir (dir)))
221              goto notfound;
222            if (ISDOT (dp))
223              continue;
224            bcopy (dp->d_name, bup, strlen (dp->d_name) + 1);
225
226            /* Save the first error for later. */
227            if (stat (up, &s))
228              {
229                if (!save_errno)
230                  save_errno = errno;
231                errno = 0;
232                continue;
233              }
234            if (s.st_dev == dev && s.st_ino == ino)
235              break;
236          }
237
238      /*
239       * Check for length of the current name, preceding slash,
240       * leading slash.
241       */
242
243      if (bpt - pt <= strlen (dp->d_name) + (first ? 1 : 2))
244        {
245          size_t len, off;
246
247          if (!ptsize)
248            {
249              errno = ERANGE;
250              goto err;
251            }
252          off = bpt - pt;
253          len = ept - bpt;
254          if (!(pt = (char *) realloc (pt, ptsize *= 2)))
255            {
256              goto err;
257            }
258          bpt = pt + off;
259          ept = pt + ptsize;
260          (void) bcopy (bpt, ept - len, len);
261          bpt = ept - len;
262        }
263      if (!first)
264        *--bpt = '/';
265      bpt -= strlen (dp->d_name);
266      bcopy (dp->d_name, bpt, strlen (dp->d_name));
267      (void) _closedir (dir);
268      dir = 0;
269
270      /* Truncate any file name. */
271      *bup = '\0';
272    }
273
274notfound:
275  /*
276   * If readdir set errno, use it, not any saved error; otherwise,
277   * didn't find the current directory in its parent directory, set
278   * errno to ENOENT.
279   */
280
281  if (!errno)
282    errno = save_errno ? save_errno : ENOENT;
283  /* FALLTHROUGH */
284
285err:
286  if(dir)
287    (void) _closedir (dir);
288  if (ptsize)
289    free (pt);
290  free (up);
291  return (char *) NULL;
292}
293
294#endif /* _REENT_ONLY */
295
296#endif
Note: See TracBrowser for help on using the repository browser.