Merge branch 'conversion' of https://github.com/cgzones/htop into cgzones-conversion

This commit is contained in:
Nathan Scott 2020-12-07 11:41:22 +11:00
commit b92cfa7d7a
26 changed files with 87 additions and 79 deletions

View File

@ -21,7 +21,7 @@ static const int BatteryMeter_attributes[] = {
BATTERY BATTERY
}; };
static void BatteryMeter_updateValues(Meter* this, char* buffer, int len) { static void BatteryMeter_updateValues(Meter* this, char* buffer, size_t len) {
ACPresence isOnAC; ACPresence isOnAC;
double percent; double percent;

View File

@ -50,7 +50,7 @@ static void CPUMeter_init(Meter* this) {
Meter_setCaption(this, "Avg"); Meter_setCaption(this, "Avg");
} }
static void CPUMeter_updateValues(Meter* this, char* buffer, int size) { static void CPUMeter_updateValues(Meter* this, char* buffer, size_t size) {
int cpu = this->param; int cpu = this->param;
if (cpu > this->pl->cpuCount) { if (cpu > this->pl->cpuCount) {
xSnprintf(buffer, size, "absent"); xSnprintf(buffer, size, "absent");

View File

@ -19,7 +19,7 @@ static const int ClockMeter_attributes[] = {
CLOCK CLOCK
}; };
static void ClockMeter_updateValues(Meter* this, char* buffer, int size) { static void ClockMeter_updateValues(Meter* this, char* buffer, size_t size) {
time_t t = time(NULL); time_t t = time(NULL);
struct tm result; struct tm result;
struct tm* lt = localtime_r(&t, &result); struct tm* lt = localtime_r(&t, &result);

View File

@ -19,7 +19,7 @@ static const int DateMeter_attributes[] = {
DATE DATE
}; };
static void DateMeter_updateValues(Meter* this, char* buffer, int size) { static void DateMeter_updateValues(Meter* this, char* buffer, size_t size) {
time_t t = time(NULL); time_t t = time(NULL);
struct tm result; struct tm result;
struct tm* lt = localtime_r(&t, &result); struct tm* lt = localtime_r(&t, &result);

View File

@ -19,7 +19,7 @@ static const int DateTimeMeter_attributes[] = {
DATETIME DATETIME
}; };
static void DateTimeMeter_updateValues(Meter* this, char* buffer, int size) { static void DateTimeMeter_updateValues(Meter* this, char* buffer, size_t size) {
time_t t = time(NULL); time_t t = time(NULL);
struct tm result; struct tm result;
struct tm* lt = localtime_r(&t, &result); struct tm* lt = localtime_r(&t, &result);

View File

@ -30,7 +30,7 @@ static unsigned long int cached_read_diff = 0;
static unsigned long int cached_write_diff = 0; static unsigned long int cached_write_diff = 0;
static double cached_utilisation_diff = 0.0; static double cached_utilisation_diff = 0.0;
static void DiskIOMeter_updateValues(Meter* this, char* buffer, int len) { static void DiskIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
static unsigned long long int cached_last_update = 0; static unsigned long long int cached_last_update = 0;
struct timeval tv; struct timeval tv;

View File

@ -19,7 +19,7 @@ static const int HostnameMeter_attributes[] = {
HOSTNAME HOSTNAME
}; };
static void HostnameMeter_updateValues(Meter* this, char* buffer, int size) { static void HostnameMeter_updateValues(Meter* this, char* buffer, size_t size) {
(void) this; (void) this;
gethostname(buffer, size - 1); gethostname(buffer, size - 1);
} }

View File

@ -24,7 +24,7 @@ static const int LoadMeter_attributes[] = {
LOAD LOAD
}; };
static void LoadAverageMeter_updateValues(Meter* this, char* buffer, int size) { static void LoadAverageMeter_updateValues(Meter* this, char* buffer, size_t size) {
Platform_getLoadAverage(&this->values[0], &this->values[1], &this->values[2]); Platform_getLoadAverage(&this->values[0], &this->values[1], &this->values[2]);
xSnprintf(buffer, size, "%.2f/%.2f/%.2f", this->values[0], this->values[1], this->values[2]); xSnprintf(buffer, size, "%.2f/%.2f/%.2f", this->values[0], this->values[1], this->values[2]);
} }
@ -40,7 +40,7 @@ static void LoadAverageMeter_display(const Object* cast, RichString* out) {
RichString_append(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer); RichString_append(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer);
} }
static void LoadMeter_updateValues(Meter* this, char* buffer, int size) { static void LoadMeter_updateValues(Meter* this, char* buffer, size_t size) {
double five, fifteen; double five, fifteen;
Platform_getLoadAverage(&this->values[0], &five, &fifteen); Platform_getLoadAverage(&this->values[0], &five, &fifteen);
if (this->values[0] > this->total) { if (this->values[0] > this->total) {

View File

@ -19,17 +19,16 @@ static const int MemoryMeter_attributes[] = {
MEMORY_CACHE MEMORY_CACHE
}; };
static void MemoryMeter_updateValues(Meter* this, char* buffer, int size) { static void MemoryMeter_updateValues(Meter* this, char* buffer, size_t size) {
int written; int written;
Platform_setMemoryValues(this); Platform_setMemoryValues(this);
written = Meter_humanUnit(buffer, this->values[0], size); written = Meter_humanUnit(buffer, this->values[0], size);
buffer += written; METER_BUFFER_CHECK(buffer, size, written);
if ((size -= written) > 0) {
*buffer++ = '/'; METER_BUFFER_APPEND_CHR(buffer, size, '/');
size--;
Meter_humanUnit(buffer, this->total, size); Meter_humanUnit(buffer, this->total, size);
}
} }
static void MemoryMeter_display(const Object* cast, RichString* out) { static void MemoryMeter_display(const Object* cast, RichString* out) {

15
Meter.c
View File

@ -49,7 +49,7 @@ Meter* Meter_new(const struct ProcessList_* pl, int param, const MeterClass* typ
return this; return this;
} }
int Meter_humanUnit(char* buffer, unsigned long int value, int size) { int Meter_humanUnit(char* buffer, unsigned long int value, size_t size) {
const char* prefix = "KMGTPEZY"; const char* prefix = "KMGTPEZY";
unsigned long int powi = 1; unsigned long int powi = 1;
unsigned int powj = 1, precision = 2; unsigned int powj = 1, precision = 2;
@ -155,7 +155,7 @@ ListItem* Meter_toListItem(Meter* this, bool moving) {
static void TextMeterMode_draw(Meter* this, int x, int y, int w) { static void TextMeterMode_draw(Meter* this, int x, int y, int w) {
char buffer[METER_BUFFER_LEN]; char buffer[METER_BUFFER_LEN];
Meter_updateValues(this, buffer, METER_BUFFER_LEN - 1); Meter_updateValues(this, buffer, sizeof(buffer));
(void) w; (void) w;
attrset(CRT_colors[METER_TEXT]); attrset(CRT_colors[METER_TEXT]);
@ -175,7 +175,7 @@ static const char BarMeterMode_characters[] = "|#*@$%&.";
static void BarMeterMode_draw(Meter* this, int x, int y, int w) { static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
char buffer[METER_BUFFER_LEN]; char buffer[METER_BUFFER_LEN];
Meter_updateValues(this, buffer, METER_BUFFER_LEN - 1); Meter_updateValues(this, buffer, sizeof(buffer));
w -= 2; w -= 2;
attrset(CRT_colors[METER_TEXT]); attrset(CRT_colors[METER_TEXT]);
@ -309,8 +309,8 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
for (int i = 0; i < nValues - 1; i++) for (int i = 0; i < nValues - 1; i++)
data->values[i] = data->values[i + 1]; data->values[i] = data->values[i + 1];
char buffer[nValues]; char buffer[METER_BUFFER_LEN];
Meter_updateValues(this, buffer, nValues - 1); Meter_updateValues(this, buffer, sizeof(buffer));
double value = 0.0; double value = 0.0;
for (uint8_t i = 0; i < this->curItems; i++) for (uint8_t i = 0; i < this->curItems; i++)
@ -378,7 +378,7 @@ static void LEDMeterMode_draw(Meter* this, int x, int y, int w) {
LEDMeterMode_digits = LEDMeterMode_digitsAscii; LEDMeterMode_digits = LEDMeterMode_digitsAscii;
char buffer[METER_BUFFER_LEN]; char buffer[METER_BUFFER_LEN];
Meter_updateValues(this, buffer, METER_BUFFER_LEN - 1); Meter_updateValues(this, buffer, sizeof(buffer));
RichString_begin(out); RichString_begin(out);
Meter_displayBuffer(this, buffer, &out); Meter_displayBuffer(this, buffer, &out);
@ -441,8 +441,7 @@ const MeterMode* const Meter_modes[] = {
/* Blank meter */ /* Blank meter */
static void BlankMeter_updateValues(Meter* this, char* buffer, int size) { static void BlankMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) {
(void) this; (void) buffer; (void) size;
if (size > 0) { if (size > 0) {
*buffer = 0; *buffer = 0;
} }

27
Meter.h
View File

@ -20,13 +20,36 @@ in the source distribution for its full text.
#define METER_BUFFER_LEN 256 #define METER_BUFFER_LEN 256
#define METER_BUFFER_CHECK(buffer, size, written) \
do { \
if ((written) < 0 || (size_t)(written) >= (size)) { \
return; \
} \
(buffer) += (written); \
(size) -= (size_t)(written); \
} while (0)
#define METER_BUFFER_APPEND_CHR(buffer, size, c) \
do { \
if ((size) < 2) { \
return; \
} \
*(buffer)++ = c; \
*(buffer) = '\0'; \
(size)--; \
if ((size) == 0) { \
return; \
} \
} while (0)
struct Meter_; struct Meter_;
typedef struct Meter_ Meter; typedef struct Meter_ Meter;
typedef void(*Meter_Init)(Meter*); typedef void(*Meter_Init)(Meter*);
typedef void(*Meter_Done)(Meter*); typedef void(*Meter_Done)(Meter*);
typedef void(*Meter_UpdateMode)(Meter*, int); typedef void(*Meter_UpdateMode)(Meter*, int);
typedef void(*Meter_UpdateValues)(Meter*, char*, int); typedef void(*Meter_UpdateValues)(Meter*, char*, size_t);
typedef void(*Meter_Draw)(Meter*, int, int, int); typedef void(*Meter_Draw)(Meter*, int, int, int);
typedef struct MeterClass_ { typedef struct MeterClass_ {
@ -101,7 +124,7 @@ extern const MeterClass Meter_class;
Meter* Meter_new(const ProcessList* pl, int param, const MeterClass* type); Meter* Meter_new(const ProcessList* pl, int param, const MeterClass* type);
int Meter_humanUnit(char* buffer, unsigned long int value, int size); int Meter_humanUnit(char* buffer, unsigned long int value, size_t size);
void Meter_delete(Object* cast); void Meter_delete(Object* cast);

View File

@ -24,7 +24,7 @@ static unsigned long int cached_rxp_diff = 0;
static unsigned long int cached_txb_diff = 0; static unsigned long int cached_txb_diff = 0;
static unsigned long int cached_txp_diff = 0; static unsigned long int cached_txp_diff = 0;
static void NetworkIOMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, int len) { static void NetworkIOMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t len) {
static unsigned long long int cached_last_update = 0; static unsigned long long int cached_last_update = 0;
struct timeval tv; struct timeval tv;

View File

@ -223,7 +223,7 @@ static inline void Process_writeCommand(const Process* this, int attr, int basea
} }
} }
void Process_outputRate(RichString* str, char* buffer, int n, double rate, int coloring) { void Process_outputRate(RichString* str, char* buffer, size_t n, double rate, int coloring) {
int largeNumberColor = CRT_colors[LARGE_NUMBER]; int largeNumberColor = CRT_colors[LARGE_NUMBER];
int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES]; int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
int processColor = CRT_colors[PROCESS]; int processColor = CRT_colors[PROCESS];
@ -258,7 +258,7 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field
char buffer[256]; buffer[255] = '\0'; char buffer[256]; buffer[255] = '\0';
int attr = CRT_colors[DEFAULT_COLOR]; int attr = CRT_colors[DEFAULT_COLOR];
int baseattr = CRT_colors[PROCESS_BASENAME]; int baseattr = CRT_colors[PROCESS_BASENAME];
int n = sizeof(buffer) - 1; size_t n = sizeof(buffer) - 1;
bool coloring = this->settings->highlightMegabytes; bool coloring = this->settings->highlightMegabytes;
switch (field) { switch (field) {
@ -312,7 +312,7 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field
} else { } else {
ret = snprintf(buf, n, " "); ret = snprintf(buf, n, " ");
} }
if (ret < 0 || ret >= n) { if (ret < 0 || (size_t)ret >= n) {
written = n; written = n;
} else { } else {
written = ret; written = ret;

View File

@ -176,7 +176,7 @@ void Process_printTime(RichString* str, unsigned long long totalHundredths);
void Process_fillStarttimeBuffer(Process* this); void Process_fillStarttimeBuffer(Process* this);
void Process_outputRate(RichString* str, char* buffer, int n, double rate, int coloring); void Process_outputRate(RichString* str, char* buffer, size_t n, double rate, int coloring);
void Process_display(const Object* cast, RichString* out); void Process_display(const Object* cast, RichString* out);

View File

@ -17,17 +17,16 @@ static const int SwapMeter_attributes[] = {
SWAP SWAP
}; };
static void SwapMeter_updateValues(Meter* this, char* buffer, int size) { static void SwapMeter_updateValues(Meter* this, char* buffer, size_t size) {
int written; int written;
Platform_setSwapValues(this); Platform_setSwapValues(this);
written = Meter_humanUnit(buffer, this->values[0], size); written = Meter_humanUnit(buffer, this->values[0], size);
buffer += written; METER_BUFFER_CHECK(buffer, size, written);
if ((size -= written) > 0) {
*buffer++ = '/'; METER_BUFFER_APPEND_CHR(buffer, size, '/');
size--;
Meter_humanUnit(buffer, this->total, size); Meter_humanUnit(buffer, this->total, size);
}
} }
static void SwapMeter_display(const Object* cast, RichString* out) { static void SwapMeter_display(const Object* cast, RichString* out) {

View File

@ -23,7 +23,7 @@ static const int TasksMeter_attributes[] = {
TASKS_RUNNING TASKS_RUNNING
}; };
static void TasksMeter_updateValues(Meter* this, char* buffer, int len) { static void TasksMeter_updateValues(Meter* this, char* buffer, size_t len) {
const ProcessList* pl = this->pl; const ProcessList* pl = this->pl;
this->values[0] = pl->kernelThreads; this->values[0] = pl->kernelThreads;
this->values[1] = pl->userlandThreads; this->values[1] = pl->userlandThreads;

View File

@ -17,7 +17,7 @@ static const int UptimeMeter_attributes[] = {
UPTIME UPTIME
}; };
static void UptimeMeter_updateValues(Meter* this, char* buffer, int len) { static void UptimeMeter_updateValues(Meter* this, char* buffer, size_t len) {
int totalseconds = Platform_getUptime(); int totalseconds = Platform_getUptime();
if (totalseconds == -1) { if (totalseconds == -1) {
xSnprintf(buffer, len, "(unknown)"); xSnprintf(buffer, len, "(unknown)");

View File

@ -184,13 +184,13 @@ int xAsprintf(char** strp, const char* fmt, ...) {
return r; return r;
} }
int xSnprintf(char* buf, int len, const char* fmt, ...) { int xSnprintf(char* buf, size_t len, const char* fmt, ...) {
va_list vl; va_list vl;
va_start(vl, fmt); va_start(vl, fmt);
int n = vsnprintf(buf, len, fmt, vl); int n = vsnprintf(buf, len, fmt, vl);
va_end(vl); va_end(vl);
if (n < 0 || n >= len) { if (n < 0 || (size_t)n >= len) {
fail(); fail();
} }

View File

@ -59,7 +59,7 @@ ATTR_FORMAT(printf, 2, 3)
int xAsprintf(char** strp, const char* fmt, ...); int xAsprintf(char** strp, const char* fmt, ...);
ATTR_FORMAT(printf, 3, 4) ATTR_FORMAT(printf, 3, 4)
int xSnprintf(char* buf, int len, const char* fmt, ...); int xSnprintf(char* buf, size_t len, const char* fmt, ...);
char* xStrdup(const char* str) ATTR_NONNULL; char* xStrdup(const char* str) ATTR_NONNULL;

View File

@ -618,7 +618,7 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces
bool coloring = this->settings->highlightMegabytes; bool coloring = this->settings->highlightMegabytes;
char buffer[256]; buffer[255] = '\0'; char buffer[256]; buffer[255] = '\0';
int attr = CRT_colors[DEFAULT_COLOR]; int attr = CRT_colors[DEFAULT_COLOR];
int n = sizeof(buffer) - 1; size_t n = sizeof(buffer) - 1;
switch ((int)field) { switch ((int)field) {
case TTY_NR: { case TTY_NR: {
if (lp->ttyDevice) { if (lp->ttyDevice) {

View File

@ -25,7 +25,7 @@ static const int PressureStallMeter_attributes[] = {
PRESSURE_STALL_THREEHUNDRED PRESSURE_STALL_THREEHUNDRED
}; };
static void PressureStallMeter_updateValues(Meter* this, char* buffer, int len) { static void PressureStallMeter_updateValues(Meter* this, char* buffer, size_t len) {
const char* file; const char* file;
if (strstr(Meter_name(this), "CPU")) { if (strstr(Meter_name(this), "CPU")) {
file = "cpu"; file = "cpu";

View File

@ -70,7 +70,7 @@ static bool isSelinuxEnforcing(void) {
return !!enforce; return !!enforce;
} }
static void SELinuxMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, int len) { static void SELinuxMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t len) {
enabled = isSelinuxEnabled(); enabled = isSelinuxEnabled();
enforcing = isSelinuxEnforcing(); enforcing = isSelinuxEnforcing();

View File

@ -229,7 +229,7 @@ static void updateViaExec(void) {
fclose(commandOutput); fclose(commandOutput);
} }
static void SystemdMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, int size) { static void SystemdMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) {
free(systemState); free(systemState);
systemState = NULL; systemState = NULL;
nFailedUnits = nInstalledJobs = nNames = nJobs = INVALID_VALUE; nFailedUnits = nInstalledJobs = nNames = nJobs = INVALID_VALUE;

View File

@ -11,7 +11,7 @@ static const int ZramMeter_attributes[] = {
ZRAM ZRAM
}; };
static void ZramMeter_updateValues(Meter* this, char* buffer, int size) { static void ZramMeter_updateValues(Meter* this, char* buffer, size_t size) {
int written; int written;
Platform_setZramValues(this); Platform_setZramValues(this);
@ -20,29 +20,18 @@ static void ZramMeter_updateValues(Meter* this, char* buffer, int size) {
this->curItems = 1; this->curItems = 1;
written = Meter_humanUnit(buffer, this->values[0], size); written = Meter_humanUnit(buffer, this->values[0], size);
buffer += written; METER_BUFFER_CHECK(buffer, size, written);
size -= written;
if (size <= 0) { METER_BUFFER_APPEND_CHR(buffer, size, '(');
return;
}
*buffer++ = '(';
size--;
if (size <= 0) {
return;
}
written = Meter_humanUnit(buffer, this->values[1], size); written = Meter_humanUnit(buffer, this->values[1], size);
buffer += written; METER_BUFFER_CHECK(buffer, size, written);
size -= written;
if (size <= 0) { METER_BUFFER_APPEND_CHR(buffer, size, ')');
return;
} METER_BUFFER_APPEND_CHR(buffer, size, '/');
*buffer++ = ')';
size--;
if ((size -= written) > 0) {
*buffer++ = '/';
size--;
Meter_humanUnit(buffer, this->total, size); Meter_humanUnit(buffer, this->total, size);
}
} }
static void ZramMeter_display(const Object* cast, RichString* out) { static void ZramMeter_display(const Object* cast, RichString* out) {

View File

@ -33,17 +33,16 @@ void ZfsArcMeter_readStats(Meter* this, const ZfsArcStats* stats) {
this->values[5] = stats->size; this->values[5] = stats->size;
} }
static void ZfsArcMeter_updateValues(Meter* this, char* buffer, int size) { static void ZfsArcMeter_updateValues(Meter* this, char* buffer, size_t size) {
int written; int written;
Platform_setZfsArcValues(this); Platform_setZfsArcValues(this);
written = Meter_humanUnit(buffer, this->values[5], size); written = Meter_humanUnit(buffer, this->values[5], size);
buffer += written; METER_BUFFER_CHECK(buffer, size, written);
if ((size -= written) > 0) {
*buffer++ = '/'; METER_BUFFER_APPEND_CHR(buffer, size, '/');
size--;
Meter_humanUnit(buffer, this->total, size); Meter_humanUnit(buffer, this->total, size);
}
} }
static void ZfsArcMeter_display(const Object* cast, RichString* out) { static void ZfsArcMeter_display(const Object* cast, RichString* out) {

View File

@ -32,11 +32,11 @@ void ZfsCompressedArcMeter_readStats(Meter* this, const ZfsArcStats* stats) {
} }
} }
static void ZfsCompressedArcMeter_printRatioString(const Meter* this, char* buffer, int size) { static void ZfsCompressedArcMeter_printRatioString(const Meter* this, char* buffer, size_t size) {
xSnprintf(buffer, size, "%.2f:1", this->total / this->values[0]); xSnprintf(buffer, size, "%.2f:1", this->total / this->values[0]);
} }
static void ZfsCompressedArcMeter_updateValues(Meter* this, char* buffer, int size) { static void ZfsCompressedArcMeter_updateValues(Meter* this, char* buffer, size_t size) {
Platform_setZfsCompressedArcValues(this); Platform_setZfsCompressedArcValues(this);
ZfsCompressedArcMeter_printRatioString(this, buffer, size); ZfsCompressedArcMeter_printRatioString(this, buffer, size);