OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems-20020807/] [cpukit/] [libcsupport/] [src/] [getcwd.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.