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 -------------------------------------------------------------------------*/
26 #include "SDCCmacro.h"
31 /** Given an array of name, value string pairs creates a new hash
32 containing all of the pairs.
35 populateStringHash(const char **pin)
41 shash_add (&pret, pin[0], pin[1]);
48 /** Given an array of string pointers and another string, adds the
49 string to the end of the list. The end of the list is assumed to
50 be the first NULL pointer.
53 addToList (const char **list, const char *str)
55 /* This is the bad way to do things :) */
58 *list = Safe_strdup (str);
61 werror (E_OUT_OF_MEM, __FILE__, 0);
67 /** Given an array of string pointers returns a string containing all
68 of the strings seperated by spaces. The returned string is on the
69 heap. The join stops when a NULL pointer is hit.
72 join(const char **pplist)
78 strncatz(buffer, *pplist, PATH_MAX);
79 strncatz(buffer, " ", PATH_MAX);
86 /** Given an array of string pointers, returns a string containing all
87 of the strings seperated by spaces. The returned string is on the
88 heap. n is the number of strings in the list.
91 joinn(char **pplist, int n)
97 strncatz(buffer, *pplist, PATH_MAX);
98 strncatz(buffer, " ", PATH_MAX);
105 /** Returns TRUE if for the host the two path characters are
109 pathCharsEquivalent(char c1, char c2)
112 /* win32 is case insensitive */
113 if (tolower(c1) == tolower(c2))
117 /* And / is equivalent to \ */
118 else if (c1 == '/' && c2 == '\\')
122 else if (c1 == '\\' && c2 == '/')
131 /* Assume a Unix host where they must match exactly. */
137 pathCharTransform(char c)
142 return DIR_SEPARATOR_CHAR;
153 /** Fixes up a potentially mixed path to the proper representation for
154 the host. Fixes up in place.
163 *p = pathCharTransform(*p);
171 /** Returns the characters in p2 past the last matching characters in
175 getPathDifference (char *pinto, const char *p1, const char *p2)
180 /* win32 can have a path at the start. */
183 p2 = strchr(p2, ':')+1;
187 while (*p1 != '\0' && *p2 != '\0')
189 if (pathCharsEquivalent(*p1, *p2) == FALSE)
202 return fixupPath(pinto);
206 /** Given a file with path information in the binary files directory,
207 returns the directory component. Used for discovery of bin
208 directory of SDCC installation. Returns NULL if the path is
212 getBinPath(const char *prel)
216 static char path[PATH_MAX];
218 if ((p = strrchr(prel, DIR_SEPARATOR_CHAR)) == NULL)
220 /* try *nix dir separator on WIN32 */
221 if ((p = strrchr(prel, UNIX_DIR_SEPARATOR_CHAR)) == NULL)
225 len = min((sizeof path) - 1, p - prel);
226 strncpy(path, prel, len);
233 /** Returns true if the given path exists.
236 pathExists (const char *ppath)
240 return stat (ppath, &s) == 0;
243 static hTab *_mainValues;
246 setMainValue (const char *pname, const char *pvalue)
250 shash_add (&_mainValues, pname, pvalue);
254 buildCmdLine2 (char *pbuffer, const char *pcmd, size_t len)
257 assert(pbuffer && pcmd);
260 poutcmd = msprintf(_mainValues, pcmd);
261 strncpyz(pbuffer, poutcmd, len);
265 populateMainValues (const char **ppin)
267 _mainValues = populateStringHash(ppin);
270 /** Returns true if sz starts with the string given in key.
273 startsWith (const char *sz, const char *key)
275 return !strncmp (sz, key, strlen (key));
278 /** Removes any newline characters from the string. Not strictly the
279 same as perl's chomp.
285 while ((nl = strrchr (sz, '\n')))
290 getRuntimeVariables(void)
296 /* strncpy() with guaranteed NULL termination. */
297 char *strncpyz(char *dest, const char *src, size_t n)
305 fprintf(stderr, "strncpyz prevented buffer overrun!\n");
308 strncpy(dest, src, n);
313 /* like strncat() with guaranteed NULL termination
314 * The passed size should be the size of the dest buffer, not the number of
317 char *strncatz(char *dest, const char *src, size_t n)
320 size_t destLen = strlen(dest);
325 maxToCopy = n - destLen - 1;
328 if (strlen(src) + destLen >= n)
330 fprintf(stderr, "strncatz prevented buffer overrun!\n");
333 strncat(dest, src, maxToCopy);
339 #if defined(HAVE_VSNPRINTF) || defined(HAVE_VSPRINTF)
340 size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...)
347 # if defined(HAVE_VSNPRINTF)
348 len = vsnprintf(dst, n, fmt, args);
350 vsprintf(dst, fmt, args);
351 len = strlen(dst) + 1;
356 /* on some gnu systems, vsnprintf returns -1 if output is truncated.
357 * In the C99 spec, vsnprintf returns the number of characters that
358 * would have been written, were space available.
360 if ((len < 0) || (size_t) len >= n) {
361 fprintf(stderr, "internal error: sprintf truncated.\n");