mirror of
https://dev.iopsys.eu/bbf/icwmp.git
synced 2026-03-11 03:28:31 +01:00
102 lines
No EOL
2 KiB
C
102 lines
No EOL
2 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;
|
|
pch = strtok (str, delimiter);
|
|
while (pch != NULL && i<occurence) {
|
|
i++;
|
|
pch = strtok(NULL, delimiter);
|
|
}
|
|
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);
|
|
} |