Dynamically adjust the size of line reads

* Dynamically adjust the size of line reads.
* Remove some more uses of fgets with arbitrary sizes.
* Fix reading of lines and width of n column.

Fixes #514.
This commit is contained in:
Hisham Muhammad 2016-06-19 18:55:35 -03:00 committed by GitHub
parent 52f814481c
commit 0fa03322a9
5 changed files with 58 additions and 25 deletions

View File

@ -86,17 +86,12 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
pdata->error = 127; pdata->error = 127;
return pdata; return pdata;
} }
while (!feof(fd)) { for (;;) {
int cmd = fgetc(fd); char* line = String_readLine(fd);
if (cmd == EOF) if (!line) {
break;
char* entry = xMalloc(1024);
if (!fgets(entry, 1024, fd)) {
free(entry);
break; break;
} }
char* newline = strrchr(entry, '\n'); unsigned char cmd = line[0];
*newline = '\0';
if (cmd == 'f') { if (cmd == 'f') {
OpenFiles_FileData* nextFile = xCalloc(1, sizeof(OpenFiles_FileData)); OpenFiles_FileData* nextFile = xCalloc(1, sizeof(OpenFiles_FileData));
if (fdata == NULL) { if (fdata == NULL) {
@ -108,7 +103,8 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
item = &(fdata->data); item = &(fdata->data);
} }
assert(cmd >= 0 && cmd <= 0xff); assert(cmd >= 0 && cmd <= 0xff);
item->data[cmd] = entry; item->data[cmd] = xStrdup(line + 1);
free(line);
} }
pdata->error = pclose(fd); pdata->error = pclose(fd);
return pdata; return pdata;
@ -132,9 +128,11 @@ void OpenFilesScreen_scan(InfoScreen* this) {
} else { } else {
OpenFiles_FileData* fdata = pdata->files; OpenFiles_FileData* fdata = pdata->files;
while (fdata) { while (fdata) {
char entry[1024];
char** data = fdata->data.data; char** data = fdata->data.data;
sprintf(entry, "%5s %4s %10s %10s %10s %s", int lenN = data['n'] ? strlen(data['n']) : 0;
int sizeEntry = 5 + 7 + 10 + 10 + 10 + lenN + 5 /*spaces*/ + 1 /*null*/;
char* entry = xMalloc(sizeEntry);
snprintf(entry, sizeEntry, "%5.5s %7.7s %10.10s %10.10s %10.10s %s",
data['f'] ? data['f'] : "", data['f'] ? data['f'] : "",
data['t'] ? data['t'] : "", data['t'] ? data['t'] : "",
data['D'] ? data['D'] : "", data['D'] ? data['D'] : "",

View File

@ -173,12 +173,15 @@ static bool Settings_read(Settings* this, const char* fileName) {
if (!fd) if (!fd)
return false; return false;
const int maxLine = 2048;
char buffer[maxLine];
bool readMeters = false; bool readMeters = false;
while (fgets(buffer, maxLine, fd)) { for (;;) {
char* line = String_readLine(fd);
if (!line) {
break;
}
int nOptions; int nOptions;
char** option = String_split(buffer, '=', &nOptions); char** option = String_split(line, '=', &nOptions);
free (line);
if (nOptions < 2) { if (nOptions < 2) {
String_freeArray(option); String_freeArray(option);
continue; continue;

View File

@ -13,9 +13,10 @@ in the source distribution for its full text.
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
/*{ /*{
#include <stdio.h>
#define String_startsWith(s, match) (strstr((s), (match)) == (s)) #define String_startsWith(s, match) (strstr((s), (match)) == (s))
#define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL) #define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL)
}*/ }*/
@ -119,3 +120,29 @@ char* String_getToken(const char* line, const unsigned short int numMatch) {
match[foundCount] = '\0'; match[foundCount] = '\0';
return((char*)xStrdup(match)); return((char*)xStrdup(match));
} }
char* String_readLine(FILE* fd) {
const int step = 1024;
int bufSize = step;
char* buffer = xMalloc(step + 1);
char* at = buffer;
for (;;) {
char* ok = fgets(at, step + 1, fd);
if (!ok) {
free(buffer);
return NULL;
}
char* newLine = strrchr(at, '\n');
if (newLine) {
*newLine = '\0';
return buffer;
} else {
if (feof(fd)) {
return buffer;
}
}
bufSize += step;
buffer = xRealloc(buffer, bufSize + 1);
at = buffer + bufSize - step;
}
}

View File

@ -9,6 +9,8 @@ Released under the GNU GPL, see the COPYING file
in the source distribution for its full text. in the source distribution for its full text.
*/ */
#include <stdio.h>
#define String_startsWith(s, match) (strstr((s), (match)) == (s)) #define String_startsWith(s, match) (strstr((s), (match)) == (s))
#define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL) #define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL)
@ -24,4 +26,6 @@ void String_freeArray(char** s);
char* String_getToken(const char* line, const unsigned short int numMatch); char* String_getToken(const char* line, const unsigned short int numMatch);
char* String_readLine(FILE* fd);
#endif #endif

View File

@ -65,10 +65,11 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
break; break;
} }
char line[50] = ""; char* line = NULL;
for (unsigned short int i = 0; i < lineNum; i++) { for (unsigned short int i = 0; i < lineNum; i++) {
char* ok = fgets(line, sizeof line, file); free(line);
if (!ok) break; line = String_readLine(file);
if (!line) break;
} }
fclose(file); fclose(file);
@ -76,6 +77,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
char *foundNumStr = String_getToken(line, wordNum); char *foundNumStr = String_getToken(line, wordNum);
const unsigned long int foundNum = atoi(foundNumStr); const unsigned long int foundNum = atoi(foundNumStr);
free(foundNumStr); free(foundNumStr);
free(line);
total += foundNum; total += foundNum;
} }
@ -116,14 +118,13 @@ static ACPresence procAcpiCheck() {
continue; continue;
} }
char line[100]; char* line = String_readLine(file);
char* ok = fgets(line, sizeof line, file); if (!line) continue;
if (!ok) continue;
line[sizeof(line) - 1] = '\0';
fclose(file); fclose(file);
const char *isOnline = String_getToken(line, 2); const char *isOnline = String_getToken(line, 2);
free(line);
if (strcmp(isOnline, "on-line") == 0) { if (strcmp(isOnline, "on-line") == 0) {
isOn = AC_PRESENT; isOn = AC_PRESENT;