1 /* lib.h: declarations for common, low-level routines in kpathsea. 2 3 Copyright 1992, 1993, 1994, 1995, 1996, 2008, 2009, 2010, 2011, 4 2012, 2015 Karl Berry. 5 Copyright 1999, 2000, 2003, 2005 Olaf Weber. 6 7 This library is free software; you can redistribute it and/or 8 modify it under the terms of the GNU Lesser General Public 9 License as published by the Free Software Foundation; either 10 version 2.1 of the License, or (at your option) any later version. 11 12 This library is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Lesser General Public License for more details. 16 17 You should have received a copy of the GNU Lesser General Public License 18 along with this library; if not, see <http://www.gnu.org/licenses/>. */ 19 20 #ifndef KPATHSEA_LIB_H 21 #define KPATHSEA_LIB_H 22 23 #include <kpathsea/c-proto.h> 24 #include <kpathsea/systypes.h> 25 #include <kpathsea/types.h> 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 /* Define common sorts of messages. */ 32 33 /* This should be called only after a system call fails. Don't exit 34 with status `errno', because that might be 256, which would mean 35 success (exit statuses are truncated to eight bits). */ 36 37 #if defined (KPSE_COMPAT_API) 38 /* This branch works as expected also in programs that use the new interface, 39 because kpathsea_set_program_name() sets up the kpse_def->invocation_name 40 whenever it is available. 41 */ 42 #define FATAL_PERROR(str) do { \ 43 fprintf (stderr, "%s: ", kpse_def->invocation_name); \ 44 perror (str); exit (EXIT_FAILURE); } while (0) 45 #else 46 /* If there is no global variable available, just output the error */ 47 #define FATAL_PERROR(str) do { \ 48 perror (str); exit (EXIT_FAILURE); } while (0) 49 #endif 50 51 #if defined (KPSE_COMPAT_API) 52 #define START_FATAL() do { \ 53 fprintf (stderr, "%s: fatal: ", kpse_def->invocation_name); 54 #else 55 #define START_FATAL() do { \ 56 fprintf (stderr, "fatal: "); 57 #endif 58 59 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0) 60 61 #define FATAL(str) \ 62 START_FATAL (); fputs (str, stderr); END_FATAL () 63 #define FATAL1(str, e1) \ 64 START_FATAL (); fprintf (stderr, str, e1); END_FATAL () 65 #define FATAL2(str, e1, e2) \ 66 START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL () 67 #define FATAL3(str, e1, e2, e3) \ 68 START_FATAL (); fprintf (stderr, str, e1, e2, e3); END_FATAL () 69 #define FATAL4(str, e1, e2, e3, e4) \ 70 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4); END_FATAL () 71 #define FATAL5(str, e1, e2, e3, e4, e5) \ 72 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5); END_FATAL () 73 #define FATAL6(str, e1, e2, e3, e4, e5, e6) \ 74 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5, e6); END_FATAL () 75 76 #define START_WARNING() do { fputs ("warning: ", stderr) 77 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0) 78 79 #define WARNING(str) \ 80 START_WARNING (); fputs (str, stderr); END_WARNING () 81 #define WARNING1(str, e1) \ 82 START_WARNING (); fprintf (stderr, str, e1); END_WARNING () 83 #define WARNING2(str, e1, e2) \ 84 START_WARNING (); fprintf (stderr, str, e1, e2); END_WARNING () 85 #define WARNING3(str, e1, e2, e3) \ 86 START_WARNING (); fprintf (stderr, str, e1, e2, e3); END_WARNING () 87 #define WARNING4(str, e1, e2, e3, e4) \ 88 START_WARNING (); fprintf (stderr, str, e1, e2, e3, e4); END_WARNING () 89 90 #define LIB_START_FATAL() do { \ 91 fprintf (stderr, "%s: fatal: ", kpse->invocation_name); 92 93 #define LIB_FATAL(str) \ 94 LIB_START_FATAL (); fputs (str, stderr); END_FATAL () 95 #define LIB_FATAL1(str, e1) \ 96 LIB_START_FATAL (); fprintf (stderr, str, e1); END_FATAL () 97 #define LIB_FATAL2(str, e1, e2) \ 98 LIB_START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL () 99 100 101 /* I find this easier to read. */ 102 #define STREQ(s1, s2) (((s1) != NULL) && ((s2) != NULL) && (strcmp (s1, s2) == 0)) 103 #define STRNEQ(s1, s2, n) ((s1) && (s2) && (strncmp (s1, s2, n) == 0)) 104 105 /* Support for FAT/ISO-9660 filesystems. Theoretically this should be 106 done at runtime, per filesystem, but that's painful to program. */ 107 #ifdef MONOCASE_FILENAMES 108 #define FILESTRCASEEQ(s1,s2) ((s1) && (s2) && (strcasecmp (s1, s2) == 0)) 109 #define FILESTRNCASEEQ(s1,s2,l) ((s1) && (s2) && (strncasecmp (s1,s2,l) == 0)) 110 #define FILECHARCASEEQ(c1,c2) (toupper (c1) == toupper (c2)) 111 #else 112 #define FILESTRCASEEQ STREQ 113 #define FILESTRNCASEEQ STRNEQ 114 #define FILECHARCASEEQ(c1,c2) ((c1) == (c2)) 115 #endif 116 117 /* This is the maximum number of numerals that result when a 64-bit 118 integer is converted to a string, plus one for a trailing null byte, 119 plus one for a sign. */ 120 #define MAX_INT_LENGTH 21 121 122 /* If the environment variable TEST is set, return it; otherwise, 123 DEFAULT. This is useful for paths that use more than one envvar. */ 124 #define ENVVAR(test, default) (getenv (test) ? (test) : (default)) 125 126 /* Return a fresh copy of S1 followed by S2, et al. */ 127 extern KPSEDLL string concat (const_string s1, const_string s2); 128 extern KPSEDLL string concat3 (const_string, const_string, const_string); 129 /* `concatn' is declared in its own include file, to avoid pulling in 130 all the varargs stuff. */ 131 132 /* A fresh copy of just S. */ 133 extern KPSEDLL string xstrdup (const_string s); 134 135 /* Convert all lowercase characters in S to uppercase. */ 136 extern KPSEDLL string uppercasify (const_string s); 137 138 /* Like `atoi', but disallow negative numbers. */ 139 extern KPSEDLL unsigned atou (const_string); 140 141 /* True if FILENAME1 and FILENAME2 are the same file. If stat fails on 142 either name, return false, no error message. 143 Cf. `SAME_FILE_P' in xstat.h. */ 144 extern KPSEDLL boolean same_file_p (const_string filename1, 145 const_string filename2); 146 147 /* Return NAME with any leading path stripped off. This returns a 148 pointer into NAME. */ 149 extern KPSEDLL const_string xbasename (const_string name); 150 151 /* Return directory part of NAME. This returns a new string. */ 152 extern KPSEDLL string xdirname (const_string name); 153 154 /* If NAME has a suffix, return a pointer to its first character (i.e., 155 the one after the `.'); otherwise, return NULL. */ 156 extern KPSEDLL const_string find_suffix (const_string name); 157 158 /* Return NAME with any suffix removed. */ 159 extern KPSEDLL string remove_suffix (const_string name); 160 161 /* Return S with the suffix SUFFIX, removing any suffix already present. 162 For example, `make_suffix ("/foo/bar.baz", "quux")' returns 163 `/foo/bar.quux'. Returns a string allocated with malloc. */ 164 extern KPSEDLL string make_suffix (const_string s, const_string suffix); 165 166 /* Return NAME with STEM_PREFIX prepended to the stem. For example, 167 `make_prefix ("/foo/bar.baz", "x")' returns `/foo/xbar.baz'. 168 Returns a string allocated with malloc. */ 169 extern KPSEDLL string make_prefix (string stem_prefix, string name); 170 171 /* If NAME has a suffix, simply return it; otherwise, return 172 `NAME.SUFFIX'. */ 173 extern KPSEDLL const_string extend_filename (const_string name, 174 const_string suffix); 175 176 /* Call putenv with the string `VAR=VALUE' and abort on error. */ 177 extern KPSEDLL void kpathsea_xputenv (kpathsea kpse, const_string var, 178 const_string value); 179 extern KPSEDLL void kpathsea_xputenv_int (kpathsea kpse, const_string var, 180 int value); 181 #if defined (KPSE_COMPAT_API) 182 extern KPSEDLL void xputenv (const_string var, const_string value); 183 extern KPSEDLL void xputenv_int (const_string var, int value); 184 #endif 185 186 /* Return the current working directory. */ 187 extern KPSEDLL string xgetcwd (void); 188 189 /* Returns true if FN is a directory or a symlink to a directory. */ 190 extern KPSEDLL boolean kpathsea_dir_p (kpathsea kpse, string fn); 191 #if defined (KPSE_COMPAT_API) 192 extern KPSEDLL boolean dir_p (string fn); 193 #endif 194 195 /* If FN is a readable directory, return the number of links it has. 196 Otherwise, return -1. The nlinks parameter is a dummy on UNIX. */ 197 #if defined (KPSE_COMPAT_API) 198 extern KPSEDLL int dir_links (const_string fn, long nlinks); 199 #endif 200 extern KPSEDLL int kpathsea_dir_links (kpathsea kpse, const_string fn, 201 long nlinks); 202 203 /* Like their stdio counterparts, but abort on error, after calling 204 perror(3) with FILENAME as its argument. */ 205 extern KPSEDLL FILE *xfopen (const_string filename, const_string mode); 206 extern KPSEDLL void xfclose (FILE *fp, const_string filename); 207 extern KPSEDLL void xfseek (FILE *fp, long offset, int wherefrom, const_string filename); 208 extern KPSEDLL void xfseeko (FILE *fp, off_t offset, int wherefrom, const_string filename); 209 extern KPSEDLL long xftell (FILE *fp, const_string filename); 210 extern KPSEDLL off_t xftello (FILE *fp, const_string filename); 211 212 /* These call the corresponding function in the standard library, and 213 abort if those routines fail. Also, `xrealloc' calls `xmalloc' if 214 OLD_ADDRESS is null. */ 215 extern KPSEDLL address xmalloc (size_t size); 216 extern KPSEDLL address xrealloc (address old_address, size_t new_size); 217 extern KPSEDLL address xcalloc (size_t nelem, size_t elsize); 218 219 /* (Re)Allocate N items of type T using xmalloc/xrealloc. */ 220 #define XTALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t))) 221 #define XTALLOC1(t) XTALLOC (1, t) 222 #define XRETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof(t))) 223 224 #ifdef __cplusplus 225 } 226 #endif 227 228 #endif /* not KPATHSEA_LIB_H */ 229