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
, ...)
148 r
= vasprintf(&cp
, fmt
, ap
);
151 fatal("addargs: argument too long");
153 nalloc
= args
->nalloc
;
154 if (args
->list
== NULL
) {
157 } else if (args
->num
+2 >= nalloc
)
160 args
->list
= xrealloc(args
->list
, nalloc
* sizeof(char *));
161 args
->nalloc
= nalloc
;
162 args
->list
[args
->num
++] = cp
;
163 args
->list
[args
->num
] = NULL
;
167 replacearg(arglist
*args
, u_int which
, char *fmt
, ...)
174 r
= vasprintf(&cp
, fmt
, ap
);
177 fatal("replacearg: argument too long");
179 if (which
>= args
->num
)
180 fatal("replacearg: tried to replace invalid arg %d >= %d",
182 xfree(args
->list
[which
]);
183 args
->list
[which
] = cp
;
187 freeargs(arglist
*args
)
191 if (args
->list
!= NULL
) {
192 for (i
= 0; i
< args
->num
; i
++)
193 xfree(args
->list
[i
]);
195 args
->nalloc
= args
->num
= 0;
201 * NB. duplicate __progname in case it is an alias for argv[0]
202 * Otherwise it may get clobbered by setproctitle()
204 char *ssh_get_progname(char *argv0
)
209 return ("unknown"); /* XXX */
210 p
= strrchr(argv0
, '/');
219 void fatal(char* fmt
,...)
223 vfprintf(stderr
, fmt
, args
);
233 if ((nullfd
= dupfd
= open(_PATH_DEVNULL
, O_RDWR
)) == -1) {
234 fprintf(stderr
, "Couldn't open /dev/null: %s", strerror(errno
));
237 while (++dupfd
<= 2) {
238 /* Only clobber closed fds */
239 if (fcntl(dupfd
, F_GETFL
, 0) >= 0)
241 if (dup2(nullfd
, dupfd
) == -1) {
242 fprintf(stderr
, "dup2: %s", strerror(errno
));