]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pkg_install/lib/str.c
This commit was generated by cvs2svn to compensate for changes in r80060,
[FreeBSD/FreeBSD.git] / usr.sbin / pkg_install / lib / str.c
1 #ifndef lint
2 static const char rcsid[] =
3   "$FreeBSD$";
4 #endif
5
6 /*
7  * FreeBSD install - a package for the installation and maintainance
8  * of non-core utilities.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * Jordan K. Hubbard
20  * 18 July 1993
21  *
22  * Miscellaneous string utilities.
23  *
24  */
25
26 #include "lib.h"
27
28 /* Return the filename portion of a path */
29 char *
30 basename_of(char *str)
31 {
32     char *basename = str + strlen(str) - 1;
33
34     while (basename != str && basename[-1] != '/')
35         --basename;
36     return basename;
37 }
38
39 char *
40 strconcat(char *s1, char *s2)
41 {
42     static char tmp[FILENAME_MAX];
43
44     tmp[0] = '\0';
45     strncpy(tmp, s1 ? s1 : s2, FILENAME_MAX);
46     if (s1 && s2)
47         strncat(tmp, s2, FILENAME_MAX - strlen(tmp));
48     return tmp;
49 }
50
51 /* Get a string parameter as a file spec or as a "contents follow -" spec */
52 char *
53 get_dash_string(char **str)
54 {
55     char *s = *str;
56
57     if (*s == '-')
58         *str = copy_string(s + 1);
59     else
60         *str = fileGetContents(s);
61     return *str;
62 }
63
64 /* Do a strlcpy and test for overflow */
65 int
66 s_strlcpy(char *dst, const char *src, size_t size)
67 {
68         return (strlcpy(dst, src, size) >= size);
69 }
70
71 /* Do a strlcat and test for overflow */
72 int
73 s_strlcat(char *dst, const char *src, size_t size)
74 {
75         return (strlcat(dst, src, size) >= size);
76 }
77
78 /* Rather Obvious */
79 char *
80 copy_string(char *str)
81 {
82     char *ret;
83
84     if (!str)
85         ret = NULL;
86     else {
87         ret = (char *)malloc(strlen(str) + 1);
88         strcpy(ret, str);
89     }
90     return ret;
91 }
92
93 /* Return TRUE if 'str' ends in suffix 'suff' */
94 Boolean
95 suffix(char *str, char *suff)
96 {
97     char *idx;
98     Boolean ret = FALSE;
99
100     idx = strrchr(str, '.');
101     if (idx && !strcmp(idx + 1, suff))
102         ret = TRUE;
103     return ret;
104 }
105
106 /* Assuming str has a suffix, brutally murder it! */
107 void
108 nuke_suffix(char *str)
109 {
110     char *idx;
111
112     idx = strrchr(str, '.');
113     if (idx)
114         *idx = '\0';  /* Yow!  Don't try this on a const! */
115 }
116
117 /* Lowercase a whole string */
118 void
119 str_lowercase(char *str)
120 {
121     while (*str) {
122         *str = tolower(*str);
123         ++str;
124     }
125 }
126
127 char *
128 get_string(char *str, int max, FILE *fp)
129 {
130     int len;
131
132     if (!str)
133         return NULL;
134     str[0] = '\0';
135     while (fgets(str, max, fp)) {
136         len = strlen(str);
137         while (len && isspace(str[len - 1]))
138             str[--len] = '\0';
139         if (len)
140            return str;
141     }
142     return NULL;
143 }
144