2999
|
1 /* lib.h: declarations for common, low-level routines in kpathsea. |
|
2 |
|
3 Copyright (C) 1992, 93, 94, 95, 96 Free Software Foundation, Inc. |
|
4 |
|
5 This library is free software; you can redistribute it and/or |
|
6 modify it under the terms of the GNU Library General Public |
|
7 License as published by the Free Software Foundation; either |
|
8 version 2 of the License, or (at your option) any later version. |
|
9 |
|
10 This library is distributed in the hope that it will be useful, |
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 Library General Public License for more details. |
|
14 |
|
15 You should have received a copy of the GNU Library General Public |
|
16 License along with this library; if not, write to the Free Software |
|
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
|
18 |
|
19 #ifndef KPATHSEA_LIB_H |
|
20 #define KPATHSEA_LIB_H |
|
21 |
|
22 #include <kpathsea/types.h> |
|
23 |
|
24 /* Define common sorts of messages. */ |
|
25 |
|
26 /* This should be called only after a system call fails. Don't exit |
|
27 with status `errno', because that might be 256, which would mean |
|
28 success (exit statuses are truncated to eight bits). */ |
|
29 #define FATAL_PERROR(str) do { \ |
|
30 fprintf (stderr, "%s: ", program_invocation_name); \ |
|
31 perror (str); exit (EXIT_FAILURE); } while (0) |
|
32 |
|
33 #define START_FATAL() do { \ |
|
34 fprintf (stderr, "%s: fatal: ", program_invocation_name); |
|
35 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0) |
|
36 |
|
37 #define FATAL(str) \ |
|
38 START_FATAL (); fputs (str, stderr); END_FATAL () |
|
39 #define FATAL1(str, e1) \ |
|
40 START_FATAL (); fprintf (stderr, str, e1); END_FATAL () |
|
41 #define FATAL2(str, e1, e2) \ |
|
42 START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL () |
|
43 #define FATAL3(str, e1, e2, e3) \ |
|
44 START_FATAL (); fprintf (stderr, str, e1, e2, e3); END_FATAL () |
|
45 #define FATAL4(str, e1, e2, e3, e4) \ |
|
46 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4); END_FATAL () |
|
47 #define FATAL5(str, e1, e2, e3, e4, e5) \ |
|
48 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5); END_FATAL () |
|
49 #define FATAL6(str, e1, e2, e3, e4, e5, e6) \ |
|
50 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5, e6); END_FATAL () |
|
51 |
|
52 |
|
53 #define START_WARNING() do { fputs ("warning: ", stderr) |
|
54 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0) |
|
55 |
|
56 #define WARNING(str) \ |
|
57 START_WARNING (); fputs (str, stderr); END_WARNING () |
|
58 #define WARNING1(str, e1) \ |
|
59 START_WARNING (); fprintf (stderr, str, e1); END_WARNING () |
|
60 #define WARNING2(str, e1, e2) \ |
|
61 START_WARNING (); fprintf (stderr, str, e1, e2); END_WARNING () |
|
62 #define WARNING3(str, e1, e2, e3) \ |
|
63 START_WARNING (); fprintf (stderr, str, e1, e2, e3); END_WARNING () |
|
64 #define WARNING4(str, e1, e2, e3, e4) \ |
|
65 START_WARNING (); fprintf (stderr, str, e1, e2, e3, e4); END_WARNING () |
|
66 |
|
67 |
|
68 /* I find this easier to read. */ |
|
69 #define STREQ(s1, s2) (strcmp (s1, s2) == 0) |
|
70 |
|
71 /* Support for FAT/ISO-9660 filesystems. Theoretically this should be |
|
72 done at runtime, per filesystem, but that's painful to program. */ |
|
73 #ifdef MONOCASE_FILENAMES |
|
74 #define FILESTRCASEEQ(s1, s2) (strcasecmp (s1, s2) == 0) |
|
75 #define FILECHARCASEEQ(c1, c2) (toupper (c1) == toupper (c2)) |
|
76 #else |
|
77 #define FILESTRCASEEQ STREQ |
|
78 #define FILECHARCASEEQ(c1, c2) ((c1) == (c2)) |
|
79 #endif |
|
80 |
|
81 /* This is the maximum number of numerals that result when a 64-bit |
|
82 integer is converted to a string, plus one for a trailing null byte, |
|
83 plus one for a sign. */ |
|
84 #define MAX_INT_LENGTH 21 |
|
85 |
|
86 /* If the environment variable TEST is set, return it; otherwise, |
|
87 DEFAULT. This is useful for paths that use more than one envvar. */ |
|
88 #define ENVVAR(test, default) (getenv (test) ? (test) : (default)) |
|
89 |
|
90 /* Return a fresh copy of S1 followed by S2, et al. */ |
|
91 extern DllImport string concat P2H(const_string s1, const_string s2); |
|
92 extern DllImport string concat3 P3H(const_string, const_string, const_string); |
|
93 /* `concatn' is declared in its own include file, to avoid pulling in |
|
94 all the varargs stuff. */ |
|
95 |
|
96 /* A fresh copy of just S. */ |
|
97 extern DllImport string xstrdup P1H(const_string s); |
|
98 |
|
99 /* Convert all lowercase characters in S to uppercase. */ |
|
100 extern DllImport string uppercasify P1H(const_string s); |
|
101 |
|
102 /* Like `atoi', but disallow negative numbers. */ |
|
103 extern DllImport unsigned atou P1H(const_string); |
|
104 |
|
105 /* True if FILENAME1 and FILENAME2 are the same file. If stat fails on |
|
106 either name, return false, no error message. |
|
107 Cf. `SAME_FILE_P' in xstat.h. */ |
|
108 extern DllImport boolean same_file_p P2H(const_string filename1, |
|
109 const_string filename2); |
|
110 |
|
111 #ifndef HAVE_BASENAME |
|
112 /* Return NAME with any leading path stripped off. This returns a |
|
113 pointer into NAME. */ |
|
114 extern DllImport const_string basename P1H(const_string name); |
|
115 #endif /* not HAVE_BASENAME */ |
|
116 |
|
117 #ifndef HAVE_STRSTR |
|
118 extern string strstr P2H(const_string haystack, const_string needle); |
|
119 #endif |
|
120 |
|
121 /* If NAME has a suffix, return a pointer to its first character (i.e., |
|
122 the one after the `.'); otherwise, return NULL. */ |
|
123 extern DllImport string find_suffix P1H(const_string name); |
|
124 |
|
125 /* Return NAME with any suffix removed. */ |
|
126 extern DllImport string remove_suffix P1H(const_string name); |
|
127 |
|
128 /* Return S with the suffix SUFFIX, removing any suffix already present. |
|
129 For example, `make_suffix ("/foo/bar.baz", "quux")' returns |
|
130 `/foo/bar.quux'. Returns a string allocated with malloc. */ |
|
131 extern DllImport string make_suffix P2H(const_string s, const_string suffix); |
|
132 |
|
133 /* Return NAME with STEM_PREFIX prepended to the stem. For example, |
|
134 `make_prefix ("/foo/bar.baz", "x")' returns `/foo/xbar.baz'. |
|
135 Returns a string allocated with malloc. */ |
|
136 extern DllImport string make_prefix P2H(string stem_prefix, string name); |
|
137 |
|
138 /* If NAME has a suffix, simply return it; otherwise, return |
|
139 `NAME.SUFFIX'. */ |
|
140 extern DllImport string extend_filename P2H(const_string name, |
|
141 const_string suffix); |
|
142 |
|
143 /* Call putenv with the string `VAR=VALUE' and abort on error. */ |
|
144 extern DllImport void xputenv P2H(const_string var, const_string value); |
|
145 extern DllImport void xputenv_int P2H(const_string var, int value); |
|
146 |
|
147 /* Return the current working directory. */ |
|
148 extern DllImport string xgetcwd P1H(void); |
|
149 |
|
150 /* Returns true if FN is a directory or a symlink to a directory. */ |
|
151 extern DllImport boolean dir_p P1H(const_string fn); |
|
152 |
|
153 /* If FN is a readable directory, return the number of links it has. |
|
154 Otherwise, return -1. */ |
|
155 extern DllImport int dir_links P1H(const_string fn); |
|
156 |
|
157 /* Like their stdio counterparts, but abort on error, after calling |
|
158 perror(3) with FILENAME as its argument. */ |
|
159 extern DllImport FILE *xfopen P2H(const_string filename, const_string mode); |
|
160 extern DllImport void xfclose P2H(FILE *, const_string filename); |
|
161 extern DllImport void xfseek P4H(FILE *, long, int, string filename); |
|
162 extern DllImport unsigned long xftell P2H(FILE *, string filename); |
|
163 |
|
164 /* These call the corresponding function in the standard library, and |
|
165 abort if those routines fail. Also, `xrealloc' calls `xmalloc' if |
|
166 OLD_ADDRESS is null. */ |
|
167 extern DllImport address xmalloc P1H(unsigned size); |
|
168 extern DllImport address xrealloc P2H(address old_address, unsigned new_size); |
|
169 extern DllImport address xcalloc P2H(unsigned nelem, unsigned elsize); |
|
170 |
|
171 /* (Re)Allocate N items of type T using xmalloc/xrealloc. */ |
|
172 #define XTALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t))) |
|
173 #define XTALLOC1(t) XTALLOC (1, t) |
|
174 #define XRETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof(t))) |
|
175 |
|
176 #endif /* not KPATHSEA_LIB_H */ |