Files
glibc/string/strfry.c
T

56 lines
1.4 KiB
C
Raw Normal View History

2002-03-12 00:38:37 +00:00
/* Copyright (C) 1992, 1996, 1999, 2002 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
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
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
size_t len, i;
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;
}
len = strlen (string);
for (i = 0; i < len; ++i)
{
1996-12-04 01:41:39 +00:00
int32_t j;
char c;
__random_r (&rdata, &j);
j %= len;
c = string[i];
1995-02-18 01:27:10 +00:00
string[i] = string[j];
string[j] = c;
}
return string;
}