1 /* lib.h: declarations for common, low-level routines in kpathsea.
3 Copyright 1999, 2000, 2003, 2005 Olaf Weber.
4 Copyright 1992, 1993, 1994, 1995, 1996, 2008 Karl Berry.
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.
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.
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/>. */
19 #ifndef KPATHSEA_LIB_H
20 #define KPATHSEA_LIB_H
22 #include <kpathsea/c-proto.h>
23 #include <kpathsea/systypes.h>
24 #include <kpathsea/types.h>
26 /* Define common sorts of messages. */
28 /* This should be called only after a system call fails. Don't exit
29 with status `errno', because that might be 256, which would mean
30 success (exit statuses are truncated to eight bits). */
31 #define FATAL_PERROR(str) do { \
32 fprintf (stderr, "%s: ", program_invocation_name); \
33 perror (str); exit (EXIT_FAILURE); } while (0)
35 #define START_FATAL() do { \
36 fprintf (stderr, "%s: fatal: ", program_invocation_name);
37 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0)
40 START_FATAL (); fputs (str, stderr); END_FATAL ()
41 #define FATAL1(str, e1) \
42 START_FATAL (); fprintf (stderr, str, e1); END_FATAL ()
43 #define FATAL2(str, e1, e2) \
44 START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL ()
45 #define FATAL3(str, e1, e2, e3) \
46 START_FATAL (); fprintf (stderr, str, e1, e2, e3); END_FATAL ()
47 #define FATAL4(str, e1, e2, e3, e4) \
48 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4); END_FATAL ()
49 #define FATAL5(str, e1, e2, e3, e4, e5) \
50 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5); END_FATAL ()
51 #define FATAL6(str, e1, e2, e3, e4, e5, e6) \
52 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5, e6); END_FATAL ()
55 #define START_WARNING() do { fputs ("warning: ", stderr)
56 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0)
58 #define WARNING(str) \
59 START_WARNING (); fputs (str, stderr); END_WARNING ()
60 #define WARNING1(str, e1) \
61 START_WARNING (); fprintf (stderr, str, e1); END_WARNING ()
62 #define WARNING2(str, e1, e2) \
63 START_WARNING (); fprintf (stderr, str, e1, e2); END_WARNING ()
64 #define WARNING3(str, e1, e2, e3) \
65 START_WARNING (); fprintf (stderr, str, e1, e2, e3); END_WARNING ()
66 #define WARNING4(str, e1, e2, e3, e4) \
67 START_WARNING (); fprintf (stderr, str, e1, e2, e3, e4); END_WARNING ()
70 /* I find this easier to read. */
71 #define STREQ(s1, s2) ((s1) && (s2) && (strcmp (s1, s2) == 0))
72 #define STRNEQ(s1, s2, n) ((s1) && (s2) && (strncmp (s1, s2, n) == 0))
74 /* Support for FAT/ISO-9660 filesystems. Theoretically this should be
75 done at runtime, per filesystem, but that's painful to program. */
76 #ifdef MONOCASE_FILENAMES
77 #define FILESTRCASEEQ(s1, s2) ((s1) && (s2) && (strcasecmp (s1, s2) == 0))
78 #define FILESTRNCASEEQ(s1, s2, l) ((s1) && (s2) && (strncasecmp (s1, s2, l) == 0))
79 #define FILECHARCASEEQ(c1, c2) (toupper (c1) == toupper (c2))
81 #define FILESTRCASEEQ STREQ
82 #define FILESTRNCASEEQ STRNEQ
83 #define FILECHARCASEEQ(c1, c2) ((c1) == (c2))
86 /* This is the maximum number of numerals that result when a 64-bit
87 integer is converted to a string, plus one for a trailing null byte,
88 plus one for a sign. */
89 #define MAX_INT_LENGTH 21
91 /* If the environment variable TEST is set, return it; otherwise,
92 DEFAULT. This is useful for paths that use more than one envvar. */
93 #define ENVVAR(test, default) (getenv (test) ? (test) : (default))
95 /* Return a fresh copy of S1 followed by S2, et al. */
96 extern KPSEDLL string concat P2H(const_string s1, const_string s2);
97 extern KPSEDLL string concat3 P3H(const_string, const_string, const_string);
98 /* `concatn' is declared in its own include file, to avoid pulling in
99 all the varargs stuff. */
101 /* A fresh copy of just S. */
102 extern KPSEDLL string xstrdup P1H(const_string s);
104 /* Convert all lowercase characters in S to uppercase. */
105 extern KPSEDLL string uppercasify P1H(const_string s);
107 /* Like `atoi', but disallow negative numbers. */
108 extern KPSEDLL unsigned atou P1H(const_string);
110 /* True if FILENAME1 and FILENAME2 are the same file. If stat fails on
111 either name, return false, no error message.
112 Cf. `SAME_FILE_P' in xstat.h. */
113 extern KPSEDLL boolean same_file_p P2H(const_string filename1,
114 const_string filename2);
116 /* Return NAME with any leading path stripped off. This returns a
117 pointer into NAME. */
118 extern KPSEDLL const_string xbasename P1H(const_string name);
120 /* Return directory part of NAME. This returns a new string. */
121 extern KPSEDLL string xdirname P1H(const_string name);
123 #if !HAVE_DECL_STRSTR
124 extern string strstr P2H(const_string haystack, const_string needle);
127 /* If NAME has a suffix, return a pointer to its first character (i.e.,
128 the one after the `.'); otherwise, return NULL. */
129 extern KPSEDLL string find_suffix P1H(const_string name);
131 /* Return NAME with any suffix removed. */
132 extern KPSEDLL string remove_suffix P1H(const_string name);
134 /* Return S with the suffix SUFFIX, removing any suffix already present.
135 For example, `make_suffix ("/foo/bar.baz", "quux")' returns
136 `/foo/bar.quux'. Returns a string allocated with malloc. */
137 extern KPSEDLL string make_suffix P2H(const_string s, const_string suffix);
139 /* Return NAME with STEM_PREFIX prepended to the stem. For example,
140 `make_prefix ("/foo/bar.baz", "x")' returns `/foo/xbar.baz'.
141 Returns a string allocated with malloc. */
142 extern KPSEDLL string make_prefix P2H(string stem_prefix, string name);
144 /* If NAME has a suffix, simply return it; otherwise, return
146 extern KPSEDLL string extend_filename P2H(const_string name,
147 const_string suffix);
149 /* Call putenv with the string `VAR=VALUE' and abort on error. */
150 extern KPSEDLL void xputenv P2H(const_string var, const_string value);
151 extern KPSEDLL void xputenv_int P2H(const_string var, int value);
153 /* Return the current working directory. */
154 extern KPSEDLL string xgetcwd P1H(void);
156 /* Returns true if FN is a directory or a symlink to a directory. */
157 extern KPSEDLL boolean dir_p P1H(const_string fn);
159 /* If FN is a readable directory, return the number of links it has.
160 Otherwise, return -1. The nlinks parameter is a dummy on UNIX. */
161 extern KPSEDLL int dir_links P2H(const_string fn, long nlinks);
163 /* Like their stdio counterparts, but abort on error, after calling
164 perror(3) with FILENAME as its argument. */
165 extern KPSEDLL FILE *xfopen P2H(const_string filename, const_string mode);
166 extern KPSEDLL void xfclose P2H(FILE *, const_string filename);
167 extern KPSEDLL void xfseek P4H(FILE *, long, int, string filename);
168 extern KPSEDLL void xfseeko P4H(FILE *, off_t, int, string filename);
169 extern KPSEDLL unsigned long xftell P2H(FILE *, string filename);
170 extern KPSEDLL off_t xftello P2H(FILE *, string filename);
172 /* These call the corresponding function in the standard library, and
173 abort if those routines fail. Also, `xrealloc' calls `xmalloc' if
174 OLD_ADDRESS is null. */
175 extern KPSEDLL address xmalloc P1H(unsigned size);
176 extern KPSEDLL address xrealloc P2H(address old_address, unsigned new_size);
177 extern KPSEDLL address xcalloc P2H(unsigned nelem, unsigned elsize);
179 /* (Re)Allocate N items of type T using xmalloc/xrealloc. */
180 #define XTALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t)))
181 #define XTALLOC1(t) XTALLOC (1, t)
182 #define XRETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof(t)))
184 #endif /* not KPATHSEA_LIB_H */