diff --git a/src/Makefile.am b/src/Makefile.am index 26e8f5ad..35a122f5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,7 +48,9 @@ libprdata_la_SOURCES = \ radrate.c \ auger_trans.c \ kissel_pe.c \ - cross_sections.c + cross_sections.c \ + xraylib-aux.c \ + xraylib-aux.h if !OS_WINDOWS libprdata_la_LIBADD = -lm diff --git a/src/crystal_diffraction.c b/src/crystal_diffraction.c index b150facf..b931e467 100644 --- a/src/crystal_diffraction.c +++ b/src/crystal_diffraction.c @@ -156,7 +156,7 @@ Crystal_Struct* Crystal_MakeCopy (Crystal_Struct *crystal, xrl_error **error) { } *crystal_out = *crystal; - crystal_out->name = strdup(crystal->name); + crystal_out->name = xrl_strdup(crystal->name); n = crystal->n_atom * sizeof(Crystal_Atom); crystal_out->atom = malloc(n); if (crystal_out->atom == NULL) { @@ -195,7 +195,7 @@ char** Crystal_GetCrystalsList(Crystal_Array *c_array, int *nCrystals, xrl_error return NULL; } for (i = 0 ; i < c_array->n_crystal ; i++) - rv[i] = strdup(c_array->crystal[i].name); + rv[i] = xrl_strdup(c_array->crystal[i].name); rv[c_array->n_crystal] = NULL; @@ -587,7 +587,7 @@ int Crystal_ReadFile(const char* file_name, Crystal_Array* c_array, xrl_error ** } crystal = &(c_array->crystal[c_array->n_crystal++]); - crystal->name = strdup(compound); + crystal->name = xrl_strdup(compound); /* * Parse lines of the crystal definition before list of atom positions. diff --git a/src/xrayfiles.c b/src/xrayfiles.c index 2472ea62..1f480a3b 100644 --- a/src/xrayfiles.c +++ b/src/xrayfiles.c @@ -54,7 +54,7 @@ void XRayInit(void) for (i = 0 ; i < MENDEL_MAX ; i++) { - MendelArraySorted[i].name = strdup(MendelArray[i].name); + MendelArraySorted[i].name = xrl_strdup(MendelArray[i].name); MendelArraySorted[i].Zatom = MendelArray[i].Zatom; } @@ -296,7 +296,7 @@ void XRayInit(void) if (nerror_lines == 0) { fprintf(stderr, "%s is not present in the linenames database: adjust xraylib-lines.h and xrayvars.c/h\n",line_name); error_lines = malloc(sizeof(char *) * ++nerror_lines); - error_lines[0] = strdup(line_name); + error_lines[0] = xrl_strdup(line_name); } else { found_error_line = 0; @@ -309,7 +309,7 @@ void XRayInit(void) if (!found_error_line) { fprintf(stderr, "%s is not present in the linenames database: adjust xraylib-lines.h and xrayvars.c/h\n",line_name); error_lines= (char **) realloc((char **) error_lines,sizeof(char *)*++nerror_lines); - error_lines[nerror_lines-1] = strdup(line_name); + error_lines[nerror_lines-1] = xrl_strdup(line_name); } } } @@ -345,7 +345,7 @@ void XRayInit(void) if (nerror_lines == 0) { fprintf(stderr,"%s is not present in the shellnames database: adjust xraylib-shells.h and xrayvars.c/h\n",shell_name); error_lines = malloc(sizeof(char *) * ++nerror_lines); - error_lines[0] = strdup(shell_name); + error_lines[0] = xrl_strdup(shell_name); } else { found_error_line = 0; @@ -358,7 +358,7 @@ void XRayInit(void) if (!found_error_line) { fprintf(stderr,"%s is not present in the shellnames database: adjust xraylib-shells.h and xrayvars.c/h\n",shell_name); error_lines= (char **) realloc((char **) error_lines,sizeof(char *)*++nerror_lines); - error_lines[nerror_lines-1] = strdup(shell_name); + error_lines[nerror_lines-1] = xrl_strdup(shell_name); } } } @@ -456,7 +456,7 @@ void XRayInit(void) if (nerror_lines == 0) { fprintf(stderr, "%s is not present in the linenames database: adjust xraylib-lines.h and xrayvars.c/h\n",line_name); error_lines = malloc(sizeof(char *) * ++nerror_lines); - error_lines[0] = strdup(line_name); + error_lines[0] = xrl_strdup(line_name); } else { found_error_line = 0; @@ -469,7 +469,7 @@ void XRayInit(void) if (!found_error_line) { fprintf(stderr, "%s is not present in the linenames database: adjust xraylib-lines.h and xrayvars.c/h\n",line_name); error_lines= (char **) realloc((char **) error_lines,sizeof(char *)*++nerror_lines); - error_lines[nerror_lines-1] = strdup(line_name); + error_lines[nerror_lines-1] = xrl_strdup(line_name); } } } @@ -512,7 +512,7 @@ void XRayInit(void) if (nerror_lines == 0) { fprintf(stderr, "%s is not present in the Auger transition names database: adjust xraylib-auger.h and xrayvars.c/h\n",auger_name); error_lines = malloc(sizeof(char *) * ++nerror_lines); - error_lines[0] = strdup(auger_name); + error_lines[0] = xrl_strdup(auger_name); } else { found_error_line = 0; @@ -525,7 +525,7 @@ void XRayInit(void) if (!found_error_line) { fprintf(stderr, "%s is not present in the Auger transition names database: adjust xraylib-auger.h and xrayvars.c/h\n",auger_name); error_lines= (char **) realloc((char **) error_lines,sizeof(char *)*++nerror_lines); - error_lines[nerror_lines-1] = strdup(auger_name); + error_lines[nerror_lines-1] = xrl_strdup(auger_name); } } } diff --git a/src/xraylib-aux.c b/src/xraylib-aux.c index 9d347d62..0c305df1 100644 --- a/src/xraylib-aux.c +++ b/src/xraylib-aux.c @@ -1,5 +1,5 @@ /* -Copyright (c) 2010, Tom Schoonjans +Copyright (c) 2010-2020, Tom Schoonjans All rights reserved. Redistribution and use in source and binary forms, with or without @@ -17,15 +17,19 @@ THIS SOFTWARE IS PROVIDED BY Tom Schoonjans ''AS IS'' AND ANY EXPRESS OR IMPLIED #include #ifndef HAVE_STRDUP -char *strdup(const char *str) { +char *xrl_strdup(const char *str) { char *dup= (char *)malloc( strlen(str)+1 ); if (dup) strcpy(dup,str); return dup; } +#else +char *xrl_strdup(const char *str) { + return strdup(str); +} #endif #ifndef HAVE_STRNDUP -char *strndup(const char *str, size_t len) { +char *xrl_strndup(const char *str, size_t len) { char *dup= (char *)malloc( len+1 ); if (dup) { strncpy(dup,str,len); @@ -33,4 +37,8 @@ char *strndup(const char *str, size_t len) { } return dup; } +#else +char *xrl_strndup(const char *str, size_t len) { + return strndup(str, len); +} #endif diff --git a/src/xraylib-aux.h b/src/xraylib-aux.h index ddc1e05a..e5aa63df 100644 --- a/src/xraylib-aux.h +++ b/src/xraylib-aux.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2010, Tom Schoonjans +Copyright (c) 2010-2020, Tom Schoonjans All rights reserved. Redistribution and use in source and binary forms, with or without @@ -14,25 +14,10 @@ THIS SOFTWARE IS PROVIDED BY Tom Schoonjans ''AS IS'' AND ANY EXPRESS OR IMPLIED #ifndef _XRAYLIB_AUX_H #define _XRAYLIB_AUX_H -#include "config.h" #include -/* - * - * The following two functions are ISO C extensions and are not available on all platforms. - * We will use our own functions if they are absent from libc. - * - */ - - -#ifndef HAVE_STRDUP -extern char *strdup(const char *str); -#endif - -#ifndef HAVE_STRNDUP -extern char *strndup(const char *str, size_t len); -#endif - +char *xrl_strdup(const char *str); +char *xrl_strndup(const char *str, size_t len); #endif diff --git a/src/xraylib-error.c b/src/xraylib-error.c index 802bd2b9..872b1ea1 100644 --- a/src/xraylib-error.c +++ b/src/xraylib-error.c @@ -11,6 +11,7 @@ THIS SOFTWARE IS PROVIDED BY Tom Schoonjans 'AS IS' AND ANY EXPRESS OR IMPLIED W */ #include "config.h" +#include "xraylib-aux.h" #include "xraylib-error-private.h" #include #include @@ -77,7 +78,7 @@ xrl_error* xrl_error_new_literal(xrl_error_code code, const char *message) { error = malloc(sizeof(xrl_error)); error->code = code; - error->message = strdup(message); + error->message = xrl_strdup(message); return error; } @@ -104,7 +105,7 @@ xrl_error* xrl_error_copy(const xrl_error *error) { copy->message = NULL; if (error->message) - copy->message = strdup(error->message); + copy->message = xrl_strdup(error->message); return copy; } diff --git a/src/xraylib-nist-compounds.c b/src/xraylib-nist-compounds.c index 29df63f2..9de2fe84 100644 --- a/src/xraylib-nist-compounds.c +++ b/src/xraylib-nist-compounds.c @@ -48,7 +48,7 @@ struct compoundDataNIST *GetCompoundDataNISTByName(const char compoundString[], xrl_set_error_literal(error, XRL_ERROR_INVALID_ARGUMENT, "compoundString cannot be NULL"); return NULL; } - key->name = strdup(compoundString); + key->name = xrl_strdup(compoundString); #ifndef _WIN32 @@ -64,7 +64,7 @@ struct compoundDataNIST *GetCompoundDataNISTByName(const char compoundString[], free(key->name); if (rv != NULL) { - key->name = strdup(rv->name); + key->name = xrl_strdup(rv->name); key->nElements = rv->nElements; key->Elements = malloc(sizeof(int)*rv->nElements); memcpy(key->Elements, rv->Elements, sizeof(int)*rv->nElements); @@ -93,7 +93,7 @@ struct compoundDataNIST *GetCompoundDataNISTByIndex(int compoundIndex, xrl_error xrl_set_error(error, XRL_ERROR_MEMORY, MALLOC_ERROR, strerror(errno)); return NULL; } - key->name = strdup(compoundDataNISTList[compoundIndex].name); + key->name = xrl_strdup(compoundDataNISTList[compoundIndex].name); key->nElements = compoundDataNISTList[compoundIndex].nElements; key->Elements = malloc(sizeof(int)*compoundDataNISTList[compoundIndex].nElements); memcpy(key->Elements, compoundDataNISTList[compoundIndex].Elements, sizeof(int)*compoundDataNISTList[compoundIndex].nElements); @@ -118,7 +118,7 @@ char **GetCompoundDataNISTList(int *nCompounds, xrl_error **error) { } for (i = 0 ; i < nCompoundDataNISTList; i++) - rv[i] = strdup(compoundDataNISTList[i].name); + rv[i] = xrl_strdup(compoundDataNISTList[i].name); rv[nCompoundDataNISTList] = NULL; diff --git a/src/xraylib-parser.c b/src/xraylib-parser.c index 2fd519a7..2ff93591 100644 --- a/src/xraylib-parser.c +++ b/src/xraylib-parser.c @@ -124,7 +124,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca, for (i = 0 ; i < nuppers ; i++) { if (islower(upper_locs[i][1]) && !islower(upper_locs[i][2])) { /*second letter is lowercase and third one isn't -> valid */ - tempElement = strndup(upper_locs[i],2); + tempElement = xrl_strndup(upper_locs[i],2); /*get corresponding atomic number */ res = bsearch(tempElement, MendelArraySorted, MENDEL_MAX, sizeof(struct MendelElement), matchMendelElement); if (res == NULL) { @@ -147,7 +147,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca, tempnAtoms = 1.0; } else { - tempSubstring = strndup(upper_locs[i] + 2, j - 2); + tempSubstring = xrl_strndup(upper_locs[i] + 2, j - 2); tempnAtoms = strtod(tempSubstring, &endPtr); if (endPtr != tempSubstring+strlen(tempSubstring)) { xrl_set_error(error, XRL_ERROR_INVALID_ARGUMENT, "Invalid chemical formula: could not convert subscript %s to a real number", tempSubstring); @@ -165,7 +165,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca, } else if (!islower(upper_locs[i][1])) { /*second letter is not lowercase -> valid */ - tempElement = strndup(upper_locs[i], 1); + tempElement = xrl_strndup(upper_locs[i], 1); /*get corresponding atomic number */ res = bsearch(tempElement, MendelArraySorted, MENDEL_MAX, sizeof(struct MendelElement), matchMendelElement); if (res == NULL) { @@ -188,7 +188,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca, tempnAtoms = 1.0; } else { - tempSubstring = strndup(upper_locs[i] + 1, j - 1); + tempSubstring = xrl_strndup(upper_locs[i] + 1, j - 1); tempnAtoms = strtod(tempSubstring, &endPtr); if (endPtr != tempSubstring + strlen(tempSubstring)) { xrl_set_error(error, XRL_ERROR_INVALID_ARGUMENT, "Invalid chemical formula: could not convert subscript %s to a real number", tempSubstring); @@ -241,7 +241,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca, /*handle the brackets... */ for (i = 0 ; i < nbracket_pairs ; i++) { tempBracketAtoms = malloc(sizeof(struct compoundAtoms)); - tempBracketString = strndup(brackets_begin_locs[i]+1,(size_t) (brackets_end_locs[i]-brackets_begin_locs[i]-1)); + tempBracketString = xrl_strndup(brackets_begin_locs[i]+1,(size_t) (brackets_end_locs[i]-brackets_begin_locs[i]-1)); tempBracketAtoms->nElements = 0; tempBracketAtoms->singleElements = NULL; /*recursive call */ @@ -265,7 +265,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca, tempnAtoms = 1.0; } else { - tempSubstring = strndup(brackets_end_locs[i]+1,j-1); + tempSubstring = xrl_strndup(brackets_end_locs[i]+1,j-1); tempnAtoms = strtod(tempSubstring,&endPtr); if (endPtr != tempSubstring+strlen(tempSubstring)) { xrl_set_error(error, XRL_ERROR_INVALID_ARGUMENT, "Invalid chemical formula: could not convert subscript %s to a real number", tempSubstring); @@ -337,7 +337,7 @@ struct compoundData* CompoundParser(const char compoundString[], xrl_error **err /* the locale is changed to default locale because we'll be using strtod later on */ backup_locale = setlocale(LC_NUMERIC, "C"); - compoundStringCopy = strdup(compoundString); + compoundStringCopy = xrl_strdup(compoundString); rvCPS = CompoundParserSimple(compoundStringCopy, &ca, error); @@ -458,7 +458,7 @@ char *AtomicNumberToSymbol(int Z, xrl_error **error) { return NULL; } - return strdup(MendelArray[Z-1].name); + return xrl_strdup(MendelArray[Z-1].name); } int SymbolToAtomicNumber(const char *symbol, xrl_error **error) { diff --git a/src/xraylib-radionuclides.c b/src/xraylib-radionuclides.c index 69bf2b51..b56d7105 100644 --- a/src/xraylib-radionuclides.c +++ b/src/xraylib-radionuclides.c @@ -52,7 +52,7 @@ struct radioNuclideData *GetRadioNuclideDataByName(const char radioNuclideString xrl_set_error_literal(error, XRL_ERROR_INVALID_ARGUMENT, "radioNuclideString cannot be NULL"); return NULL; } - key->name = strdup(radioNuclideString); + key->name = xrl_strdup(radioNuclideString); #ifndef _WIN32 nelp = nNuclideDataList; @@ -66,7 +66,7 @@ struct radioNuclideData *GetRadioNuclideDataByName(const char radioNuclideString free(key->name); if (rv != NULL) { - key->name = strdup(rv->name); + key->name = xrl_strdup(rv->name); key->Z = rv->Z; key->A = rv->A; key->N = rv->N; @@ -104,7 +104,7 @@ struct radioNuclideData *GetRadioNuclideDataByIndex(int radioNuclideIndex, xrl_e xrl_set_error(error, XRL_ERROR_MEMORY, MALLOC_ERROR, strerror(errno)); return NULL; } - key->name = strdup(nuclideDataList[radioNuclideIndex].name); + key->name = xrl_strdup(nuclideDataList[radioNuclideIndex].name); key->Z = nuclideDataList[radioNuclideIndex].Z; key->A = nuclideDataList[radioNuclideIndex].A; key->N = nuclideDataList[radioNuclideIndex].N; @@ -137,7 +137,7 @@ char **GetRadioNuclideDataList(int *nRadioNuclides, xrl_error **error) { } for (i = 0 ; i < nNuclideDataList; i++) - rv[i] = strdup(nuclideDataList[i].name); + rv[i] = xrl_strdup(nuclideDataList[i].name); rv[nNuclideDataList] = NULL; diff --git a/tests/test-crystal_diffraction.c b/tests/test-crystal_diffraction.c index 4c34b64b..ff3dd61e 100644 --- a/tests/test-crystal_diffraction.c +++ b/tests/test-crystal_diffraction.c @@ -13,6 +13,7 @@ THIS SOFTWARE IS PROVIDED BY Tom Schoonjans ''AS IS'' AND ANY EXPRESS OR IMPLIED #include #include "xraylib.h" +#include "xraylib-aux.h" #include "xraylib-error-private.h" #ifdef NDEBUG #undef NDEBUG @@ -119,7 +120,7 @@ int main(int argc, char **argv) { assert(error == NULL); assert(cs_copy != NULL); free(cs_copy->name); - cs_copy->name = strdup("Diamond-copy"); + cs_copy->name = xrl_strdup("Diamond-copy"); rv = Crystal_AddCrystal(cs, NULL, &error); assert(rv == 0);