]> 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 r75107,
[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 /* Rather Obvious */
65 char *
66 copy_string(char *str)
67 {
68     char *ret;
69
70     if (!str)
71         ret = NULL;
72     else {
73         ret = (char *)malloc(strlen(str) + 1);
74         strcpy(ret, str);
75     }
76     return ret;
77 }
78
79 /* Return TRUE if 'str' ends in suffix 'suff' */
80 Boolean
81 suffix(char *str, 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 }
130