2
* Copyright (c) 2000 Markus Friedl. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
/*RCSID("$OpenBSD: misc.c,v 1.22 2003/09/18 08:49:45 markus Exp $");*/
27
/* For xmalloc, xfree etc:
28
* Author: Tatu Ylonen <ylo@cs.hut.fi>
29
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
31
* Versions of malloc and friends that check their results, and never return
32
* failure (they call fatal if they encounter an error).
34
* As far as I am concerned, the code I have written for this software
35
* can be used freely for any purpose. Any derived versions of this
36
* software must be clearly marked as such, and if the derived work is
37
* incompatible with the protocol description in the RFC file, it must be
38
* called by a name other than "ssh" or "Secure Shell".
41
/*RCSID("$OpenBSD: xmalloc.c,v 1.16 2001/07/23 18:21:46 stevesk Exp $");*/
52
fprintf(stderr, "xmalloc: zero size\n");
57
fprintf(stderr, "xmalloc: out of memory (allocating %lu bytes)\n", (u_long) size);
64
xrealloc(void *ptr, size_t new_size)
69
fprintf(stderr, "xrealloc: zero size\n");
73
new_ptr = malloc(new_size);
75
new_ptr = realloc(ptr, new_size);
76
if (new_ptr == NULL) {
77
fprintf(stderr, "xrealloc: out of memory (new_size %lu bytes)\n", (u_long) new_size);
87
fprintf(stderr, "xfree: NULL pointer given as argument\n");
94
xstrdup(const char *str)
99
len = strlen(str) + 1;
101
strncpy(cp, str, len);
106
cleanhostname(char *host)
108
if (*host == '[' && host[strlen(host) - 1] == ']') {
109
host[strlen(host) - 1] = '\0';
120
if (*cp == ':') /* Leading colon is part of file name. */
126
if (*cp == '@' && *(cp+1) == '[')
128
if (*cp == ']' && *(cp+1) == ':' && flag)
130
if (*cp == ':' && !flag)
138
/* function to assist building execv() arguments */
140
addargs(arglist *args, char *fmt, ...)
147
vsnprintf(buf, sizeof(buf), fmt, ap);
150
nalloc = args->nalloc;
151
if (args->list == NULL) {
154
} else if (args->num+2 >= nalloc)
157
args->list = xrealloc(args->list, nalloc * sizeof(char *));
158
args->nalloc = nalloc;
159
args->list[args->num++] = xstrdup(buf);
160
args->list[args->num] = NULL;