]> granicus.if.org Git - postgresql/blob
438b529604
[postgresql] /
1 /*-------------------------------------------------------------------------
2  *
3  * path.c
4  *        portable path handling routines
5  *
6  * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/port/path.c
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #ifndef FRONTEND
17 #include "postgres.h"
18 #else
19 #include "postgres_fe.h"
20 #endif
21
22 #include <ctype.h>
23 #include <sys/stat.h>
24 #ifdef WIN32
25 #ifdef _WIN32_IE
26 #undef _WIN32_IE
27 #endif
28 #define _WIN32_IE 0x0500
29 #ifdef near
30 #undef near
31 #endif
32 #define near
33 #include <shlobj.h>
34 #else
35 #include <unistd.h>
36 #endif
37
38 #include "pg_config_paths.h"
39
40
41 #ifndef WIN32
42 #define IS_PATH_VAR_SEP(ch) ((ch) == ':')
43 #else
44 #define IS_PATH_VAR_SEP(ch) ((ch) == ';')
45 #endif
46
47 static void make_relative_path(char *ret_path, const char *target_path,
48                                    const char *bin_path, const char *my_exec_path);
49 static void trim_directory(char *path);
50 static void trim_trailing_separator(char *path);
51
52
53 /*
54  * skip_drive
55  *
56  * On Windows, a path may begin with "C:" or "//network/".      Advance over
57  * this and point to the effective start of the path.
58  */
59 #ifdef WIN32
60
61 static char *
62 skip_drive(const char *path)
63 {
64         if (IS_DIR_SEP(path[0]) && IS_DIR_SEP(path[1]))
65         {
66                 path += 2;
67                 while (*path && !IS_DIR_SEP(*path))
68                         path++;
69         }
70         else if (isalpha((unsigned char) path[0]) && path[1] == ':')
71         {
72                 path += 2;
73         }
74         return (char *) path;
75 }
76 #else
77
78 #define skip_drive(path)        (path)
79 #endif
80
81 /*
82  *      has_drive_prefix
83  *
84  * Return true if the given pathname has a drive prefix.
85  */
86 bool
87 has_drive_prefix(const char *path)
88 {
89         return skip_drive(path) != path;
90 }
91
92 /*
93  *      first_dir_separator
94  *
95  * Find the location of the first directory separator, return
96  * NULL if not found.
97  */
98 char *
99 first_dir_separator(const char *filename)
100 {
101         const char *p;
102
103         for (p = skip_drive(filename); *p; p++)
104                 if (IS_DIR_SEP(*p))
105                         return (char *) p;
106         return NULL;
107 }
108
109 /*
110  *      first_path_var_separator
111  *
112  * Find the location of the first path separator (i.e. ':' on
113  * Unix, ';' on Windows), return NULL if not found.
114  */
115 char *
116 first_path_var_separator(const char *pathlist)
117 {
118         const char *p;
119
120         /* skip_drive is not needed */
121         for (p = pathlist; *p; p++)
122                 if (IS_PATH_VAR_SEP(*p))
123                         return (char *) p;
124         return NULL;
125 }
126
127 /*
128  *      last_dir_separator
129  *
130  * Find the location of the last directory separator, return
131  * NULL if not found.
132  */
133 char *
134 last_dir_separator(const char *filename)
135 {
136         const char *p,
137                            *ret = NULL;
138
139         for (p = skip_drive(filename); *p; p++)
140                 if (IS_DIR_SEP(*p))
141                         ret = p;
142         return (char *) ret;
143 }
144
145
146 /*
147  *      make_native_path - on WIN32, change / to \ in the path
148  *
149  *      This effectively undoes canonicalize_path.
150  *
151  *      This is required because WIN32 COPY is an internal CMD.EXE
152  *      command and doesn't process forward slashes in the same way
153  *      as external commands.  Quoting the first argument to COPY
154  *      does not convert forward to backward slashes, but COPY does
155  *      properly process quoted forward slashes in the second argument.
156  *
157  *      COPY works with quoted forward slashes in the first argument
158  *      only if the current directory is the same as the directory
159  *      of the first argument.
160  */
161 void
162 make_native_path(char *filename)
163 {
164 #ifdef WIN32
165         char       *p;
166
167         for (p = filename; *p; p++)
168                 if (*p == '/')
169                         *p = '\\';
170 #endif
171 }
172
173
174 /*
175  * join_path_components - join two path components, inserting a slash
176  *
177  * We omit the slash if either given component is empty.
178  *
179  * ret_path is the output area (must be of size MAXPGPATH)
180  *
181  * ret_path can be the same as head, but not the same as tail.
182  */
183 void
184 join_path_components(char *ret_path,
185                                          const char *head, const char *tail)
186 {
187         if (ret_path != head)
188                 strlcpy(ret_path, head, MAXPGPATH);
189
190         /*
191          * Remove any leading "." in the tail component.
192          *
193          * Note: we used to try to remove ".." as well, but that's tricky to get
194          * right; now we just leave it to be done by canonicalize_path() later.
195          */
196         while (tail[0] == '.' && IS_DIR_SEP(tail[1]))
197                 tail += 2;
198
199         if (*tail)
200         {
201                 /* only separate with slash if head wasn't empty */
202                 snprintf(ret_path + strlen(ret_path), MAXPGPATH - strlen(ret_path),
203                                  "%s%s",
204                                  (*(skip_drive(head)) != '\0') ? "/" : "",
205                                  tail);
206         }
207 }
208
209
210 /*
211  *      Clean up path by:
212  *              o  make Win32 path use Unix slashes
213  *              o  remove trailing quote on Win32
214  *              o  remove trailing slash
215  *              o  remove duplicate adjacent separators
216  *              o  remove trailing '.'
217  *              o  process trailing '..' ourselves
218  */
219 void
220 canonicalize_path(char *path)
221 {
222         char       *p,
223                            *to_p;
224         char       *spath;
225         bool            was_sep = false;
226         int                     pending_strips;
227
228 #ifdef WIN32
229         /*
230          * The Windows command processor will accept suitably quoted paths with
231          * forward slashes, but barfs badly with mixed forward and back slashes.
232          */
233         for (p = path; *p; p++)
234         {
235                 if (*p == '\\')
236                         *p = '/';
237         }
238
239         /*
240          * In Win32, if you do: prog.exe "a b" "\c\d\" the system will pass \c\d"
241          * as argv[2], so trim off trailing quote.
242          */
243         if (p > path && *(p - 1) == '"')
244                 *(p - 1) = '/';
245 #endif
246
247         /*
248          * Removing the trailing slash on a path means we never get ugly double
249          * trailing slashes. Also, Win32 can't stat() a directory with a trailing
250          * slash. Don't remove a leading slash, though.
251          */
252         trim_trailing_separator(path);
253
254         /*
255          * Remove duplicate adjacent separators
256          */
257         p = path;
258 #ifdef WIN32
259         /* Don't remove leading double-slash on Win32 */
260         if (*p)
261                 p++;
262 #endif
263         to_p = p;
264         for (; *p; p++, to_p++)
265         {
266                 /* Handle many adjacent slashes, like "/a///b" */
267                 while (*p == '/' && was_sep)
268                         p++;
269                 if (to_p != p)
270                         *to_p = *p;
271                 was_sep = (*p == '/');
272         }
273         *to_p = '\0';
274
275         /*
276          * Remove any trailing uses of "." and process ".." ourselves
277          *
278          * Note that "/../.." should reduce to just "/", while "../.." has to be
279          * kept as-is.  In the latter case we put back mistakenly trimmed ".."
280          * components below.  Also note that we want a Windows drive spec to be
281          * visible to trim_directory(), but it's not part of the logic that's
282          * looking at the name components; hence distinction between path and
283          * spath.
284          */
285         spath = skip_drive(path);
286         pending_strips = 0;
287         for (;;)
288         {
289                 int                     len = strlen(spath);
290
291                 if (len >= 2 && strcmp(spath + len - 2, "/.") == 0)
292                         trim_directory(path);
293                 else if (strcmp(spath, ".") == 0)
294                 {
295                         /* Want to leave "." alone, but "./.." has to become ".." */
296                         if (pending_strips > 0)
297                                 *spath = '\0';
298                         break;
299                 }
300                 else if ((len >= 3 && strcmp(spath + len - 3, "/..") == 0) ||
301                                  strcmp(spath, "..") == 0)
302                 {
303                         trim_directory(path);
304                         pending_strips++;
305                 }
306                 else if (pending_strips > 0 && *spath != '\0')
307                 {
308                         /* trim a regular directory name canceled by ".." */
309                         trim_directory(path);
310                         pending_strips--;
311                         /* foo/.. should become ".", not empty */
312                         if (*spath == '\0')
313                                 strcpy(spath, ".");
314                 }
315                 else
316                         break;
317         }
318
319         if (pending_strips > 0)
320         {
321                 /*
322                  * We could only get here if path is now totally empty (other than a
323                  * possible drive specifier on Windows). We have to put back one or
324                  * more ".."'s that we took off.
325                  */
326                 while (--pending_strips > 0)
327                         strcat(path, "../");
328                 strcat(path, "..");
329         }
330 }
331
332 /*
333  * Detect whether a path contains any parent-directory references ("..")
334  *
335  * The input *must* have been put through canonicalize_path previously.
336  *
337  * This is a bit tricky because we mustn't be fooled by "..a.." (legal)
338  * nor "C:.." (legal on Unix but not Windows).
339  */
340 bool
341 path_contains_parent_reference(const char *path)
342 {
343         int                     path_len;
344
345         path = skip_drive(path);        /* C: shouldn't affect our conclusion */
346
347         path_len = strlen(path);
348
349         /*
350          * ".." could be the whole path; otherwise, if it's present it must be at
351          * the beginning, in the middle, or at the end.
352          */
353         if (strcmp(path, "..") == 0 ||
354                 strncmp(path, "../", 3) == 0 ||
355                 strstr(path, "/../") != NULL ||
356                 (path_len >= 3 && strcmp(path + path_len - 3, "/..") == 0))
357                 return true;
358
359         return false;
360 }
361
362 /*
363  * Detect whether a path is only in or below the current working directory.
364  * An absolute path that matches the current working directory should
365  * return false (we only want relative to the cwd).  We don't allow
366  * "/../" even if that would keep us under the cwd (it is too hard to
367  * track that).
368  */
369 bool
370 path_is_relative_and_below_cwd(const char *path)
371 {
372         if (is_absolute_path(path))
373                 return false;
374         /* don't allow anything above the cwd */
375         else if (path_contains_parent_reference(path))
376                 return false;
377 #ifdef WIN32
378         /*
379          * On Win32, a drive letter _not_ followed by a slash, e.g. 'E:abc', is
380          * relative to the cwd on that drive, or the drive's root directory if
381          * that drive has no cwd.  Because the path itself cannot tell us which is
382          * the case, we have to assume the worst, i.e. that it is not below the
383          * cwd.  We could use GetFullPathName() to find the full path but that
384          * could change if the current directory for the drive changes underneath
385          * us, so we just disallow it.
386          */
387         else if (isalpha((unsigned char) path[0]) && path[1] == ':' &&
388                          !IS_DIR_SEP(path[2]))
389                 return false;
390 #endif
391         else
392                 return true;
393 }
394
395 /*
396  * Detect whether path1 is a prefix of path2 (including equality).
397  *
398  * This is pretty trivial, but it seems better to export a function than
399  * to export IS_DIR_SEP.
400  */
401 bool
402 path_is_prefix_of_path(const char *path1, const char *path2)
403 {
404         int                     path1_len = strlen(path1);
405
406         if (strncmp(path1, path2, path1_len) == 0 &&
407                 (IS_DIR_SEP(path2[path1_len]) || path2[path1_len] == '\0'))
408                 return true;
409         return false;
410 }
411
412 /*
413  * Extracts the actual name of the program as called -
414  * stripped of .exe suffix if any
415  */
416 const char *
417 get_progname(const char *argv0)
418 {
419         const char *nodir_name;
420         char       *progname;
421
422         nodir_name = last_dir_separator(argv0);
423         if (nodir_name)
424                 nodir_name++;
425         else
426                 nodir_name = skip_drive(argv0);
427
428         /*
429          * Make a copy in case argv[0] is modified by ps_status. Leaks memory, but
430          * called only once.
431          */
432         progname = strdup(nodir_name);
433         if (progname == NULL)
434         {
435                 fprintf(stderr, "%s: out of memory\n", nodir_name);
436                 abort();                                /* This could exit the postmaster */
437         }
438
439 #if defined(__CYGWIN__) || defined(WIN32)
440         /* strip ".exe" suffix, regardless of case */
441         if (strlen(progname) > sizeof(EXE) - 1 &&
442         pg_strcasecmp(progname + strlen(progname) - (sizeof(EXE) - 1), EXE) == 0)
443                 progname[strlen(progname) - (sizeof(EXE) - 1)] = '\0';
444 #endif
445
446         return progname;
447 }
448
449
450 /*
451  * dir_strcmp: strcmp except any two DIR_SEP characters are considered equal,
452  * and we honor filesystem case insensitivity if known
453  */
454 static int
455 dir_strcmp(const char *s1, const char *s2)
456 {
457         while (*s1 && *s2)
458         {
459                 if (
460 #ifndef WIN32
461                         *s1 != *s2
462 #else
463                 /* On windows, paths are case-insensitive */
464                         pg_tolower((unsigned char) *s1) != pg_tolower((unsigned char) *s2)
465 #endif
466                         && !(IS_DIR_SEP(*s1) && IS_DIR_SEP(*s2)))
467                         return (int) *s1 - (int) *s2;
468                 s1++, s2++;
469         }
470         if (*s1)
471                 return 1;                               /* s1 longer */
472         if (*s2)
473                 return -1;                              /* s2 longer */
474         return 0;
475 }
476
477
478 /*
479  * make_relative_path - make a path relative to the actual binary location
480  *
481  * This function exists to support relocation of installation trees.
482  *
483  *      ret_path is the output area (must be of size MAXPGPATH)
484  *      target_path is the compiled-in path to the directory we want to find
485  *      bin_path is the compiled-in path to the directory of executables
486  *      my_exec_path is the actual location of my executable
487  *
488  * We determine the common prefix of target_path and bin_path, then compare
489  * the remainder of bin_path to the last directory component(s) of
490  * my_exec_path.  If they match, build the result as the part of my_exec_path
491  * preceding the match, joined to the remainder of target_path.  If no match,
492  * return target_path as-is.
493  *
494  * For example:
495  *              target_path  = '/usr/local/share/postgresql'
496  *              bin_path         = '/usr/local/bin'
497  *              my_exec_path = '/opt/pgsql/bin/postmaster'
498  * Given these inputs, the common prefix is '/usr/local/', the tail of
499  * bin_path is 'bin' which does match the last directory component of
500  * my_exec_path, so we would return '/opt/pgsql/share/postgresql'
501  */
502 static void
503 make_relative_path(char *ret_path, const char *target_path,
504                                    const char *bin_path, const char *my_exec_path)
505 {
506         int                     prefix_len;
507         int                     tail_start;
508         int                     tail_len;
509         int                     i;
510
511         /*
512          * Determine the common prefix --- note we require it to end on a
513          * directory separator, consider eg '/usr/lib' and '/usr/libexec'.
514          */
515         prefix_len = 0;
516         for (i = 0; target_path[i] && bin_path[i]; i++)
517         {
518                 if (IS_DIR_SEP(target_path[i]) && IS_DIR_SEP(bin_path[i]))
519                         prefix_len = i + 1;
520                 else if (target_path[i] != bin_path[i])
521                         break;
522         }
523         if (prefix_len == 0)
524                 goto no_match;                  /* no common prefix? */
525         tail_len = strlen(bin_path) - prefix_len;
526
527         /*
528          * Set up my_exec_path without the actual executable name, and
529          * canonicalize to simplify comparison to bin_path.
530          */
531         strlcpy(ret_path, my_exec_path, MAXPGPATH);
532         trim_directory(ret_path);       /* remove my executable name */
533         canonicalize_path(ret_path);
534
535         /*
536          * Tail match?
537          */
538         tail_start = (int) strlen(ret_path) - tail_len;
539         if (tail_start > 0 &&
540                 IS_DIR_SEP(ret_path[tail_start - 1]) &&
541                 dir_strcmp(ret_path + tail_start, bin_path + prefix_len) == 0)
542         {
543                 ret_path[tail_start] = '\0';
544                 trim_trailing_separator(ret_path);
545                 join_path_components(ret_path, ret_path, target_path + prefix_len);
546                 canonicalize_path(ret_path);
547                 return;
548         }
549
550 no_match:
551         strlcpy(ret_path, target_path, MAXPGPATH);
552         canonicalize_path(ret_path);
553 }
554
555
556 /*
557  * make_absolute_path
558  *
559  * If the given pathname isn't already absolute, make it so, interpreting
560  * it relative to the current working directory.
561  *
562  * Also canonicalizes the path.  The result is always a malloc'd copy.
563  *
564  * In backend, failure cases result in ereport(ERROR); in frontend,
565  * we write a complaint on stderr and return NULL.
566  *
567  * Note: interpretation of relative-path arguments during postmaster startup
568  * should happen before doing ChangeToDataDir(), else the user will probably
569  * not like the results.
570  */
571 char *
572 make_absolute_path(const char *path)
573 {
574         char       *new;
575
576         /* Returning null for null input is convenient for some callers */
577         if (path == NULL)
578                 return NULL;
579
580         if (!is_absolute_path(path))
581         {
582                 char       *buf;
583                 size_t          buflen;
584
585                 buflen = MAXPGPATH;
586                 for (;;)
587                 {
588                         buf = malloc(buflen);
589                         if (!buf)
590                         {
591 #ifndef FRONTEND
592                                 ereport(ERROR,
593                                                 (errcode(ERRCODE_OUT_OF_MEMORY),
594                                                  errmsg("out of memory")));
595 #else
596                                 fprintf(stderr, _("out of memory\n"));
597                                 return NULL;
598 #endif
599                         }
600
601                         if (getcwd(buf, buflen))
602                                 break;
603                         else if (errno == ERANGE)
604                         {
605                                 free(buf);
606                                 buflen *= 2;
607                                 continue;
608                         }
609                         else
610                         {
611                                 free(buf);
612 #ifndef FRONTEND
613                                 elog(ERROR, "could not get current working directory: %m");
614 #else
615                                 fprintf(stderr, _("could not get current working directory: %s\n"),
616                                                 strerror(errno));
617                                 return NULL;
618 #endif
619                         }
620                 }
621
622                 new = malloc(strlen(buf) + strlen(path) + 2);
623                 if (!new)
624                 {
625                         free(buf);
626 #ifndef FRONTEND
627                         ereport(ERROR,
628                                         (errcode(ERRCODE_OUT_OF_MEMORY),
629                                          errmsg("out of memory")));
630 #else
631                         fprintf(stderr, _("out of memory\n"));
632                         return NULL;
633 #endif
634                 }
635                 sprintf(new, "%s/%s", buf, path);
636                 free(buf);
637         }
638         else
639         {
640                 new = strdup(path);
641                 if (!new)
642                 {
643 #ifndef FRONTEND
644                         ereport(ERROR,
645                                         (errcode(ERRCODE_OUT_OF_MEMORY),
646                                          errmsg("out of memory")));
647 #else
648                         fprintf(stderr, _("out of memory\n"));
649                         return NULL;
650 #endif
651                 }
652         }
653
654         /* Make sure punctuation is canonical, too */
655         canonicalize_path(new);
656
657         return new;
658 }
659
660
661 /*
662  *      get_share_path
663  */
664 void
665 get_share_path(const char *my_exec_path, char *ret_path)
666 {
667         make_relative_path(ret_path, PGSHAREDIR, PGBINDIR, my_exec_path);
668 }
669
670 /*
671  *      get_etc_path
672  */
673 void
674 get_etc_path(const char *my_exec_path, char *ret_path)
675 {
676         make_relative_path(ret_path, SYSCONFDIR, PGBINDIR, my_exec_path);
677 }
678
679 /*
680  *      get_include_path
681  */
682 void
683 get_include_path(const char *my_exec_path, char *ret_path)
684 {
685         make_relative_path(ret_path, INCLUDEDIR, PGBINDIR, my_exec_path);
686 }
687
688 /*
689  *      get_pkginclude_path
690  */
691 void
692 get_pkginclude_path(const char *my_exec_path, char *ret_path)
693 {
694         make_relative_path(ret_path, PKGINCLUDEDIR, PGBINDIR, my_exec_path);
695 }
696
697 /*
698  *      get_includeserver_path
699  */
700 void
701 get_includeserver_path(const char *my_exec_path, char *ret_path)
702 {
703         make_relative_path(ret_path, INCLUDEDIRSERVER, PGBINDIR, my_exec_path);
704 }
705
706 /*
707  *      get_lib_path
708  */
709 void
710 get_lib_path(const char *my_exec_path, char *ret_path)
711 {
712         make_relative_path(ret_path, LIBDIR, PGBINDIR, my_exec_path);
713 }
714
715 /*
716  *      get_pkglib_path
717  */
718 void
719 get_pkglib_path(const char *my_exec_path, char *ret_path)
720 {
721         make_relative_path(ret_path, PKGLIBDIR, PGBINDIR, my_exec_path);
722 }
723
724 /*
725  *      get_locale_path
726  */
727 void
728 get_locale_path(const char *my_exec_path, char *ret_path)
729 {
730         make_relative_path(ret_path, LOCALEDIR, PGBINDIR, my_exec_path);
731 }
732
733 /*
734  *      get_doc_path
735  */
736 void
737 get_doc_path(const char *my_exec_path, char *ret_path)
738 {
739         make_relative_path(ret_path, DOCDIR, PGBINDIR, my_exec_path);
740 }
741
742 /*
743  *      get_html_path
744  */
745 void
746 get_html_path(const char *my_exec_path, char *ret_path)
747 {
748         make_relative_path(ret_path, HTMLDIR, PGBINDIR, my_exec_path);
749 }
750
751 /*
752  *      get_man_path
753  */
754 void
755 get_man_path(const char *my_exec_path, char *ret_path)
756 {
757         make_relative_path(ret_path, MANDIR, PGBINDIR, my_exec_path);
758 }
759
760
761 /*
762  *      get_home_path
763  *
764  * On Unix, this actually returns the user's home directory.  On Windows
765  * it returns the PostgreSQL-specific application data folder.
766  */
767 bool
768 get_home_path(char *ret_path)
769 {
770 #ifndef WIN32
771         char            pwdbuf[BUFSIZ];
772         struct passwd pwdstr;
773         struct passwd *pwd = NULL;
774
775         if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
776                 return false;
777         strlcpy(ret_path, pwd->pw_dir, MAXPGPATH);
778         return true;
779 #else
780         char       *tmppath;
781
782         /*
783          * Note: We use getenv here because the more modern
784          * SHGetSpecialFolderPath() will force us to link with shell32.lib which
785          * eats valuable desktop heap.
786          */
787         tmppath = getenv("APPDATA");
788         if (!tmppath)
789                 return false;
790         snprintf(ret_path, MAXPGPATH, "%s/postgresql", tmppath);
791         return true;
792 #endif
793 }
794
795
796 /*
797  * get_parent_directory
798  *
799  * Modify the given string in-place to name the parent directory of the
800  * named file.
801  *
802  * If the input is just a file name with no directory part, the result is
803  * an empty string, not ".".  This is appropriate when the next step is
804  * join_path_components(), but might need special handling otherwise.
805  *
806  * Caution: this will not produce desirable results if the string ends
807  * with "..".  For most callers this is not a problem since the string
808  * is already known to name a regular file.  If in doubt, apply
809  * canonicalize_path() first.
810  */
811 void
812 get_parent_directory(char *path)
813 {
814         trim_directory(path);
815 }
816
817
818 /*
819  *      trim_directory
820  *
821  *      Trim trailing directory from path, that is, remove any trailing slashes,
822  *      the last pathname component, and the slash just ahead of it --- but never
823  *      remove a leading slash.
824  */
825 static void
826 trim_directory(char *path)
827 {
828         char       *p;
829
830         path = skip_drive(path);
831
832         if (path[0] == '\0')
833                 return;
834
835         /* back up over trailing slash(es) */
836         for (p = path + strlen(path) - 1; IS_DIR_SEP(*p) && p > path; p--)
837                 ;
838         /* back up over directory name */
839         for (; !IS_DIR_SEP(*p) && p > path; p--)
840                 ;
841         /* if multiple slashes before directory name, remove 'em all */
842         for (; p > path && IS_DIR_SEP(*(p - 1)); p--)
843                 ;
844         /* don't erase a leading slash */
845         if (p == path && IS_DIR_SEP(*p))
846                 p++;
847         *p = '\0';
848 }
849
850
851 /*
852  *      trim_trailing_separator
853  *
854  * trim off trailing slashes, but not a leading slash
855  */
856 static void
857 trim_trailing_separator(char *path)
858 {
859         char       *p;
860
861         path = skip_drive(path);
862         p = path + strlen(path);
863         if (p > path)
864                 for (p--; p > path && IS_DIR_SEP(*p); p--)
865                         *p = '\0';
866 }