2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
17 * Miscellaneous string utilities.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
27 strconcat(const char *s1, const char *s2)
29 static char tmp[FILENAME_MAX];
32 strncpy(tmp, s1 ? s1 : s2, FILENAME_MAX); /* XXX: what if both are NULL? */
34 strncat(tmp, s2, FILENAME_MAX - strlen(tmp));
38 /* Get a string parameter as a file spec or as a "contents follow -" spec */
40 get_dash_string(char **str)
45 *str = copy_string_adds_newline(s + 1);
47 *str = fileGetContents(s);
53 copy_string(const char *str)
55 return (str ? strdup(str) : NULL);
58 /* Rather Obvious but adds a trailing \n newline */
60 copy_string_adds_newline(const char *str)
68 line_length = strlen(str) + 2;
69 if ((copy = malloc(line_length)) == NULL)
71 memcpy(copy, str, line_length - 2);
72 copy[line_length - 2] = '\n'; /* Adds trailing \n */
73 copy[line_length - 1] = '\0';
79 /* Return TRUE if 'str' ends in suffix 'suff' */
81 suffix(const char *str, const char *suff)
86 idx = strrchr(str, '.');
87 if (idx && !strcmp(idx + 1, suff))
92 /* Assuming str has a suffix, brutally murder it! */
94 nuke_suffix(char *str)
98 idx = strrchr(str, '.');
100 *idx = '\0'; /* Yow! Don't try this on a const! */
103 /* Lowercase a whole string */
105 str_lowercase(char *str)
108 *str = tolower(*str);
114 get_string(char *str, int max, FILE *fp)
121 while (fgets(str, max, fp)) {
123 while (len && isspace(str[len - 1]))