Files
glibc/string/strfry.c
T

53 lines
1.4 KiB
C
Raw Normal View History

/* Copyright (C) 1992-2018 Free Software Foundation, Inc.
1996-12-04 01:41:39 +00:00
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
1996-12-04 01:41:39 +00:00
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1996-12-04 01:41:39 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1995-02-18 01:27:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
2012-02-09 23:18:22 +00:00
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
1995-02-18 01:27:10 +00:00
#include <string.h>
#include <stdlib.h>
#include <time.h>
2002-03-12 00:38:37 +00:00
#include <unistd.h>
1995-02-18 01:27:10 +00:00
char *
1996-12-04 01:41:39 +00:00
strfry (char *string)
1995-02-18 01:27:10 +00:00
{
1999-04-28 23:13:52 +00:00
static int init;
1996-12-04 01:41:39 +00:00
static struct random_data rdata;
1995-02-18 01:27:10 +00:00
if (!init)
{
2000-04-03 07:15:10 +00:00
static char state[32];
1996-12-04 01:41:39 +00:00
rdata.state = NULL;
2002-03-12 00:38:37 +00:00
__initstate_r (time ((time_t *) NULL) ^ getpid (),
state, sizeof (state), &rdata);
1995-02-18 01:27:10 +00:00
init = 1;
}
2008-03-31 18:30:26 +00:00
size_t len = strlen (string);
if (len > 0)
for (size_t i = 0; i < len - 1; ++i)
{
int32_t j;
__random_r (&rdata, &j);
j = j % (len - i) + i;
1996-12-04 01:41:39 +00:00
2008-03-31 18:30:26 +00:00
char c = string[i];
string[i] = string[j];
string[j] = c;
}
1995-02-18 01:27:10 +00:00
return string;
}