aboutsummaryrefslogtreecommitdiff
path: root/devel/psptoolchain-newlib/files/patch-newlib-libc-sys-psp-pspcwd.c
blob: 7117218833cba20decf294b2f47f1a1c33949234 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
--- ./newlib/libc/sys/psp/pspcwd.c.orig	2012-01-25 19:33:12.000000000 +0000
+++ ./newlib/libc/sys/psp/pspcwd.c	2012-01-25 19:33:12.000000000 +0000
@@ -0,0 +1,166 @@
+/*
+ * PSP Software Development Kit - http://www.pspdev.org
+ * -----------------------------------------------------------------------
+ * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
+ *
+ * pspcwd.c - Current working directory emulation helper functions
+ *
+ * Copyright (c) 2005 Marcus R. Brown <mrbrown@ocgnet.org>
+ * Copyright (c) 2005 James Forshaw <tyranid@gmail.com>
+ * Copyright (c) 2005 John Kelley <ps2dev@kelley.ca>
+ * Copyright (c) 2005 Jim Paris <jim@jtan.com>
+ *
+ */
+#include <string.h>
+#include <sys/types.h>
+#include <sys/unistd.h>
+
+#include <psptypes.h>
+#include <pspiofilemgr.h>
+
+char __psp_cwd[MAXPATHLEN + 1] = { 0 };
+
+/* Set the current working directory (CWD) to the path where the module was launched. */
+void __psp_init_cwd(char *argv_0)
+{
+	if (argv_0 != NULL) {
+		char base_path[MAXPATHLEN + 1];
+		char *end;
+
+		strncpy(base_path, argv_0, sizeof(base_path) - 1);
+		base_path[sizeof(base_path) - 1] = '\0';
+		end = strrchr(base_path, '/');
+		if (end != NULL) {
+			*(end + 1) = '\0';
+			chdir(base_path);
+		}
+	}
+}
+
+/* Return the number of bytes taken up by the "drive:" prefix,
+   or -1 if it's not found */
+int __psp_get_drive(const char *d)
+{
+	int i;
+	for(i=0; d[i]; i++) {
+		if(! ((d[i] >= 'a' && d[i] <= 'z') ||
+		      (d[i] >= '0' && d[i] <= '9') ))
+			break;
+	}
+	if(d[i] == ':') return i+1;
+	return -1;
+}
+
+/* Like strcpy, but returns 0 if the string doesn't fit */
+int __psp_safe_strcpy(char *out, const char *in, int maxlen)
+{
+	for( ; maxlen > 0 && *in ; maxlen-- )
+		*(out++) = *(in++);
+	if(maxlen < 1) return 0;
+	*out = 0;
+	return 1;
+}
+
+/* Like strcat, but returns 0 if the string doesn't fit */
+int __psp_safe_strcat(char *out, const char *in, int maxlen)
+{
+	for( ; *out ; out++,maxlen-- )
+		continue;
+	return __psp_safe_strcpy(out, in, maxlen);
+}
+
+/* Normalize a pathname (without leading "drive:") by removing
+   . and .. components, duplicated /, etc. */
+int __psp_path_normalize(char *out, int len)
+{
+	int i, j;
+	int first, next;
+
+	/* First append "/" to make the rest easier */
+	if(!__psp_safe_strcat(out,"/",len)) return -10;
+
+	/* Convert "//" to "/" */
+	for(i=0; out[i+1]; i++) {
+		if(out[i]=='/' && out[i+1]=='/') {
+			for(j=i+1; out[j]; j++)
+				out[j] = out[j+1];
+			i--;
+		}
+	}
+
+	/* Convert "/./" to "/" */
+	for(i=0; out[i] && out[i+1] && out[i+2]; i++) {
+		if(out[i]=='/' && out[i+1]=='.' && out[i+2]=='/') {
+			for(j=i+1; out[j]; j++)
+				out[j] = out[j+2];
+			i--;
+		}
+	}
+
+	/* Convert "/asdf/../" to "/" until we can't anymore.  Also
+	 * convert leading "/../" to "/" */
+	first = next = 0;
+	while(1) {
+		/* If a "../" follows, remove it and the parent */
+		if(out[next+1] && out[next+1]=='.' && 
+		   out[next+2] && out[next+2]=='.' &&
+		   out[next+3] && out[next+3]=='/') {
+			for(j=0; out[first+j+1]; j++)
+				out[first+j+1] = out[next+j+4];
+			first = next = 0;
+			continue;
+		}
+
+		/* Find next slash */
+		first = next;
+		for(next=first+1; out[next] && out[next] != '/'; next++)
+			continue;
+		if(!out[next]) break;
+	}
+
+	/* Remove trailing "/" */
+	for(i=1; out[i]; i++)
+		continue;
+	if(i >= 1 && out[i-1] == '/') 
+		out[i-1] = 0;
+
+	return 0;
+}
+
+/* Convert relative path to absolute path. */
+int __psp_path_absolute(const char *in, char *out, int len)
+{
+	int dr;
+
+	/* See what the relative URL starts with */
+	dr = __psp_get_drive(in);
+	if(dr > 0 && in[dr] == '/') {
+		/* It starts with "drive:/", so it's already absolute */
+		if(!__psp_safe_strcpy(out, in, len))
+			return -1;
+	} else if(in[0] == '/') {
+		/* It's absolute, but missing the drive, so use cwd's drive */
+		if(strlen(__psp_cwd) >= len)
+			return -2;
+		strcpy(out, __psp_cwd);
+		dr = __psp_get_drive(out);
+		out[dr] = 0;
+		if(!__psp_safe_strcat(out, in, len))
+			return -3;
+	} else {
+		/* It's not absolute, so append it to the current cwd */
+		if(strlen(__psp_cwd) >= len)
+			return -4;
+		strcpy(out, __psp_cwd);
+		if(!__psp_safe_strcat(out, "/", len)) 
+			return -6;
+		if(!__psp_safe_strcat(out, in, len)) 
+			return -7;
+	}
+
+	/* Now normalize the pathname portion */
+	dr = __psp_get_drive(out);
+	if(dr < 0) dr = 0;
+	return __psp_path_normalize(out + dr, len - dr);
+}
+