Nincs mit. Közben beleraktam még egy két dolgot (pl. nagyobb chartömböket, mert a terminator is a string része és az fread kimenetének vizsgálását is, hogy ha nincs NUL a végén, akkor se folytassa a buf olvasását, mert akkor rámehet olyan adatokra, amik az előző körből maradtak ott), átszerveztem a kiszálló ágakat, hogy átláthatóbb legyen és megcsináltam visszafelé is, azaz processnév kiolvasása pid alapján. Ehun gp.c:#ifndef _GP_C
#define _GP_C 1
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *get_process_name_by_pid(long int pid)
{
FILE *f;
char spid[11];
char entry[25];
char buf[256];
long int l, c;
char *result;
sprintf(spid, "%d", pid);
strcpy(entry, "/proc/");
strcat(entry, spid);
strcat(entry, "/cmdline");
f = fopen(entry, "rb");
if (f == NULL)
{
return NULL;
}
c = fread(&buf[0], 1, 256, f);
fclose(f);
l = -1;
while ((++l < 257) && (l < c) && (buf[l]) && (buf[l] != 32));
buf[l] -= (buf[l] == 32) << 5;
if (!l)
{
return NULL;
}
while ((buf[--l] != 47) && l);
l += (buf[l] == 47);
result = malloc(strlen(&buf[l]));
if (result != NULL)
{
strcpy(result, &buf[l]);
}
return result;
}
long int get_pid_by_process_name(char *pname)
{
DIR *d;
struct dirent *dir;
FILE *f;
char entry[25];
char buf[256];
long int l, c;
d = opendir("/proc");
if (!d)
{
return -2;
}
while ((dir = readdir(d)) != NULL)
{
strcpy(entry, "/proc/");
strcat(entry, dir->d_name);
strcat(entry, "/cmdline");
f = fopen(entry, "rb");
if (f != NULL)
{
c = fread(&buf[0], 1, 256, f);
fclose(f);
l = -1;
while ((++l < 257) && (l < c) && (buf[l]) && (buf[l] != 32));
buf[l] -= (buf[l] == 32) << 5;
if (l)
{
while ((buf[--l] != 47) && l);
l += (buf[l] == 47);
if (!strcmp(pname, &buf[l]))
{
return atol(dir->d_name);
}
}
}
}
closedir(d);
return -1;
}
#endif
|