Fix display of open files.

Cleanup the code to prevent this bug from happening again.
This commit is contained in:
Hisham Muhammad 2014-04-22 18:45:47 -03:00
parent d28b281165
commit 1de8762e71
1 changed files with 27 additions and 22 deletions

View File

@ -28,14 +28,18 @@ in the source distribution for its full text.
#include "Panel.h" #include "Panel.h"
#include "FunctionBar.h" #include "FunctionBar.h"
typedef struct OpenFiles_ProcessData_ { typedef struct OpenFiles_Data_ {
struct OpenFiles_FileData_* files;
int error;
char* data[256]; char* data[256];
} OpenFiles_Data;
typedef struct OpenFiles_ProcessData_ {
OpenFiles_Data data;
int error;
struct OpenFiles_FileData_* files;
} OpenFiles_ProcessData; } OpenFiles_ProcessData;
typedef struct OpenFiles_FileData_ { typedef struct OpenFiles_FileData_ {
char* data[256]; OpenFiles_Data data;
struct OpenFiles_FileData_* next; struct OpenFiles_FileData_* next;
} OpenFiles_FileData; } OpenFiles_FileData;
@ -85,17 +89,15 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
FILE* fd = popen(command, "r"); FILE* fd = popen(command, "r");
OpenFiles_ProcessData* pdata = calloc(1, sizeof(OpenFiles_ProcessData)); OpenFiles_ProcessData* pdata = calloc(1, sizeof(OpenFiles_ProcessData));
OpenFiles_FileData* fdata = NULL; OpenFiles_FileData* fdata = NULL;
OpenFiles_ProcessData* item = pdata; OpenFiles_Data* item = &(pdata->data);
bool anyRead = false;
if (!fd) { if (!fd) {
pdata->error = 127; pdata->error = 127;
return pdata; return pdata;
} }
while (!feof(fd)) { while (!feof(fd)) {
int cmd = fgetc(fd); int cmd = fgetc(fd);
if (cmd == EOF && !anyRead) if (cmd == EOF)
break; break;
anyRead = true;
char* entry = malloc(1024); char* entry = malloc(1024);
if (!fgets(entry, 1024, fd)) { if (!fgets(entry, 1024, fd)) {
free(entry); free(entry);
@ -104,14 +106,14 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
char* newline = strrchr(entry, '\n'); char* newline = strrchr(entry, '\n');
*newline = '\0'; *newline = '\0';
if (cmd == 'f') { if (cmd == 'f') {
OpenFiles_FileData* nextFile = calloc(1, sizeof(OpenFiles_ProcessData)); OpenFiles_FileData* nextFile = calloc(1, sizeof(OpenFiles_FileData));
if (fdata == NULL) { if (fdata == NULL) {
pdata->files = nextFile; pdata->files = nextFile;
} else { } else {
fdata->next = nextFile; fdata->next = nextFile;
} }
fdata = nextFile; fdata = nextFile;
item = (OpenFiles_ProcessData*) fdata; item = &(fdata->data);
} }
item->data[cmd] = entry; item->data[cmd] = entry;
} }
@ -125,6 +127,12 @@ static inline void addLine(const char* line, Vector* lines, Panel* panel, const
Panel_add(panel, (Object*)Vector_get(lines, Vector_size(lines)-1)); Panel_add(panel, (Object*)Vector_get(lines, Vector_size(lines)-1));
} }
static inline void OpenFiles_Data_clear(OpenFiles_Data* data) {
for (int i = 0; i < 255; i++)
if (data->data[i])
free(data->data[i]);
}
static void OpenFilesScreen_scan(OpenFilesScreen* this, Vector* lines, IncSet* inc) { static void OpenFilesScreen_scan(OpenFilesScreen* this, Vector* lines, IncSet* inc) {
Panel* panel = this->display; Panel* panel = this->display;
int idx = Panel_getSelectedIndex(panel); int idx = Panel_getSelectedIndex(panel);
@ -138,24 +146,21 @@ static void OpenFilesScreen_scan(OpenFilesScreen* this, Vector* lines, IncSet* i
OpenFiles_FileData* fdata = pdata->files; OpenFiles_FileData* fdata = pdata->files;
while (fdata) { while (fdata) {
char entry[1024]; char entry[1024];
char** data = fdata->data.data;
sprintf(entry, "%5s %4s %10s %10s %10s %s", sprintf(entry, "%5s %4s %10s %10s %10s %s",
fdata->data['f'] ? fdata->data['f'] : "", data['f'] ? data['f'] : "",
fdata->data['t'] ? fdata->data['t'] : "", data['t'] ? data['t'] : "",
fdata->data['D'] ? fdata->data['D'] : "", data['D'] ? data['D'] : "",
fdata->data['s'] ? fdata->data['s'] : "", data['s'] ? data['s'] : "",
fdata->data['i'] ? fdata->data['i'] : "", data['i'] ? data['i'] : "",
fdata->data['n'] ? fdata->data['n'] : ""); data['n'] ? data['n'] : "");
addLine(entry, lines, panel, IncSet_filter(inc)); addLine(entry, lines, panel, IncSet_filter(inc));
for (int i = 0; i < 255; i++) OpenFiles_Data_clear(&fdata->data);
if (fdata->data[i])
free(fdata->data[i]);
OpenFiles_FileData* old = fdata; OpenFiles_FileData* old = fdata;
fdata = fdata->next; fdata = fdata->next;
free(old); free(old);
} }
for (int i = 0; i < 255; i++) OpenFiles_Data_clear(&pdata->data);
if (pdata->data[i])
free(pdata->data[i]);
} }
free(pdata); free(pdata);
Vector_insertionSort(lines); Vector_insertionSort(lines);