OSDN Git Service

branch: yandytex with kpathsea.
[putex/putex.git] / src / texsourc / kpathsea / kpathsea / absolute.c
1 /* absolute.c: test if a filename is absolute or explicitly relative.
2
3    Copyright 1993, 1994, 1995, 2008, 2009, 2010, 2011 Karl Berry.
4    Copyright 1997, 2002, 2005 Olaf Weber.
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public License
17    along with this library; if not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <kpathsea/config.h>
20
21 #include <kpathsea/absolute.h>
22 #include <kpathsea/c-pathch.h>
23
24 /* Sorry this is such a system-dependent mess, but I can't see any way
25    to usefully generalize.  */
26
27 boolean
28 kpathsea_absolute_p (kpathsea kpse, const_string filename, boolean relative_ok)
29 {
30 #ifdef VMS
31 #include <string.h>
32   (void)kpse; /* currenty not used */
33   return strcspn (filename, "]>:") != strlen (filename);
34 #else /* not VMS */
35   boolean absolute;
36   boolean explicit_relative;
37
38   absolute = IS_DIR_SEP (*filename)
39 #ifdef DOSISH
40                      /* Novell allows non-alphanumeric drive letters. */
41                      || (*filename && IS_DEVICE_SEP (filename[1]))
42 #endif /* DOSISH */
43 #ifdef WIN32
44                      /* UNC names */
45                      || (*filename == '\\' && filename[1] == '\\')
46                      || (*filename == '/' && filename[1] == '/')
47 #endif /* WIN32 */
48 #ifdef AMIGA
49                      /* Colon anywhere means a device.  */
50                      || strchr (filename, ':')
51 #endif /* AMIGA */
52                       ;
53   explicit_relative
54     = relative_ok
55 #ifdef AMIGA
56       /* Leading / is like `../' on Unix and DOS.  Allow Unix syntax,
57          too, though, because of possible patch programs like
58          `UnixDirsII' by Martin Scott.  */
59       && IS_DIR_SEP (*filename) || 0
60 #endif /* AMIGA */
61       && (*filename == '.' && (IS_DIR_SEP (filename[1])
62                          || (filename[1] == '.' && IS_DIR_SEP (filename[2]))));
63
64   (void)kpse; /* currenty not used */
65   /* FIXME: On UNIX an IS_DIR_SEP of any but the last character in the name
66      implies relative.  */
67   return absolute || explicit_relative;
68 #endif /* not VMS */
69 }
70
71 #if defined (KPSE_COMPAT_API)
72 boolean
73 kpse_absolute_p (const_string filename, boolean relative_ok)
74 {
75     return kpathsea_absolute_p (kpse_def, filename, relative_ok);
76 }
77 #endif
78
79 #ifdef TEST
80 int main()
81 {
82   char **name;
83   char *t[] = { "./foo", "\\\\server\\foo\\bar", "ftp://localhost/foo" };
84
85   for (name = t; name - t < sizeof(t)/sizeof(char*); name++) {
86     printf ("Path `%s' %s absolute.\n", *name,
87             kpse_absolute_p(*name, true) ? "is" : "is not");
88   }
89 }
90 #endif /* TEST */
91
92 /*
93 Local variables:
94 standalone-compile-command: "gcc -g -I. -I.. -DTEST absolute.c kpathsea.a"
95 End:
96 */