3
* Copyright (c) 2004-2007 Kevin Worcester
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20
* See the COPYING file for full license text.
32
#include "log.h" // FIXME debugging
34
#ifdef HAVE_GETTIMEOFDAY
37
#include <sys/timeb.h>
38
#endif // HAVE_GETTIMEOFDAY
42
char * PORT_getenv( const char * name )
45
return "C:/"; // TODO do registry lookup (if it's ever needed)
47
return getenv( name );
51
int PORT_lstat( const char * filename, struct stat * buf )
54
return stat( filename, buf );
56
return lstat( filename, buf );
60
// FIXME add thorough testing for the manual part of this (the case where
61
// realpath fails because the directories don't exist).
62
char * PORT_realpath( const char * path, char * resolved_path, size_t len )
65
char * rval = _fullpath( resolved_path, path, len );
67
char * rval = realpath( path, resolved_path );
75
strncpy( resolved_path, path, len );
80
getcwd(pwd, sizeof(pwd));
81
PORT_snprintf( resolved_path, len, "%s/%s", pwd, path );
83
resolved_path[ len - 1 ] = '\0';
86
// Remove "/./" and "//"
87
char * end = rval + strlen(rval);
90
while ( (s = strstr(s, "/./")) != NULL )
92
memmove( s, s + 2, end - (s + 2) );
98
while ( (s = strstr(s, "//")) != NULL )
100
memmove( s, s + 1, end - (s + 1) );
107
while ( (s = strstr(s, "/../")) != NULL )
109
char * lastSlash = s - 1;
110
while ( lastSlash >= rval )
112
if ( *lastSlash != '/' )
118
if ( lastSlash >= rval )
120
int len = (end - s) - 3;
121
memmove( lastSlash, s+3, len );
122
end = lastSlash + len;
132
//log_warning( "normalized path = '%s'\n", rval );
138
struct tm * PORT_localtime_r( const time_t * timep, struct tm * result )
141
*result = *localtime( timep );
143
localtime_r( timep, result );
148
void PORT_gettimeofday( PORT_timeval * tv )
150
#ifdef HAVE_GETTIMEOFDAY
152
gettimeofday( &tval, NULL );
153
tv->tv_sec = tval.tv_sec;
154
tv->tv_msec = tval.tv_usec / 1000;
158
tv->tv_sec = tb.time;
159
tv->tv_msec = tb.millitm;
160
#endif // HAVE_GETTIMEOFDAY
163
char * PORT_asctime_r( const struct tm * tmval, char * buf )
166
char *tmptmstr = asctime( tmval );
167
strcpy( buf, tmptmstr );
169
asctime_r( tmval, buf );
174
int PORT_symlink( const char * oldpath, const char * newpath )
179
return symlink( oldpath, newpath );
183
int PORT_mkdir( const char * pathname, mode_t mode )
186
return mkdir( pathname );
188
return mkdir( pathname, mode );
192
int PORT_snprintf( char * dest, size_t len, const char * fmt, ... )
195
if ( dest && fmt && len > 0 )
198
va_start( args, fmt );
199
rval = PORT_vsnprintf( dest, len, fmt, args );
204
int PORT_vsnprintf( char * dest, size_t len, const char * fmt, va_list args )
207
if ( dest && fmt && len > 0 )
209
rval = vsnprintf( dest, len, fmt, args );
210
dest[ len - 1 ] = '\0';
216
char * PORT_strcasestr( const char * haystack, const char * needle )
220
size_t hlen = strlen( haystack );
221
size_t nlen = strlen( needle );
228
for ( i = 0; !match && i < hlen - nlen; i++ )
231
for ( j = 0; match && j < nlen; j++ )
233
if ( toupper(haystack[i+j]) != toupper(needle[j]) )
242
return (char *) &haystack[i];
250
char * PORT_strcasestr( const char * haystack, const char * needle )
252
return strcasestr( haystack, needle );
256
char * PORT_basename( const char * path )
258
static char rval[ PATH_MAX ] = "";
261
char * start = strrchr( path, '/' );
265
// no forward slash, try backslash
266
start = strrchr( path, '\\' );
272
strncpy( rval, start, PATH_MAX );
273
rval[ PATH_MAX - 1 ] = '\0';
277
// no directory, just filename
278
strncpy( rval, path, PATH_MAX );
279
rval[ PATH_MAX - 1 ] = '\0';
288
char * PORT_dirname( const char * path )
290
static char rval[ PATH_MAX ] = "";
293
strncpy( rval, path, PATH_MAX );
294
rval[ PATH_MAX - 1 ] = '\0';
296
char * end = strrchr( rval, '/' );
300
// no forward slash, try backslash
301
end = strrchr( rval, '\\' );
314
// path not set, or no slash character