]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/pkg_install/lib/str.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / pkg_install / lib / str.c
1 /*
2  * FreeBSD install - a package for the installation and maintenance
3  * of non-core utilities.
4  *
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.
13  *
14  * Jordan K. Hubbard
15  * 18 July 1993
16  *
17  * Miscellaneous string utilities.
18  *
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include "lib.h"
25
26 char *
27 strconcat(const char *s1, const char *s2)
28 {
29     static char tmp[FILENAME_MAX];
30
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;
36 }
37
38 /* Get a string parameter as a file spec or as a "contents follow -" spec */
39 char *
40 get_dash_string(char **str)
41 {
42     char *s = *str;
43
44     if (*s == '-')
45         *str = copy_string_adds_newline(s + 1);
46     else
47         *str = fileGetContents(s);
48     return *str;
49 }
50
51 /* Rather Obvious */
52 char *
53 copy_string(const char *str)
54 {
55     return (str ? strdup(str) : NULL);
56 }
57
58 /* Rather Obvious but adds a trailing \n newline */
59 char *
60 copy_string_adds_newline(const char *str)
61 {
62     if (str == NULL) {
63         return (NULL);
64     } else  {
65         char *copy;
66         size_t line_length;
67
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';
74
75         return (copy);
76    }
77 }
78
79 /* Return TRUE if 'str' ends in suffix 'suff' */
80 Boolean
81 suffix(const char *str, const char *suff)
82 {
83     char *idx;
84     Boolean ret = FALSE;
85
86     idx = strrchr(str, '.');
87     if (idx && !strcmp(idx + 1, suff))
88         ret = TRUE;
89     return ret;
90 }
91
92 /* Assuming str has a suffix, brutally murder it! */
93 void
94 nuke_suffix(char *str)
95 {
96     char *idx;
97
98     idx = strrchr(str, '.');
99     if (idx)
100         *idx = '\0';  /* Yow!  Don't try this on a const! */
101 }
102
103 /* Lowercase a whole string */
104 void
105 str_lowercase(char *str)
106 {
107     while (*str) {
108         *str = tolower(*str);
109         ++str;
110     }
111 }
112
113 char *
114 get_string(char *str, int max, FILE *fp)
115 {
116     int len;
117
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;
127     }
128     return NULL;
129 }