xref: /trueos/usr.bin/migcom/string.c (revision b6e6246829d5cb1b6c2f704d82f8267a0d3a3362)
1 
2 /*-
3  * Copyright (c) 2014, Matthew Macy <kmacy@FreeBSD.ORG>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 /*
29  * Copyright 1991-1998 by Open Software Foundation, Inc.
30  *              All Rights Reserved
31  *
32  * Permission to use, copy, modify, and distribute this software and
33  * its documentation for any purpose and without fee is hereby granted,
34  * provided that the above copyright notice appears in all copies and
35  * that both the copyright notice and this permission notice appear in
36  * supporting documentation.
37  *
38  * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
39  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
40  * FOR A PARTICULAR PURPOSE.
41  *
42  * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
43  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
44  * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
45  * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
46  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47  */
48 /*
49  * cmk1.1
50  */
51 /*
52  * Mach Operating System
53  * Copyright (c) 1991,1990 Carnegie Mellon University
54  * All Rights Reserved.
55  *
56  * Permission to use, copy, modify and distribute this software and its
57  * documentation is hereby granted, provided that both the copyright
58  * notice and this permission notice appear in all copies of the
59  * software, derivative works or modified versions, and any portions
60  * thereof, and that both notices appear in supporting documentation.
61  *
62  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
63  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
64  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
65  *
66  * Carnegie Mellon requests users of this software to return to
67  *
68  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
69  *  School of Computer Science
70  *  Carnegie Mellon University
71  *  Pittsburgh PA 15213-3890
72  *
73  * any improvements or extensions that they make and grant Carnegie the
74  * rights to redistribute these changes.
75  */
76 /*
77  * 91/02/05  17:55:52  mrt
78  * 	Changed to new Mach copyright
79  * 	[91/02/01  17:55:57  mrt]
80  *
81  * 90/06/02  15:05:46  rpd
82  * 	Created for new IPC.
83  * 	[90/03/26  21:13:46  rpd]
84  *
85  * 07-Apr-89  Richard Draves (rpd) at Carnegie-Mellon University
86  *	Extensive revamping.  Added polymorphic arguments.
87  *	Allow multiple variable-sized inline arguments in messages.
88  *
89  * 15-Jun-87  David Black (dlb) at Carnegie-Mellon University
90  *	Declare and initialize charNULL here for strNull def in string.h
91  *
92  * 27-May-87  Richard Draves (rpd) at Carnegie-Mellon University
93  *	Created.
94  */
95 
96 #include <mach/boolean.h>
97 #include <ctype.h>
98 #include "error.h"
99 #include "alloc.h"
100 #include "strdefs.h"
101 
102 string_t
strmake(const char * string)103 strmake(const char *string)
104 {
105     register char *saved;
106 
107     saved = malloc(strlen(string) + 1);
108     if (saved == strNULL)
109 		fatal("strmake('%s'): %s", string, strerror(errno));
110     return strcpy(saved, string);
111 }
112 
113 string_t
strconcat(left,right)114 strconcat(left, right)
115     string_t left, right;
116 {
117     register char *saved;
118 
119     saved = malloc(strlen(left) + strlen(right) + 1);
120     if (saved == strNULL)
121 		fatal("strconcat('%s', '%s'): %s",
122 			  left, right, strerror(errno));
123     return strcat(strcpy(saved, left), right);
124 }
125 
126 string_t
strphrase(left,right)127 strphrase(left, right)
128     string_t left, right;
129 {
130     char *saved;
131     char *current;
132     size_t llen;
133 
134     llen = strlen(left);
135     saved = malloc(llen + strlen(right) + 2);
136     if (saved == strNULL)
137 		fatal("strphrase('%s', '%s'): %s",
138 			  left, right, strerror(errno));
139     strcpy(saved, left);
140     current = saved + llen;
141     *(current++) = ' ';
142     strcpy(current, right);
143     free(__DECONST(char *, left));
144     return(saved);
145 }
146 
147 void
strfree(string_t string)148 strfree(string_t string)
149 {
150     free(__DECONST(char *,string));
151 }
152 
153 const char *
154 strbool(bool)
155     boolean_t bool;
156 {
157     if (bool)
158 		return "TRUE";
159     else
160 		return "FALSE";
161 }
162 
163 const char *
strstring(string)164 strstring(string)
165     string_t string;
166 {
167     if (string == strNULL)
168 		return "NULL";
169     else
170 		return string;
171 }
172 
173 char *
toupperstr(char * p)174 toupperstr(char *p)
175 {
176     register char *s = p;
177     char c;
178 
179     while ((c = *s)) {
180         if (islower(c))
181 			*s = toupper(c);
182         s++;
183     }
184     return(p);
185 }
186