]> 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 r90908,
[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 char *
29 strconcat(const char *s1, const char *s2)
30 {
31     static char tmp[FILENAME_MAX];
32
33     tmp[0] = '\0';
34     strncpy(tmp, s1 ? s1 : s2, FILENAME_MAX);  /* XXX: what if both are NULL? */
35     if (s1 && s2)
36         strncat(tmp, s2, FILENAME_MAX - strlen(tmp));
37     return tmp;
38 }
39
40 /* Get a string parameter as a file spec or as a "contents follow -" spec */
41 char *
42 get_dash_string(char **str)
43 {
44     char *s = *str;
45
46     if (*s == '-')
47         *str = copy_string(s + 1);
48     else
49         *str = fileGetContents(s);
50     return *str;
51 }
52
53 /* Rather Obvious */
54 char *
55 copy_string(const char *str)
56 {
57     return (str ? strdup(str) : NULL);
58 }
59
60 /* Return TRUE if 'str' ends in suffix 'suff' */
61 Boolean
62 suffix(const char *str, const char *suff)
63 {
64     char *idx;
65     Boolean ret = FALSE;
66
67     idx = strrchr(str, '.');
68     if (idx && !strcmp(idx + 1, suff))
69         ret = TRUE;
70     return ret;
71 }
72
73 /* Assuming str has a suffix, brutally murder it! */
74 void
75 nuke_suffix(char *str)
76 {
77     char *idx;
78
79     idx = strrchr(str, '.');
80     if (idx)
81         *idx = '\0';  /* Yow!  Don't try this on a const! */
82 }
83
84 /* Lowercase a whole string */
85 void
86 str_lowercase(char *str)
87 {
88     while (*str) {
89         *str = tolower(*str);
90         ++str;
91     }
92 }
93
94 char *
95 get_string(char *str, int max, FILE *fp)
96 {
97     int len;
98
99     if (!str)
100         return NULL;
101     str[0] = '\0';
102     while (fgets(str, max, fp)) {
103         len = strlen(str);
104         while (len && isspace(str[len - 1]))
105             str[--len] = '\0';
106         if (len)
107            return str;
108     }
109     return NULL;
110 }