1 /*-------------------------------------------------------------------------
2 SDCCutil.c - Small utility functions.
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
29 #include "SDCCglobl.h"
30 #include "SDCCmacro.h"
34 /** Given an array of name, value string pairs creates a new hash
35 containing all of the pairs.
38 populateStringHash(const char **pin)
44 shash_add (&pret, pin[0], pin[1]);
51 /** Given an array of string pointers and another string, adds the
52 string to the end of the list. The end of the list is assumed to
53 be the first NULL pointer.
56 addToList (const char **list, const char *str)
58 /* This is the bad way to do things :) */
61 *list = Safe_strdup (str);
64 werror (E_OUT_OF_MEM, __FILE__, 0);
70 /** Given an array of string pointers returns a string containing all
71 of the strings seperated by spaces. The returned string is on the
72 heap. The join stops when a NULL pointer is hit.
75 join(const char **pplist)
81 strncatz(buffer, *pplist, PATH_MAX);
82 strncatz(buffer, " ", PATH_MAX);
89 /** Given an array of string pointers, returns a string containing all
90 of the strings seperated by spaces. The returned string is on the
91 heap. n is the number of strings in the list.
94 joinn(char **pplist, int n)
100 strncatz(buffer, *pplist, PATH_MAX);
101 strncatz(buffer, " ", PATH_MAX);
108 /** Returns TRUE if for the host the two path characters are
112 pathCharsEquivalent(char c1, char c2)
115 /* win32 is case insensitive */
116 if (tolower(c1) == tolower(c2))
120 /* And / is equivalent to \ */
121 else if (c1 == '/' && c2 == '\\')
125 else if (c1 == '\\' && c2 == '/')
134 /* Assume a Unix host where they must match exactly. */
140 pathCharTransform(char c)
145 return DIR_SEPARATOR_CHAR;
156 /** Fixes up a potentially mixed path to the proper representation for
157 the host. Fixes up in place.
166 *p = pathCharTransform(*p);
174 /** Returns the characters in p2 past the last matching characters in
178 getPathDifference (char *pinto, const char *p1, const char *p2)
183 /* win32 can have a path at the start. */
186 p2 = strchr(p2, ':')+1;
190 while (*p1 != '\0' && *p2 != '\0')
192 if (pathCharsEquivalent(*p1, *p2) == FALSE)
205 return fixupPath(pinto);
209 /** Given a file with path information in the binary files directory,
210 returns the directory component. Used for discovery of bin
211 directory of SDCC installation. Returns NULL if the path is
216 getBinPath(const char *prel)
220 static char path[PATH_MAX];
222 /* try DOS and *nix dir separator on WIN32 */
223 if (NULL != (p = strrchr(prel, DIR_SEPARATOR_CHAR)) ||
224 NULL != (p = strrchr(prel, UNIX_DIR_SEPARATOR_CHAR))) {
225 len = min((sizeof path) - 1, p - prel);
226 strncpy(path, prel, len);
230 /* not enough info in prel; do it with module name */
231 else if (0 != GetModuleFileName(NULL, path, sizeof path) != 0 &&
232 NULL != (p = strrchr(path, DIR_SEPARATOR_CHAR))) {
241 getBinPath(const char *prel)
245 static char path[PATH_MAX];
247 if ((p = strrchr(prel, DIR_SEPARATOR_CHAR)) == NULL)
250 len = min((sizeof path) - 1, p - prel);
251 strncpy(path, prel, len);
258 /** Returns true if the given path exists.
261 pathExists (const char *ppath)
265 return stat (ppath, &s) == 0;
268 static hTab *_mainValues;
271 setMainValue (const char *pname, const char *pvalue)
275 shash_add (&_mainValues, pname, pvalue);
279 buildCmdLine2 (char *pbuffer, const char *pcmd, size_t len)
282 assert(pbuffer && pcmd);
285 poutcmd = msprintf(_mainValues, pcmd);
286 strncpyz(pbuffer, poutcmd, len);
290 populateMainValues (const char **ppin)
292 _mainValues = populateStringHash(ppin);
295 /** Returns true if sz starts with the string given in key.
298 startsWith (const char *sz, const char *key)
300 return !strncmp (sz, key, strlen (key));
303 /** Removes any newline characters from the string. Not strictly the
304 same as perl's chomp.
310 while ((nl = strrchr (sz, '\n')))
315 getRuntimeVariables(void)
321 /* strncpy() with guaranteed NULL termination. */
322 char *strncpyz(char *dest, const char *src, size_t n)
330 fprintf(stderr, "strncpyz prevented buffer overrun!\n");
333 strncpy(dest, src, n);
338 /* like strncat() with guaranteed NULL termination
339 * The passed size should be the size of the dest buffer, not the number of
342 char *strncatz(char *dest, const char *src, size_t n)
345 size_t destLen = strlen(dest);
350 maxToCopy = n - destLen - 1;
353 if (strlen(src) + destLen >= n)
355 fprintf(stderr, "strncatz prevented buffer overrun!\n");
358 strncat(dest, src, maxToCopy);
364 #if defined(HAVE_VSNPRINTF) || defined(HAVE_VSPRINTF)
365 size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...)
372 # if defined(HAVE_VSNPRINTF)
373 len = vsnprintf(dst, n, fmt, args);
375 vsprintf(dst, fmt, args);
376 len = strlen(dst) + 1;
381 /* on some gnu systems, vsnprintf returns -1 if output is truncated.
382 * In the C99 spec, vsnprintf returns the number of characters that
383 * would have been written, were space available.
385 if ((len < 0) || (size_t) len >= n) {
386 fprintf(stderr, "internal error: sprintf truncated.\n");