* Adrian Hunter
*/
+#include <sys/time.h>
+#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
+#include <unistd.h>
#include "common.h"
/**
++p;
}
}
+
+/**
+ * ubiutils_srand - randomly seed the standard pseudo-random generator.
+ *
+ * This helper function seeds the standard libc pseudo-random generator with a
+ * more or less random value to make sure the 'rand()' call does not return the
+ * same sequence every time UBI utilities run. Returns zero in case of success
+ * and a %-1 in case of error.
+ */
+int ubiutils_srand(void)
+{
+ struct timeval tv;
+ struct timezone tz;
+ unsigned int seed;
+
+ /*
+ * Just assume that a combination of the PID + current time is a
+ * reasonably random number.
+ */
+ if (gettimeofday(&tv, &tz))
+ return -1;
+
+ seed = (unsigned int)tv.tv_sec;
+ seed += (unsigned int)tv.tv_usec;
+ seed *= getpid();
+ seed %= RAND_MAX;
+ srand(seed);
+ return 0;
+}
long long ubiutils_get_bytes(const char *str);
void ubiutils_print_bytes(long long bytes, int bracket);
void ubiutils_print_text(FILE *stream, const char *txt, int len);
+int ubiutils_srand(void);
#ifdef __cplusplus
}
*/
#define MAX_CONSECUTIVE_BAD_BLOCKS 4
-#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
static int parse_opt(int argc, char * const argv[])
{
- srand(getpid());
- args.image_seq = random();
+ ubiutils_srand();
+ args.image_seq = rand();
while (1) {
int key;
* Oliver Lohmann
*/
-#include <sys/stat.h>
-#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
-#include <unistd.h>
#include <fcntl.h>
#include <mtd/ubi-media.h>
static int parse_opt(int argc, char * const argv[])
{
- srand(getpid());
- args.image_seq = random();
+ ubiutils_srand();
+ args.image_seq = rand();
while (1) {
int key;