1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Move code from jid_random_resource() into own function

Move the code that creates a random string into it's own function
+get_random_string().
This commit is contained in:
Michael Vetter 2019-10-16 10:39:35 +02:00
parent 1e09a055ca
commit f9eb302a59
3 changed files with 26 additions and 12 deletions

View File

@ -489,3 +489,23 @@ get_file_paths_recursive(const char *path, GSList **contents)
entry = g_dir_read_name(directory); entry = g_dir_read_name(directory);
} }
} }
char*
get_random_string(int length)
{
GRand *prng;
char *rand;
char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
rand = calloc(length+1, sizeof(char));
prng = g_rand_new();
int i;
for (i = 0; i < length; i++) {
rand[i] = alphabet[g_rand_int_range(prng, 0, sizeof(alphabet))];
}
g_rand_free(prng);
return rand;
}

View File

@ -103,4 +103,6 @@ int is_regular_file(const char *path);
int is_dir(const char *path); int is_dir(const char *path);
void get_file_paths_recursive(const char *directory, GSList **contents); void get_file_paths_recursive(const char *directory, GSList **contents);
char* get_random_string(int length);
#endif #endif

View File

@ -198,18 +198,10 @@ jid_fulljid_or_barejid(Jid *jid)
char* char*
jid_random_resource(void) jid_random_resource(void)
{ {
GRand *prng; char *rand = get_random_string(4);
char rand[5];
char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
prng = g_rand_new(); gchar *result = g_strdup_printf("profanity.%s", rand);
free(rand);
int i; return result;
for (i = 0; i < 4; i++) {
rand[i] = alphabet[g_rand_int_range(prng, 0, sizeof(alphabet))];
}
rand[4] = '\0';
g_rand_free(prng);
return g_strdup_printf("profanity.%s", rand);
} }