comparison scpmisc.c @ 4:fe6bca95afa7

Makefile.in contains updated files required
author Matt Johnston <matt@ucc.asn.au>
date Tue, 01 Jun 2004 02:46:09 +0000
parents
children 5c6f9d27ea1c
comparison
equal deleted inserted replaced
-1:000000000000 4:fe6bca95afa7
1 /*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
23 */
24
25 /*RCSID("$OpenBSD: misc.c,v 1.22 2003/09/18 08:49:45 markus Exp $");*/
26
27 /* For xmalloc, xfree etc:
28 * Author: Tatu Ylonen <[email protected]>
29 * Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
30 * All rights reserved
31 * Versions of malloc and friends that check their results, and never return
32 * failure (they call fatal if they encounter an error).
33 *
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".
39 */
40
41 /*RCSID("$OpenBSD: xmalloc.c,v 1.16 2001/07/23 18:21:46 stevesk Exp $");*/
42
43 #include "includes.h"
44 #include "scpmisc.h"
45
46 void *
47 xmalloc(size_t size)
48 {
49 void *ptr;
50
51 if (size == 0) {
52 fprintf(stderr, "xmalloc: zero size\n");
53 exit(EXIT_FAILURE);
54 }
55 ptr = malloc(size);
56 if (ptr == NULL) {
57 fprintf(stderr, "xmalloc: out of memory (allocating %lu bytes)\n", (u_long) size);
58 exit(EXIT_FAILURE);
59 }
60 return ptr;
61 }
62
63 void *
64 xrealloc(void *ptr, size_t new_size)
65 {
66 void *new_ptr;
67
68 if (new_size == 0) {
69 fprintf(stderr, "xrealloc: zero size\n");
70 exit(EXIT_FAILURE);
71 }
72 if (ptr == NULL)
73 new_ptr = malloc(new_size);
74 else
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);
78 exit(EXIT_FAILURE);
79 }
80 return new_ptr;
81 }
82
83 void
84 xfree(void *ptr)
85 {
86 if (ptr == NULL) {
87 fprintf(stderr, "xfree: NULL pointer given as argument\n");
88 exit(EXIT_FAILURE);
89 }
90 free(ptr);
91 }
92
93 char *
94 xstrdup(const char *str)
95 {
96 size_t len;
97 char *cp;
98
99 len = strlen(str) + 1;
100 cp = xmalloc(len);
101 strncpy(cp, str, len);
102 return cp;
103 }
104
105 char *
106 cleanhostname(char *host)
107 {
108 if (*host == '[' && host[strlen(host) - 1] == ']') {
109 host[strlen(host) - 1] = '\0';
110 return (host + 1);
111 } else
112 return host;
113 }
114
115 char *
116 colon(char *cp)
117 {
118 int flag = 0;
119
120 if (*cp == ':') /* Leading colon is part of file name. */
121 return (0);
122 if (*cp == '[')
123 flag = 1;
124
125 for (; *cp; ++cp) {
126 if (*cp == '@' && *(cp+1) == '[')
127 flag = 1;
128 if (*cp == ']' && *(cp+1) == ':' && flag)
129 return (cp+1);
130 if (*cp == ':' && !flag)
131 return (cp);
132 if (*cp == '/')
133 return (0);
134 }
135 return (0);
136 }
137
138 /* function to assist building execv() arguments */
139 void
140 addargs(arglist *args, char *fmt, ...)
141 {
142 va_list ap;
143 char buf[1024];
144 int nalloc;
145
146 va_start(ap, fmt);
147 vsnprintf(buf, sizeof(buf), fmt, ap);
148 va_end(ap);
149
150 nalloc = args->nalloc;
151 if (args->list == NULL) {
152 nalloc = 32;
153 args->num = 0;
154 } else if (args->num+2 >= nalloc)
155 nalloc *= 2;
156
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;
161 }