mail_counter.c (1771B)
1 // Fichier: dwmstatus.c 2 // Crée le 10 déc. 2012 22:28:11 3 // Dernière modification: 21 déc. 2012 16:25:16 4 5 #define _BSD_SOURCE 6 #define _GNU_SOURCE 7 #include <unistd.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <stdarg.h> 11 #include <string.h> 12 #include <strings.h> 13 #include <sys/time.h> 14 #include <time.h> 15 #include <sys/types.h> 16 #include <sys/wait.h> 17 #include <dirent.h> 18 19 #include <X11/Xlib.h> 20 21 static Display *dpy; 22 23 char * 24 smprintf(char *fmt, ...) 25 { 26 va_list fmtargs; 27 char *buf = NULL; 28 29 va_start(fmtargs, fmt); 30 if (vasprintf(&buf, fmt, fmtargs) == -1){ 31 fprintf(stderr, "malloc vasprintf\n"); 32 exit(1); 33 } 34 va_end(fmtargs); 35 36 return buf; 37 } 38 39 void 40 setstatus(char *str) 41 { 42 XStoreName(dpy, DefaultRootWindow(dpy), str); 43 XSync(dpy, False); 44 } 45 46 char *get_nmail(char *directory, char *label) 47 { 48 /* directory : Maildir path 49 * return label : number_of_new_mails 50 */ 51 52 int n = 0; 53 DIR* dir = NULL; 54 struct dirent* rf = NULL; 55 56 dir = opendir(directory); /* try to open directory */ 57 if (dir == NULL) 58 perror(""); 59 60 while ((rf = readdir(dir)) != NULL) /*count number of file*/ 61 { 62 if (strcmp(rf->d_name, ".") != 0 && 63 strcmp(rf->d_name, "..") != 0) 64 n++; 65 } 66 closedir(dir); 67 68 if (n == 0) 69 return smprintf(""); 70 else 71 return smprintf("%s%d",label, n); 72 73 } 74 75 int 76 main(void) 77 { 78 char *status; 79 char *newmails; 80 81 if (!(dpy = XOpenDisplay(NULL))) { 82 fprintf(stderr, "dwmstatus: cannot open display.\n"); 83 return 1; 84 } 85 86 for (;;sleep(60)) { 87 newmails = get_nmail("/home/xavier/Maildir/laposte/new", "Mails:"); 88 89 90 status = smprintf("%s",newmails); 91 setstatus(status); 92 free(newmails); 93 free(status); 94 } 95 96 XCloseDisplay(dpy); 97 98 return 0; 99 } 100