summaryrefslogtreecommitdiff
path: root/src/port/path.c
blob: c093efa4bc4ab67eaca83e18ab16a7ddf8f768ce (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
/*-------------------------------------------------------------------------
 *
 * 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
 *	  $PostgreSQL: pgsql/src/port/path.c,v 1.4 2003/11/29 19:52:13 pgsql Exp $
 *
 *-------------------------------------------------------------------------
 */

#include "c.h"
#include <ctype.h>

/*
 *	is_absolute_path
 */
bool
is_absolute_path(const char *filename)
{
	return filename[0] == '/'
#ifdef WIN32					/* WIN32 paths can either have forward or
								 * backward slashes */
		|| filename[0] == '\\'
		|| (isalpha(filename[0]) && filename[1] == ':'
			&& (filename[2] == '\\' || filename[2] == '/'))
#endif
		;
}



/*
 *	first_path_separator
 */
char *
first_path_separator(const char *filename)
{
#ifndef WIN32
	return strchr(filename, '/');
#else
	char	   *slash,
			   *bslash;

	/* 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
 */
char *
last_path_separator(const char *filename)
{
#ifndef WIN32
	return strrchr(filename, '/');
#else
	char	   *slash,
			   *bslash;

	/* 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
}


/*
 * 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;
}