X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2FSDCCutil.c;h=3cd6a0c8b8c7b9c515444c6f23b1ad794bd33f92;hb=430335203e350c61246e703a2b01d559b9978b37;hp=ba665ff1694c707beee66a13bbbbf0e8572ffe25;hpb=7131c95b14af27494c070111ef89a541889c3a79;p=fw%2Fsdcc diff --git a/src/SDCCutil.c b/src/SDCCutil.c index ba665ff1..3cd6a0c8 100644 --- a/src/SDCCutil.c +++ b/src/SDCCutil.c @@ -55,7 +55,7 @@ addToList (const char **list, const char *str) /* This is the bad way to do things :) */ while (*list) list++; - *list = strdup (str); + *list = Safe_strdup (str); if (!*list) { werror (E_OUT_OF_MEM, __FILE__, 0); @@ -71,18 +71,16 @@ addToList (const char **list, const char *str) char * join(const char **pplist) { - char *pinto = buffer; - *pinto = '\0'; - - while (*pplist) + buffer[0] = 0; + + while (*pplist) { - strcpy(pinto, *pplist); - pinto += strlen(*pplist); - *pinto++ = ' '; - pplist++; + strncatz(buffer, *pplist, PATH_MAX); + strncatz(buffer, " ", PATH_MAX); + pplist++; } - return buffer; + return buffer; } /** Given an array of string pointers, returns a string containing all @@ -92,19 +90,16 @@ join(const char **pplist) char * joinn(char **pplist, int n) { - char *pinto = buffer; - *pinto = '\0'; - - while (n--) + buffer[0] = 0; + + while (n--) { - strcpy(pinto, *pplist); - pinto += strlen(*pplist); - *pinto++ = ' '; - pplist++; + strncatz(buffer, *pplist, PATH_MAX); + strncatz(buffer, " ", PATH_MAX); + pplist++; } - *pinto = '\0'; - return buffer; + return buffer; } /** Returns TRUE if for the host the two path characters are @@ -138,22 +133,6 @@ pathCharsEquivalent(char c1, char c2) #endif } -static bool -pathEquivalent(const char *p1, const char *p2) -{ - while (*p1 != '\0' && *p2 != '\0') - { - if (pathCharsEquivalent (*p1, *p2) == FALSE) - { - break; - } - p1++; - p2++; - } - - return *p1 == *p2; -} - static char pathCharTransform(char c) { @@ -223,39 +202,34 @@ getPathDifference (char *pinto, const char *p1, const char *p2) return fixupPath(pinto); } + /** Given a file with path information in the binary files directory, - returns what PREFIX must be to get this path. Used for discovery - of where SDCC is installed. Returns NULL if the path is + returns the directory component. Used for discovery of bin + directory of SDCC installation. Returns NULL if the path is impossible. */ char * -getPrefixFromBinPath (const char *prel) +getBinPath(const char *prel) { - strcpy(scratchFileName, prel); - /* Strip off the /sdcc at the end */ - *strrchr(scratchFileName, DIR_SEPARATOR_CHAR) = '\0'; - /* Compute what the difference between the prefix and the bin dir - should be. */ - getPathDifference (buffer, PREFIX, BINDIR); - - /* Verify that the path in has the expected suffix */ - if (strlen(buffer) > strlen(scratchFileName)) - { - /* Not long enough */ - return NULL; - } - - if (pathEquivalent (buffer, scratchFileName + strlen(scratchFileName) - strlen(buffer)) == FALSE) - { - /* Doesn't match */ + char *p; + size_t len; + static char path[PATH_MAX]; + + if ((p = strrchr(prel, DIR_SEPARATOR_CHAR)) == NULL) +#ifdef _WIN32 + /* try *nix dir separator on WIN32 */ + if ((p = strrchr(prel, UNIX_DIR_SEPARATOR_CHAR)) == NULL) +#endif return NULL; - } - scratchFileName[strlen(scratchFileName) - strlen(buffer)] = '\0'; + len = min((sizeof path) - 1, p - prel); + strncpy(path, prel, len); + path[len] = '\0'; - return gc_strdup (scratchFileName); + return path; } + /** Returns true if the given path exists. */ bool @@ -271,14 +245,20 @@ static hTab *_mainValues; void setMainValue (const char *pname, const char *pvalue) { + assert(pname); + shash_add (&_mainValues, pname, pvalue); } void -buildCmdLine2 (char *pbuffer, const char *pcmd) +buildCmdLine2 (char *pbuffer, const char *pcmd, size_t len) { - char *poutcmd = msprintf(_mainValues, pcmd); - strcpy(pbuffer, poutcmd); + char *poutcmd; + assert(pbuffer && pcmd); + assert(_mainValues); + + poutcmd = msprintf(_mainValues, pcmd); + strncpyz(pbuffer, poutcmd, len); } void @@ -287,15 +267,6 @@ populateMainValues (const char **ppin) _mainValues = populateStringHash(ppin); } -char * -gc_strdup (const char *s) -{ - char *ret; - ret = Safe_calloc (1, strlen (s) + 1); - strcpy (ret, s); - return ret; -} - /** Returns true if sz starts with the string given in key. */ bool @@ -315,3 +286,82 @@ chomp (char *sz) *nl = '\0'; } +hTab * +getRuntimeVariables(void) +{ + return _mainValues; +} + + +/* strncpy() with guaranteed NULL termination. */ +char *strncpyz(char *dest, const char *src, size_t n) +{ + assert(n > 0); + + --n; + // paranoia... + if (strlen(src) > n) + { + fprintf(stderr, "strncpyz prevented buffer overrun!\n"); + } + + strncpy(dest, src, n); + dest[n] = 0; + return dest; +} + +/* like strncat() with guaranteed NULL termination + * The passed size should be the size of the dest buffer, not the number of + * bytes to copy. + */ +char *strncatz(char *dest, const char *src, size_t n) +{ + size_t maxToCopy; + size_t destLen = strlen(dest); + + assert(n > 0); + assert(n > destLen); + + maxToCopy = n - destLen - 1; + + // paranoia... + if (strlen(src) + destLen >= n) + { + fprintf(stderr, "strncatz prevented buffer overrun!\n"); + } + + strncat(dest, src, maxToCopy); + dest[n - 1] = 0; + return dest; +} + + +#if defined(HAVE_VSNPRINTF) || defined(HAVE_VSPRINTF) +size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...) +{ + va_list args; + int len; + + va_start(args, fmt); + +# if defined(HAVE_VSNPRINTF) + len = vsnprintf(dst, n, fmt, args); +# else + vsprintf(dst, fmt, args); + len = strlen(dst) + 1; +# endif + + va_end(args); + + /* on some gnu systems, vsnprintf returns -1 if output is truncated. + * In the C99 spec, vsnprintf returns the number of characters that + * would have been written, were space available. + */ + if ((len < 0) || (size_t) len >= n) { + fprintf(stderr, "internal error: sprintf truncated.\n"); + } + + return len; +} + +#endif