aboutsummaryrefslogtreecommitdiff
path: root/shared/common/os.c
blob: 9129f82851351deae2510c4c10f75124270359e1 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Copyright (c) 2004-2005 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#ifdef MSW
#include <io.h>
#else
#include <unistd.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "m_pd.h"
#include "os.h"

static int ospath_doabsolute(char *path, char *cwd, char *result)
{
    if (*path == 0)
    {
	if (result)
	    strcpy(result, cwd);
	else
	    return (strlen(cwd));
    }
    else if (*path == '~')
    {
	path++;
	if (*path == '/' || *path == 0)
	{
#ifdef UNIX
	    char *home = getenv("HOME");
	    if (home)
	    {
		if (result)
		{
		    strcpy(result, home);
		    if (*path)
			strcat(result, path);
		}
		else return (strlen(home) + strlen(path));
	    }
	    else goto badpath;
#else
	    goto badpath;
#endif
	}
	else goto badpath;
    }
    else if (*path == '/')
    {
#ifdef MSW
	/* path is absolute, drive is implicit, LATER UNC? */
	if (*cwd && cwd[1] == ':')
	{
	    if (result)
	    {
		*result = *cwd;
		result[1] = ':';
		strcpy(result + 2, path);
	    }
	    else return (2 + strlen(path));
	}
	else goto badpath;
#else
	/* path is absolute */
	if (result)
	    strcpy(result, path);
	else
	    return (strlen(path));
#endif
    }
    else
    {
#ifdef MSW
	if (path[1] == ':')
	{
	    if (path[2] == '/')
	    {
		/* path is absolute */
		if (result)
		    strcpy(result, path);
		else
		    return (strlen(path));
	    }
	    else if (*cwd == *path)
	    {
		/* path is relative, drive is explicitly current */
		if (result)
		{
		    int ndx = strlen(cwd);
		    strcpy(result, cwd);
		    result[ndx++] = '/';
		    strcpy(result + ndx, path + 2);
		}
		else return (strlen(cwd) + strlen(path) - 1);
	    }
	    /* we do not maintain per-drive cwd, LATER rethink */
	    else goto badpath;
	}
	/* LATER devices? */
	else
	{
	    /* path is relative */
	    if (result)
	    {
		int ndx = strlen(cwd);
		strcpy(result, cwd);
		result[ndx++] = '/';
		strcpy(result + ndx, path);
	    }
	    else return (strlen(cwd) + 1 + strlen(path));
	}
#else
	/* path is relative */
	if (result)
	{
	    int ndx = strlen(cwd);
	    strcpy(result, cwd);
	    result[ndx++] = '/';
	    strcpy(result + ndx, path);
	}
	else return (strlen(cwd) + 1 + strlen(path));
#endif
    }
    if (result && *result && *result != '.')
    {
	/* clean-up */
	char *inptr, *outptr = result;
	int ndx = strlen(result);
	if (result[ndx - 1] == '.')
	{
	    result[ndx] = '/';  /* guarding slash */
	    result[ndx + 1] = 0;
	}
	for (inptr = result + 1; *inptr; inptr++)
	{
	    if (*inptr == '/')
	    {
		if (*outptr == '/')
		    continue;
		else if (*outptr == '.')
		{
		    if (outptr[-1] == '/')
		    {
			outptr--;
			continue;
		    }
		    else if (outptr[-1] == '.' && outptr[-2] == '/')
		    {
			outptr -= 2;
			if (outptr == result)
			    continue;
			else for (outptr--; outptr != result; outptr--)
			    if (*outptr == '/')
				break;
			continue;
		    }
		}
	    }
	    *++outptr = *inptr;
	}
	if (*outptr == '/' && outptr != result)
	    *outptr = 0;
	else
	    outptr[1] = 0;
    }
    else bug("ospath_doabsolute 1");
    return (0);
badpath:
    if (result)
	bug("ospath_doabsolute 2");
    return (0);
}

/* Returns an estimated length of an absolute path made up from the first arg.
   The actual ospath_absolute()'s length may be shorter (since it erases
   superfluous slashes and dots), but not longer.  Both args should be unbashed
   (system-independent), cwd should be absolute.  Returns 0 in case of any
   error (LATER revisit). */
int ospath_length(char *path, char *cwd)
{
    /* one extra byte used internally (guarding slash) */
    return (ospath_doabsolute(path, cwd, 0) + 1);
}

/* Copies an absolute path to result.  Arguments: path and cwd, are the same
   as in ospath_length().  Caller should first consult ospath_length(), and
   allocate at least ospath_length() + 1 bytes to the result buffer.
   Should never fail (failure is a bug). */
char *ospath_absolute(char *path, char *cwd, char *result)
{
    ospath_doabsolute(path, cwd, result);
    return (result);
}

FILE *fileread_open(char *filename, t_canvas *cv, int textmode)
{
    int fd;
    char path[MAXPDSTRING+2], *nameptr;
    t_symbol *dirsym = (cv ? canvas_getdir(cv) : 0);
    /* path arg is returned unbashed (system-independent) */
    if ((fd = open_via_path((dirsym ? dirsym->s_name : ""), filename,
			    "", path, &nameptr, MAXPDSTRING, 1)) < 0)
    	return (0);
    /* Closing/reopening dance.  This is unnecessary under linux, and we
       could have tried to convert fd to fp, but under windows open_via_path()
       returns what seems to be an invalid fd.
       LATER try to understand what is going on here... */
    close(fd);
    if (path != nameptr)
    {
	char *slashpos = path + strlen(path);
	*slashpos++ = '/';
	/* try not to be dependent on current open_via_path() implementation */
	if (nameptr != slashpos)
	    strcpy(slashpos, nameptr);
    }
    sys_bashfilename(path, path);
    return (fopen(path, (textmode ? "r" : "rb")));
}

FILE *filewrite_open(char *filename, t_canvas *cv, int textmode)
{
    char path[MAXPDSTRING+2];
    if (cv)
	/* path arg is returned unbashed (system-independent) */
	canvas_makefilename(cv, filename, path, MAXPDSTRING);
    else
    {
    	strncpy(path, filename, MAXPDSTRING);
    	path[MAXPDSTRING-1] = 0;
    }
    sys_bashfilename(path, path);
    return (fopen(path, (textmode ? "w" : "wb")));
}