icwmp/dm/dmcommon.c
Anis Ellouze 6775878f54 use strtok_r instead of strtok.
strtok_r is more secure
2015-09-04 10:06:05 +01:00

123 lines
2.5 KiB
C

/*
* 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, either version 2 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2012-2014 PIVA SOFTWARE (www.pivasoftware.com)
* Author: Imen Bhiri <imen.bhiri@pivasoftware.com>
* Author: Feten Besbes <feten.besbes@pivasoftware.com>
*/
#include <arpa/inet.h>
#include <glob.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include "dmcwmp.h"
void compress_spaces(char *str)
{
char *dst = str;
for (; *str; ++str) {
*dst++ = *str;
if (isspace(*str)) {
do ++str;
while (isspace(*str));
--str;
}
}
*dst = '\0';
}
char *cut_fx(char *str, char *delimiter, int occurence)
{
int i = 1;
char *pch, *spch;
pch = strtok_r(str, delimiter, &spch);
while (pch != NULL && i<occurence) {
i++;
pch = strtok_r(NULL, delimiter, &spch);
}
return pch;
}
pid_t get_pid(char *pname)
{
DIR* dir;
struct dirent* ent;
char* endptr;
char buf[512];
if (!(dir = opendir("/proc"))) {
return -1;
}
while((ent = readdir(dir)) != NULL) {
long lpid = strtol(ent->d_name, &endptr, 10);
if (*endptr != '\0') {
continue;
}
snprintf(buf, sizeof(buf), "/proc/%ld/cmdline", lpid);
FILE* fp = fopen(buf, "r");
if (fp) {
if (fgets(buf, sizeof(buf), fp) != NULL) {
char* first = strtok(buf, " ");
if (strstr(first, pname)) {
fclose(fp);
closedir(dir);
return (pid_t)lpid;
}
}
fclose(fp);
}
}
closedir(dir);
return -1;
}
int check_file(char *path)
{
glob_t globbuf;
if(glob(path, 0, NULL, &globbuf) == 0) {
globfree(&globbuf);
return 1;
}
return 0;
}
char *cidr2netmask(int bits)
{
uint32_t mask;
struct in_addr ip_addr;
uint8_t u_bits = (uint8_t)bits;
char *netmask;
char tmp[32] = {0};
mask = ((0xFFFFFFFFUL << (32 - u_bits)) & 0xFFFFFFFFUL);
mask = htonl(mask);
ip_addr.s_addr = mask;
return inet_ntoa(ip_addr);
}
void remove_substring(char *s, const char *str_remove)
{
int len = strlen(str_remove);
while (s = strstr(s, str_remove)) {
memmove(s, s+len, 1+strlen(s+len));
}
}
bool is_strword_in_optionvalue(char *optionvalue, char *str)
{
int len;
char *s = optionvalue;
while ((s = strstr(s, str))) {
len = strlen(str); //should be inside while, optimization reason
if(s[len] == '\0' || s[len] == ' ')
return true;
s++;
}
return false;
}