1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-06 05:17:57 +00:00

cleaned code; adding initial graph stuff, I will probably finish this later, but I wanted to get started

git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@22 7f574dfc-610e-0410-a909-a81674777703
This commit is contained in:
Brenden Matthews 2005-07-28 04:48:27 +00:00
parent d898d143ef
commit b6632fd510
14 changed files with 3178 additions and 2673 deletions

45
cairo.c
View File

@ -23,10 +23,7 @@
/* Fill the given area with checks in the standard style /* Fill the given area with checks in the standard style
* for showing compositing effects. * for showing compositing effects.
*/ */
static void static void fill_checks(cairo_t * cr, int x, int y, int width, int height)
fill_checks (cairo_t *cr,
int x, int y,
int width, int height)
{ {
cairo_surface_t *check; cairo_surface_t *check;
cairo_pattern_t *check_pattern; cairo_pattern_t *check_pattern;
@ -35,7 +32,8 @@ static void
#define CHECK_SIZE 32 #define CHECK_SIZE 32
check = cairo_surface_create_similar (cairo_current_target_surface (cr), check =
cairo_surface_create_similar(cairo_current_target_surface(cr),
CAIRO_FORMAT_RGB24, CAIRO_FORMAT_RGB24,
2 * CHECK_SIZE, 2 * CHECK_SIZE); 2 * CHECK_SIZE, 2 * CHECK_SIZE);
cairo_surface_set_repeat(check, 1); cairo_surface_set_repeat(check, 1);
@ -57,7 +55,8 @@ static void
cairo_rectangle(cr, x, y, CHECK_SIZE, CHECK_SIZE); cairo_rectangle(cr, x, y, CHECK_SIZE, CHECK_SIZE);
cairo_fill(cr); cairo_fill(cr);
cairo_rectangle (cr, x + CHECK_SIZE, y + CHECK_SIZE, CHECK_SIZE, CHECK_SIZE); cairo_rectangle(cr, x + CHECK_SIZE, y + CHECK_SIZE,
CHECK_SIZE, CHECK_SIZE);
cairo_fill(cr); cairo_fill(cr);
cairo_restore(cr); cairo_restore(cr);
@ -82,10 +81,7 @@ static void draw_pee (cairo_t *cr, double xc, double yc)
cairo_show_text(cr, "Conky"); cairo_show_text(cr, "Conky");
} }
static void static void draw(cairo_t * cr, int width, int height)
draw (cairo_t *cr,
int width,
int height)
{ {
cairo_surface_t *overlay; cairo_surface_t *overlay;
@ -93,9 +89,10 @@ static void
double xc = width / 2.; double xc = width / 2.;
double yc = height / 2.; double yc = height / 2.;
overlay = cairo_surface_create_similar (cairo_current_target_surface (cr), overlay =
CAIRO_FORMAT_ARGB32, cairo_surface_create_similar(cairo_current_target_surface(cr),
width, height); CAIRO_FORMAT_ARGB32, width,
height);
if (overlay == NULL) if (overlay == NULL)
return; return;
@ -115,8 +112,7 @@ static void
cairo_surface_destroy(overlay); cairo_surface_destroy(overlay);
} }
int int do_it(void)
do_it (void)
{ {
Display *dpy; Display *dpy;
int screen; int screen;
@ -138,7 +134,8 @@ int
w = XCreateSimpleWindow(dpy, RootWindow(dpy, screen), w = XCreateSimpleWindow(dpy, RootWindow(dpy, screen),
0, 0, width, height, 0, 0, 0, width, height, 0,
BlackPixel (dpy, screen), WhitePixel (dpy, screen)); BlackPixel(dpy, screen), WhitePixel(dpy,
screen));
normalhints = XAllocSizeHints(); normalhints = XAllocSizeHints();
normalhints->flags = 0; normalhints->flags = 0;
@ -161,12 +158,16 @@ int
XFree(classhint); XFree(classhint);
XFree(normalhints); XFree(normalhints);
pixmap = XCreatePixmap (dpy, w, width, height, DefaultDepth (dpy, screen)); pixmap =
XCreatePixmap(dpy, w, width, height,
DefaultDepth(dpy, screen));
gc = XCreateGC(dpy, pixmap, 0, NULL); gc = XCreateGC(dpy, pixmap, 0, NULL);
quit_keycode = XKeysymToKeycode(dpy, XStringToKeysym("Q")); quit_keycode = XKeysymToKeycode(dpy, XStringToKeysym("Q"));
XSelectInput (dpy, w, ExposureMask | StructureNotifyMask | ButtonPressMask | KeyPressMask); XSelectInput(dpy, w,
ExposureMask | StructureNotifyMask | ButtonPressMask |
KeyPressMask);
XMapWindow(dpy, w); XMapWindow(dpy, w);
needs_redraw = 1; needs_redraw = 1;
@ -188,9 +189,7 @@ int
cairo_destroy(cr); cairo_destroy(cr);
XCopyArea(dpy, pixmap, w, gc, XCopyArea(dpy, pixmap, w, gc,
0, 0, 0, 0, width, height, 0, 0);
width, height,
0, 0);
needs_redraw = 0; needs_redraw = 0;
} }
@ -210,7 +209,9 @@ int
width = xev.xconfigure.width; width = xev.xconfigure.width;
height = xev.xconfigure.height; height = xev.xconfigure.height;
XFreePixmap(dpy, pixmap); XFreePixmap(dpy, pixmap);
pixmap = XCreatePixmap (dpy, w, width, height, DefaultDepth (dpy, screen)); pixmap =
XCreatePixmap(dpy, w, width, height,
DefaultDepth(dpy, screen));
needs_redraw = 1; needs_redraw = 1;
break; break;
case Expose: case Expose:

View File

@ -53,8 +53,7 @@ void variable_substitute(const char *s, char *dest, unsigned int n)
s++; s++;
} else { } else {
a = s; a = s;
while (*s while (*s && (isalnum((int) *s)
&& (isalnum((int) *s)
|| *s == '_')) || *s == '_'))
s++; s++;
} }
@ -197,7 +196,8 @@ void update_stuff()
update_top(); update_top();
#ifdef MLDONKEY #ifdef MLDONKEY
if (NEED(INFO_MLDONKEY)) get_mldonkey_status(&mlconfig, &mlinfo); if (NEED(INFO_MLDONKEY))
get_mldonkey_status(&mlconfig, &mlinfo);
#endif #endif
#ifdef SETI #ifdef SETI
@ -214,7 +214,9 @@ void update_stuff()
update_load_average(); update_load_average();
#ifdef METAR #ifdef METAR
if (NEED(INFO_METAR) && current_update_time - last_metar_update > 1200.9 && info.looped) { if (NEED(INFO_METAR)
&& current_update_time - last_metar_update > 1200.9
&& info.looped) {
update_metar(); update_metar();
last_metar_update = current_update_time; last_metar_update = current_update_time;
} }

985
conky.c

File diff suppressed because it is too large Load Diff

View File

@ -72,6 +72,8 @@ struct mpd_s {
char host[128]; char host[128];
float progress; float progress;
int bitrate; int bitrate;
int length;
int elapsed;
}; };
#endif #endif
@ -385,4 +387,10 @@ extern int show_nice_processes;
extern int do_it(void); extern int do_it(void);
/* struct for graphs */
typedef struct graph {
unsigned int *value;
} graph;
#endif #endif

View File

@ -30,7 +30,8 @@ static inline unsigned long long int rdtsc( void );
/* cpu frequency detection code based on mplayer's one */ /* cpu frequency detection code based on mplayer's one */
static unsigned int get_timer() { static unsigned int get_timer()
{
struct timeval tv; struct timeval tv;
struct timezone tz; struct timezone tz;
gettimeofday(&tv, &tz); gettimeofday(&tv, &tz);
@ -410,15 +411,18 @@ char *get_acpi_fan()
return ""; return "";
} }
char* get_adt746x_cpu() { char *get_adt746x_cpu()
{
return ""; return "";
} }
char* get_adt746x_fan() { char *get_adt746x_fan()
{
return ""; return "";
} }
char* get_freq() { char *get_freq()
{
#if defined(i386) || defined(__i386__) #if defined(i386) || defined(__i386__)
int i; int i;
char *cpuspeed; char *cpuspeed;
@ -430,7 +434,8 @@ char* get_freq() {
if ((i = get_cpu_speed()) > 0) { if ((i = get_cpu_speed()) > 0) {
if (i < 1000000) { if (i < 1000000) {
i += 50; /* for rounding */ i += 50; /* for rounding */
snprintf(cpuspeed, 15, "%d.%d MHz", i/1000, (i/100)%10); snprintf(cpuspeed, 15, "%d.%d MHz", i / 1000,
(i / 100) % 10);
} else { } else {
snprintf(cpuspeed, 15, "%d MHz", i / 1000); snprintf(cpuspeed, 15, "%d MHz", i / 1000);
} }

343
ftp.c
View File

@ -35,7 +35,8 @@ static int dataFd = -1;
* Initialize the FTP handling. * Initialize the FTP handling.
*/ */
void initFtp(void) { void initFtp(void)
{
gethostname(hostname, sizeof(hostname)); gethostname(hostname, sizeof(hostname));
} }
@ -45,10 +46,12 @@ void initFtp(void) {
* +XXX for last line of response * +XXX for last line of response
* -XXX for response to be continued * -XXX for response to be continued
*/ */
int parseFtpResponse(char *buf, int len) { int parseFtpResponse(char *buf, int len)
{
int val = 0; int val = 0;
if (len < 3) return(-1); if (len < 3)
return (-1);
if ((*buf >= '0') && (*buf <= '9')) if ((*buf >= '0') && (*buf <= '9'))
val = val * 10 + (*buf - '0'); val = val * 10 + (*buf - '0');
else else
@ -74,16 +77,19 @@ int parseFtpResponse(char *buf, int len) {
* Returns the code number * Returns the code number
* *
*/ */
int readFtpResponse(char *buf, int size) { int readFtpResponse(char *buf, int size)
{
char *ptr, *end; char *ptr, *end;
int len; int len;
int res = -1; int res = -1;
if (size <= 0) return(-1); if (size <= 0)
return (-1);
get_more: get_more:
if ((len = recv(ftpFd, buf, size - 1, 0)) < 0) { if ((len = recv(ftpFd, buf, size - 1, 0)) < 0) {
close(ftpFd); ftpFd = -1; close(ftpFd);
ftpFd = -1;
ftpFd = -1; ftpFd = -1;
return (-1); return (-1);
} }
@ -99,24 +105,31 @@ get_more:
ptr = buf; ptr = buf;
while (ptr < end) { while (ptr < end) {
res = parseFtpResponse(ptr, end - ptr); res = parseFtpResponse(ptr, end - ptr);
if (res > 0) break; if (res > 0)
break;
if (res == 0) { if (res == 0) {
#ifdef DEBUG_FTP #ifdef DEBUG_FTP
fprintf(stderr, "readFtpResponse failed: %s\n", ptr); fprintf(stderr, "readFtpResponse failed: %s\n",
ptr);
#endif #endif
return (-1); return (-1);
} }
while ((ptr < end) && (*ptr != '\n')) ptr++; while ((ptr < end) && (*ptr != '\n'))
ptr++;
if (ptr >= end) { if (ptr >= end) {
#ifdef DEBUG_FTP #ifdef DEBUG_FTP
fprintf(stderr, "readFtpResponse: unexpected end %s\n", buf); fprintf(stderr,
"readFtpResponse: unexpected end %s\n",
buf);
#endif #endif
return ((-res) / 100); return ((-res) / 100);
} }
if (*ptr != '\r') ptr++; if (*ptr != '\r')
ptr++;
} }
if (res < 0) goto get_more; if (res < 0)
goto get_more;
#ifdef DEBUG_FTP #ifdef DEBUG_FTP
printf("Got %d\n", res); printf("Got %d\n", res);
@ -129,7 +142,8 @@ get_more:
* Returns the code number * Returns the code number
* *
*/ */
int getFtpResponse(void) { int getFtpResponse(void)
{
char buf[16 * 1024 + 1]; char buf[16 * 1024 + 1];
/************** /**************
@ -152,7 +166,8 @@ int getFtpResponse(void) {
* Check if there is a response from the FTP server after a command. * Check if there is a response from the FTP server after a command.
* Returns the code number, or 0 * Returns the code number, or 0
*/ */
int checkFtpResponse(void) { int checkFtpResponse(void)
{
char buf[1024 + 1]; char buf[1024 + 1];
fd_set rfd; fd_set rfd;
struct timeval tv; struct timeval tv;
@ -179,7 +194,8 @@ int checkFtpResponse(void) {
* Send the user authentification * Send the user authentification
*/ */
int sendUser(void) { int sendUser(void)
{
char buf[200]; char buf[200];
int len; int len;
int res; int res;
@ -189,7 +205,8 @@ int sendUser(void) {
printf(buf); printf(buf);
#endif #endif
res = send(ftpFd, buf, len, 0); res = send(ftpFd, buf, len, 0);
if (res < 0) return(res); if (res < 0)
return (res);
return (0); return (0);
} }
@ -197,17 +214,20 @@ int sendUser(void) {
* Send the password authentification * Send the password authentification
*/ */
int sendPasswd(void) { int sendPasswd(void)
{
char buf[200]; char buf[200];
int len; int len;
int res; int res;
len = snprintf(buf, sizeof(buf), "PASS mirrorfind@%s\r\n", hostname); len =
snprintf(buf, sizeof(buf), "PASS mirrorfind@%s\r\n", hostname);
#ifdef DEBUG_FTP #ifdef DEBUG_FTP
printf(buf); printf(buf);
#endif #endif
res = send(ftpFd, buf, len, 0); res = send(ftpFd, buf, len, 0);
if (res < 0) return(res); if (res < 0)
return (res);
return (0); return (0);
} }
@ -215,7 +235,8 @@ int sendPasswd(void) {
* Send a QUIT * Send a QUIT
*/ */
int sendQuit(void) { int sendQuit(void)
{
char buf[200]; char buf[200];
int len; int len;
int res; int res;
@ -232,7 +253,8 @@ int sendQuit(void) {
* Connecting to the server, port 21 by default. * Connecting to the server, port 21 by default.
*/ */
int connectFtp(const char *server, int port) { int connectFtp(const char *server, int port)
{
struct hostent *hp; struct hostent *hp;
int res; int res;
@ -261,7 +283,8 @@ int connectFtp(const char *server, int port) {
*/ */
if (connect(ftpFd, (struct sockaddr *) &ftpAddr, if (connect(ftpFd, (struct sockaddr *) &ftpAddr,
sizeof(struct sockaddr_in)) < 0) { sizeof(struct sockaddr_in)) < 0) {
close(ftpFd); ftpFd = -1; close(ftpFd);
ftpFd = -1;
ftpFd = -1; ftpFd = -1;
return (-1); return (-1);
} }
@ -271,7 +294,8 @@ int connectFtp(const char *server, int port) {
*/ */
res = getFtpResponse(); res = getFtpResponse();
if (res != 2) { if (res != 2) {
close(ftpFd); ftpFd = -1; close(ftpFd);
ftpFd = -1;
ftpFd = -1; ftpFd = -1;
return (-1); return (-1);
} }
@ -310,7 +334,8 @@ int connectFtp(const char *server, int port) {
*/ */
res = sendUser(); res = sendUser();
if (res < 0) { if (res < 0) {
close(ftpFd); ftpFd = -1; close(ftpFd);
ftpFd = -1;
ftpFd = -1; ftpFd = -1;
return (-1); return (-1);
} }
@ -325,13 +350,15 @@ int connectFtp(const char *server, int port) {
case 5: case 5:
case -1: case -1:
default: default:
close(ftpFd); ftpFd = -1; close(ftpFd);
ftpFd = -1;
ftpFd = -1; ftpFd = -1;
return (-1); return (-1);
} }
res = sendPasswd(); res = sendPasswd();
if (res < 0) { if (res < 0) {
close(ftpFd); ftpFd = -1; close(ftpFd);
ftpFd = -1;
ftpFd = -1; ftpFd = -1;
return (-1); return (-1);
} }
@ -340,13 +367,15 @@ int connectFtp(const char *server, int port) {
case 2: case 2:
return (0); return (0);
case 3: case 3:
fprintf(stderr, "FTP server asking for ACCNT on anonymous\n"); fprintf(stderr,
"FTP server asking for ACCNT on anonymous\n");
case 1: case 1:
case 4: case 4:
case 5: case 5:
case -1: case -1:
default: default:
close(ftpFd); ftpFd = -1; close(ftpFd);
ftpFd = -1;
ftpFd = -1; ftpFd = -1;
return (-1); return (-1);
} }
@ -358,7 +387,8 @@ int connectFtp(const char *server, int port) {
* Check an FTP directory on the server * Check an FTP directory on the server
*/ */
int changeFtpDirectory(char *directory) { int changeFtpDirectory(char *directory)
{
char buf[400]; char buf[400];
int len; int len;
int res; int res;
@ -375,14 +405,17 @@ int changeFtpDirectory(char *directory) {
printf(buf); printf(buf);
#endif #endif
res = send(ftpFd, buf, len, 0); res = send(ftpFd, buf, len, 0);
if (res < 0) return(res); if (res < 0)
return (res);
res = getFtpResponse(); res = getFtpResponse();
if (res == 4) { if (res == 4) {
close(ftpFd); ftpFd = -1; close(ftpFd);
ftpFd = -1;
ftpFd = -1; ftpFd = -1;
return (-1); return (-1);
} }
if (res == 2) return(1); if (res == 2)
return (1);
if (res == 5) { if (res == 5) {
return (0); return (0);
} }
@ -392,7 +425,8 @@ int changeFtpDirectory(char *directory) {
/* /*
* dataConnectFtp * dataConnectFtp
*/ */
int dataConnectFtp() { int dataConnectFtp()
{
char buf[200]; char buf[200];
int len, i; int len, i;
int res; int res;
@ -403,7 +437,8 @@ int dataConnectFtp() {
dataFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); dataFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (dataFd < 0) { if (dataFd < 0) {
fprintf(stderr, "dataConnectFtp: failed to create socket\n"); fprintf(stderr,
"dataConnectFtp: failed to create socket\n");
} }
dataAddrLen = sizeof(dataAddr); dataAddrLen = sizeof(dataAddr);
memset(&dataAddr, 0, dataAddrLen); memset(&dataAddr, 0, dataAddrLen);
@ -416,58 +451,77 @@ int dataConnectFtp() {
#endif #endif
res = send(ftpFd, buf, len, 0); res = send(ftpFd, buf, len, 0);
if (res < 0) { if (res < 0) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (res); return (res);
} }
res = readFtpResponse(buf, sizeof(buf) - 1); res = readFtpResponse(buf, sizeof(buf) - 1);
if (res != 2) { if (res != 2) {
if (res == 5) { if (res == 5) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-1); return (-1);
} else { } else {
/* /*
* retry with an active connection * retry with an active connection
*/ */
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
ftpPassive = 0; ftpPassive = 0;
} }
} }
cur = &buf[4]; cur = &buf[4];
while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++; while (((*cur < '0') || (*cur > '9')) && *cur != '\0')
if (sscanf(cur, "%d,%d,%d,%d,%d,%d", &temp[0], &temp[1], &temp[2], cur++;
&temp[3], &temp[4], &temp[5]) != 6) { if (sscanf
(cur, "%d,%d,%d,%d,%d,%d", &temp[0], &temp[1],
&temp[2], &temp[3], &temp[4], &temp[5]) != 6) {
fprintf(stderr, "Invalid answer to PASV\n"); fprintf(stderr, "Invalid answer to PASV\n");
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-1); return (-1);
} }
for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff); for (i = 0; i < 6; i++)
ad[i] = (unsigned char) (temp[i] & 0xff);
memcpy(&dataAddr.sin_addr, &ad[0], 4); memcpy(&dataAddr.sin_addr, &ad[0], 4);
memcpy(&dataAddr.sin_port, &ad[4], 2); memcpy(&dataAddr.sin_port, &ad[4], 2);
if (connect(dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) { if (connect
fprintf(stderr, "Failed to create a data connection\n"); (dataFd, (struct sockaddr *) &dataAddr,
close(dataFd); dataFd = -1; dataAddrLen) < 0) {
fprintf(stderr,
"Failed to create a data connection\n");
close(dataFd);
dataFd = -1;
return (-1); return (-1);
} }
} else { } else {
getsockname(dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen); getsockname(dataFd, (struct sockaddr *) &dataAddr,
&dataAddrLen);
dataAddr.sin_port = 0; dataAddr.sin_port = 0;
if (bind(dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) { if (bind
(dataFd, (struct sockaddr *) &dataAddr,
dataAddrLen) < 0) {
fprintf(stderr, "Failed to bind a port\n"); fprintf(stderr, "Failed to bind a port\n");
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-1); return (-1);
} }
getsockname(dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen); getsockname(dataFd, (struct sockaddr *) &dataAddr,
&dataAddrLen);
if (listen(dataFd, 1) < 0) { if (listen(dataFd, 1) < 0) {
fprintf(stderr, "Could not listen on port %d\n", fprintf(stderr, "Could not listen on port %d\n",
ntohs(dataAddr.sin_port)); ntohs(dataAddr.sin_port));
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-1); return (-1);
} }
adp = (unsigned char *) &dataAddr.sin_addr; adp = (unsigned char *) &dataAddr.sin_addr;
portp = (unsigned char *) &dataAddr.sin_port; portp = (unsigned char *) &dataAddr.sin_port;
len = snprintf(buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n", len =
adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff, snprintf(buf, sizeof(buf),
"PORT %d,%d,%d,%d,%d,%d\r\n", adp[0] & 0xff,
adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
portp[0] & 0xff, portp[1] & 0xff); portp[0] & 0xff, portp[1] & 0xff);
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = 0;
#ifdef DEBUG_FTP #ifdef DEBUG_FTP
@ -476,12 +530,14 @@ int dataConnectFtp() {
res = send(ftpFd, buf, len, 0); res = send(ftpFd, buf, len, 0);
if (res < 0) { if (res < 0) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (res); return (res);
} }
res = getFtpResponse(); res = getFtpResponse();
if (res != 2) { if (res != 2) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-1); return (-1);
} }
} }
@ -492,14 +548,18 @@ int dataConnectFtp() {
/* /*
* dataConnectEndFtp * dataConnectEndFtp
*/ */
int dataConnectEndFtp() { int dataConnectEndFtp()
{
int res; int res;
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
res = getFtpResponse(); res = getFtpResponse();
if (res != 2) { if (res != 2) {
close(dataFd); dataFd = -1; close(dataFd);
close(ftpFd); ftpFd = -1; dataFd = -1;
close(ftpFd);
ftpFd = -1;
return (-1); return (-1);
} }
return (0); return (0);
@ -509,7 +569,9 @@ int dataConnectEndFtp() {
* parseListFtp * parseListFtp
*/ */
int parseListFtp(const char *list, ftpListCallback callback, void *userData) { int parseListFtp(const char *list, ftpListCallback callback,
void *userData)
{
const char *cur = list; const char *cur = list;
char filename[151]; char filename[151];
char attrib[11]; char attrib[11];
@ -526,7 +588,8 @@ int parseListFtp(const char *list, ftpListCallback callback, void *userData) {
if (!strncmp(cur, "total", 5)) { if (!strncmp(cur, "total", 5)) {
cur += 5; cur += 5;
while (*cur == ' ') cur++; while (*cur == ' ')
cur++;
while ((*cur >= '0') && (*cur <= '9')) while ((*cur >= '0') && (*cur <= '9'))
links = (links * 10) + (*cur++ - '0'); links = (links * 10) + (*cur++ - '0');
while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r')) while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
@ -537,78 +600,102 @@ int parseListFtp(const char *list, ftpListCallback callback, void *userData) {
} else { } else {
while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r')) while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
cur++; cur++;
if (*cur == 0) return(0); if (*cur == 0)
return (0);
i = 0; i = 0;
while (*cur != ' ') { while (*cur != ' ') {
if (i < 10) if (i < 10)
attrib[i++] = *cur; attrib[i++] = *cur;
cur++; cur++;
if (*cur == 0) return(0); if (*cur == 0)
return (0);
} }
attrib[10] = 0; attrib[10] = 0;
while (*cur == ' ') cur++; while (*cur == ' ')
if (*cur == 0) return(0); cur++;
if (*cur == 0)
return (0);
while ((*cur >= '0') && (*cur <= '9')) while ((*cur >= '0') && (*cur <= '9'))
links = (links * 10) + (*cur++ - '0'); links = (links * 10) + (*cur++ - '0');
while (*cur == ' ') cur++; while (*cur == ' ')
if (*cur == 0) return(0); cur++;
if (*cur == 0)
return (0);
i = 0; i = 0;
while (*cur != ' ') { while (*cur != ' ') {
if (i < 10) if (i < 10)
owner[i++] = *cur; owner[i++] = *cur;
cur++; cur++;
if (*cur == 0) return(0); if (*cur == 0)
return (0);
} }
owner[i] = 0; owner[i] = 0;
while (*cur == ' ') cur++; while (*cur == ' ')
if (*cur == 0) return(0); cur++;
if (*cur == 0)
return (0);
i = 0; i = 0;
while (*cur != ' ') { while (*cur != ' ') {
if (i < 10) if (i < 10)
group[i++] = *cur; group[i++] = *cur;
cur++; cur++;
if (*cur == 0) return(0); if (*cur == 0)
return (0);
} }
group[i] = 0; group[i] = 0;
while (*cur == ' ') cur++; while (*cur == ' ')
if (*cur == 0) return(0); cur++;
if (*cur == 0)
return (0);
while ((*cur >= '0') && (*cur <= '9')) while ((*cur >= '0') && (*cur <= '9'))
size = (size * 10) + (*cur++ - '0'); size = (size * 10) + (*cur++ - '0');
while (*cur == ' ') cur++; while (*cur == ' ')
if (*cur == 0) return(0); cur++;
if (*cur == 0)
return (0);
i = 0; i = 0;
while (*cur != ' ') { while (*cur != ' ') {
if (i < 3) if (i < 3)
month[i++] = *cur; month[i++] = *cur;
cur++; cur++;
if (*cur == 0) return(0); if (*cur == 0)
return (0);
} }
month[i] = 0; month[i] = 0;
while (*cur == ' ') cur++; while (*cur == ' ')
if (*cur == 0) return(0); cur++;
if (*cur == 0)
return (0);
while ((*cur >= '0') && (*cur <= '9')) while ((*cur >= '0') && (*cur <= '9'))
day = (day * 10) + (*cur++ - '0'); day = (day * 10) + (*cur++ - '0');
while (*cur == ' ') cur++; while (*cur == ' ')
if (*cur == 0) return(0); cur++;
if ((cur[1] == 0) || (cur[2] == 0)) return(0); if (*cur == 0)
return (0);
if ((cur[1] == 0) || (cur[2] == 0))
return (0);
if ((cur[1] == ':') || (cur[2] == ':')) { if ((cur[1] == ':') || (cur[2] == ':')) {
while ((*cur >= '0') && (*cur <= '9')) while ((*cur >= '0') && (*cur <= '9'))
hour = (hour * 10) + (*cur++ - '0'); hour = (hour * 10) + (*cur++ - '0');
if (*cur == ':') cur++; if (*cur == ':')
cur++;
while ((*cur >= '0') && (*cur <= '9')) while ((*cur >= '0') && (*cur <= '9'))
minute = (minute * 10) + (*cur++ - '0'); minute = (minute * 10) + (*cur++ - '0');
} else { } else {
while ((*cur >= '0') && (*cur <= '9')) while ((*cur >= '0') && (*cur <= '9'))
year = (year * 10) + (*cur++ - '0'); year = (year * 10) + (*cur++ - '0');
} }
while (*cur == ' ') cur++; while (*cur == ' ')
if (*cur == 0) return(0); cur++;
if (*cur == 0)
return (0);
i = 0; i = 0;
while ((*cur != '\n') && (*cur != '\r')) { while ((*cur != '\n') && (*cur != '\r')) {
if (i < 150) if (i < 150)
filename[i++] = *cur; filename[i++] = *cur;
cur++; cur++;
if (*cur == 0) return(0); if (*cur == 0)
return (0);
} }
filename[i] = 0; filename[i] = 0;
if ((*cur != '\n') && (*cur != '\r')) if ((*cur != '\n') && (*cur != '\r'))
@ -617,8 +704,8 @@ int parseListFtp(const char *list, ftpListCallback callback, void *userData) {
cur++; cur++;
} }
if (callback != NULL) { if (callback != NULL) {
callback(userData, filename, attrib, owner, group, size, links, callback(userData, filename, attrib, owner, group, size,
year, month, day, minute); links, year, month, day, minute);
} }
return (cur - list); return (cur - list);
} }
@ -626,7 +713,8 @@ int parseListFtp(const char *list, ftpListCallback callback, void *userData) {
/* /*
* listFtp * listFtp
*/ */
int listFtp(ftpListCallback callback, void *userData) { int listFtp(ftpListCallback callback, void *userData)
{
char buf[4096 + 1]; char buf[4096 + 1];
int len, res; int len, res;
int index = 0, base; int index = 0, base;
@ -641,12 +729,14 @@ int listFtp(ftpListCallback callback, void *userData) {
#endif #endif
res = send(ftpFd, buf, len, 0); res = send(ftpFd, buf, len, 0);
if (res < 0) { if (res < 0) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (res); return (res);
} }
res = readFtpResponse(buf, sizeof(buf) - 1); res = readFtpResponse(buf, sizeof(buf) - 1);
if (res != 1) { if (res != 1) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-res); return (-res);
} }
@ -662,29 +752,35 @@ int listFtp(ftpListCallback callback, void *userData) {
#ifdef DEBUG_FTP #ifdef DEBUG_FTP
perror("select"); perror("select");
#endif #endif
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-1); return (-1);
} }
if (res == 0) { if (res == 0) {
res = checkFtpResponse(); res = checkFtpResponse();
if (res < 0) { if (res < 0) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
dataFd = -1; dataFd = -1;
return (-1); return (-1);
} }
if (res == 2) { if (res == 2) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (0); return (0);
} }
continue; continue;
} }
if ((len = read(dataFd, &buf[index], sizeof(buf) - (index + 1))) < 0) { if ((len =
read(dataFd, &buf[index],
sizeof(buf) - (index + 1))) < 0) {
#ifdef DEBUG_FTP #ifdef DEBUG_FTP
perror("read"); perror("read");
#endif #endif
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
dataFd = -1; dataFd = -1;
return (-1); return (-1);
} }
@ -710,7 +806,8 @@ int listFtp(ftpListCallback callback, void *userData) {
* getFtpSocket: * getFtpSocket:
*/ */
int getFtpSocket(const char *filename) { int getFtpSocket(const char *filename)
{
char buf[300]; char buf[300];
int res, len; int res, len;
if (filename == NULL) if (filename == NULL)
@ -723,12 +820,14 @@ int getFtpSocket(const char *filename) {
#endif #endif
res = send(ftpFd, buf, len, 0); res = send(ftpFd, buf, len, 0);
if (res < 0) { if (res < 0) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (res); return (res);
} }
res = readFtpResponse(buf, sizeof(buf) - 1); res = readFtpResponse(buf, sizeof(buf) - 1);
if (res != 2) { if (res != 2) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-res); return (-res);
} }
len = snprintf(buf, sizeof(buf), "RETR %s\r\n", filename); len = snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
@ -737,12 +836,14 @@ int getFtpSocket(const char *filename) {
#endif #endif
res = send(ftpFd, buf, len, 0); res = send(ftpFd, buf, len, 0);
if (res < 0) { if (res < 0) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (res); return (res);
} }
res = readFtpResponse(buf, sizeof(buf) - 1); res = readFtpResponse(buf, sizeof(buf) - 1);
if (res != 1) { if (res != 1) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-res); return (-res);
} }
return (dataFd); return (dataFd);
@ -752,14 +853,16 @@ int getFtpSocket(const char *filename) {
* closeFtpSocket * closeFtpSocket
*/ */
int closeFtpSocket() { int closeFtpSocket()
{
return (dataConnectEndFtp()); return (dataConnectEndFtp());
} }
/* /*
* listFtp * listFtp
*/ */
int getFtp(ftpDataCallback callback, void *userData, const char *filename) { int getFtp(ftpDataCallback callback, void *userData, const char *filename)
{
char buf[4096]; char buf[4096];
int len = 0, res; int len = 0, res;
fd_set rfd; fd_set rfd;
@ -782,18 +885,21 @@ int getFtp(ftpDataCallback callback, void *userData, const char *filename) {
#ifdef DEBUG_FTP #ifdef DEBUG_FTP
perror("select"); perror("select");
#endif #endif
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (-1); return (-1);
} }
if (res == 0) { if (res == 0) {
res = checkFtpResponse(); res = checkFtpResponse();
if (res < 0) { if (res < 0) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
dataFd = -1; dataFd = -1;
return (-1); return (-1);
} }
if (res == 2) { if (res == 2) {
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
return (0); return (0);
} }
@ -801,7 +907,8 @@ int getFtp(ftpDataCallback callback, void *userData, const char *filename) {
} }
if ((len = read(dataFd, &buf, sizeof(buf))) < 0) { if ((len = read(dataFd, &buf, sizeof(buf))) < 0) {
callback(userData, buf, len); callback(userData, buf, len);
close(dataFd); dataFd = -1; close(dataFd);
dataFd = -1;
ftpFd = -1; ftpFd = -1;
return (-1); return (-1);
} }
@ -815,22 +922,28 @@ int getFtp(ftpDataCallback callback, void *userData, const char *filename) {
* Disconnect from the FTP server. * Disconnect from the FTP server.
*/ */
int disconnectFtp(void) { int disconnectFtp(void)
if (ftpFd < 0) return(-1); {
if (ftpFd < 0)
return (-1);
sendQuit(); sendQuit();
close(ftpFd); ftpFd = -1; close(ftpFd);
ftpFd = -1;
ftpFd = -1; ftpFd = -1;
return (0); return (0);
} }
#ifdef STANDALONE #ifdef STANDALONE
void ftpList(void *userData, const char *filename, const char *attrib, void ftpList(void *userData, const char *filename, const char *attrib,
const char *owner, const char *group, unsigned long size, int links, const char *owner, const char *group, unsigned long size,
int year, const char *month, int day, int minute) { int links, int year, const char *month, int day, int minute)
{
printf("%s %s %s %ld %s\n", attrib, owner, group, size, filename); printf("%s %s %s %ld %s\n", attrib, owner, group, size, filename);
} }
void ftpData(void *userData, const char *data, int len) { void ftpData(void *userData, const char *data, int len)
if (userData == NULL) return; {
if (userData == NULL)
return;
if (len <= 0) { if (len <= 0) {
fclose(userData); fclose(userData);
return; return;
@ -838,7 +951,8 @@ void ftpData(void *userData, const char *data, int len) {
fwrite(data, len, 1, userData); fwrite(data, len, 1, userData);
} }
int main(int argc, char **argv) { int main(int argc, char **argv)
{
int res; int res;
FILE *output; FILE *output;
const char *tstfile = "tstfile"; const char *tstfile = "tstfile";
@ -880,7 +994,8 @@ int main(int argc, char **argv) {
output = fopen("/tmp/tstdata", "w"); output = fopen("/tmp/tstdata", "w");
if (output != NULL) { if (output != NULL) {
if (getFtp(ftpData, (void *) output, tstfile) < 0) if (getFtp(ftpData, (void *) output, tstfile) < 0)
fprintf(stderr, "Failed to get file %s\n", tstfile); fprintf(stderr, "Failed to get file %s\n",
tstfile);
} }
disconnectFtp(); disconnectFtp();

56
linux.c
View File

@ -219,8 +219,11 @@ inline void update_net_stats()
for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) { for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) {
struct net_stat *ns; struct net_stat *ns;
ns = get_net_stat(((struct ifreq*)conf.ifc_buf)[k].ifr_ifrn.ifrn_name); ns = get_net_stat(((struct ifreq *) conf.
ns->addr = ((struct ifreq*)conf.ifc_buf)[k].ifr_ifru.ifru_addr; ifc_buf)[k].ifr_ifrn.ifrn_name);
ns->addr =
((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.
ifru_addr;
} }
close((long) i); close((long) i);
@ -258,7 +261,8 @@ inline void update_net_stats()
/* fclose(net_dev_fp); net_dev_fp = NULL; */ /* fclose(net_dev_fp); net_dev_fp = NULL; */
} }
inline void update_wifi_stats() { inline void update_wifi_stats()
{
/** wireless stats patch by Bobby Beckmann **/ /** wireless stats patch by Bobby Beckmann **/
static int rep; static int rep;
int i; int i;
@ -268,7 +272,8 @@ inline void update_wifi_stats() {
net_wireless_fp = open_file("/proc/net/wireless", &rep); net_wireless_fp = open_file("/proc/net/wireless", &rep);
else else
fseek(net_wireless_fp, 0, SEEK_SET); fseek(net_wireless_fp, 0, SEEK_SET);
if (!net_wireless_fp) return; if (!net_wireless_fp)
return;
fgets(buf, 255, net_wireless_fp); /* garbage */ fgets(buf, 255, net_wireless_fp); /* garbage */
fgets(buf, 255, net_wireless_fp); /* garbage (field names) */ fgets(buf, 255, net_wireless_fp); /* garbage (field names) */
@ -279,22 +284,24 @@ inline void update_wifi_stats() {
char *s, *p; char *s, *p;
int l, m, n; int l, m, n;
if (fgets(buf, 255, net_wireless_fp) == NULL) break; if (fgets(buf, 255, net_wireless_fp) == NULL)
break;
p = buf; p = buf;
while (isspace((int) *p)) p++; while (isspace((int) *p))
p++;
s = p; s = p;
while (*p && *p != ':') p++; while (*p && *p != ':')
if (*p == '\0') continue; p++;
if (*p == '\0')
continue;
*p = '\0'; *p = '\0';
p++; p++;
ns = get_net_stat(s); ns = get_net_stat(s);
sscanf(p, sscanf(p, "%*d %d. %d. %d", &l, &m, &n);
"%*d %d. %d. %d",
&l, &m, &n);
ns->linkstatus = (int) (log(l) / log(92) * 100); ns->linkstatus = (int) (log(l) / log(92) * 100);
} }
@ -480,9 +487,9 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
type = "in"; type = "in";
if (strcmp(type, "tempf") == 0) { if (strcmp(type, "tempf") == 0) {
snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, "temp", n); snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, "temp",
} n);
else { } else {
snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, type, n); snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, type, n);
} }
strcpy(devtype, path); strcpy(devtype, path);
@ -492,7 +499,8 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
if (fd < 0) if (fd < 0)
ERR("can't open '%s': %s", path, strerror(errno)); ERR("can't open '%s': %s", path, strerror(errno));
if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0 || strcmp(type, "tempf") == 0) if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
|| strcmp(type, "tempf") == 0)
*div = 1; *div = 1;
else else
*div = 0; *div = 0;
@ -502,9 +510,9 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
/* test if *_div file exist, open it and use it as divisor */ /* test if *_div file exist, open it and use it as divisor */
if (strcmp(type, "tempf") == 0) { if (strcmp(type, "tempf") == 0) {
snprintf(path, 255, I2C_DIR "%s/%s%d_div", "one", "two", n); snprintf(path, 255, I2C_DIR "%s/%s%d_div", "one", "two",
} n);
else { } else {
snprintf(path, 255, I2C_DIR "%s/%s%d_div", dev, type, n); snprintf(path, 255, I2C_DIR "%s/%s%d_div", dev, type, n);
} }
@ -567,8 +575,7 @@ double get_i2c_info(int *fd, int div, char *devtype, char *type)
return ((val / 1000.0 + 40) * 9.0 / 5) - 40; return ((val / 1000.0 + 40) * 9.0 / 5) - 40;
else else
return ((val + 40) * 9.0 / 5) - 40; return ((val + 40) * 9.0 / 5) - 40;
} } else {
else {
if (div > 1) if (div > 1)
return val / div; return val / div;
else if (div) else if (div)
@ -673,8 +680,7 @@ char *get_freq()
microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) + microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
(tvstop.tv_usec - tvstart.tv_usec); (tvstop.tv_usec - tvstart.tv_usec);
sprintf(buffer, "%lldMHz", sprintf(buffer, "%lldMHz", (cycles[1] - cycles[0]) / microseconds);
(cycles[1]-cycles[0])/microseconds);
return buffer; return buffer;
} }
@ -973,9 +979,11 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat)
/* charged */ /* charged */
/* thanks to Lukas Zapletal <lzap@seznam.cz> */ /* thanks to Lukas Zapletal <lzap@seznam.cz> */
else if (strcmp(charging_state, "charged") == 0) { else if (strcmp(charging_state, "charged") == 0) {
if (acpi_last_full != 0 && remaining_capacity != acpi_last_full) if (acpi_last_full != 0
&& remaining_capacity != acpi_last_full)
sprintf(last_battery_str, "charged %d%%", sprintf(last_battery_str, "charged %d%%",
remaining_capacity * 100 / acpi_last_full); remaining_capacity * 100 /
acpi_last_full);
else else
strcpy(last_battery_str, "charged"); strcpy(last_battery_str, "charged");
} }

View File

@ -10,22 +10,30 @@
static int status = 0; static int status = 0;
int calculateRelativeHumidity(int temp, int dew) { int calculateRelativeHumidity(int temp, int dew)
{
float rh = 0.0; float rh = 0.0;
rh = 100.0 * powf ((112 - (0.1 * temp)+dew ) / (112 + (0.9 * temp)), 8); rh = 100.0 * powf((112 - (0.1 * temp) + dew) /
(112 + (0.9 * temp)), 8);
return (int) rh; return (int) rh;
} }
int calculateWindChill(int temperatureC, int windKn) { int calculateWindChill(int temperatureC, int windKn)
{
double windKmh = knTokph(windKn); double windKmh = knTokph(windKn);
return (int)(13.112+0.6215*temperatureC - 11.37*powf(windKmh,.16) + 0.3965*temperatureC*powf(windKmh,.16) ); return (int) (13.112 + 0.6215 * temperatureC -
11.37 * powf(windKmh,
.16) +
0.3965 * temperatureC * powf(windKmh, .16));
} }
int knTokph(int knSpeed ){ int knTokph(int knSpeed)
{
return (knSpeed * 1.852); return (knSpeed * 1.852);
} }
const char *calculateWindDirectionString(int degree) { const char *calculateWindDirectionString(int degree)
{
if (degree < 22.5) { if (degree < 22.5) {
return "North"; return "North";
} else if (degree < 67.5) { } else if (degree < 67.5) {
@ -46,7 +54,8 @@ const char *calculateWindDirectionString(int degree) {
return "North"; return "North";
} }
} }
const char*calculateShortWindDirectionString(int degree){ const char *calculateShortWindDirectionString(int degree)
{
if (degree < 22.5) { if (degree < 22.5) {
return "N"; return "N";
} else if (degree < 67.5) { } else if (degree < 67.5) {
@ -70,7 +79,8 @@ const char*calculateShortWindDirectionString(int degree){
void ftpData(void *userData, const char *data, int len) void ftpData(void *userData, const char *data, int len)
{ {
if(userData) {} // do nothing to make stupid warning go away if (userData) {
} // do nothing to make stupid warning go away
if (len <= 0) if (len <= 0)
return; return;
@ -81,7 +91,8 @@ void ftpData(void *userData, const char *data, int len)
extern unsigned int sleep(unsigned int); extern unsigned int sleep(unsigned int);
void *fetch_ftp( ) { void *fetch_ftp()
{
// try three times if it fails // try three times if it fails
int tries = 0; int tries = 0;
do { do {
@ -98,7 +109,8 @@ void *fetch_ftp( ) {
if (metar_server == NULL) if (metar_server == NULL)
metar_server = strdup("weather.noaa.gov"); metar_server = strdup("weather.noaa.gov");
if (metar_path == NULL) if (metar_path == NULL)
metar_path = strdup("/data/observations/metar/stations"); metar_path =
strdup("/data/observations/metar/stations");
int res; int res;
initFtp(); initFtp();
@ -110,7 +122,8 @@ void *fetch_ftp( ) {
} }
res = changeFtpDirectory(metar_path); res = changeFtpDirectory(metar_path);
if (res < 0) { if (res < 0) {
ERR("Metar update failed (couldn't CWD to %s)\n", metar_path); ERR("Metar update failed (couldn't CWD to %s)\n",
metar_path);
disconnectFtp(); disconnectFtp();
status = 1; status = 1;
return NULL; return NULL;
@ -131,8 +144,7 @@ void *fetch_ftp( ) {
line = NULL; line = NULL;
ftp_ok = 1; ftp_ok = 1;
metar_worked = 1; metar_worked = 1;
} } else {
else {
ftp_ok = 0; ftp_ok = 0;
} }
@ -143,16 +155,15 @@ void *fetch_ftp( ) {
static pthread_t thread1; static pthread_t thread1;
void update_metar() { void update_metar()
{
int iret1; int iret1;
if (!status) { if (!status) {
status = 2; status = 2;
iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL); iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL);
} } else if (status == 2) { /* thread is still running. what else can we do? */
else if (status == 2) { /* thread is still running. what else can we do? */
return; return;
} } else {
else {
pthread_join(thread1, NULL); pthread_join(thread1, NULL);
status = 2; status = 2;
iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL); iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL);

View File

@ -1,10 +1,11 @@
#include "conky.h"
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
#include <fcntl.h> #include <fcntl.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include "conky.h"
int64 buf_to_int(char *buf, int pos, int size); int64 buf_to_int(char *buf, int pos, int size);
void int_to_buf(int64 i, char *buf, int pos, int size); void int_to_buf(int64 i, char *buf, int pos, int size);
@ -119,7 +120,9 @@ void int_to_buf(int64 i, char *buf, int pos, int size)
/* Write operations --------------------- */ /* Write operations --------------------- */
void init_message() void init_message()
{ write_pos = 0; } {
write_pos = 0;
}
void write_int8(int code) void write_int8(int code)
{ {
@ -156,7 +159,8 @@ void write_string(char *str)
} else { } else {
int len = strlen(str); int len = strlen(str);
write_int16(len); write_int16(len);
memcpy((void *) (write_buf + write_pos), (void *) str, (size_t) len); memcpy((void *) (write_buf + write_pos), (void *) str,
(size_t) len);
write_pos += len; write_pos += len;
} }
} }
@ -168,7 +172,8 @@ int write_message(char *mtype)
INT_TO_BUF32(write_pos, header, 0); INT_TO_BUF32(write_pos, header, 0);
if (4 != write(mldonkey_sock, header, 4) || if (4 != write(mldonkey_sock, header, 4) ||
write_pos != write(mldonkey_sock,(void *)write_buf,(size_t)write_pos)){ write_pos != write(mldonkey_sock, (void *) write_buf,
(size_t) write_pos)) {
ERR("Error in transmitting %s\n", mtype); ERR("Error in transmitting %s\n", mtype);
write_pos = 0; write_pos = 0;
@ -249,13 +254,17 @@ int cut_messages(int reinit)
while (1) { while (1) {
if (toread == 0) { if (toread == 0) {
nread = read(mldonkey_sock, read_buf+pos,4-pos); nread =
read(mldonkey_sock, read_buf + pos, 4 - pos);
if (nread <= 0) { if (nread <= 0) {
if (errno == EAGAIN) { if (errno == EAGAIN) {
return 0; return 0;
} else { } else {
close_sock(); close_sock();
pos = 0; toread = 0; return -1; } pos = 0;
toread = 0;
return -1;
}
} }
pos += nread; pos += nread;
if (pos == 4) { if (pos == 4) {
@ -263,12 +272,18 @@ int cut_messages(int reinit)
pos = 0; pos = 0;
} }
} else { } else {
nread = read(mldonkey_sock, read_buf+pos, toread - pos); nread =
read(mldonkey_sock, read_buf + pos,
toread - pos);
if (nread <= 0) { if (nread <= 0) {
if(errno == EAGAIN) return 0; else { if (errno == EAGAIN)
pos = 0; toread = 0; return 0;
else {
pos = 0;
toread = 0;
close_sock(); close_sock();
return -1; } return -1;
}
} }
pos += nread; pos += nread;
if (pos == toread) { if (pos == toread) {
@ -287,7 +302,8 @@ int cut_messages(int reinit)
void close_sock() void close_sock()
{ {
old_config = NULL; old_config = NULL;
if(mldonkey_sock >= 0) close(mldonkey_sock); if (mldonkey_sock >= 0)
close(mldonkey_sock);
mldonkey_sock = -1; mldonkey_sock = -1;
mldonkey_state = MLDONKEY_DISCONNECTED; mldonkey_state = MLDONKEY_DISCONNECTED;
cut_messages(1); cut_messages(1);
@ -310,18 +326,24 @@ int mldonkey_connect(mldonkey_config *config)
if (config->mldonkey_hostname[0] >= '0' && if (config->mldonkey_hostname[0] >= '0' &&
config->mldonkey_hostname[0] <= '9') { config->mldonkey_hostname[0] <= '9') {
#ifdef HAS_INET_ATON #ifdef HAS_INET_ATON
if (inet_aton(config->mldonkey_hostname, &sa.sin_addr) == 0) return -1; if (inet_aton
(config->mldonkey_hostname, &sa.sin_addr) == 0)
return -1;
#else #else
sa.sin_addr.s_addr = inet_addr(config->mldonkey_hostname); sa.sin_addr.s_addr =
if (sa.sin_addr.s_addr == (unsigned int) -1) return -1; inet_addr(config->mldonkey_hostname);
if (sa.sin_addr.s_addr == (unsigned int) -1)
return -1;
#endif #endif
} else { } else {
struct hostent *hp; struct hostent *hp;
hp = gethostbyname(config->mldonkey_hostname); hp = gethostbyname(config->mldonkey_hostname);
if (hp == (struct hostent *) NULL) return -1; if (hp == (struct hostent *) NULL)
sa.sin_addr.s_addr = (unsigned long)hp->h_addr_list[0]; return -1;
sa.sin_addr.s_addr =
(unsigned long) hp->h_addr_list[0];
} }
sa.sin_port = htons(config->mldonkey_port); sa.sin_port = htons(config->mldonkey_port);
@ -333,11 +355,12 @@ int mldonkey_connect(mldonkey_config *config)
return -1; return -1;
} }
if( connect(mldonkey_sock, (struct sockaddr *) &sa, sizeof(sa)) < 0) { if (connect
if (errno != EAGAIN && (mldonkey_sock, (struct sockaddr *) &sa,
errno != EINTR && sizeof(sa)) < 0) {
errno != EINPROGRESS && if (errno != EAGAIN && errno != EINTR
errno != EWOULDBLOCK) { && errno != EINPROGRESS
&& errno != EWOULDBLOCK) {
// ERR("Connection failed"); // ERR("Connection failed");
close_sock(); close_sock();
return -1; return -1;
@ -346,7 +369,8 @@ int mldonkey_connect(mldonkey_config *config)
retcode = fcntl(mldonkey_sock, F_GETFL, 0); retcode = fcntl(mldonkey_sock, F_GETFL, 0);
if (retcode == -1 || if (retcode == -1 ||
fcntl(mldonkey_sock, F_SETFL, retcode | O_NONBLOCK) == -1){ fcntl(mldonkey_sock, F_SETFL,
retcode | O_NONBLOCK) == -1) {
return -1; return -1;
} }

43
mpd.c
View File

@ -15,7 +15,8 @@ void update_mpd()
//fprintf(stderr, "%s\n", current_info->conn->errorStr); //fprintf(stderr, "%s\n", current_info->conn->errorStr);
mpd_closeConnection(current_info->conn); mpd_closeConnection(current_info->conn);
if (current_info->mpd.artist == NULL) if (current_info->mpd.artist == NULL)
current_info->mpd.artist = malloc(TEXT_BUFFER_SIZE); current_info->mpd.artist =
malloc(TEXT_BUFFER_SIZE);
if (current_info->mpd.album == NULL) if (current_info->mpd.album == NULL)
current_info->mpd.album = malloc(TEXT_BUFFER_SIZE); current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
if (current_info->mpd.title == NULL) if (current_info->mpd.title == NULL)
@ -24,6 +25,10 @@ void update_mpd()
strcpy(current_info->mpd.album, "Unknown"); strcpy(current_info->mpd.album, "Unknown");
strcpy(current_info->mpd.title, "Unknown"); strcpy(current_info->mpd.title, "Unknown");
current_info->mpd.status = "MPD not responding"; current_info->mpd.status = "MPD not responding";
current_info->mpd.bitrate = 0;
current_info->mpd.progress = 0;
current_info->mpd.elapsed = 0;
current_info->mpd.length = 0;
return; return;
} }
@ -37,7 +42,8 @@ void update_mpd()
//fprintf(stderr, "%s\n", current_info->conn->errorStr); //fprintf(stderr, "%s\n", current_info->conn->errorStr);
mpd_closeConnection(current_info->conn); mpd_closeConnection(current_info->conn);
if (current_info->mpd.artist == NULL) if (current_info->mpd.artist == NULL)
current_info->mpd.artist = malloc(TEXT_BUFFER_SIZE); current_info->mpd.artist =
malloc(TEXT_BUFFER_SIZE);
if (current_info->mpd.album == NULL) if (current_info->mpd.album == NULL)
current_info->mpd.album = malloc(TEXT_BUFFER_SIZE); current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
if (current_info->mpd.title == NULL) if (current_info->mpd.title == NULL)
@ -46,32 +52,35 @@ void update_mpd()
strcpy(current_info->mpd.album, "Unknown"); strcpy(current_info->mpd.album, "Unknown");
strcpy(current_info->mpd.title, "Unknown"); strcpy(current_info->mpd.title, "Unknown");
current_info->mpd.status = "MPD not responding"; current_info->mpd.status = "MPD not responding";
current_info->mpd.bitrate = 0;
current_info->mpd.progress = 0;
current_info->mpd.elapsed = 0;
current_info->mpd.length = 0;
return; return;
} }
current_info->mpd.volume = status->volume; current_info->mpd.volume = status->volume;
//if (status->error) //if (status->error)
//printf("error: %s\n", status->error); //printf("error: %s\n", status->error);
if(status->state == MPD_STATUS_STATE_PLAY) if (status->state == MPD_STATUS_STATE_PLAY) {
{
current_info->mpd.status = "Playing"; current_info->mpd.status = "Playing";
} }
if(status->state == MPD_STATUS_STATE_STOP) if (status->state == MPD_STATUS_STATE_STOP) {
{
current_info->mpd.status = "Stopped"; current_info->mpd.status = "Stopped";
} }
if(status->state == MPD_STATUS_STATE_PAUSE) if (status->state == MPD_STATUS_STATE_PAUSE) {
{
current_info->mpd.status = "Paused"; current_info->mpd.status = "Paused";
} }
if(status->state == MPD_STATUS_STATE_UNKNOWN) if (status->state == MPD_STATUS_STATE_UNKNOWN) {
{
current_info->mpd.status = "Unknown"; current_info->mpd.status = "Unknown";
} }
if (status->state == MPD_STATUS_STATE_PLAY || if (status->state == MPD_STATUS_STATE_PLAY ||
status->state == MPD_STATUS_STATE_PAUSE) { status->state == MPD_STATUS_STATE_PAUSE) {
current_info->mpd.bitrate = status->bitRate; current_info->mpd.bitrate = status->bitRate;
current_info->mpd.progress = (float)status->elapsedTime/status->totalTime; current_info->mpd.progress =
(float) status->elapsedTime / status->totalTime;
current_info->mpd.elapsed = status->elapsedTime;
current_info->mpd.length = status->totalTime;
} }
@ -91,27 +100,25 @@ void update_mpd()
} }
if (current_info->mpd.artist == NULL) if (current_info->mpd.artist == NULL)
current_info->mpd.artist = malloc(TEXT_BUFFER_SIZE); current_info->mpd.artist =
malloc(TEXT_BUFFER_SIZE);
if (current_info->mpd.album == NULL) if (current_info->mpd.album == NULL)
current_info->mpd.album = malloc(TEXT_BUFFER_SIZE); current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
if (current_info->mpd.title == NULL) if (current_info->mpd.title == NULL)
current_info->mpd.title = malloc(TEXT_BUFFER_SIZE); current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
if (song->artist) { if (song->artist) {
strcpy(current_info->mpd.artist, song->artist); strcpy(current_info->mpd.artist, song->artist);
} } else {
else {
strcpy(current_info->mpd.artist, "Unknown"); strcpy(current_info->mpd.artist, "Unknown");
} }
if (song->album) { if (song->album) {
strcpy(current_info->mpd.album, song->album); strcpy(current_info->mpd.album, song->album);
} } else {
else {
strcpy(current_info->mpd.album, "Unknown"); strcpy(current_info->mpd.album, "Unknown");
} }
if (song->title) { if (song->title) {
strcpy(current_info->mpd.title, song->title); strcpy(current_info->mpd.title, song->title);
} } else {
else {
strcpy(current_info->mpd.title, "Unknown"); strcpy(current_info->mpd.title, "Unknown");
} }
if (entity != NULL) { if (entity != NULL) {

56
top.c
View File

@ -347,7 +347,8 @@ inline static void calc_cpu_each(int total)
/*p->amount = total ? /*p->amount = total ?
(100.0 * (float) (p->user_time + p->kernel_time) / (100.0 * (float) (p->user_time + p->kernel_time) /
total) : 0; */ total) : 0; */
p->amount = (100.0 * (p->user_time + p->kernel_time) / total); p->amount =
(100.0 * (p->user_time + p->kernel_time) / total);
/* if (p->amount > 100) /* if (p->amount > 100)
p->amount = 0;*/ p->amount = 0;*/
@ -399,23 +400,25 @@ inline void process_find_top(struct process **cpu, struct process **mem)
if (i < sorttmp_size && pr->counted) { if (i < sorttmp_size && pr->counted) {
sorttmp[i] = pr; sorttmp[i] = pr;
i++; i++;
} } else if (i == sorttmp_size && pr->counted) {
else if (i == sorttmp_size && pr->counted) {
sorttmp_size++; sorttmp_size++;
sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size); sorttmp =
realloc(sorttmp,
sizeof(struct process) *
sorttmp_size);
sorttmp[i] = pr; sorttmp[i] = pr;
i++; i++;
} }
pr = pr->next; pr = pr->next;
} }
if (i + 1 < sorttmp_size) { if (i + 1 < sorttmp_size) {
sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size); sorttmp =
realloc(sorttmp,
sizeof(struct process) * sorttmp_size);
} }
max = i; max = i;
for(i=0;i<max-1;i++) for (i = 0; i < max - 1; i++) {
{ while (sorttmp[i + 1]->amount > sorttmp[i]->amount) {
while (sorttmp[i+1]->amount > sorttmp[i]->amount)
{
pr = sorttmp[i]; pr = sorttmp[i];
sorttmp[i] = sorttmp[i + 1]; sorttmp[i] = sorttmp[i + 1];
sorttmp[i + 1] = pr; sorttmp[i + 1] = pr;
@ -428,8 +431,7 @@ inline void process_find_top(struct process **cpu, struct process **mem)
} }
for (i = max; i > 1; i--); for (i = max; i > 1; i--);
{ {
while (sorttmp[i]->amount > sorttmp[i-1]->amount) while (sorttmp[i]->amount > sorttmp[i - 1]->amount) {
{
pr = sorttmp[i]; pr = sorttmp[i];
sorttmp[i] = sorttmp[i - 1]; sorttmp[i] = sorttmp[i - 1];
sorttmp[i - 1] = pr; sorttmp[i - 1] = pr;
@ -439,8 +441,7 @@ inline void process_find_top(struct process **cpu, struct process **mem)
break; break;
} }
} }
for(i=0;i<10;i++) for (i = 0; i < 10; i++) {
{
cpu[i] = sorttmp[i]; cpu[i] = sorttmp[i];
} }
@ -452,22 +453,26 @@ inline void process_find_top(struct process **cpu, struct process **mem)
if (i < sorttmp_size && pr->counted) { if (i < sorttmp_size && pr->counted) {
sorttmp[i] = pr; sorttmp[i] = pr;
i++; i++;
} } else if (i == sorttmp_size && pr->counted) {
else if (i == sorttmp_size && pr->counted) {
sorttmp_size++; sorttmp_size++;
sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size); sorttmp =
realloc(sorttmp,
sizeof(struct process) *
sorttmp_size);
sorttmp[i] = pr; sorttmp[i] = pr;
i++; i++;
} }
pr = pr->next; pr = pr->next;
} }
if (i + 1 < sorttmp_size) { if (i + 1 < sorttmp_size) {
sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size); sorttmp =
} max = i; realloc(sorttmp,
for(i=0;i<max-1;i++) sizeof(struct process) * sorttmp_size);
{ }
while (sorttmp[i+1]->totalmem > sorttmp[i]->totalmem) max = i;
{ for (i = 0; i < max - 1; i++) {
while (sorttmp[i + 1]->totalmem >
sorttmp[i]->totalmem) {
pr = sorttmp[i]; pr = sorttmp[i];
sorttmp[i] = sorttmp[i + 1]; sorttmp[i] = sorttmp[i + 1];
sorttmp[i + 1] = pr; sorttmp[i + 1] = pr;
@ -480,8 +485,8 @@ inline void process_find_top(struct process **cpu, struct process **mem)
} }
for (i = max; i > 1; i--); for (i = max; i > 1; i--);
{ {
while (sorttmp[i]->totalmem > sorttmp[i-1]->totalmem) while (sorttmp[i]->totalmem >
{ sorttmp[i - 1]->totalmem) {
pr = sorttmp[i]; pr = sorttmp[i];
sorttmp[i] = sorttmp[i - 1]; sorttmp[i] = sorttmp[i - 1];
sorttmp[i - 1] = pr; sorttmp[i - 1] = pr;
@ -491,8 +496,7 @@ inline void process_find_top(struct process **cpu, struct process **mem)
break; break;
} }
} }
for(i=0;i<10;i++) for (i = 0; i < 10; i++) {
{
mem[i] = sorttmp[i]; mem[i] = sorttmp[i];
} }

3
x11.c
View File

@ -313,7 +313,8 @@ long get_x11_color(const char *name)
{ {
XColor color; XColor color;
color.pixel = 0; color.pixel = 0;
if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) { if (!XParseColor
(display, DefaultColormap(display, screen), name, &color)) {
ERR("can't parse X color '%s'", name); ERR("can't parse X color '%s'", name);
return 0xFF00FF; return 0xFF00FF;
} }