annotate utils.c @ 3:bfa97d43197e

support code
author David A. Holland
date Sun, 19 Dec 2010 16:51:57 -0500
parents
children 9dda765ee85c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
1 /*-
bfa97d43197e support code
David A. Holland
parents:
diff changeset
2 * Copyright (c) 2010 The NetBSD Foundation, Inc.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
3 * All rights reserved.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
4 *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
bfa97d43197e support code
David A. Holland
parents:
diff changeset
6 * by David A. Holland.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
7 *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
bfa97d43197e support code
David A. Holland
parents:
diff changeset
9 * modification, are permitted provided that the following conditions
bfa97d43197e support code
David A. Holland
parents:
diff changeset
10 * are met:
bfa97d43197e support code
David A. Holland
parents:
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
bfa97d43197e support code
David A. Holland
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
bfa97d43197e support code
David A. Holland
parents:
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
bfa97d43197e support code
David A. Holland
parents:
diff changeset
15 * documentation and/or other materials provided with the distribution.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
16 *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
bfa97d43197e support code
David A. Holland
parents:
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
bfa97d43197e support code
David A. Holland
parents:
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
bfa97d43197e support code
David A. Holland
parents:
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
bfa97d43197e support code
David A. Holland
parents:
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
bfa97d43197e support code
David A. Holland
parents:
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
bfa97d43197e support code
David A. Holland
parents:
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
bfa97d43197e support code
David A. Holland
parents:
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
bfa97d43197e support code
David A. Holland
parents:
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
bfa97d43197e support code
David A. Holland
parents:
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
28 */
bfa97d43197e support code
David A. Holland
parents:
diff changeset
29
bfa97d43197e support code
David A. Holland
parents:
diff changeset
30 #include <stdlib.h>
bfa97d43197e support code
David A. Holland
parents:
diff changeset
31 #include <string.h>
bfa97d43197e support code
David A. Holland
parents:
diff changeset
32 #include <err.h>
bfa97d43197e support code
David A. Holland
parents:
diff changeset
33
bfa97d43197e support code
David A. Holland
parents:
diff changeset
34 #include "utils.h"
bfa97d43197e support code
David A. Holland
parents:
diff changeset
35
bfa97d43197e support code
David A. Holland
parents:
diff changeset
36 void *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
37 domalloc(size_t len)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
38 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
39 void *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
40
bfa97d43197e support code
David A. Holland
parents:
diff changeset
41 ret = malloc(len);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
42 if (ret == NULL) {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
43 warnx("Out of memory");
bfa97d43197e support code
David A. Holland
parents:
diff changeset
44 die();
bfa97d43197e support code
David A. Holland
parents:
diff changeset
45 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
46 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
47 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
48
bfa97d43197e support code
David A. Holland
parents:
diff changeset
49 void *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
50 dorealloc(void *ptr, size_t len)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
51 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
52 void *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
53
bfa97d43197e support code
David A. Holland
parents:
diff changeset
54 ret = realloc(ptr, len);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
55 if (ret == NULL) {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
56 warnx("Out of memory");
bfa97d43197e support code
David A. Holland
parents:
diff changeset
57 die();
bfa97d43197e support code
David A. Holland
parents:
diff changeset
58 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
59 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
60 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
61
bfa97d43197e support code
David A. Holland
parents:
diff changeset
62 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
63 dostrdup(const char *s)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
64 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
65 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
66 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
67
bfa97d43197e support code
David A. Holland
parents:
diff changeset
68 len = strlen(s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
69 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
70 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
71 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
72 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
73
bfa97d43197e support code
David A. Holland
parents:
diff changeset
74 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
75 dostrdup2(const char *s, const char *t)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
76 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
77 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
78 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
79
bfa97d43197e support code
David A. Holland
parents:
diff changeset
80 len = strlen(s) + strlen(t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
81 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
82 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
83 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
84 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
85 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
86
bfa97d43197e support code
David A. Holland
parents:
diff changeset
87 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
88 dostrdup3(const char *s, const char *t, const char *u)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
89 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
90 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
91 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
92
bfa97d43197e support code
David A. Holland
parents:
diff changeset
93 len = strlen(s) + strlen(t) + strlen(u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
94 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
95 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
96 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
97 strcat(ret, u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
98 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
99 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
100