comparison scpmisc.c @ 287:b72f98803e46

Update to scp from OpenSSH portable 4.3p2
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 14:20:24 +0000
parents 5c6f9d27ea1c
children 973fccb59ea4 49f68a7b7a55
comparison
equal deleted inserted replaced
286:92cc938b59a2 287:b72f98803e46
138 /* function to assist building execv() arguments */ 138 /* function to assist building execv() arguments */
139 void 139 void
140 addargs(arglist *args, char *fmt, ...) 140 addargs(arglist *args, char *fmt, ...)
141 { 141 {
142 va_list ap; 142 va_list ap;
143 char buf[1024]; 143 char *cp;
144 int nalloc; 144 u_int nalloc;
145 int r;
145 146
146 va_start(ap, fmt); 147 va_start(ap, fmt);
147 vsnprintf(buf, sizeof(buf), fmt, ap); 148 r = vasprintf(&cp, fmt, ap);
148 va_end(ap); 149 va_end(ap);
150 if (r == -1)
151 fatal("addargs: argument too long");
149 152
150 nalloc = args->nalloc; 153 nalloc = args->nalloc;
151 if (args->list == NULL) { 154 if (args->list == NULL) {
152 nalloc = 32; 155 nalloc = 32;
153 args->num = 0; 156 args->num = 0;
154 } else if (args->num+2 >= nalloc) 157 } else if (args->num+2 >= nalloc)
155 nalloc *= 2; 158 nalloc *= 2;
156 159
157 args->list = xrealloc(args->list, nalloc * sizeof(char *)); 160 args->list = xrealloc(args->list, nalloc * sizeof(char *));
158 args->nalloc = nalloc; 161 args->nalloc = nalloc;
159 args->list[args->num++] = xstrdup(buf); 162 args->list[args->num++] = cp;
160 args->list[args->num] = NULL; 163 args->list[args->num] = NULL;
161 } 164 }
165
166 void
167 replacearg(arglist *args, u_int which, char *fmt, ...)
168 {
169 va_list ap;
170 char *cp;
171 int r;
172
173 va_start(ap, fmt);
174 r = vasprintf(&cp, fmt, ap);
175 va_end(ap);
176 if (r == -1)
177 fatal("replacearg: argument too long");
178
179 if (which >= args->num)
180 fatal("replacearg: tried to replace invalid arg %d >= %d",
181 which, args->num);
182 xfree(args->list[which]);
183 args->list[which] = cp;
184 }
185
186 void
187 freeargs(arglist *args)
188 {
189 u_int i;
190
191 if (args->list != NULL) {
192 for (i = 0; i < args->num; i++)
193 xfree(args->list[i]);
194 xfree(args->list);
195 args->nalloc = args->num = 0;
196 args->list = NULL;
197 }
198 }
199
200 /*
201 * NB. duplicate __progname in case it is an alias for argv[0]
202 * Otherwise it may get clobbered by setproctitle()
203 */
204 char *ssh_get_progname(char *argv0)
205 {
206 char *p;
207
208 if (argv0 == NULL)
209 return ("unknown"); /* XXX */
210 p = strrchr(argv0, '/');
211 if (p == NULL)
212 p = argv0;
213 else
214 p++;
215
216 return (xstrdup(p));
217 }
218
219 void fatal(char* fmt,...)
220 {
221 va_list args;
222 va_start(args, fmt);
223 vfprintf(stderr, fmt, args);
224 va_end(args);
225 exit(255);
226 }
227
228 void
229 sanitise_stdfd(void)
230 {
231 int nullfd, dupfd;
232
233 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
234 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
235 exit(1);
236 }
237 while (++dupfd <= 2) {
238 /* Only clobber closed fds */
239 if (fcntl(dupfd, F_GETFL, 0) >= 0)
240 continue;
241 if (dup2(nullfd, dupfd) == -1) {
242 fprintf(stderr, "dup2: %s", strerror(errno));
243 exit(1);
244 }
245 }
246 if (nullfd > 2)
247 close(nullfd);
248 }