aboutsummaryrefslogtreecommitdiff
path: root/dpid/misc_new.c
diff options
context:
space:
mode:
authorJohannes Hofmann <Johannes.Hofmann@gmx.de>2010-08-20 23:24:19 +0200
committerJohannes Hofmann <Johannes.Hofmann@gmx.de>2010-08-20 23:24:19 +0200
commitf5c598b518d1f906148534d015f50075d3e8242d (patch)
tree21dd70add5b366c3dd80641b77f6b18e0baa009e /dpid/misc_new.c
parente98d02a01ffeb18ede86af025e51ae1ec011c75a (diff)
parent5f0fc0e48b8cbee7e1795935da0abff6627fd498 (diff)
merge
Diffstat (limited to 'dpid/misc_new.c')
-rw-r--r--dpid/misc_new.c55
1 files changed, 42 insertions, 13 deletions
diff --git a/dpid/misc_new.c b/dpid/misc_new.c
index f85f43e4..7f963aed 100644
--- a/dpid/misc_new.c
+++ b/dpid/misc_new.c
@@ -1,17 +1,23 @@
-#include <stdio.h>
-#include <time.h>
-#include <stdlib.h>
-#include <string.h>
+/*
+ * File: misc_new.c
+ *
+ * Copyright 2008 Jorge Arellano Cid <jcid@dillo.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <errno.h> /* errno, err-codes */
#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <fcntl.h>
-#include "d_size.h"
-#include "misc_new.h"
-#include "dpid_common.h"
+#include <time.h>
+#include <sys/stat.h> /* stat */
+#include <stdlib.h> /* rand, srand */
-#include "misc_new.h" /* for function prototypes */
+#include "../dlib/dlib.h"
+#include "dpid_common.h"
+#include "misc_new.h" /* for function prototypes */
/*
@@ -159,7 +165,7 @@ char *a_Misc_mkfname(char *template)
{
char *tmp = template + strlen(template) - 6;
int i;
- unsigned int random;
+ uint_t random;
struct stat stat_buf;
if (tmp < template)
@@ -185,3 +191,26 @@ char *a_Misc_mkfname(char *template)
MSG_ERR("a_Misc_mkfname: another round for %s \n", template);
}
}
+
+/*
+ * Return a new, random hexadecimal string of 'nchar' characters.
+ */
+char *a_Misc_mksecret(int nchar)
+{
+ int i;
+ uint_t random;
+ char *secret = dNew(char, nchar + 1);
+
+ srand((uint_t)(time(0) ^ getpid()));
+ random = (unsigned) rand();
+ for (i = 0; i < nchar; ++i) {
+ int hexdigit = (random >> (i * 5)) & 0x0f;
+
+ secret[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
+ }
+ secret[i] = 0;
+ MSG("a_Misc_mksecret: %s\n", secret);
+
+ return secret;
+}
+