1717 *
1818 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
1919 *
20- * $PostgreSQL: pgsql/src/bin/pg_config/pg_config.c,v 1.13 2005/09/27 17:39:33 tgl Exp $
20+ * $PostgreSQL: pgsql/src/bin/pg_config/pg_config.c,v 1.14 2005/10/05 12:16:28 momjian Exp $
2121 *
2222 *-------------------------------------------------------------------------
2323 */
@@ -30,6 +30,64 @@ static const char *progname;
3030static char mypath [MAXPGPATH ];
3131
3232
33+ /*
34+ * This function cleans up the paths for use with either cmd.exe or Msys
35+ * on Windows. We need them to use double backslashes and filenames without
36+ * spaces (for which a short filename is the safest equivalent) eg:
37+ * C:\\Progra~1\\
38+ *
39+ * This can fail in 2 ways - if the path doesn't exist, or short names are
40+ * disabled. In the first case, don't return any path. In the second case,
41+ * we leave the path in the long form. In this case, it does still seem to
42+ * fix elements containing spaces which is all we actually need.
43+ */
44+ static void
45+ cleanup_path (char * path )
46+ {
47+ #ifdef WIN32
48+ int x = 0 ,y = 0 ;
49+ char temp [MAXPGPATH ];
50+
51+ if (GetShortPathName (path ,path ,MAXPGPATH - 1 )== 0 )
52+ {
53+ /* Ignore ERROR_INVALID_PARAMETER as it almost certainly
54+ * means that short names are disabled
55+ */
56+ if (GetLastError ()!= ERROR_INVALID_PARAMETER )
57+ {
58+ path [0 ]= '\0' ;
59+ return ;
60+ }
61+ }
62+
63+
64+ /* Replace '\' with '\\'. */
65+ for (x = 0 ;x < strlen (path );x ++ )
66+ {
67+ if (path [x ]== '/' || path [x ]== '\\' )
68+ {
69+ temp [y ]= '\\' ;
70+ y ++ ;
71+ temp [y ]= '\\' ;
72+ }
73+ else
74+ {
75+ temp [y ]= path [x ];
76+ }
77+
78+ y ++ ;
79+
80+ /* Bail out if we're too close to MAXPGPATH */
81+ if (y >=MAXPGPATH - 2 )
82+ break ;
83+ }
84+ temp [y ]= '\0' ;
85+
86+ strncpy (path ,temp ,MAXPGPATH - 1 );
87+ #endif
88+ }
89+
90+
3391/*
3492 * For each piece of information known to pg_config, we define a subroutine
3593 * to print it. This is probably overkill, but it avoids code duplication
@@ -39,138 +97,152 @@ static charmypath[MAXPGPATH];
3997static void
4098show_bindir (bool all )
4199{
42- char path [MAXPGPATH ];
43- char * lastsep ;
100+ char path [MAXPGPATH ];
101+ char * lastsep ;
44102
45103if (all )
46104printf ("BINDIR = " );
47105/* assume we are located in the bindir */
48106strcpy (path ,mypath );
49107lastsep = strrchr (path ,'/' );
108+
50109if (lastsep )
51110* lastsep = '\0' ;
111+
112+ cleanup_path (path );
52113printf ("%s\n" ,path );
53114}
54115
55116static void
56117show_docdir (bool all )
57118{
58- char path [MAXPGPATH ];
119+ char path [MAXPGPATH ];
59120
60121if (all )
61122printf ("DOCDIR = " );
62123get_doc_path (mypath ,path );
124+ cleanup_path (path );
63125printf ("%s\n" ,path );
64126}
65127
66128static void
67129show_includedir (bool all )
68130{
69- char path [MAXPGPATH ];
131+ char path [MAXPGPATH ];
70132
71133if (all )
72134printf ("INCLUDEDIR = " );
73135get_include_path (mypath ,path );
136+ cleanup_path (path );
74137printf ("%s\n" ,path );
75138}
76139
77140static void
78141show_pkgincludedir (bool all )
79142{
80- char path [MAXPGPATH ];
143+ char path [MAXPGPATH ];
81144
82145if (all )
83146printf ("PKGINCLUDEDIR = " );
84147get_pkginclude_path (mypath ,path );
148+ cleanup_path (path );
85149printf ("%s\n" ,path );
86150}
87151
88152static void
89153show_includedir_server (bool all )
90154{
91- char path [MAXPGPATH ];
155+ char path [MAXPGPATH ];
92156
93157if (all )
94158printf ("INCLUDEDIR-SERVER = " );
95159get_includeserver_path (mypath ,path );
160+ cleanup_path (path );
96161printf ("%s\n" ,path );
97162}
98163
99164static void
100165show_libdir (bool all )
101166{
102- char path [MAXPGPATH ];
167+ char path [MAXPGPATH ];
103168
104169if (all )
105170printf ("LIBDIR = " );
106171get_lib_path (mypath ,path );
172+ cleanup_path (path );
107173printf ("%s\n" ,path );
108174}
109175
110176static void
111177show_pkglibdir (bool all )
112178{
113- char path [MAXPGPATH ];
179+ char path [MAXPGPATH ];
114180
115181if (all )
116182printf ("PKGLIBDIR = " );
117183get_pkglib_path (mypath ,path );
184+ cleanup_path (path );
118185printf ("%s\n" ,path );
119186}
120187
121188static void
122189show_localedir (bool all )
123190{
124- char path [MAXPGPATH ];
191+ char path [MAXPGPATH ];
125192
126193if (all )
127194printf ("LOCALEDIR = " );
128195get_locale_path (mypath ,path );
196+ cleanup_path (path );
129197printf ("%s\n" ,path );
130198}
131199
132200static void
133201show_mandir (bool all )
134202{
135- char path [MAXPGPATH ];
203+ char path [MAXPGPATH ];
136204
137205if (all )
138206printf ("MANDIR = " );
139207get_man_path (mypath ,path );
208+ cleanup_path (path );
140209printf ("%s\n" ,path );
141210}
142211
143212static void
144213show_sharedir (bool all )
145214{
146- char path [MAXPGPATH ];
215+ char path [MAXPGPATH ];
147216
148217if (all )
149218printf ("SHAREDIR = " );
150219get_share_path (mypath ,path );
220+ cleanup_path (path );
151221printf ("%s\n" ,path );
152222}
153223
154224static void
155225show_sysconfdir (bool all )
156226{
157- char path [MAXPGPATH ];
227+ char path [MAXPGPATH ];
158228
159229if (all )
160230printf ("SYSCONFDIR = " );
161231get_etc_path (mypath ,path );
232+ cleanup_path (path );
162233printf ("%s\n" ,path );
163234}
164235
165236static void
166237show_pgxs (bool all )
167238{
168- char path [MAXPGPATH ];
239+ char path [MAXPGPATH ];
169240
170241if (all )
171242printf ("PGXS = " );
172243get_pkglib_path (mypath ,path );
173244strncat (path ,"/pgxs/src/makefiles/pgxs.mk" ,MAXPGPATH - 1 );
245+ cleanup_path (path );
174246printf ("%s\n" ,path );
175247}
176248