Dear Maximilian, I think, my attached daemon actually written not in C++
but in C. I just use g++ because in the future I will expand/extend its
possibilities, very lot, and to them I need C++ for the objectoriented
programming. And, I preferred the "//" not the /*... */

I think, almost enough rename my file from .cpp to c and you can compile it
with gcc. I tryed it, and succeeded. Now I attach my renamed file. But I
not know, how can I add it to the wiki. And I not have enough good English.
Try it my daemon, and if you like it, put it to the wiki and please create
to them good "manual", usage-howto. My full name: "Viola Zoltán". (In
english name-order: "Zoltán Viola"). The program under GPL license, of
course...

I very like DWM, it is the best WM, super, highly, but excuse me, I develop
more far my daemon, but I will use later g++. I know and understand, that
the generated C++ code more fat as the C, but I would like written a very
complex, function-rich daemon, almost a small "mini-operating-system", and
I need, "musthave" the objectoriented-possibility.

Sorry...

Zoli

2013/6/27 Maximilian Dietrich <d...@lavabit.com>

> On Wed, Jun 26, 2013 at 08:51:46PM -0400, Viola Zoltán wrote:
> >
> >    Hi, DWM users! I wrote a daemon to the DWM status bar! I send it in
> >    attachment. The program name "kajjam". Start it simple: ./kajjam
> >    Compile it with this command:
> >    g++ -funsigned-char -funsigned-bitfields -Wall -Wno-long-long -Wunused
> >    -Wextra -pedantic -lX11 kajjam.cpp -o kajjam
> >    How to stop it: kill its process.
> >    The "kajjam" print to the status bar in every 1 sec the upload and
> >    download speed of the internet connection, and the current date and
> >    time. (The name of the day of week in the hungarian abbreviation. It
> >    can simple changed in the source code).
> >    Zoli
>
> I suppose you should add it to the wiki, a rewrite in c (as opposed to
> c++) might be desirable.
>
> Cheers,
> Maximilian D.
>
>
>
/* kajjam démon. Készült a DWM ablakkezelőhöz. Indítás után a statuszbaron kijelzi az aktuális időt és dátumot,
valamint az internetkapcsolatunk aktuális le- és feltöltési sebességét.
Így kell lefordítani:
gcc -lX11 kajjam.c -o kajjam

*/




#define _BSD_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>

#include <stdbool.h>
#include <stdarg.h>
#include <strings.h>
#include <sys/time.h>
#include <time.h>
#include <sys/wait.h>

#include <X11/Xlib.h>





static const char logfilepath[]="/Programs/Kajjam/log/kajjam.log";
static const char munkakonyvtar[]="/Programs/Kajjam";
static char idoformatum[]="%m.%d %H:%M ";
static const char hetnapjai[][4]={"V  ", "H  ", "K  ", "Sze", "Cs ", "P  ", "Szo"};
static char HET[]="      ";
static Display *dpy;
FILE *logfilefp;



char * smprintf(char *fmt, ...) // -------------------------------------------------------------
{
va_list fmtargs;
char *ret;
int len;

va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);

ret = (char *)malloc(++len);
if (ret == NULL) {
fprintf(logfilefp, "malloc probléma!\n");
exit(1);
}

va_start(fmtargs, fmt);
vsnprintf(ret, len, fmt, fmtargs);
va_end(fmtargs);

return ret;
}
// ---------------------------------------------------------------------------------------------

void settz(char *tzname)
{
setenv("TZ", tzname, 1);
}
// ------------------------------------------------------------------------------------------------------------
int parse_netdev(unsigned long long int *receivedabs, unsigned long long int *sentabs)
{
char *buf;
char *eth0start;
static int bufsize;
FILE *devfd;

buf = (char *) calloc(255, 1);
bufsize = 255;
devfd = fopen("/proc/net/dev", "r");

// ignore the first two lines of the file
fgets(buf, bufsize, devfd);
fgets(buf, bufsize, devfd);

while (fgets(buf, bufsize, devfd)) {
    if ((eth0start = strstr(buf, "eth0:")) != NULL) {

// Ez a conkyból lett csórva
sscanf(eth0start + 6, "%llu  %*d     %*d  %*d  %*d  %*d   %*d        %*d       %llu",\
       receivedabs, sentabs);
fclose(devfd);
free(buf);
return 0;
    }
}
fclose(devfd);
free(buf);
return 1;
}
// ------------------------------------------------------------------------------------------------------
char * get_netusage()
{
unsigned long long int oldrec, oldsent, newrec, newsent;
double downspeed, upspeed;
char *downspeedstr, *upspeedstr;
char *retstr;
int retval;

downspeedstr = (char *) malloc(15);
upspeedstr = (char *) malloc(15);
retstr = (char *) malloc(42);

retval = parse_netdev(&oldrec, &oldsent);
if (retval) {
    fprintf(logfilefp, "Error when parsing /proc/net/dev file.\n");
    exit(1);
}

sleep(1);
retval = parse_netdev(&newrec, &newsent);
if (retval) {
    fprintf(logfilefp, "Error when parsing /proc/net/dev file.\n");
    exit(1);
}

downspeed = (newrec - oldrec) / 1024.0;
if (downspeed > 1024.0) {
    downspeed /= 1024.0;
    sprintf(downspeedstr, "%.2f M", downspeed);
} else {
//    sprintf(downspeedstr, "%.2f K", downspeed);
    sprintf(downspeedstr, "%.f K", downspeed);
}

upspeed = (newsent - oldsent) / 1024.0;
if (upspeed > 1024.0) {
    upspeed /= 1024.0;
    sprintf(upspeedstr, "%.2f M", upspeed);
} else {
//    sprintf(upspeedstr, "%.2f K", upspeed);
    sprintf(upspeedstr, "%.f K", upspeed);
}
sprintf(retstr, "ˇ%s ^%s", downspeedstr, upspeedstr);

free(downspeedstr);
free(upspeedstr);
return retstr;
}
// --------------------------------------------------------------------------------------------------
char * mktimes(char *fmt)
{
char buf[129];
time_t *ido;
time_t rawtime;
struct tm ideiglenes;
time(&rawtime);
ido=&rawtime;
ideiglenes = *localtime(ido);
    bzero(buf, sizeof(buf));
if (!strftime(buf, sizeof(buf)-1, fmt, &ideiglenes)) {
fprintf(logfilefp, "strftime == 0\n");
exit(1);
}
HET[0]=hetnapjai[ideiglenes.tm_wday][0];
HET[1]=hetnapjai[ideiglenes.tm_wday][1];
HET[2]=hetnapjai[ideiglenes.tm_wday][2];
HET[3]=0;
return smprintf("%s", buf);
}
// ------------------------------------------------------------------------------------------

void setstatus(char *str)
{
XStoreName(dpy, DefaultRootWindow(dpy), str);
XSync(dpy, False);
}
// --------------------------------------------------------------------------------------------------

// --------------------------------------------------------------------------------------------------



int main(void) {

char *status;
char *tmutc;
char *netstats;


        
        /* Our process ID and Session ID */
        pid_t pid, sid;
        
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);

// Nyitunk egy log file-ot

logfilefp=fopen(logfilepath,"wb+");
if (!logfilefp) { printf ("Nem tudom megnyitni a %s állományt!\n", logfilepath); return(1); }



    /* Create a new SID for the child process */
    sid = setsid();
    if (sid < 0) {
    /* Log the failure */
    exit(EXIT_FAILURE);
}
                                                                                                        
                                                                                                                        
                                                                
// Az aktuális munkakönyvtár váltása
if ((chdir(munkakonyvtar)) < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
                                                                                                                                                                                /* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

/* Demon-specifikus inicializaciok */


if (!(dpy = XOpenDisplay(NULL))) {
fprintf(logfilefp, "kajjámstátusz: cannot open display.\n");
return 1;
}




// *************************************************************
// *************************************************************
// *************************************************************
// *************************************************************
//  FŐCIKLUS !!!!
// *************************************************************
// *************************************************************
// *************************************************************
// *************************************************************

while (1) {

// Ide jönnek a démon által végrehajtott feladatok



tmutc = mktimes(idoformatum);
netstats = get_netusage();

status = smprintf("%s %s %s",netstats, tmutc, HET);


setstatus(status);




sleep(1); // Vár 1 másodpercig


}

// FŐCIKLUS VÉGE
// *************************************************************
// *************************************************************
// *************************************************************



XCloseDisplay(dpy);

free(netstats);
free(tmutc);
free(status);


fclose(logfilefp); // bezárjuk a logfile állományt

exit(EXIT_SUCCESS);
}

Reply via email to