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.
19 * $FreeBSD: src/usr.sbin/pkg_install/lib/str.c,v 1.15 2003/05/26 17:12:22 lioux Exp $
20 * $DragonFly: src/usr.sbin/pkg_install/lib/Attic/str.c,v 1.3 2004/07/30 04:46:13 dillon Exp $
26 strconcat(const char *s1, const char *s2)
28 static char tmp[FILENAME_MAX];
31 strncpy(tmp, s1 ? s1 : s2, FILENAME_MAX); /* XXX: what if both are NULL? */
33 strncat(tmp, s2, FILENAME_MAX - strlen(tmp));
37 /* Get a string parameter as a file spec or as a "contents follow -" spec */
39 get_dash_string(char **str)
44 *str = copy_string_adds_newline(s + 1);
46 *str = fileGetContents(s);
52 copy_string(const char *str)
54 return (str ? strdup(str) : NULL);
57 /* Rather Obvious but adds a trailing \n newline */
59 copy_string_adds_newline(const char *str)
67 line_length = strlen(str) + 2;
68 if ((copy = malloc(line_length)) == NULL)
70 memcpy(copy, str, line_length - 2);
71 copy[line_length - 2] = '\n'; /* Adds trailing \n */
72 copy[line_length - 1] = '\0';
78 /* Return TRUE if 'str' ends in suffix 'suff' */
80 suffix(const char *str, const char *suff)
85 idx = strrchr(str, '.');
86 if (idx && !strcmp(idx + 1, suff))
91 /* Assuming str has a suffix, brutally murder it! */
93 nuke_suffix(char *str)
97 idx = strrchr(str, '.');
99 *idx = '\0'; /* Yow! Don't try this on a const! */
102 /* Lowercase a whole string */
104 str_lowercase(char *str)
107 *str = tolower(*str);
113 get_string(char *str, int max, FILE *fp)
120 while (fgets(str, max, fp)) {
122 while (len && isspace(str[len - 1]))