2003-11-12 00:52:45 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* path.c
|
|
|
|
* portable path handling routines
|
|
|
|
*
|
|
|
|
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2004-05-11 23:57:15 +02:00
|
|
|
* $PostgreSQL: pgsql/src/port/path.c,v 1.6 2004/05/11 21:57:15 momjian Exp $
|
2003-11-12 00:52:45 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2003-04-04 22:42:13 +02:00
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* is_absolute_path
|
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
bool
|
|
|
|
is_absolute_path(const char *filename)
|
2003-04-04 22:42:13 +02:00
|
|
|
{
|
|
|
|
return filename[0] == '/'
|
2003-08-04 02:43:34 +02:00
|
|
|
#ifdef WIN32 /* WIN32 paths can either have forward or
|
|
|
|
* backward slashes */
|
2003-04-04 22:42:13 +02:00
|
|
|
|| filename[0] == '\\'
|
|
|
|
|| (isalpha(filename[0]) && filename[1] == ':'
|
2003-08-04 02:43:34 +02:00
|
|
|
&& (filename[2] == '\\' || filename[2] == '/'))
|
2003-04-04 22:42:13 +02:00
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-04-04 22:42:13 +02:00
|
|
|
/*
|
|
|
|
* first_path_separator
|
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
char *
|
|
|
|
first_path_separator(const char *filename)
|
2003-04-04 22:42:13 +02:00
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
return strchr(filename, '/');
|
|
|
|
#else
|
2003-08-04 02:43:34 +02:00
|
|
|
char *slash,
|
|
|
|
*bslash;
|
2003-04-04 22:42:13 +02:00
|
|
|
|
|
|
|
/* How should we handle "C:file.c"? */
|
|
|
|
slash = strchr(filename, '/');
|
|
|
|
bslash = strchr(filename, '\\');
|
|
|
|
if (slash == NULL)
|
|
|
|
return bslash;
|
|
|
|
else if (bslash == NULL)
|
|
|
|
return slash;
|
|
|
|
else
|
|
|
|
return (slash < bslash) ? slash : bslash;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* last_path_separator
|
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
char *
|
|
|
|
last_path_separator(const char *filename)
|
2003-04-04 22:42:13 +02:00
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
return strrchr(filename, '/');
|
|
|
|
#else
|
2003-08-04 02:43:34 +02:00
|
|
|
char *slash,
|
|
|
|
*bslash;
|
2003-04-04 22:42:13 +02:00
|
|
|
|
|
|
|
/* How should we handle "C:file.c"? */
|
|
|
|
slash = strrchr(filename, '/');
|
|
|
|
bslash = strrchr(filename, '\\');
|
|
|
|
if (slash == NULL)
|
|
|
|
return bslash;
|
|
|
|
else if (bslash == NULL)
|
|
|
|
return slash;
|
|
|
|
else
|
|
|
|
return (slash > bslash) ? slash : bslash;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-09 05:49:02 +01:00
|
|
|
/*
|
|
|
|
* make all paths look like unix, with forward slashes
|
|
|
|
* also strip any trailing slash.
|
|
|
|
*
|
|
|
|
* The Windows command processor will accept suitably quoted paths
|
|
|
|
* with forward slashes, but barfs badly with mixed forward and back
|
|
|
|
* slashes. Removing the trailing slash on a path means we never get
|
|
|
|
* ugly double slashes. Don't remove a leading slash, though.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
canonicalize_path(char *path)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (p = path; *p; p++)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
if (*p == '\\')
|
|
|
|
*p = '/';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (p > path+1 && *--p == '/')
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 22:42:13 +02:00
|
|
|
/*
|
|
|
|
* Extracts the actual name of the program as called.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
get_progname(char *argv0)
|
|
|
|
{
|
|
|
|
if (!last_path_separator(argv0))
|
|
|
|
return argv0;
|
|
|
|
else
|
|
|
|
return last_path_separator(argv0) + 1;
|
|
|
|
}
|
2004-05-11 23:57:15 +02:00
|
|
|
|