diff options
Diffstat (limited to 'string-utils.c')
-rw-r--r-- | string-utils.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/string-utils.c b/string-utils.c new file mode 100644 index 0000000..01ddebc --- /dev/null +++ b/string-utils.c @@ -0,0 +1,122 @@ +/* + * nvidia-installer: A tool for installing NVIDIA software packages on + * Unix and Linux systems. + * + * Copyright (C) 2003-2009 NVIDIA Corporation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the: + * + * Free Software Foundation, Inc. + * 59 Temple Place - Suite 330 + * Boston, MA 02111-1307, USA + * + * + * string-utils.c: this file contains various helper function used for + * string manipulation in nvidia-installer. + */ + +#include <ctype.h> +#include <string.h> +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include "string-utils.h" +#include "alloc-utils.h" + +/* + * nvstrdup() - wrapper for strdup() that checks the return + * value; if an error occurs, an error is printed to + * stderr and exit is called. This function will only return + * on success. + */ + +char *nvstrdup(const char *s) +{ + char *m; + + if (!s) return NULL; + + m = strdup(s); + + if (!m) { + fprintf(stderr, "%s: memory allocation failure during strdup (%s)! \n", + PROGRAM_NAME, strerror(errno)); + exit(1); + } + return m; + +} /* nvstrdup() */ + + + +/* + * nvstrtolower() - convert the given string to lowercase. + */ + +char *nvstrtolower(char *s) +{ + char *start = s; + + if (s == NULL) return NULL; + + while (*s) { + *s = tolower(*s); + s++; + } + + return start; + +} /* nvstrtolower() */ + + + +/* + * nvstrcat() - allocate a new string, copying all given strings + * into it. taken from glib + */ + +char *nvstrcat(const char *str, ...) +{ + unsigned int l; + va_list args; + char *s; + char *concat; + + l = 1 + strlen(str); + va_start(args, str); + s = va_arg(args, char *); + + while (s) { + l += strlen(s); + s = va_arg(args, char *); + } + va_end(args); + + concat = nvalloc(l); + concat[0] = 0; + + strcat(concat, str); + va_start(args, str); + s = va_arg(args, char *); + while (s) { + strcat(concat, s); + s = va_arg(args, char *); + } + va_end(args); + + return concat; + +} /* nvstrcat() */ |