source: trunk/libs/newlib/src/libgloss/spu/getcwd.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 3.2 KB
Line 
1/*
2  Copyright 2007
3  International Business Machines Corporation,
4  Sony Computer Entertainment, Incorporated,
5  Toshiba Corporation,
6
7  All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions are met:
11
12    * Redistributions of source code must retain the above copyright notice,
13  this list of conditions and the following disclaimer.
14    * Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions and the following disclaimer in the
16  documentation and/or other materials provided with the distribution.
17    * Neither the names of the copyright holders nor the names of their
18  contributors may be used to endorse or promote products derived from
19  this software without specific prior written permission.
20
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*/
33
34#include <stdlib.h>
35#include <unistd.h>
36#include <errno.h>
37#include "jsre.h"
38
39#define ALLOC_BASE  64
40#define ALLOC_INCR  64
41
42typedef struct
43{
44  unsigned int buf;
45  unsigned int pad0[3];
46  unsigned int size;
47  unsigned int pad1[3];
48} syscall_getcwd_t;
49
50char *
51getcwd (char *buf, size_t size)
52{
53  syscall_getcwd_t sys;
54  int retry_alloc, local_alloc;
55  char *newbuf;
56  char *res;
57
58  /*
59   * Do not let the ppu side allocate memory, since it has to be used on
60   * the SPU (must be in LS), and it can't easily be freed on the spu
61   * side. So check for NULL buf, handle allocations here, and only call
62   * the assist call with a non-NULL buf.
63   */
64  retry_alloc = 0;
65  local_alloc = 0;
66  if (!buf) {
67    local_alloc = 1;
68    if (size == 0) {
69      retry_alloc = 1;
70      size = ALLOC_BASE;
71    }
72    buf = malloc (size);
73    if (!buf) {
74      /*
75       * Leave errno as set by malloc.
76       */
77      return NULL;
78    }
79  }
80
81  /*
82   * Let the assist call check for error cases, specifically let it handle
83   * non-NULL buf and size of zero.
84   */
85
86  sys.buf = (unsigned int) buf;
87  sys.size = (unsigned int) size;
88  res = (char*) __send_to_ppe (JSRE_POSIX1_SIGNALCODE, JSRE_GETCWD, &sys);
89
90  while (!res && retry_alloc && errno == ERANGE) {
91    size += ALLOC_INCR;
92    newbuf = realloc (buf, size);
93    if (!newbuf) {
94      free (buf);
95      return NULL;
96    }
97    buf = newbuf;
98
99    sys.buf = (unsigned int) buf;
100    sys.size = (unsigned int) size;
101    res = (char*) __send_to_ppe (JSRE_POSIX1_SIGNALCODE, JSRE_GETCWD, &sys);
102  }
103
104  if (!res && local_alloc) {
105    free (buf);
106  }
107
108  return res;
109}
Note: See TracBrowser for help on using the repository browser.