+/* cpu percentage */
+char *
+cpu_perc(const char *null)
+{
+ int perc;
+ long double a[4], b[4];
+ FILE *fp;
+
+ /* open stat file */
+ if (!(fp = fopen("/proc/stat","r"))) {
+ fprintf(stderr, "Error opening stat file.\n");
+ return smprintf(unknowntext);
+ }
+
+ /* read values */
+ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
+
+ /* close stat file */
+ fclose(fp);
+
+ /* wait a second (for avg values) */
+ sleep(1);
+
+ /* open stat file */
+ if (!(fp = fopen("/proc/stat","r"))) {
+ fprintf(stderr, "Error opening stat file.\n");
+ return smprintf(unknowntext);
+ }
+
+ /* read values */
+ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
+
+ /* close stat file */
+ fclose(fp);
+
+ /* calculate avg in this second */
+ perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
+
+ /* return perc as string */
+ return smprintf("%d%%", perc);
+}
+
+/* date and time */
+char *
+datetime(const char *timeformat)
+{
+ time_t tm;
+ size_t bufsize = 64;
+ char *buf = malloc(bufsize);
+ if (buf == NULL) {
+ fprintf(stderr, "Failed to get date/time.\n");
+ return smprintf(unknowntext);
+ }
+
+ /* get time in format */
+ time(&tm);
+ setlocale(LC_TIME, "");
+ if (!strftime(buf, bufsize, timeformat, localtime(&tm))) {
+ setlocale(LC_TIME, "C");
+ free(buf);
+ fprintf(stderr, "Strftime failed.\n");
+ return smprintf(unknowntext);
+ }
+
+ setlocale(LC_TIME, "C");
+ /* return time */
+ char *ret = smprintf("%s", buf);
+ free(buf);
+ return ret;
+}