turns printfs back on
[freebsd-src/fkvm-freebsd.git] / usr.sbin / pkg_install / lib / str.c
blob0d9e288b75794f467eb7fce7d4a79ddefc8280ae
1 /*
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
7 * are met:
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.
14 * Jordan K. Hubbard
15 * 18 July 1993
17 * Miscellaneous string utilities.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include "lib.h"
26 char *
27 strconcat(const char *s1, const char *s2)
29 static char tmp[FILENAME_MAX];
31 tmp[0] = '\0';
32 strncpy(tmp, s1 ? s1 : s2, FILENAME_MAX); /* XXX: what if both are NULL? */
33 if (s1 && s2)
34 strncat(tmp, s2, FILENAME_MAX - strlen(tmp));
35 return tmp;
38 /* Get a string parameter as a file spec or as a "contents follow -" spec */
39 char *
40 get_dash_string(char **str)
42 char *s = *str;
44 if (*s == '-')
45 *str = copy_string_adds_newline(s + 1);
46 else
47 *str = fileGetContents(s);
48 return *str;
51 /* Rather Obvious */
52 char *
53 copy_string(const char *str)
55 return (str ? strdup(str) : NULL);
58 /* Rather Obvious but adds a trailing \n newline */
59 char *
60 copy_string_adds_newline(const char *str)
62 if (str == NULL) {
63 return (NULL);
64 } else {
65 char *copy;
66 size_t line_length;
68 line_length = strlen(str) + 2;
69 if ((copy = malloc(line_length)) == NULL)
70 return (NULL);
71 memcpy(copy, str, line_length - 2);
72 copy[line_length - 2] = '\n'; /* Adds trailing \n */
73 copy[line_length - 1] = '\0';
75 return (copy);
79 /* Return TRUE if 'str' ends in suffix 'suff' */
80 Boolean
81 suffix(const char *str, const char *suff)
83 char *idx;
84 Boolean ret = FALSE;
86 idx = strrchr(str, '.');
87 if (idx && !strcmp(idx + 1, suff))
88 ret = TRUE;
89 return ret;
92 /* Assuming str has a suffix, brutally murder it! */
93 void
94 nuke_suffix(char *str)
96 char *idx;
98 idx = strrchr(str, '.');
99 if (idx)
100 *idx = '\0'; /* Yow! Don't try this on a const! */
103 /* Lowercase a whole string */
104 void
105 str_lowercase(char *str)
107 while (*str) {
108 *str = tolower(*str);
109 ++str;
113 char *
114 get_string(char *str, int max, FILE *fp)
116 int len;
118 if (!str)
119 return NULL;
120 str[0] = '\0';
121 while (fgets(str, max, fp)) {
122 len = strlen(str);
123 while (len && isspace(str[len - 1]))
124 str[--len] = '\0';
125 if (len)
126 return str;
128 return NULL;