mirror of https://github.com/xzeldon/htop.git
Refactor code for reading process environment from procfs
This commit is contained in:
parent
a3bb7cbe64
commit
c98d4577c9
|
@ -263,24 +263,35 @@ char* Platform_getProcessEnv(pid_t pid) {
|
||||||
char procname[128];
|
char procname[128];
|
||||||
xSnprintf(procname, sizeof(procname), PROCDIR "/%d/environ", pid);
|
xSnprintf(procname, sizeof(procname), PROCDIR "/%d/environ", pid);
|
||||||
FILE* fd = fopen(procname, "r");
|
FILE* fd = fopen(procname, "r");
|
||||||
|
if(!fd)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
char *env = NULL;
|
char *env = NULL;
|
||||||
if (fd) {
|
|
||||||
size_t capacity = 4096, size = 0, bytes;
|
size_t capacity = 0;
|
||||||
env = xMalloc(capacity);
|
size_t size = 0;
|
||||||
while ((bytes = fread(env+size, 1, capacity-size, fd)) > 0) {
|
ssize_t bytes = 0;
|
||||||
size += bytes;
|
|
||||||
capacity *= 2;
|
do {
|
||||||
env = xRealloc(env, capacity);
|
size += bytes;
|
||||||
}
|
capacity += 4096;
|
||||||
fclose(fd);
|
env = xRealloc(env, capacity);
|
||||||
if (size < 2 || env[size-1] || env[size-2]) {
|
} while ((bytes = fread(env + size, 1, capacity - size, fd)) > 0);
|
||||||
if (size + 2 < capacity) {
|
|
||||||
env = xRealloc(env, capacity+2);
|
fclose(fd);
|
||||||
}
|
|
||||||
env[size] = 0;
|
if (bytes < 0) {
|
||||||
env[size+1] = 0;
|
free(env);
|
||||||
}
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size += bytes;
|
||||||
|
|
||||||
|
env = xRealloc(env, size + 2);
|
||||||
|
|
||||||
|
env[size] = '\0';
|
||||||
|
env[size+1] = '\0';
|
||||||
|
|
||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue