Commit d6231bab authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Initial import.

parents
/* Do not edit this file. It was automatically genarated. */
#ifndef HEADER_Settings
#define HEADER_Settings
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "String.h"
#include "ProcessList.h"
#include "Header.h"
#include "debug.h"
typedef struct Settings_ {
char* userSettings;
ProcessList* pl;
Header* header;
int colorScheme;
int delay;
bool changed;
} Settings;
Settings* Settings_new(ProcessList* pl, Header* header);
void Settings_delete(Settings* this);
bool Settings_read(Settings* this, char* fileName);
bool Settings_write(Settings* this);
#endif
/*
htop - SignalItem.c
(C) 2004,2005 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "SignalItem.h"
#include "String.h"
#include "Object.h"
#include "RichString.h"
#include <string.h>
#include "debug.h"
#define SIGNAL_COUNT 34
/*{
typedef struct Signal_ {
Object super;
char* name;
int number;
} Signal;
extern char* SIGNAL_CLASS;
}*/
/* private property */
char* SIGNAL_CLASS = "Signal";
Signal* Signal_new(char* name, int number) {
Signal* this = malloc(sizeof(Signal));
((Object*)this)->class = SIGNAL_CLASS;
((Object*)this)->display = Signal_display;
((Object*)this)->delete = Signal_delete;
this->name = name;
this->number = number;
return this;
}
void Signal_delete(Object* cast) {
Signal* this = (Signal*)cast;
assert (this != NULL);
// names are string constants, so we're not deleting them.
free(this);
}
void Signal_display(Object* cast, RichString* out) {
Signal* this = (Signal*)cast;
assert (this != NULL);
char buffer[31];
snprintf(buffer, 30, "%2d %s", this->number, this->name);
RichString_write(out, CRT_colors[DEFAULT_COLOR], buffer);
}
int Signal_getSignalCount() {
return SIGNAL_COUNT;
}
Signal** Signal_getSignalTable() {
Signal** signals = malloc(sizeof(Signal*) * SIGNAL_COUNT);
signals[0] = Signal_new("Cancel", 0);
signals[1] = Signal_new("SIGHUP", 1);
signals[2] = Signal_new("SIGINT", 2);
signals[3] = Signal_new("SIGQUIT", 3);
signals[4] = Signal_new("SIGILL", 4);
signals[5] = Signal_new("SIGTRAP", 5);
signals[6] = Signal_new("SIGABRT", 6);
signals[7] = Signal_new("SIGIOT", 6);
signals[8] = Signal_new("SIGBUS", 7);
signals[9] = Signal_new("SIGFPE", 8);
signals[10] = Signal_new("SIGKILL", 9);
signals[11] = Signal_new("SIGUSR1", 10);
signals[12] = Signal_new("SIGSEGV", 11);
signals[13] = Signal_new("SIGUSR2", 12);
signals[14] = Signal_new("SIGPIPE", 13);
signals[15] = Signal_new("SIGALRM", 14);
signals[16] = Signal_new("SIGTERM", 15);
signals[17] = Signal_new("SIGSTKFLT", 16);
signals[18] = Signal_new("SIGCHLD", 17);
signals[19] = Signal_new("SIGCONT", 18);
signals[20] = Signal_new("SIGSTOP", 19);
signals[21] = Signal_new("SIGTSTP", 20);
signals[22] = Signal_new("SIGTTIN", 21);
signals[23] = Signal_new("SIGTTOU", 22);
signals[24] = Signal_new("SIGURG", 23);
signals[25] = Signal_new("SIGXCPU", 24);
signals[26] = Signal_new("SIGXFSZ", 25);
signals[27] = Signal_new("SIGVTALRM", 26);
signals[28] = Signal_new("SIGPROF", 27);
signals[29] = Signal_new("SIGWINCH", 28);
signals[30] = Signal_new("SIGIO", 29);
signals[31] = Signal_new("SIGPOLL", 29);
signals[32] = Signal_new("SIGPWR", 30);
signals[33] = Signal_new("SIGSYS", 31);
return signals;
}
/* Do not edit this file. It was automatically genarated. */
#ifndef HEADER_Signal
#define HEADER_Signal
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "String.h"
#include "Object.h"
#include "RichString.h"
#include <string.h>
#include "debug.h"
#define SIGNAL_COUNT 34
typedef struct Signal_ {
Object super;
char* name;
int number;
} Signal;
extern char* SIGNAL_CLASS;
Signal* Signal_new(char* name, int number);
void Signal_delete(Object* cast);
void Signal_display(Object* cast, RichString* out);
int Signal_getSignalCount();
Signal** Signal_getSignalTable();
#endif
#include "SignalsListBox.h"
#include "ListBox.h"
#include "SignalItem.h"
#include "RichString.h"
#include "debug.h"
#include <assert.h>
#include <ctype.h>
/*{
typedef struct SignalsListBox_ {
ListBox super;
int state;
Signal** signals;
} SignalsListBox;
}*/
SignalsListBox* SignalsListBox_new(int x, int y, int w, int h) {
SignalsListBox* this = (SignalsListBox*) malloc(sizeof(SignalsListBox));
ListBox* super = (ListBox*) this;
ListBox_init(super, x, y, w, h, SIGNAL_CLASS, true);
((Object*)this)->delete = SignalsListBox_delete;
this->signals = Signal_getSignalTable();
super->eventHandler = SignalsListBox_EventHandler;
int sigCount = Signal_getSignalCount();
for(int i = 0; i < sigCount; i++)
ListBox_set(super, i, (Object*) this->signals[i]);
SignalsListBox_reset(this);
return this;
}
void SignalsListBox_delete(Object* object) {
ListBox* super = (ListBox*) object;
SignalsListBox* this = (SignalsListBox*) object;
ListBox_done(super);
free(this->signals);
free(this);
}
void SignalsListBox_reset(SignalsListBox* this) {
ListBox* super = (ListBox*) this;
ListBox_setHeader(super, "Send signal:");
ListBox_setSelected(super, 16); // 16th item is SIGTERM
this->state = 0;
}
HandlerResult SignalsListBox_EventHandler(ListBox* super, int ch) {
SignalsListBox* this = (SignalsListBox*) super;
int size = ListBox_getSize(super);
if (ch <= 255 && isdigit(ch)) {
int signal = ch-48 + this->state;
for (int i = 0; i < size; i++)
if (((Signal*) ListBox_get(super, i))->number == signal) {
ListBox_setSelected(super, i);
break;
}
this->state = signal * 10;
if (this->state > 100)
this->state = 0;
return HANDLED;
} else {
this->state = 0;
}
if (ch == 13) {
return BREAK_LOOP;
}
return IGNORED;
}
/* Do not edit this file. It was automatically genarated. */
#ifndef HEADER_SignalsListBox
#define HEADER_SignalsListBox
#include "ListBox.h"
#include "SignalItem.h"
#include "RichString.h"
#include "debug.h"
#include <assert.h>
#include <ctype.h>
typedef struct SignalsListBox_ {
ListBox super;
int state;
Signal** signals;
} SignalsListBox;
SignalsListBox* SignalsListBox_new(int x, int y, int w, int h);
void SignalsListBox_delete(Object* object);
void SignalsListBox_reset(SignalsListBox* this);
HandlerResult SignalsListBox_EventHandler(ListBox* super, int ch);
#endif
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#define _GNU_SOURCE
#include "String.h"
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include "debug.h"
inline void String_delete(char* s) {
free(s);
}
inline char* String_copy(char* orig) {
return strdup(orig);
}
char* String_cat(char* s1, char* s2) {
int l1 = strlen(s1);
int l2 = strlen(s2);
char* out = malloc(l1 + l2 + 1);
strncpy(out, s1, l1);
strncpy(out+l1, s2, l2+1);
return out;
}
char* String_trim(char* in) {
while (in[0] == ' ' || in[0] == '\t' || in[0] == '\n') {
in++;
}
int len = strlen(in);
while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) {
len--;
}
char* out = malloc(len+1);
strncpy(out, in, len);
out[len] = '\0';
return out;
}
char* String_copyUpTo(char* orig, char upTo) {
int len;
int origLen = strlen(orig);
char* at = strchr(orig, upTo);
if (at != NULL)
len = at - orig;
else
len = origLen;
char* copy = (char*) malloc(len+1);
strncpy(copy, orig, len);
copy[len] = '\0';
return copy;
}
char* String_sub(char* orig, int from, int to) {
char* copy;
int len;
len = strlen(orig);
if (to > len)
to = len;
if (from > len)
to = len;
len = to-from+1;
copy = (char*) malloc(len+1);
strncpy(copy, orig+from, len);
copy[len] = '\0';
return copy;
}
void String_println(char* s) {
printf("%s\n", s);
}
void String_print(char* s) {
printf("%s", s);
}
void String_printInt(int i) {
printf("%i", i);
}
void String_printPointer(void* p) {
printf("%p", p);
}
inline int String_eq(char* s1, char* s2) {
if (s1 == NULL || s2 == NULL) {
if (s1 == NULL && s2 == NULL)
return 1;
else
return 0;
}
return (strcmp(s1, s2) == 0);
}
inline int String_startsWith(char* s, char* match) {
return (strstr(s, match) == s);
}
char** String_split(char* s, char sep) {
const int rate = 10;
char** out = (char**) malloc(sizeof(char*) * rate);
int ctr = 0;
int blocks = rate;
char* where;
while ((where = strchr(s, sep)) != NULL) {
int size = where - s;
char* token = (char*) malloc(size + 1);
strncpy(token, s, size);
token[size] = '\0';
out[ctr] = token;
ctr++;
if (ctr == blocks) {
blocks += rate;
out = (char**) realloc(out, sizeof(char*) * blocks);
}
s += size + 1;
}
if (s[0] != '\0') {
int size = strlen(s);
char* token = (char*) malloc(size + 1);
strncpy(token, s, size + 1);
out[ctr] = token;
ctr++;
}
out = realloc(out, sizeof(char*) * (ctr + 1));
out[ctr] = NULL;
return out;
}
void String_freeArray(char** s) {
for (int i = 0; s[i] != NULL; i++) {
free(s[i]);
}
free(s);
}
int String_startsWith_i(char* s, char* match) {
return (strncasecmp(s, match, strlen(match)) == 0);
}
int String_contains_i(char* s, char* match) {
int lens = strlen(s);
int lenmatch = strlen(match);
for (int i = 0; i < lens-lenmatch; i++) {
if (strncasecmp(s, match, strlen(match)) == 0)
return 1;
s++;
}
return 0;
}
/* Do not edit this file. It was automatically genarated. */
#ifndef HEADER_String
#define HEADER_String
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#define _GNU_SOURCE
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include "debug.h"
inline void String_delete(char* s);
inline char* String_copy(char* orig);
char* String_cat(char* s1, char* s2);
char* String_trim(char* in);
char* String_copyUpTo(char* orig, char upTo);
char* String_sub(char* orig, int from, int to);
void String_println(char* s);
void String_print(char* s);
void String_printInt(int i);
void String_printPointer(void* p);
inline int String_eq(char* s1, char* s2);
inline int String_startsWith(char* s, char* match);
char** String_split(char* s, char sep);
void String_freeArray(char** s);
int String_startsWith_i(char* s, char* match);
int String_contains_i(char* s, char* match);
#endif
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "SwapMeter.h"
#include "Meter.h"
#include "ProcessList.h"
#include <stdlib.h>
#include <curses.h>
#include <string.h>
#include <math.h>
#include <sys/param.h>
#include "debug.h"
#include <assert.h>
/*{
typedef struct SwapMeter_ SwapMeter;
struct SwapMeter_ {
Meter super;
ProcessList* pl;
};
}*/
SwapMeter* SwapMeter_new(ProcessList* pl) {
SwapMeter* this = malloc(sizeof(SwapMeter));
Meter_init((Meter*)this, String_copy("Swap"), String_copy("Swp"), 1);
((Meter*)this)->attributes[0] = SWAP;
((Meter*)this)->setValues = SwapMeter_setValues;
((Object*)this)->display = SwapMeter_display;
this->pl = pl;
Meter_setMode((Meter*)this, BAR);
return this;
}
void SwapMeter_setValues(Meter* cast) {
SwapMeter* this = (SwapMeter*)cast;
double totalSwap = (double)this->pl->totalSwap;
long int usedSwap = this->pl->usedSwap;
cast->total = totalSwap;
cast->values[0] = usedSwap;
snprintf(cast->displayBuffer.c, 14, "%ld/%ldMB", usedSwap / 1024, this->pl->totalSwap / 1024);
}
void SwapMeter_display(Object* cast, RichString* out) {
char buffer[50];
Meter* meter = (Meter*)cast;
long int swap = (long int) meter->values[0];
RichString_prune(out);
RichString_append(out, CRT_colors[METER_TEXT], ":");
sprintf(buffer, "%ldM ", (long int) meter->total / 1024);
RichString_append(out, CRT_colors[METER_VALUE], buffer);
sprintf(buffer, "%ldk", swap);
RichString_append(out, CRT_colors[METER_TEXT], "used:");
RichString_append(out, CRT_colors[METER_VALUE], buffer);
}
/* Do not edit this file. It was automatically genarated. */
#ifndef HEADER_SwapMeter
#define HEADER_SwapMeter
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "Meter.h"
#include "ProcessList.h"
#include <stdlib.h>
#include <curses.h>
#include <string.h>
#include <math.h>
#include <sys/param.h>
#include "debug.h"
#include <assert.h>
typedef struct SwapMeter_ SwapMeter;
struct SwapMeter_ {
Meter super;
ProcessList* pl;
};
SwapMeter* SwapMeter_new(ProcessList* pl);
void SwapMeter_setValues(Meter* cast);
void SwapMeter_display(Object* cast, RichString* out);
#endif
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "TasksMeter.h"
#include "Meter.h"
#include "ProcessList.h"
#include "CRT.h"
#include "debug.h"
/*{
typedef struct TasksMeter_ TasksMeter;
struct TasksMeter_ {
Meter super;
ProcessList* pl;
};
}*/
TasksMeter* TasksMeter_new(ProcessList* pl) {
TasksMeter* this = malloc(sizeof(TasksMeter));
Meter_init((Meter*)this, String_copy("Tasks"), String_copy("Tasks: "), 1);
((Meter*)this)->attributes[0] = TASKS_RUNNING;
((Object*)this)->display = TasksMeter_display;
((Meter*)this)->setValues = TasksMeter_setValues;
this->pl = pl;
Meter_setMode((Meter*)this, TEXT);
return this;
}
void TasksMeter_setValues(Meter* cast) {
TasksMeter* this = (TasksMeter*)cast;
cast->total = this->pl->totalTasks;
cast->values[0] = this->pl->runningTasks;
snprintf(cast->displayBuffer.c, 20, "%d/%d", (int) cast->values[0], (int) cast->total);
}
void TasksMeter_display(Object* cast, RichString* out) {
Meter* this = (Meter*)cast;
RichString_prune(out);
char buffer[20];
sprintf(buffer, "%d", (int)this->total);
RichString_append(out, CRT_colors[METER_VALUE], buffer);
RichString_append(out, CRT_colors[METER_TEXT], " total, ");
sprintf(buffer, "%d", (int)this->values[0]);
RichString_append(out, CRT_colors[TASKS_RUNNING], buffer);
RichString_append(out, CRT_colors[METER_TEXT], " running");
}
/* Do not edit this file. It was automatically genarated. */
#ifndef HEADER_TasksMeter
#define HEADER_TasksMeter
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "Meter.h"
#include "ProcessList.h"
#include "CRT.h"
#include "debug.h"
typedef struct TasksMeter_ TasksMeter;
struct TasksMeter_ {
Meter super;
ProcessList* pl;
};
TasksMeter* TasksMeter_new(ProcessList* pl);
void TasksMeter_setValues(Meter* cast);
void TasksMeter_display(Object* cast, RichString* out);
#endif
/*
htop - TraceScreen.c
(C) 2005 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include "TraceScreen.h"
#include "ProcessList.h"
#include "Process.h"
#include "ListItem.h"
/*{
typedef struct TraceScreen_ {
Process* process;
ListBox* display;
FunctionBar* bar;
bool tracing;
} TraceScreen;
}*/
/* private property */
static char* tbFunctions[3] = {"AutoScroll ", "Stop Tracing ", "Done "};
/* private property */
static char* tbKeys[3] = {"F4", "F5", "Esc"};
/* private property */
static int tbEvents[3] = {KEY_F(4), KEY_F(5), 27};
TraceScreen* TraceScreen_new(Process* process) {
TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen));
this->process = process;
this->display = ListBox_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, true);
this->bar = FunctionBar_new(3, tbFunctions, tbKeys, tbEvents);
this->tracing = true;
return this;
}
void TraceScreen_delete(TraceScreen* this) {
ListBox_delete((Object*)this->display);
FunctionBar_delete((Object*)this->bar);
free(this);
}
void TraceScreen_draw(TraceScreen* this) {
attrset(CRT_colors[PANEL_HEADER_FOCUS]);
mvhline(0, 0, ' ', COLS);
mvprintw(0, 0, "Trace of process %d - %s", this->process->pid, this->process->comm);
attrset(CRT_colors[DEFAULT_COLOR]);
FunctionBar_draw(this->bar, NULL);
}
void TraceScreen_run(TraceScreen* this) {
// if (this->process->pid == getpid()) return;
char buffer[1001];
int fdpair[2];
int err = pipe(fdpair);
if (err == -1) return;
int child = fork();
if (child == -1) return;
if (child == 0) {
dup2(fdpair[1], STDERR_FILENO);
fcntl(fdpair[1], F_SETFL, O_NONBLOCK);
sprintf(buffer, "%d", this->process->pid);
execlp("strace", "strace", "-p", buffer, NULL);
exit(1);
}
fcntl(fdpair[0], F_SETFL, O_NONBLOCK);
FILE* strace = fdopen(fdpair[0], "r");
ListBox* lb = this->display;
int fd_strace = fileno(strace);
TraceScreen_draw(this);
CRT_disableDelay();
bool contLine = false;
bool follow = false;
bool looping = true;
while (looping) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd_strace, &fds);
struct timeval tv;
tv.tv_sec = 0; tv.tv_usec = 500;
int ready = select(fd_strace+1, &fds, NULL, NULL, &tv);
int nread = 0;
if (ready > 0)
nread = fread(buffer, 1, 1000, strace);
if (nread && this->tracing) {
char* line = buffer;
buffer[nread] = '\0';
for (int i = 0; i < nread; i++) {
if (buffer[i] == '\n') {
buffer[i] = '\0';
if (contLine) {
ListItem_append((ListItem*)ListBox_get(lb,
ListBox_getSize(lb)-1), line);
contLine = false;
} else {
ListBox_add(lb, (Object*) ListItem_new(line, 0));
}
line = buffer+i+1;
}
}
if (line < buffer+nread) {
ListBox_add(lb, (Object*) ListItem_new(line, 0));
buffer[nread] = '\0';
contLine = true;
}
if (follow)
ListBox_setSelected(lb, ListBox_getSize(lb)-1);
ListBox_draw(lb, true);
}
int ch = getch();
if (ch == KEY_MOUSE) {
MEVENT mevent;
int ok = getmouse(&mevent);
if (ok == OK)
if (mevent.y >= lb->y && mevent.y < LINES - 1) {
ListBox_setSelected(lb, mevent.y - lb->y + lb->scrollV);
follow = false;
ch = 0;
} if (mevent.y == LINES - 1)
ch = FunctionBar_synthesizeEvent(this->bar, mevent.x);
}
switch(ch) {
case ERR:
continue;
case KEY_F(5):
this->tracing = !this->tracing;
FunctionBar_setLabel(this->bar, KEY_F(5), this->tracing?"Stop Tracing ":"Resume Tracing ");
TraceScreen_draw(this);
break;
case 'f':
case KEY_F(4):
follow = !follow;
if (follow)
ListBox_setSelected(lb, ListBox_getSize(lb)-1);
break;
case 'q':
case 27:
looping = false;
break;
case KEY_RESIZE:
ListBox_resize(lb, COLS, LINES-2);
TraceScreen_draw(this);
break;
default:
follow = false;
ListBox_onKey(lb, ch);
}
ListBox_draw(lb, true);
}
kill(child, SIGTERM);
fclose(strace);
}
/*
htop - TraceScreen.h
(C) 2005 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#ifndef HEADER_TraceScreen
#define HEADER_TraceScreen
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include "ProcessList.h"
#include "ListBox.h"
#include "FunctionBar.h"
typedef struct TraceScreen_ {
Process* process;
ListBox* display;
FunctionBar* bar;
bool tracing;
} TraceScreen;
TraceScreen* TraceScreen_new(Process* process);
void TraceScreen_delete(TraceScreen* this);
void TraceScreen_draw(TraceScreen* this);
void TraceScreen_run(TraceScreen* this);
#endif
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "TypedVector.h"
#include "Object.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "debug.h"
#include <assert.h>
/*{
#ifndef DEFAULT_SIZE
#define DEFAULT_SIZE -1
#endif
typedef void(*TypedVector_procedure)(void*);
typedef struct TypedVector_ {
Object **array;
int arraySize;
int growthRate;
int items;
char* vectorType;
bool owner;
} TypedVector;
}*/
TypedVector* TypedVector_new(char* vectorType_, bool owner, int size) {
TypedVector* this;
if (size == DEFAULT_SIZE)
size = 10;
this = (TypedVector*) malloc(sizeof(TypedVector));
this->growthRate = size;
this->array = (Object**) calloc(size, sizeof(Object*));
this->arraySize = size;
this->items = 0;
this->vectorType = vectorType_;
this->owner = owner;
return this;
}
void TypedVector_delete(TypedVector* this) {
if (this->owner) {
for (int i = 0; i < this->items; i++)
if (this->array[i])
(this->array[i])->delete(this->array[i]);
}
free(this->array);
free(this);
}
/* private */
bool TypedVector_isConsistent(TypedVector* this) {
if (this->owner) {
for (int i = 0; i < this->items; i++)
if (this->array[i] && this->array[i]->class != this->vectorType)
return false;
return true;
} else {
return true;
}
}
void TypedVector_prune(TypedVector* this) {
assert(TypedVector_isConsistent(this));
int i;
for (i = 0; i < this->items; i++)
if (this->array[i]) {
if (this->owner)
(this->array[i])->delete(this->array[i]);
this->array[i] = NULL;
}
this->items = 0;
}
void TypedVector_sort(TypedVector* this) {
assert(TypedVector_isConsistent(this));
int i, j;
for (i = 1; i < this->items; i++) {
void* t = this->array[i];
for (j = i-1; j >= 0 && this->array[j]->compare(this->array[j], t) < 0; j--)
this->array[j+1] = this->array[j];
this->array[j+1] = t;
}
assert(TypedVector_isConsistent(this));
/*
for (int i = 0; i < this->items; i++) {
for (int j = i+1; j < this->items; j++) {
if (this->array[j]->compare(this->array[j], t) < 0) {
void* tmp = this->array[i];
this->array[i] = this->array[j];
this->array[j] = tmp;
}
}
}
*/
}
/* private */
void TypedVector_checkArraySize(TypedVector* this) {
assert(TypedVector_isConsistent(this));
if (this->items >= this->arraySize) {
int i;
i = this->arraySize;
this->arraySize = this->items + this->growthRate;
this->array = (Object**) realloc(this->array, sizeof(Object*) * this->arraySize);
for (; i < this->arraySize; i++)
this->array[i] = NULL;
}
assert(TypedVector_isConsistent(this));
}
void TypedVector_insert(TypedVector* this, int index, void* data_) {
assert(index >= 0);
assert(((Object*)data_)->class == this->vectorType);
Object* data = data_;
assert(TypedVector_isConsistent(this));
TypedVector_checkArraySize(this);
assert(this->array[this->items] == NULL);
for (int i = this->items; i >= index; i--) {
this->array[i+1] = this->array[i];
}
this->array[index] = data;
this->items++;
assert(TypedVector_isConsistent(this));
}
Object* TypedVector_take(TypedVector* this, int index) {
assert(index >= 0 && index < this->items);
assert(TypedVector_isConsistent(this));
Object* removed = this->array[index];
assert (removed != NULL);
this->items--;
for (int i = index; i < this->items; i++)
this->array[i] = this->array[i+1];
this->array[this->items] = NULL;
assert(TypedVector_isConsistent(this));
return removed;
}
Object* TypedVector_remove(TypedVector* this, int index) {
Object* removed = TypedVector_take(this, index);
if (this->owner) {
removed->delete(removed);
return NULL;
} else
return removed;
}
void TypedVector_moveUp(TypedVector* this, int index) {
assert(index >= 0 && index < this->items);
assert(TypedVector_isConsistent(this));
if (index == 0)
return;
Object* temp = this->array[index];
this->array[index] = this->array[index - 1];
this->array[index - 1] = temp;
}
void TypedVector_moveDown(TypedVector* this, int index) {
assert(index >= 0 && index < this->items);
assert(TypedVector_isConsistent(this));
if (index == this->items - 1)
return;
Object* temp = this->array[index];
this->array[index] = this->array[index + 1];
this->array[index + 1] = temp;
}
void TypedVector_set(TypedVector* this, int index, void* data_) {
assert(index >= 0);
assert(((Object*)data_)->class == this->vectorType);
Object* data = data_;
assert(TypedVector_isConsistent(this));
TypedVector_checkArraySize(this);
if (index >= this->items) {
this->items = index+1;
} else {
if (this->owner) {
Object* removed = this->array[index];
assert (removed != NULL);
if (this->owner) {
removed->delete(removed);
}
}
}
this->array[index] = data;
assert(TypedVector_isConsistent(this));
}
inline Object* TypedVector_get(TypedVector* this, int index) {
assert(index < this->items);
assert(TypedVector_isConsistent(this));
return this->array[index];
}
inline int TypedVector_size(TypedVector* this) {
assert(TypedVector_isConsistent(this));
return this->items;
}
void TypedVector_merge(TypedVector* this, TypedVector* v2) {
int i;
assert(TypedVector_isConsistent(this));
for (i = 0; i < v2->items; i++)
TypedVector_add(this, v2->array[i]);
v2->items = 0;
TypedVector_delete(v2);
assert(TypedVector_isConsistent(this));
}
void TypedVector_add(TypedVector* this, void* data_) {
assert(data_ && ((Object*)data_)->class == this->vectorType);
Object* data = data_;
assert(TypedVector_isConsistent(this));
TypedVector_set(this, this->items, data);
assert(TypedVector_isConsistent(this));
}
inline int TypedVector_indexOf(TypedVector* this, void* search_) {
assert(((Object*)search_)->class == this->vectorType);
Object* search = search_;
assert(TypedVector_isConsistent(this));
int i;
for (i = 0; i < this->items; i++) {
Object* o = (Object*)this->array[i];
if (o && o->compare(o, search) == 0)
return i;
}
return -1;
}
void TypedVector_foreach(TypedVector* this, TypedVector_procedure f) {
int i;
assert(TypedVector_isConsistent(this));
for (i = 0; i < this->items; i++)
f(this->array[i]);
assert(TypedVector_isConsistent(this));
}
/* Do not edit this file. It was automatically genarated. */
#ifndef HEADER_TypedVector
#define HEADER_TypedVector
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "Object.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "debug.h"
#include <assert.h>
#ifndef DEFAULT_SIZE
#define DEFAULT_SIZE -1
#endif
typedef void(*TypedVector_procedure)(void*);
typedef struct TypedVector_ {
Object **array;
int arraySize;
int growthRate;
int items;
char* vectorType;
bool owner;
} TypedVector;
TypedVector* TypedVector_new(char* vectorType_, bool owner, int size);
void TypedVector_delete(TypedVector* this);
void TypedVector_prune(TypedVector* this);
void TypedVector_sort(TypedVector* this);
void TypedVector_insert(TypedVector* this, int index, void* data_);
Object* TypedVector_take(TypedVector* this, int index);
Object* TypedVector_remove(TypedVector* this, int index);
void TypedVector_moveUp(TypedVector* this, int index);
void TypedVector_moveDown(TypedVector* this, int index);
void TypedVector_set(TypedVector* this, int index, void* data_);
inline Object* TypedVector_get(TypedVector* this, int index);
inline int TypedVector_size(TypedVector* this);
void TypedVector_merge(TypedVector* this, TypedVector* v2);
void TypedVector_add(TypedVector* this, void* data_);
inline int TypedVector_indexOf(TypedVector* this, void* search_);
void TypedVector_foreach(TypedVector* this, TypedVector_procedure f);
#endif
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "UptimeMeter.h"
#include "Meter.h"
#include "ProcessList.h"
#include "CRT.h"
#include "debug.h"
/*{
typedef struct UptimeMeter_ UptimeMeter;
struct UptimeMeter_ {
Meter super;
ProcessList* pl;
int seconds;
int minutes;
int hours;
int days;
};
}*/
UptimeMeter* UptimeMeter_new() {
UptimeMeter* this = malloc(sizeof(UptimeMeter));
Meter_init((Meter*)this, String_copy("Uptime"), String_copy("Uptime: "), 1);
((Meter*)this)->attributes[0] = UPTIME;
((Object*)this)->display = UptimeMeter_display;
((Meter*)this)->setValues = UptimeMeter_setValues;
Meter_setMode((Meter*)this, TEXT);
((Meter*)this)->total = 100.0;
return this;
}
void UptimeMeter_setValues(Meter* cast) {
UptimeMeter* this = (UptimeMeter*)cast;
double uptime;
FILE* fd = fopen(PROCDIR "/uptime", "r");
fscanf(fd, "%lf", &uptime);
fclose(fd);
int totalseconds = (int) ceil(uptime);
this->seconds = totalseconds % 60;
this->minutes = (totalseconds-this->seconds) % 3600 / 60;
this->hours = (totalseconds-this->seconds-(this->minutes*60)) % 86400 / 3600;
this->days = (totalseconds-this->seconds-(this->minutes*60)-(this->hours*3600)) / 86400;
cast->values[0] = this->days;
if (this->days > cast->total) {
cast->total = this->days;
}
snprintf(cast->displayBuffer.c, 14, "%d", this->days);
}
void UptimeMeter_display(Object* cast, RichString* out) {
UptimeMeter* this = (UptimeMeter*)cast;
char buffer[20];
RichString_prune(out);
if (this->days > 100) {
sprintf(buffer, "%d days, ", this->days);
RichString_write(out, CRT_colors[LARGE_NUMBER], buffer);
} else if (this->days > 1) {
sprintf(buffer, "%d days, ", this->days);
RichString_write(out, CRT_colors[UPTIME], buffer);
} else if (this->days == 1) {
sprintf(buffer, "%d day, ", this->days);
RichString_write(out, CRT_colors[UPTIME], buffer);
}
sprintf(buffer, "%02d:%02d:%02d ", this->hours, this->minutes, this->seconds);
RichString_append(out, CRT_colors[UPTIME], buffer);
}
/* Do not edit this file. It was automatically genarated. */
#ifndef HEADER_UptimeMeter
#define HEADER_UptimeMeter
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "Meter.h"
#include "ProcessList.h"
#include "CRT.h"
#include "debug.h"
typedef struct UptimeMeter_ UptimeMeter;
struct UptimeMeter_ {
Meter super;
ProcessList* pl;
int seconds;
int minutes;
int hours;
int days;
};
UptimeMeter* UptimeMeter_new();
void UptimeMeter_setValues(Meter* cast);
void UptimeMeter_display(Object* cast, RichString* out);
#endif
/*
htop - UsersTable.c
(C) 2004,2005 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "UsersTable.h"
#include "Hashtable.h"
#include "String.h"
#include <stdio.h>
#include <strings.h>
#include <pwd.h>
#include <sys/types.h>
#include "debug.h"
#include <assert.h>
/*{
typedef struct UsersTable_ {
Hashtable* users;
} UsersTable;
}*/
UsersTable* UsersTable_new() {
UsersTable* this;
this = malloc(sizeof(UsersTable));
this->users = Hashtable_new(20, true);
return this;
}
void UsersTable_delete(UsersTable* this) {
Hashtable_delete(this->users);
free(this);
}
char* UsersTable_getRef(UsersTable* this, int uid) {
char* name = (char*) (Hashtable_get(this->users, uid));
if (name == NULL) {
struct passwd* userData = getpwuid(uid);
if (userData != NULL) {
name = String_copy(userData->pw_name);
Hashtable_put(this->users, uid, name);
}
}
return name;
}
inline int UsersTable_size(UsersTable* this) {
return (Hashtable_size(this->users));
}
inline void UsersTable_foreach(UsersTable* this, Hashtable_PairFunction f, void* userData) {
Hashtable_foreach(this->users, f, userData);
}
/* Do not edit this file. It was automatically genarated. */
#ifndef HEADER_UsersTable
#define HEADER_UsersTable
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "Hashtable.h"
#include "String.h"
#include <stdio.h>
#include <strings.h>
#include <pwd.h>
#include <sys/types.h>
#include "debug.h"
#include <assert.h>
#ifndef SYSCONFDIR
#define SYSCONFDIR "/etc"
#endif
typedef struct UsersTable_ {
Hashtable* users;
} UsersTable;
UsersTable* UsersTable_new();
void UsersTable_delete(UsersTable* this);
char* UsersTable_getRef(UsersTable* this, int uid);
inline int UsersTable_size(UsersTable* this);
void UsersTable_foreach(UsersTable*, Hashtable_PairFunction, void*);
#endif
#!/bin/sh
aclocal
autoconf
autoheader
automake
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment