From feae1d930d1762aa987369609e4a9a94863c4e62 Mon Sep 17 00:00:00 2001 From: Mr-Dave Date: Sat, 16 Apr 2022 15:31:23 -0600 Subject: [PATCH] Remove mystrdup and mystrcpy --- src/util.cpp | 120 +++++++++------------------------------------------ src/util.hpp | 2 - 2 files changed, 21 insertions(+), 101 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 6e69c3e6..c1db7b8b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -143,48 +143,39 @@ void *myrealloc(void *ptr, size_t size, const char *desc) /** * mycreate_path - * - * This function creates a whole path, like mkdir -p. Example paths: - * this/is/an/example/ - * /this/is/an/example/ - * Warning: a path *must* end with a slash! - * - * Parameters: - * - * cam - current thread's context structure (for logging) - * path - the path to create - * - * Returns: 0 on success, -1 on failure + * Create whole path. + * Path provided *must* end with a slash! */ int mycreate_path(const char *path) { - char *start; + std::string tmp; mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; + size_t indx_pos; + int retcd; - if (path[0] == '/') { - start = (char*)strchr(path + 1, '/'); + tmp = std::string(path); + + if (tmp.substr(0, 1) == "/") { + indx_pos = tmp.find("/", 1); } else { - start = (char*)strchr(path, '/'); + indx_pos = tmp.find("/", 0); } - while (start) { - char *buffer = mystrdup(path); - buffer[start-path] = 0x00; - - if (mkdir(buffer, mode) == -1 && errno != EEXIST) { + while (indx_pos != std::string::npos) { + MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO + ,_("Creating %s"), tmp.substr(0, indx_pos + 1).c_str()); + retcd = mkdir(tmp.substr(0, indx_pos + 1).c_str(), mode); + if (retcd == -1 && errno != EEXIST) { MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO - ,_("Problem creating directory %s"), buffer); - free(buffer); + ,_("Problem creating directory %s") + , tmp.substr(0, indx_pos + 1).c_str()); return -1; } - - start = strchr(start + 1, '/'); - - if (!start) { - MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("creating directory %s"), buffer); + indx_pos++; + if (indx_pos >= tmp.length()) { + break; } - - free(buffer); + indx_pos = tmp.find("/", indx_pos); } return 0; @@ -642,75 +633,6 @@ char* mytranslate_text(const char *msgid, int setnls) } } -/** - * mystrcpy - * Is used to assign string type fields (e.g. config options) - * In a way so that we the memory is malloc'ed to fit the string. - * If a field is already pointing to a string (not NULL) the memory of the - * old string is free'd and new memory is malloc'ed and filled with the - * new string is copied into the the memory and with the char pointer - * pointing to the new string. - * - * from - pointer to the new string we want to copy - * to - the pointer to the current string (or pointing to NULL) - * If not NULL the memory it points to is free'd. - * - * Returns pointer to the new string which is in malloc'ed memory - * FIXME The strings that are malloc'ed with this function should be freed - * when the motion program is terminated normally instead of relying on the - * OS to clean up. - */ -char *mystrcpy(char *to, const char *from) -{ - /* - * Free the memory used by the to string, if such memory exists, - * and return a pointer to a freshly malloc()'d string with the - * same value as from. - */ - - if (to != NULL) { - free(to); - } - - return mystrdup(from); -} - -/** - * mystrdup - * Truncates the string to the length given by the environment - * variable PATH_MAX to ensure that config options can always contain - * a really long path but no more than that. - * - * Returns a pointer to a freshly malloc()'d string with the same - * value as the string that the input parameter 'from' points to, - * or NULL if the from string is 0 characters. - */ -char *mystrdup(const char *from) -{ - char *tmp; - size_t stringlength; - - if (from == NULL || !strlen(from)) { - tmp = NULL; - } else { - stringlength = strlen(from); - stringlength = (stringlength < PATH_MAX ? stringlength : PATH_MAX); - tmp = (char*)mymalloc(stringlength + 1); - strncpy(tmp, from, stringlength); - - /* - * We must ensure the string always has a NULL terminator. - * This necessary because strncpy will not append a NULL terminator - * if the original string is greater than string length. - */ - tmp += stringlength; - *tmp = '\0'; - tmp -= stringlength; - } - - return tmp; -} - /**************************************************************************** * The section below is the "my" section of functions. * These are designed to be extremely simple version specific diff --git a/src/util.hpp b/src/util.hpp index 786af349..d7b49286 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -120,8 +120,6 @@ int mystrcne(const char* var1, const char* var2); int mystreq(const char* var1, const char* var2); int mystrne(const char* var1, const char* var2); - char *mystrdup(const char *from); - char *mystrcpy(char *to, const char *from); void myltrim(std::string &parm); void myrtrim(std::string &parm); void mytrim(std::string &parm);