]> 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 r93982,
[FreeBSD/FreeBSD.git] / usr.sbin / pkg_install / lib / str.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
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(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 /* Return TRUE if 'str' ends in suffix 'suff' */
59 Boolean
60 suffix(const char *str, const char *suff)
61 {
62     char *idx;
63     Boolean ret = FALSE;
64
65     idx = strrchr(str, '.');
66     if (idx && !strcmp(idx + 1, suff))
67         ret = TRUE;
68     return ret;
69 }
70
71 /* Assuming str has a suffix, brutally murder it! */
72 void
73 nuke_suffix(char *str)
74 {
75     char *idx;
76
77     idx = strrchr(str, '.');
78     if (idx)
79         *idx = '\0';  /* Yow!  Don't try this on a const! */
80 }
81
82 /* Lowercase a whole string */
83 void
84 str_lowercase(char *str)
85 {
86     while (*str) {
87         *str = tolower(*str);
88         ++str;
89     }
90 }
91
92 char *
93 get_string(char *str, int max, FILE *fp)
94 {
95     int len;
96
97     if (!str)
98         return NULL;
99     str[0] = '\0';
100     while (fgets(str, max, fp)) {
101         len = strlen(str);
102         while (len && isspace(str[len - 1]))
103             str[--len] = '\0';
104         if (len)
105            return str;
106     }
107     return NULL;
108 }