1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-11-16 10:44:48 +00:00
ympd/src/global.c

52 lines
1.7 KiB
C
Raw Normal View History

/* myMPD
2019-01-05 00:01:34 +00:00
(c) 2018-2019 Juergen Mang <mail@jcgames.de>
This project's homepage is: https://github.com/jcorporation/mympd
myMPD ist fork of:
ympd
(c) 2013-2014 Andrew Karpow <andy@ndyk.de>
This project's homepage is: http://www.ympd.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; version 2 of the License.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include "global.h"
2019-01-06 09:36:34 +00:00
int randrange(int n) {
return rand() / (RAND_MAX / (n + 1) + 1);
}
void sanitize_string(const char *data) {
static char ok_chars[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890_-. ";
char *cp = data;
const char *end = data + strlen(data);
for (cp += strspn(cp, ok_chars); cp != end; cp += strspn(cp, ok_chars))
*cp = '_';
}
2019-01-05 00:01:34 +00:00
int copy_string(char * const dest, char const * const src, size_t const dst_len, size_t const src_len) {
if (dst_len == 0 || src_len == 0)
return 0;
2019-01-05 00:01:34 +00:00
size_t const max = (src_len < dst_len) ? src_len : dst_len -1;
memcpy(dest, src, max);
dest[max] = '\0';
return max;
}