12c54bbb6459b4012fca8b833dbdb8f0061a43bd
[dragonfly.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  * $FreeBSD: src/usr.sbin/pkg_install/lib/str.c,v 1.6.2.6 2003/06/09 17:02:31 lioux Exp $
20  * $DragonFly: src/usr.sbin/pkg_install/lib/Attic/str.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
21  */
22
23 #include "lib.h"
24
25 char *
26 strconcat(const char *s1, const char *s2)
27 {
28     static char tmp[FILENAME_MAX];
29
30     tmp[0] = '\0';
31     strncpy(tmp, s1 ? s1 : s2, FILENAME_MAX);  /* XXX: what if both are NULL? */
32     if (s1 && s2)
33         strncat(tmp, s2, FILENAME_MAX - strlen(tmp));
34     return tmp;
35 }
36
37 /* Get a string parameter as a file spec or as a "contents follow -" spec */
38 char *
39 get_dash_string(char **str)
40 {
41     char *s = *str;
42
43     if (*s == '-')
44         *str = copy_string_plus_newline(s + 1);
45     else
46         *str = fileGetContents(s);
47     return *str;
48 }
49
50 /* Rather Obvious */
51 char *
52 copy_string(const char *str)
53 {
54     return (str ? strdup(str) : NULL);
55 }
56
57 /* Rather Obvious but adds a trailing \n newline */
58 char *
59 copy_string_plus_newline(const char *str)
60 {
61     if (str == NULL) {
62         return (NULL);
63     } else  {
64         char *copy;
65         size_t line_length;
66
67         line_length = strlen(str) + 2;
68         if ((copy = malloc(line_length)) == NULL)
69                 return (NULL);
70         memcpy(copy, str, line_length - 2);
71         copy[line_length - 2] = '\n';   /* Adds trailing \n */
72         copy[line_length - 1] = '\0';
73
74         return (copy);
75    }
76 }
77
78 /* Return TRUE if 'str' ends in suffix 'suff' */
79 Boolean
80 suffix(const char *str, const char *suff)
81 {
82     char *idx;
83     Boolean ret = FALSE;
84
85     idx = strrchr(str, '.');
86     if (idx && !strcmp(idx + 1, suff))
87         ret = TRUE;
88     return ret;
89 }
90
91 /* Assuming str has a suffix, brutally murder it! */
92 void
93 nuke_suffix(char *str)
94 {
95     char *idx;
96
97     idx = strrchr(str, '.');
98     if (idx)
99         *idx = '\0';  /* Yow!  Don't try this on a const! */
100 }
101
102 /* Lowercase a whole string */
103 void
104 str_lowercase(char *str)
105 {
106     while (*str) {
107         *str = tolower(*str);
108         ++str;
109     }
110 }
111
112 char *
113 get_string(char *str, int max, FILE *fp)
114 {
115     int len;
116
117     if (!str)
118         return NULL;
119     str[0] = '\0';
120     while (fgets(str, max, fp)) {
121         len = strlen(str);
122         while (len && isspace(str[len - 1]))
123             str[--len] = '\0';
124         if (len)
125            return str;
126     }
127     return NULL;
128 }