summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2005-06-16 13:18:34 +1000
committerDamien Miller <djm@mindrot.org>2005-06-16 13:18:34 +1000
commit6476cad9bb6b8a9524a153639b4ebceb3427e743 (patch)
tree59e9068c25f8d9c6d7e51f66e3c8c983c52db0d9 /misc.c
parent05656967b111a7c2b1f831eab0c31002dfac6fa9 (diff)
- djm@cvs.openbsd.org 2005/06/06 11:20:36
[auth.c auth.h misc.c misc.h ssh.c ssh_config.5 sshconnect.c] introduce a generic %foo expansion function. replace existing % expansion and add expansion to ControlPath; ok markus@
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c65
1 files changed, 64 insertions, 1 deletions
diff --git a/misc.c b/misc.c
index 4bc07a42a..fc094f874 100644
--- a/misc.c
+++ b/misc.c
@@ -1,5 +1,6 @@
1/* 1/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved. 2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 * Copyright (c) 2005 Damien Miller. All rights reserved.
3 * 4 *
4 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -23,7 +24,7 @@
23 */ 24 */
24 25
25#include "includes.h" 26#include "includes.h"
26RCSID("$OpenBSD: misc.c,v 1.30 2005/04/09 04:32:54 djm Exp $"); 27RCSID("$OpenBSD: misc.c,v 1.31 2005/06/06 11:20:36 djm Exp $");
27 28
28#include "misc.h" 29#include "misc.h"
29#include "log.h" 30#include "log.h"
@@ -421,6 +422,68 @@ tilde_expand_filename(const char *filename, uid_t uid)
421} 422}
422 423
423/* 424/*
425 * Expand a string with a set of %[char] escapes. A number of escapes may be
426 * specified as (char *escape_chars, char *replacement) pairs. The list must
427 * be terminated by an escape_char of -1. Returns replaced string in memory
428 * allocated by xmalloc.
429 */
430char *
431percent_expand(const char *string, ...)
432{
433#define EXPAND_MAX_KEYS 16
434 struct {
435 const char *key;
436 const char *repl;
437 } keys[EXPAND_MAX_KEYS];
438 int num_keys, i, j;
439 char buf[4096];
440 va_list ap;
441
442 /* Gather keys */
443 va_start(ap, string);
444 for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
445 keys[num_keys].key = va_arg(ap, char *);
446 if (keys[num_keys].key == NULL)
447 break;
448 keys[num_keys].repl = va_arg(ap, char *);
449 if (keys[num_keys].repl == NULL)
450 fatal("percent_expand: NULL replacement");
451 }
452 va_end(ap);
453
454 if (num_keys >= EXPAND_MAX_KEYS)
455 fatal("percent_expand: too many keys");
456
457 /* Expand string */
458 *buf = '\0';
459 for (i = 0; *string != '\0'; string++) {
460 if (*string != '%') {
461 append:
462 buf[i++] = *string;
463 if (i >= sizeof(buf))
464 fatal("percent_expand: string too long");
465 buf[i] = '\0';
466 continue;
467 }
468 string++;
469 if (*string == '%')
470 goto append;
471 for (j = 0; j < num_keys; j++) {
472 if (strchr(keys[j].key, *string) != NULL) {
473 i = strlcat(buf, keys[j].repl, sizeof(buf));
474 if (i >= sizeof(buf))
475 fatal("percent_expand: string too long");
476 break;
477 }
478 }
479 if (j >= num_keys)
480 fatal("percent_expand: unknown key %%%c", *string);
481 }
482 return (xstrdup(buf));
483#undef EXPAND_MAX_KEYS
484}
485
486/*
424 * Read an entire line from a public key file into a static buffer, discarding 487 * Read an entire line from a public key file into a static buffer, discarding
425 * lines that exceed the buffer size. Returns 0 on success, -1 on failure. 488 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
426 */ 489 */