*/
#include "includes.h"
-RCSID("$OpenBSD: misc.c,v 1.31 2005/06/06 11:20:36 djm Exp $");
+RCSID("$OpenBSD: misc.c,v 1.35 2005/09/13 23:40:07 djm Exp $");
#include "misc.h"
#include "log.h"
const char *path;
char user[128], ret[MAXPATHLEN];
struct passwd *pw;
- int len;
+ u_int len, slash;
if (*filename != '~')
return (xstrdup(filename));
path = strchr(filename, '/');
if (path != NULL && path > filename) { /* ~user/path */
- if (path - filename > sizeof(user) - 1)
+ slash = path - filename;
+ if (slash > sizeof(user) - 1)
fatal("tilde_expand_filename: ~username too long");
- memcpy(user, filename, path - filename);
- user[path - filename] = '\0';
+ memcpy(user, filename, slash);
+ user[slash] = '\0';
if ((pw = getpwnam(user)) == NULL)
fatal("tilde_expand_filename: No such user %s", user);
} else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
/*
* Expand a string with a set of %[char] escapes. A number of escapes may be
* specified as (char *escape_chars, char *replacement) pairs. The list must
- * be terminated by an escape_char of -1. Returns replaced string in memory
+ * be terminated by a NULL escape_char. Returns replaced string in memory
* allocated by xmalloc.
*/
char *
const char *key;
const char *repl;
} keys[EXPAND_MAX_KEYS];
- int num_keys, i, j;
+ u_int num_keys, i, j;
char buf[4096];
va_list ap;
}
return -1;
}
+
+void
+sanitise_stdfd(void)
+{
+ int nullfd;
+
+ if ((nullfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
+ fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
+ exit(1);
+ }
+ while (nullfd < 2) {
+ if (dup2(nullfd, nullfd + 1) == -1) {
+ fprintf(stderr, "dup2: %s", strerror(errno));
+ exit(1);
+ }
+ nullfd++;
+ }
+ if (nullfd > 2)
+ close(nullfd);
+}
+
+char *
+tohex(const u_char *d, u_int l)
+{
+ char b[3], *r;
+ u_int i, hl;
+
+ hl = l * 2 + 1;
+ r = xmalloc(hl);
+ *r = '\0';
+ for (i = 0; i < l; i++) {
+ snprintf(b, sizeof(b), "%02x", d[i]);
+ strlcat(r, b, hl);
+ }
+ return (r);
+}
+