clang-format-3.5 -i **/*.[ch], update modeline

From here on, we’ll use clang-format to automatically format the source.

This has worked well in i3, so we are introducing it for i3status.
This commit is contained in:
Michael Stapelberg 2015-03-16 10:00:32 +01:00
parent 6996f0a4a3
commit d74e904bf4
22 changed files with 2928 additions and 2925 deletions

10
.clang-format Normal file
View File

@ -0,0 +1,10 @@
BasedOnStyle: google
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortBlocksOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
IndentWidth: 4
PointerBindsToType: false
ColumnLimit: 0
SpaceBeforeParens: ControlStatements

1033
i3status.c

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,11 @@
#ifndef _I3STATUS_H
#define _I3STATUS_H
enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_TERM, O_NONE } output_format;
enum { O_DZEN2,
O_XMOBAR,
O_I3BAR,
O_TERM,
O_NONE } output_format;
#include <stdbool.h>
#include <confuse.h>
@ -45,105 +49,104 @@ enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_TERM, O_NONE } output_format;
/* Allows for the definition of a variable without opening a new scope, thus
* suited for usage in a macro. Idea from wmii. */
#define with(type, var, init) \
for (type var = (type)-1; (var == (type)-1) && ((var=(init)) || 1); )
for (type var = (type)-1; (var == (type)-1) && ((var = (init)) || 1);)
#define CASE_SEC(name) \
if (BEGINS_WITH(current, name)) \
with(cfg_t *, sec, cfg_section = cfg_getsec(cfg, name)) \
if (sec != NULL)
#define CASE_SEC(name) \
if (BEGINS_WITH(current, name)) \
with(cfg_t *, sec, cfg_section = cfg_getsec(cfg, name)) if (sec != NULL)
#define CASE_SEC_TITLE(name) \
if (BEGINS_WITH(current, name)) \
with(const char *, title, current + strlen(name) + 1) \
with(cfg_t *, sec, cfg_section = cfg_gettsec(cfg, name, title)) \
if (sec != NULL)
#define CASE_SEC_TITLE(name) \
if (BEGINS_WITH(current, name)) \
with(const char *, title, current + strlen(name) + 1) \
with(cfg_t *, sec, cfg_section = cfg_gettsec(cfg, name, title)) if (sec != NULL)
/* Macro which any plugin can use to output the full_text part (when the output
* format is JSON) or just output to stdout (any other output format). */
#define OUTPUT_FULL_TEXT(text) \
do { \
/* Terminate the output buffer here in any case, so that its \
* not forgotten in the module */ \
*outwalk = '\0'; \
if (output_format == O_I3BAR) { \
yajl_gen_string(json_gen, (const unsigned char *)"full_text", strlen("full_text")); \
yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \
} else { \
printf("%s", text); \
} \
} while (0)
#define OUTPUT_FULL_TEXT(text) \
do { \
/* Terminate the output buffer here in any case, so that its \
* not forgotten in the module */ \
*outwalk = '\0'; \
if (output_format == O_I3BAR) { \
yajl_gen_string(json_gen, (const unsigned char *) "full_text", strlen("full_text")); \
yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \
} else { \
printf("%s", text); \
} \
} while (0)
#define SEC_OPEN_MAP(name) \
do { \
if (output_format == O_I3BAR) { \
yajl_gen_map_open(json_gen); \
yajl_gen_string(json_gen, (const unsigned char *)"name", strlen("name")); \
yajl_gen_string(json_gen, (const unsigned char *)name, strlen(name)); \
} \
} while (0)
#define SEC_OPEN_MAP(name) \
do { \
if (output_format == O_I3BAR) { \
yajl_gen_map_open(json_gen); \
yajl_gen_string(json_gen, (const unsigned char *) "name", strlen("name")); \
yajl_gen_string(json_gen, (const unsigned char *)name, strlen(name)); \
} \
} while (0)
#define SEC_CLOSE_MAP \
do { \
if (output_format == O_I3BAR) { \
char *_align = cfg_getstr(sec, "align"); \
if (_align) { \
yajl_gen_string(json_gen, (const unsigned char *)"align", strlen("align")); \
yajl_gen_string(json_gen, (const unsigned char *)_align, strlen(_align)); \
} \
struct min_width *_width = cfg_getptr(sec, "min_width"); \
if (_width) { \
/* if the value can be parsed as a number, we use the numerical value */ \
if (_width->num > 0) { \
yajl_gen_string(json_gen, (const unsigned char *)"min_width", strlen("min_width")); \
yajl_gen_integer(json_gen, _width->num); \
} else { \
yajl_gen_string(json_gen, (const unsigned char *)"min_width", strlen("min_width")); \
yajl_gen_string(json_gen, (const unsigned char *)_width->str, strlen(_width->str)); \
} \
} \
const char *_sep = cfg_getstr(cfg_general, "separator"); \
if (strlen(_sep) == 0) {\
yajl_gen_string(json_gen, (const unsigned char *)"separator", strlen("separator")); \
yajl_gen_bool(json_gen, false); \
} \
yajl_gen_map_close(json_gen); \
} \
} while (0)
#define SEC_CLOSE_MAP \
do { \
if (output_format == O_I3BAR) { \
char *_align = cfg_getstr(sec, "align"); \
if (_align) { \
yajl_gen_string(json_gen, (const unsigned char *) "align", strlen("align")); \
yajl_gen_string(json_gen, (const unsigned char *)_align, strlen(_align)); \
} \
struct min_width *_width = cfg_getptr(sec, "min_width"); \
if (_width) { \
/* if the value can be parsed as a number, we use the numerical value */ \
if (_width->num > 0) { \
yajl_gen_string(json_gen, (const unsigned char *) "min_width", strlen("min_width")); \
yajl_gen_integer(json_gen, _width->num); \
} else { \
yajl_gen_string(json_gen, (const unsigned char *) "min_width", strlen("min_width")); \
yajl_gen_string(json_gen, (const unsigned char *)_width->str, strlen(_width->str)); \
} \
} \
const char *_sep = cfg_getstr(cfg_general, "separator"); \
if (strlen(_sep) == 0) { \
yajl_gen_string(json_gen, (const unsigned char *) "separator", strlen("separator")); \
yajl_gen_bool(json_gen, false); \
} \
yajl_gen_map_close(json_gen); \
} \
} while (0)
#define START_COLOR(colorstr) \
do { \
if (cfg_getbool(cfg_general, "colors")) { \
const char *_val = NULL; \
if (cfg_section) \
_val = cfg_getstr(cfg_section, colorstr); \
if (!_val) \
_val = cfg_getstr(cfg_general, colorstr); \
if (output_format == O_I3BAR) { \
yajl_gen_string(json_gen, (const unsigned char *)"color", strlen("color")); \
yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \
} else { \
outwalk += sprintf(outwalk, "%s", color(colorstr)); \
} \
} \
} while (0)
#define START_COLOR(colorstr) \
do { \
if (cfg_getbool(cfg_general, "colors")) { \
const char *_val = NULL; \
if (cfg_section) \
_val = cfg_getstr(cfg_section, colorstr); \
if (!_val) \
_val = cfg_getstr(cfg_general, colorstr); \
if (output_format == O_I3BAR) { \
yajl_gen_string(json_gen, (const unsigned char *) "color", strlen("color")); \
yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \
} else { \
outwalk += sprintf(outwalk, "%s", color(colorstr)); \
} \
} \
} while (0)
#define END_COLOR \
do { \
if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
outwalk += sprintf(outwalk, "%s", endcolor()); \
} \
} while (0)
#define END_COLOR \
do { \
if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
outwalk += sprintf(outwalk, "%s", endcolor()); \
} \
} while (0)
#define INSTANCE(instance) \
do { \
if (output_format == O_I3BAR) { \
yajl_gen_string(json_gen, (const unsigned char *)"instance", strlen("instance")); \
yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \
} \
} while (0)
#define INSTANCE(instance) \
do { \
if (output_format == O_I3BAR) { \
yajl_gen_string(json_gen, (const unsigned char *) "instance", strlen("instance")); \
yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \
} \
} while (0)
typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t;
typedef enum { CS_DISCHARGING,
CS_CHARGING,
CS_FULL } charging_status_t;
/*
* The "min_width" module option may either be defined as a string or a number.
@ -161,7 +164,7 @@ bool slurp(const char *filename, char *destination, int size);
/* src/output.c */
void print_separator(const char *separator);
char *color(const char *colorstr);
char *endcolor() __attribute__ ((pure));
char *endcolor() __attribute__((pure));
void reset_cursor(void);
/* src/auto_detect_format.c */
@ -172,8 +175,8 @@ void set_timezone(const char *tz);
/* src/first_network_device.c */
typedef enum {
NET_TYPE_WIRELESS = 0,
NET_TYPE_ETHERNET = 1
NET_TYPE_WIRELESS = 0,
NET_TYPE_ETHERNET = 1
} net_type_t;
const char *first_eth_interface(const net_type_t type);

View File

@ -32,8 +32,8 @@
* @(#)queue.h 8.5 (Berkeley) 8/20/94
*/
#ifndef _SYS_QUEUE_H_
#define _SYS_QUEUE_H_
#ifndef _SYS_QUEUE_H_
#define _SYS_QUEUE_H_
/*
* This file defines five types of data structures: singly-linked lists,
@ -91,437 +91,456 @@
/*
* Singly-linked List definitions.
*/
#define SLIST_HEAD(name, type) \
struct name { \
struct type *slh_first; /* first element */ \
}
#define SLIST_HEAD(name, type) \
struct name { \
struct type *slh_first; /* first element */ \
}
#define SLIST_HEAD_INITIALIZER(head) \
{ NULL }
#define SLIST_HEAD_INITIALIZER(head) \
{ NULL }
#define SLIST_ENTRY(type) \
struct { \
struct type *sle_next; /* next element */ \
}
#define SLIST_ENTRY(type) \
struct { \
struct type *sle_next; /* next element */ \
}
/*
* Singly-linked List access methods.
*/
#define SLIST_FIRST(head) ((head)->slh_first)
#define SLIST_END(head) NULL
#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
#define SLIST_FIRST(head) ((head)->slh_first)
#define SLIST_END(head) NULL
#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
#define SLIST_FOREACH(var, head, field) \
for((var) = SLIST_FIRST(head); \
(var) != SLIST_END(head); \
(var) = SLIST_NEXT(var, field))
#define SLIST_FOREACH(var, head, field) \
for ((var) = SLIST_FIRST(head); \
(var) != SLIST_END(head); \
(var) = SLIST_NEXT(var, field))
#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
for ((varp) = &SLIST_FIRST((head)); \
((var) = *(varp)) != SLIST_END(head); \
(varp) = &SLIST_NEXT((var), field))
#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
for ((varp) = &SLIST_FIRST((head)); \
((var) = *(varp)) != SLIST_END(head); \
(varp) = &SLIST_NEXT((var), field))
/*
* Singly-linked List functions.
*/
#define SLIST_INIT(head) { \
SLIST_FIRST(head) = SLIST_END(head); \
}
#define SLIST_INIT(head) \
{ \
SLIST_FIRST(head) = SLIST_END(head); \
}
#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
(elm)->field.sle_next = (slistelm)->field.sle_next; \
(slistelm)->field.sle_next = (elm); \
} while (0)
#define SLIST_INSERT_AFTER(slistelm, elm, field) \
do { \
(elm)->field.sle_next = (slistelm)->field.sle_next; \
(slistelm)->field.sle_next = (elm); \
} while (0)
#define SLIST_INSERT_HEAD(head, elm, field) do { \
(elm)->field.sle_next = (head)->slh_first; \
(head)->slh_first = (elm); \
} while (0)
#define SLIST_INSERT_HEAD(head, elm, field) \
do { \
(elm)->field.sle_next = (head)->slh_first; \
(head)->slh_first = (elm); \
} while (0)
#define SLIST_REMOVE_NEXT(head, elm, field) do { \
(elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
} while (0)
#define SLIST_REMOVE_NEXT(head, elm, field) \
do { \
(elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
} while (0)
#define SLIST_REMOVE_HEAD(head, field) do { \
(head)->slh_first = (head)->slh_first->field.sle_next; \
} while (0)
#define SLIST_REMOVE_HEAD(head, field) \
do { \
(head)->slh_first = (head)->slh_first->field.sle_next; \
} while (0)
#define SLIST_REMOVE(head, elm, type, field) do { \
if ((head)->slh_first == (elm)) { \
SLIST_REMOVE_HEAD((head), field); \
} else { \
struct type *curelm = (head)->slh_first; \
\
while (curelm->field.sle_next != (elm)) \
curelm = curelm->field.sle_next; \
curelm->field.sle_next = \
curelm->field.sle_next->field.sle_next; \
_Q_INVALIDATE((elm)->field.sle_next); \
} \
} while (0)
#define SLIST_REMOVE(head, elm, type, field) \
do { \
if ((head)->slh_first == (elm)) { \
SLIST_REMOVE_HEAD((head), field); \
} else { \
struct type *curelm = (head)->slh_first; \
\
while (curelm->field.sle_next != (elm)) \
curelm = curelm->field.sle_next; \
curelm->field.sle_next = curelm->field.sle_next->field.sle_next; \
_Q_INVALIDATE((elm)->field.sle_next); \
} \
} while (0)
/*
* List definitions.
*/
#define LIST_HEAD(name, type) \
struct name { \
struct type *lh_first; /* first element */ \
}
#define LIST_HEAD(name, type) \
struct name { \
struct type *lh_first; /* first element */ \
}
#define LIST_HEAD_INITIALIZER(head) \
{ NULL }
#define LIST_HEAD_INITIALIZER(head) \
{ NULL }
#define LIST_ENTRY(type) \
struct { \
struct type *le_next; /* next element */ \
struct type **le_prev; /* address of previous next element */ \
}
#define LIST_ENTRY(type) \
struct { \
struct type *le_next; /* next element */ \
struct type **le_prev; /* address of previous next element */ \
}
/*
* List access methods
*/
#define LIST_FIRST(head) ((head)->lh_first)
#define LIST_END(head) NULL
#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
#define LIST_FIRST(head) ((head)->lh_first)
#define LIST_END(head) NULL
#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
#define LIST_FOREACH(var, head, field) \
for((var) = LIST_FIRST(head); \
(var)!= LIST_END(head); \
(var) = LIST_NEXT(var, field))
#define LIST_FOREACH(var, head, field) \
for ((var) = LIST_FIRST(head); \
(var) != LIST_END(head); \
(var) = LIST_NEXT(var, field))
/*
* List functions.
*/
#define LIST_INIT(head) do { \
LIST_FIRST(head) = LIST_END(head); \
} while (0)
#define LIST_INIT(head) \
do { \
LIST_FIRST(head) = LIST_END(head); \
} while (0)
#define LIST_INSERT_AFTER(listelm, elm, field) do { \
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
(listelm)->field.le_next->field.le_prev = \
&(elm)->field.le_next; \
(listelm)->field.le_next = (elm); \
(elm)->field.le_prev = &(listelm)->field.le_next; \
} while (0)
#define LIST_INSERT_AFTER(listelm, elm, field) \
do { \
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
(listelm)->field.le_next->field.le_prev = &(elm)->field.le_next; \
(listelm)->field.le_next = (elm); \
(elm)->field.le_prev = &(listelm)->field.le_next; \
} while (0)
#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
(elm)->field.le_prev = (listelm)->field.le_prev; \
(elm)->field.le_next = (listelm); \
*(listelm)->field.le_prev = (elm); \
(listelm)->field.le_prev = &(elm)->field.le_next; \
} while (0)
#define LIST_INSERT_BEFORE(listelm, elm, field) \
do { \
(elm)->field.le_prev = (listelm)->field.le_prev; \
(elm)->field.le_next = (listelm); \
*(listelm)->field.le_prev = (elm); \
(listelm)->field.le_prev = &(elm)->field.le_next; \
} while (0)
#define LIST_INSERT_HEAD(head, elm, field) do { \
if (((elm)->field.le_next = (head)->lh_first) != NULL) \
(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
(head)->lh_first = (elm); \
(elm)->field.le_prev = &(head)->lh_first; \
} while (0)
#define LIST_INSERT_HEAD(head, elm, field) \
do { \
if (((elm)->field.le_next = (head)->lh_first) != NULL) \
(head)->lh_first->field.le_prev = &(elm)->field.le_next; \
(head)->lh_first = (elm); \
(elm)->field.le_prev = &(head)->lh_first; \
} while (0)
#define LIST_REMOVE(elm, field) do { \
if ((elm)->field.le_next != NULL) \
(elm)->field.le_next->field.le_prev = \
(elm)->field.le_prev; \
*(elm)->field.le_prev = (elm)->field.le_next; \
_Q_INVALIDATE((elm)->field.le_prev); \
_Q_INVALIDATE((elm)->field.le_next); \
} while (0)
#define LIST_REMOVE(elm, field) \
do { \
if ((elm)->field.le_next != NULL) \
(elm)->field.le_next->field.le_prev = (elm)->field.le_prev; \
*(elm)->field.le_prev = (elm)->field.le_next; \
_Q_INVALIDATE((elm)->field.le_prev); \
_Q_INVALIDATE((elm)->field.le_next); \
} while (0)
#define LIST_REPLACE(elm, elm2, field) do { \
if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
(elm2)->field.le_next->field.le_prev = \
&(elm2)->field.le_next; \
(elm2)->field.le_prev = (elm)->field.le_prev; \
*(elm2)->field.le_prev = (elm2); \
_Q_INVALIDATE((elm)->field.le_prev); \
_Q_INVALIDATE((elm)->field.le_next); \
} while (0)
#define LIST_REPLACE(elm, elm2, field) \
do { \
if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
(elm2)->field.le_next->field.le_prev = &(elm2)->field.le_next; \
(elm2)->field.le_prev = (elm)->field.le_prev; \
*(elm2)->field.le_prev = (elm2); \
_Q_INVALIDATE((elm)->field.le_prev); \
_Q_INVALIDATE((elm)->field.le_next); \
} while (0)
/*
* Simple queue definitions.
*/
#define SIMPLEQ_HEAD(name, type) \
struct name { \
struct type *sqh_first; /* first element */ \
struct type **sqh_last; /* addr of last next element */ \
}
#define SIMPLEQ_HEAD(name, type) \
struct name { \
struct type *sqh_first; /* first element */ \
struct type **sqh_last; /* addr of last next element */ \
}
#define SIMPLEQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).sqh_first }
#define SIMPLEQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).sqh_first }
#define SIMPLEQ_ENTRY(type) \
struct { \
struct type *sqe_next; /* next element */ \
}
#define SIMPLEQ_ENTRY(type) \
struct { \
struct type *sqe_next; /* next element */ \
}
/*
* Simple queue access methods.
*/
#define SIMPLEQ_FIRST(head) ((head)->sqh_first)
#define SIMPLEQ_END(head) NULL
#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
#define SIMPLEQ_FIRST(head) ((head)->sqh_first)
#define SIMPLEQ_END(head) NULL
#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
#define SIMPLEQ_FOREACH(var, head, field) \
for((var) = SIMPLEQ_FIRST(head); \
(var) != SIMPLEQ_END(head); \
(var) = SIMPLEQ_NEXT(var, field))
#define SIMPLEQ_FOREACH(var, head, field) \
for ((var) = SIMPLEQ_FIRST(head); \
(var) != SIMPLEQ_END(head); \
(var) = SIMPLEQ_NEXT(var, field))
/*
* Simple queue functions.
*/
#define SIMPLEQ_INIT(head) do { \
(head)->sqh_first = NULL; \
(head)->sqh_last = &(head)->sqh_first; \
} while (0)
#define SIMPLEQ_INIT(head) \
do { \
(head)->sqh_first = NULL; \
(head)->sqh_last = &(head)->sqh_first; \
} while (0)
#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
(head)->sqh_last = &(elm)->field.sqe_next; \
(head)->sqh_first = (elm); \
} while (0)
#define SIMPLEQ_INSERT_HEAD(head, elm, field) \
do { \
if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
(head)->sqh_last = &(elm)->field.sqe_next; \
(head)->sqh_first = (elm); \
} while (0)
#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
(elm)->field.sqe_next = NULL; \
*(head)->sqh_last = (elm); \
(head)->sqh_last = &(elm)->field.sqe_next; \
} while (0)
#define SIMPLEQ_INSERT_TAIL(head, elm, field) \
do { \
(elm)->field.sqe_next = NULL; \
*(head)->sqh_last = (elm); \
(head)->sqh_last = &(elm)->field.sqe_next; \
} while (0)
#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
(head)->sqh_last = &(elm)->field.sqe_next; \
(listelm)->field.sqe_next = (elm); \
} while (0)
#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \
do { \
if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
(head)->sqh_last = &(elm)->field.sqe_next; \
(listelm)->field.sqe_next = (elm); \
} while (0)
#define SIMPLEQ_REMOVE_HEAD(head, field) do { \
if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
(head)->sqh_last = &(head)->sqh_first; \
} while (0)
#define SIMPLEQ_REMOVE_HEAD(head, field) \
do { \
if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
(head)->sqh_last = &(head)->sqh_first; \
} while (0)
/*
* Tail queue definitions.
*/
#define TAILQ_HEAD(name, type) \
struct name { \
struct type *tqh_first; /* first element */ \
struct type **tqh_last; /* addr of last next element */ \
}
#define TAILQ_HEAD(name, type) \
struct name { \
struct type *tqh_first; /* first element */ \
struct type **tqh_last; /* addr of last next element */ \
}
#define TAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).tqh_first }
#define TAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).tqh_first }
#define TAILQ_ENTRY(type) \
struct { \
struct type *tqe_next; /* next element */ \
struct type **tqe_prev; /* address of previous next element */ \
}
#define TAILQ_ENTRY(type) \
struct { \
struct type *tqe_next; /* next element */ \
struct type **tqe_prev; /* address of previous next element */ \
}
/*
* tail queue access methods
*/
#define TAILQ_FIRST(head) ((head)->tqh_first)
#define TAILQ_END(head) NULL
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
#define TAILQ_LAST(head, headname) \
(*(((struct headname *)((head)->tqh_last))->tqh_last))
#define TAILQ_FIRST(head) ((head)->tqh_first)
#define TAILQ_END(head) NULL
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
#define TAILQ_LAST(head, headname) \
(*(((struct headname *)((head)->tqh_last))->tqh_last))
/* XXX */
#define TAILQ_PREV(elm, headname, field) \
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
#define TAILQ_EMPTY(head) \
(TAILQ_FIRST(head) == TAILQ_END(head))
#define TAILQ_PREV(elm, headname, field) \
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
#define TAILQ_EMPTY(head) \
(TAILQ_FIRST(head) == TAILQ_END(head))
#define TAILQ_FOREACH(var, head, field) \
for((var) = TAILQ_FIRST(head); \
(var) != TAILQ_END(head); \
(var) = TAILQ_NEXT(var, field))
#define TAILQ_FOREACH(var, head, field) \
for ((var) = TAILQ_FIRST(head); \
(var) != TAILQ_END(head); \
(var) = TAILQ_NEXT(var, field))
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
for((var) = TAILQ_LAST(head, headname); \
(var) != TAILQ_END(head); \
(var) = TAILQ_PREV(var, headname, field))
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
for ((var) = TAILQ_LAST(head, headname); \
(var) != TAILQ_END(head); \
(var) = TAILQ_PREV(var, headname, field))
/*
* Tail queue functions.
*/
#define TAILQ_INIT(head) do { \
(head)->tqh_first = NULL; \
(head)->tqh_last = &(head)->tqh_first; \
} while (0)
#define TAILQ_INIT(head) \
do { \
(head)->tqh_first = NULL; \
(head)->tqh_last = &(head)->tqh_first; \
} while (0)
#define TAILQ_INSERT_HEAD(head, elm, field) do { \
if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
(head)->tqh_first->field.tqe_prev = \
&(elm)->field.tqe_next; \
else \
(head)->tqh_last = &(elm)->field.tqe_next; \
(head)->tqh_first = (elm); \
(elm)->field.tqe_prev = &(head)->tqh_first; \
} while (0)
#define TAILQ_INSERT_HEAD(head, elm, field) \
do { \
if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
(head)->tqh_first->field.tqe_prev = &(elm)->field.tqe_next; \
else \
(head)->tqh_last = &(elm)->field.tqe_next; \
(head)->tqh_first = (elm); \
(elm)->field.tqe_prev = &(head)->tqh_first; \
} while (0)
#define TAILQ_INSERT_TAIL(head, elm, field) do { \
(elm)->field.tqe_next = NULL; \
(elm)->field.tqe_prev = (head)->tqh_last; \
*(head)->tqh_last = (elm); \
(head)->tqh_last = &(elm)->field.tqe_next; \
} while (0)
#define TAILQ_INSERT_TAIL(head, elm, field) \
do { \
(elm)->field.tqe_next = NULL; \
(elm)->field.tqe_prev = (head)->tqh_last; \
*(head)->tqh_last = (elm); \
(head)->tqh_last = &(elm)->field.tqe_next; \
} while (0)
#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
(elm)->field.tqe_next->field.tqe_prev = \
&(elm)->field.tqe_next; \
else \
(head)->tqh_last = &(elm)->field.tqe_next; \
(listelm)->field.tqe_next = (elm); \
(elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
} while (0)
#define TAILQ_INSERT_AFTER(head, listelm, elm, field) \
do { \
if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL) \
(elm)->field.tqe_next->field.tqe_prev = &(elm)->field.tqe_next; \
else \
(head)->tqh_last = &(elm)->field.tqe_next; \
(listelm)->field.tqe_next = (elm); \
(elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
} while (0)
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
(elm)->field.tqe_next = (listelm); \
*(listelm)->field.tqe_prev = (elm); \
(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
} while (0)
#define TAILQ_INSERT_BEFORE(listelm, elm, field) \
do { \
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
(elm)->field.tqe_next = (listelm); \
*(listelm)->field.tqe_prev = (elm); \
(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
} while (0)
#define TAILQ_REMOVE(head, elm, field) do { \
if (((elm)->field.tqe_next) != NULL) \
(elm)->field.tqe_next->field.tqe_prev = \
(elm)->field.tqe_prev; \
else \
(head)->tqh_last = (elm)->field.tqe_prev; \
*(elm)->field.tqe_prev = (elm)->field.tqe_next; \
_Q_INVALIDATE((elm)->field.tqe_prev); \
_Q_INVALIDATE((elm)->field.tqe_next); \
} while (0)
#define TAILQ_REMOVE(head, elm, field) \
do { \
if (((elm)->field.tqe_next) != NULL) \
(elm)->field.tqe_next->field.tqe_prev = (elm)->field.tqe_prev; \
else \
(head)->tqh_last = (elm)->field.tqe_prev; \
*(elm)->field.tqe_prev = (elm)->field.tqe_next; \
_Q_INVALIDATE((elm)->field.tqe_prev); \
_Q_INVALIDATE((elm)->field.tqe_next); \
} while (0)
#define TAILQ_REPLACE(head, elm, elm2, field) do { \
if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
(elm2)->field.tqe_next->field.tqe_prev = \
&(elm2)->field.tqe_next; \
else \
(head)->tqh_last = &(elm2)->field.tqe_next; \
(elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
*(elm2)->field.tqe_prev = (elm2); \
_Q_INVALIDATE((elm)->field.tqe_prev); \
_Q_INVALIDATE((elm)->field.tqe_next); \
} while (0)
#define TAILQ_REPLACE(head, elm, elm2, field) \
do { \
if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
(elm2)->field.tqe_next->field.tqe_prev = &(elm2)->field.tqe_next; \
else \
(head)->tqh_last = &(elm2)->field.tqe_next; \
(elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
*(elm2)->field.tqe_prev = (elm2); \
_Q_INVALIDATE((elm)->field.tqe_prev); \
_Q_INVALIDATE((elm)->field.tqe_next); \
} while (0)
/*
* Circular queue definitions.
*/
#define CIRCLEQ_HEAD(name, type) \
struct name { \
struct type *cqh_first; /* first element */ \
struct type *cqh_last; /* last element */ \
}
#define CIRCLEQ_HEAD(name, type) \
struct name { \
struct type *cqh_first; /* first element */ \
struct type *cqh_last; /* last element */ \
}
#define CIRCLEQ_HEAD_INITIALIZER(head) \
{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
#define CIRCLEQ_HEAD_INITIALIZER(head) \
{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
#define CIRCLEQ_ENTRY(type) \
struct { \
struct type *cqe_next; /* next element */ \
struct type *cqe_prev; /* previous element */ \
}
#define CIRCLEQ_ENTRY(type) \
struct { \
struct type *cqe_next; /* next element */ \
struct type *cqe_prev; /* previous element */ \
}
/*
* Circular queue access methods
*/
#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
#define CIRCLEQ_LAST(head) ((head)->cqh_last)
#define CIRCLEQ_END(head) ((void *)(head))
#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
#define CIRCLEQ_EMPTY(head) \
(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
#define CIRCLEQ_LAST(head) ((head)->cqh_last)
#define CIRCLEQ_END(head) ((void *)(head))
#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
#define CIRCLEQ_EMPTY(head) \
(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
#define CIRCLEQ_FOREACH(var, head, field) \
for((var) = CIRCLEQ_FIRST(head); \
(var) != CIRCLEQ_END(head); \
(var) = CIRCLEQ_NEXT(var, field))
#define CIRCLEQ_FOREACH(var, head, field) \
for ((var) = CIRCLEQ_FIRST(head); \
(var) != CIRCLEQ_END(head); \
(var) = CIRCLEQ_NEXT(var, field))
#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
for((var) = CIRCLEQ_LAST(head); \
(var) != CIRCLEQ_END(head); \
(var) = CIRCLEQ_PREV(var, field))
#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
for ((var) = CIRCLEQ_LAST(head); \
(var) != CIRCLEQ_END(head); \
(var) = CIRCLEQ_PREV(var, field))
/*
* Circular queue functions.
*/
#define CIRCLEQ_INIT(head) do { \
(head)->cqh_first = CIRCLEQ_END(head); \
(head)->cqh_last = CIRCLEQ_END(head); \
} while (0)
#define CIRCLEQ_INIT(head) \
do { \
(head)->cqh_first = CIRCLEQ_END(head); \
(head)->cqh_last = CIRCLEQ_END(head); \
} while (0)
#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
(elm)->field.cqe_next = (listelm)->field.cqe_next; \
(elm)->field.cqe_prev = (listelm); \
if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \
(head)->cqh_last = (elm); \
else \
(listelm)->field.cqe_next->field.cqe_prev = (elm); \
(listelm)->field.cqe_next = (elm); \
} while (0)
#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) \
do { \
(elm)->field.cqe_next = (listelm)->field.cqe_next; \
(elm)->field.cqe_prev = (listelm); \
if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \
(head)->cqh_last = (elm); \
else \
(listelm)->field.cqe_next->field.cqe_prev = (elm); \
(listelm)->field.cqe_next = (elm); \
} while (0)
#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
(elm)->field.cqe_next = (listelm); \
(elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \
(head)->cqh_first = (elm); \
else \
(listelm)->field.cqe_prev->field.cqe_next = (elm); \
(listelm)->field.cqe_prev = (elm); \
} while (0)
#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) \
do { \
(elm)->field.cqe_next = (listelm); \
(elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \
(head)->cqh_first = (elm); \
else \
(listelm)->field.cqe_prev->field.cqe_next = (elm); \
(listelm)->field.cqe_prev = (elm); \
} while (0)
#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
(elm)->field.cqe_next = (head)->cqh_first; \
(elm)->field.cqe_prev = CIRCLEQ_END(head); \
if ((head)->cqh_last == CIRCLEQ_END(head)) \
(head)->cqh_last = (elm); \
else \
(head)->cqh_first->field.cqe_prev = (elm); \
(head)->cqh_first = (elm); \
} while (0)
#define CIRCLEQ_INSERT_HEAD(head, elm, field) \
do { \
(elm)->field.cqe_next = (head)->cqh_first; \
(elm)->field.cqe_prev = CIRCLEQ_END(head); \
if ((head)->cqh_last == CIRCLEQ_END(head)) \
(head)->cqh_last = (elm); \
else \
(head)->cqh_first->field.cqe_prev = (elm); \
(head)->cqh_first = (elm); \
} while (0)
#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
(elm)->field.cqe_next = CIRCLEQ_END(head); \
(elm)->field.cqe_prev = (head)->cqh_last; \
if ((head)->cqh_first == CIRCLEQ_END(head)) \
(head)->cqh_first = (elm); \
else \
(head)->cqh_last->field.cqe_next = (elm); \
(head)->cqh_last = (elm); \
} while (0)
#define CIRCLEQ_INSERT_TAIL(head, elm, field) \
do { \
(elm)->field.cqe_next = CIRCLEQ_END(head); \
(elm)->field.cqe_prev = (head)->cqh_last; \
if ((head)->cqh_first == CIRCLEQ_END(head)) \
(head)->cqh_first = (elm); \
else \
(head)->cqh_last->field.cqe_next = (elm); \
(head)->cqh_last = (elm); \
} while (0)
#define CIRCLEQ_REMOVE(head, elm, field) do { \
if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \
(head)->cqh_last = (elm)->field.cqe_prev; \
else \
(elm)->field.cqe_next->field.cqe_prev = \
(elm)->field.cqe_prev; \
if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \
(head)->cqh_first = (elm)->field.cqe_next; \
else \
(elm)->field.cqe_prev->field.cqe_next = \
(elm)->field.cqe_next; \
_Q_INVALIDATE((elm)->field.cqe_prev); \
_Q_INVALIDATE((elm)->field.cqe_next); \
} while (0)
#define CIRCLEQ_REMOVE(head, elm, field) \
do { \
if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \
(head)->cqh_last = (elm)->field.cqe_prev; \
else \
(elm)->field.cqe_next->field.cqe_prev = (elm)->field.cqe_prev; \
if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \
(head)->cqh_first = (elm)->field.cqe_next; \
else \
(elm)->field.cqe_prev->field.cqe_next = (elm)->field.cqe_next; \
_Q_INVALIDATE((elm)->field.cqe_prev); \
_Q_INVALIDATE((elm)->field.cqe_next); \
} while (0)
#define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
CIRCLEQ_END(head)) \
(head)->cqh_last = (elm2); \
else \
(elm2)->field.cqe_next->field.cqe_prev = (elm2); \
if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
CIRCLEQ_END(head)) \
(head)->cqh_first = (elm2); \
else \
(elm2)->field.cqe_prev->field.cqe_next = (elm2); \
_Q_INVALIDATE((elm)->field.cqe_prev); \
_Q_INVALIDATE((elm)->field.cqe_next); \
} while (0)
#define CIRCLEQ_REPLACE(head, elm, elm2, field) \
do { \
if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == CIRCLEQ_END(head)) \
(head)->cqh_last = (elm2); \
else \
(elm2)->field.cqe_next->field.cqe_prev = (elm2); \
if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == CIRCLEQ_END(head)) \
(head)->cqh_first = (elm2); \
else \
(elm2)->field.cqe_prev->field.cqe_next = (elm2); \
_Q_INVALIDATE((elm)->field.cqe_prev); \
_Q_INVALIDATE((elm)->field.cqe_next); \
} while (0)
#endif /* !_SYS_QUEUE_H_ */
#endif /* !_SYS_QUEUE_H_ */

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <sys/stat.h>
#include <stdlib.h>
#include <ifaddrs.h>
@ -6,34 +6,33 @@
#include "i3status.h"
const char *first_eth_interface(const net_type_t type) {
static char *interface = NULL;
struct ifaddrs *ifaddr, *addrp;
struct stat stbuf;
static char path[1024];
static char *interface = NULL;
struct ifaddrs *ifaddr, *addrp;
struct stat stbuf;
static char path[1024];
getifaddrs(&ifaddr);
getifaddrs(&ifaddr);
if (ifaddr == NULL)
return NULL;
if (ifaddr == NULL)
return NULL;
free(interface);
interface = NULL;
for (addrp = ifaddr;
addrp != NULL;
addrp = addrp->ifa_next) {
if (strncasecmp("lo", addrp->ifa_name, strlen("lo")) == 0)
continue;
// Skip this interface if it is a wireless interface.
snprintf(path, sizeof(path), "/sys/class/net/%s/wireless", addrp->ifa_name);
const bool is_wireless = (stat(path, &stbuf) == 0);
if (( is_wireless && type == NET_TYPE_ETHERNET) ||
(!is_wireless && type == NET_TYPE_WIRELESS))
continue;
interface = strdup(addrp->ifa_name);
break;
}
free(interface);
interface = NULL;
for (addrp = ifaddr;
addrp != NULL;
addrp = addrp->ifa_next) {
if (strncasecmp("lo", addrp->ifa_name, strlen("lo")) == 0)
continue;
// Skip this interface if it is a wireless interface.
snprintf(path, sizeof(path), "/sys/class/net/%s/wireless", addrp->ifa_name);
const bool is_wireless = (stat(path, &stbuf) == 0);
if ((is_wireless && type == NET_TYPE_ETHERNET) ||
(!is_wireless && type == NET_TYPE_WIRELESS))
continue;
interface = strdup(addrp->ifa_name);
break;
}
freeifaddrs(ifaddr);
return interface;
freeifaddrs(ifaddr);
return interface;
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <sys/types.h>
#include <string.h>
#include <stdarg.h>
@ -15,18 +15,18 @@
*
*/
bool slurp(const char *filename, char *destination, int size) {
int fd;
int fd;
if ((fd = open(filename, O_RDONLY)) == -1)
return false;
if ((fd = open(filename, O_RDONLY)) == -1)
return false;
/* We need one byte for the trailing 0 byte */
int n = read(fd, destination, size-1);
if (n != -1)
destination[n] = '\0';
(void)close(fd);
/* We need one byte for the trailing 0 byte */
int n = read(fd, destination, size - 1);
if (n != -1)
destination[n] = '\0';
(void)close(fd);
return true;
return true;
}
/*
@ -35,15 +35,15 @@ bool slurp(const char *filename, char *destination, int size) {
*
*/
char *skip_character(char *input, char character, int amount) {
char *walk;
size_t len = strlen(input);
int blanks = 0;
char *walk;
size_t len = strlen(input);
int blanks = 0;
for (walk = input; ((size_t)(walk - input) < len) && (blanks < amount); walk++)
if (*walk == character)
blanks++;
for (walk = input; ((size_t)(walk - input) < len) && (blanks < amount); walk++)
if (*walk == character)
blanks++;
return (walk == input ? walk : walk-1);
return (walk == input ? walk : walk - 1);
}
/*
@ -51,12 +51,12 @@ char *skip_character(char *input, char character, int amount) {
*
*/
void die(const char *fmt, ...) {
char buffer[512];
va_list ap;
va_start(ap, fmt);
(void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
char buffer[512];
va_list ap;
va_start(ap, fmt);
(void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
fprintf(stderr, "%s", buffer);
exit(EXIT_FAILURE);
fprintf(stderr, "%s", buffer);
exit(EXIT_FAILURE);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
@ -15,29 +15,29 @@
*
*/
char *color(const char *colorstr) {
static char colorbuf[32];
if (!cfg_getbool(cfg_general, "colors")) {
colorbuf[0] = '\0';
return colorbuf;
}
if (output_format == O_DZEN2)
(void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr));
else if (output_format == O_XMOBAR)
(void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", cfg_getstr(cfg_general, colorstr));
else if (output_format == O_TERM) {
/* The escape-sequence for color is <CSI><col>;1m (bright/bold
* output), where col is a 3-bit rgb-value with b in the
* least-significant bit. We round the given color to the
* nearist 3-bit-depth color and output the escape-sequence */
char *str = cfg_getstr(cfg_general, colorstr);
int col = strtol(str + 1, NULL, 16);
int r = (col & (0xFF << 0)) / 0x80;
int g = (col & (0xFF << 8)) / 0x8000;
int b = (col & (0xFF << 16)) / 0x800000;
col = (r << 2) | (g << 1) | b;
(void)snprintf(colorbuf, sizeof(colorbuf), "\033[3%d;1m", col);
}
static char colorbuf[32];
if (!cfg_getbool(cfg_general, "colors")) {
colorbuf[0] = '\0';
return colorbuf;
}
if (output_format == O_DZEN2)
(void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr));
else if (output_format == O_XMOBAR)
(void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", cfg_getstr(cfg_general, colorstr));
else if (output_format == O_TERM) {
/* The escape-sequence for color is <CSI><col>;1m (bright/bold
* output), where col is a 3-bit rgb-value with b in the
* least-significant bit. We round the given color to the
* nearist 3-bit-depth color and output the escape-sequence */
char *str = cfg_getstr(cfg_general, colorstr);
int col = strtol(str + 1, NULL, 16);
int r = (col & (0xFF << 0)) / 0x80;
int g = (col & (0xFF << 8)) / 0x8000;
int b = (col & (0xFF << 16)) / 0x800000;
col = (r << 2) | (g << 1) | b;
(void)snprintf(colorbuf, sizeof(colorbuf), "\033[3%d;1m", col);
}
return colorbuf;
}
/*
@ -45,30 +45,31 @@ char *color(const char *colorstr) {
*
*/
char *endcolor(void) {
if (output_format == O_XMOBAR)
return "</fc>";
else if (output_format == O_TERM)
return "\033[0m";
else return "";
if (output_format == O_XMOBAR)
return "</fc>";
else if (output_format == O_TERM)
return "\033[0m";
else
return "";
}
void print_separator(const char *separator) {
if (output_format == O_I3BAR || strlen(separator) == 0)
return;
if (output_format == O_I3BAR || strlen(separator) == 0)
return;
if (output_format == O_DZEN2)
printf("^fg(%s)%s^fg()", cfg_getstr(cfg_general, "color_separator"), separator);
else if (output_format == O_XMOBAR)
printf("<fc=%s>%s</fc>", cfg_getstr(cfg_general, "color_separator"), separator);
else if (output_format == O_TERM)
printf("%s%s%s", color("color_separator"), separator, endcolor());
else if (output_format == O_NONE)
printf("%s", separator);
if (output_format == O_DZEN2)
printf("^fg(%s)%s^fg()", cfg_getstr(cfg_general, "color_separator"), separator);
else if (output_format == O_XMOBAR)
printf("<fc=%s>%s</fc>", cfg_getstr(cfg_general, "color_separator"), separator);
else if (output_format == O_TERM)
printf("%s%s%s", color("color_separator"), separator, endcolor());
else if (output_format == O_NONE)
printf("%s", separator);
}
/*
* The term-output hides the cursor. We call this on exit to reset that.
*/
void reset_cursor(void) {
printf("\033[?25h");
printf("\033[?25h");
}

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
@ -13,8 +13,8 @@
#include <sys/types.h>
#include <sys/sysctl.h>
#define TZ_ZEROC 2732
#define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
#define TZ_AVG(x) ((x) - TZ_ZEROC) / 10
#define TZ_KELVTOC(x) (((x)-TZ_ZEROC) / 10), abs(((x)-TZ_ZEROC) % 10)
#define TZ_AVG(x) ((x)-TZ_ZEROC) / 10
#endif
#if defined(__DragonFly__)
@ -43,171 +43,170 @@
#define MUKTOC(v) ((v - 273150000) / 1000000.0)
#endif
/*
* Reads the CPU temperature from /sys/class/thermal/thermal_zone%d/temp (or
* the user provided path) and returns the temperature in degree celcius.
*
*/
void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) {
char *outwalk = buffer;
char *outwalk = buffer;
#ifdef THERMAL_ZONE
const char *walk;
bool colorful_output = false;
char *thermal_zone;
const char *walk;
bool colorful_output = false;
char *thermal_zone;
if (path == NULL)
asprintf(&thermal_zone, THERMAL_ZONE, zone);
else
asprintf(&thermal_zone, path, zone);
if (path == NULL)
asprintf(&thermal_zone, THERMAL_ZONE, zone);
else
asprintf(&thermal_zone, path, zone);
INSTANCE(thermal_zone);
INSTANCE(thermal_zone);
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "degrees")) {
if (BEGINS_WITH(walk + 1, "degrees")) {
#if defined(LINUX)
static char buf[16];
long int temp;
if (!slurp(thermal_zone, buf, sizeof(buf)))
goto error;
temp = strtol(buf, NULL, 10);
if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
*(outwalk++) = '?';
else {
if ((temp/1000) >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
outwalk += sprintf(outwalk, "%ld", (temp/1000));
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
}
static char buf[16];
long int temp;
if (!slurp(thermal_zone, buf, sizeof(buf)))
goto error;
temp = strtol(buf, NULL, 10);
if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
*(outwalk++) = '?';
else {
if ((temp / 1000) >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
outwalk += sprintf(outwalk, "%ld", (temp / 1000));
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
}
#elif defined(__DragonFly__)
struct sensor th_sensor;
size_t th_sensorlen;
struct sensor th_sensor;
size_t th_sensorlen;
th_sensorlen = sizeof(th_sensor);
th_sensorlen = sizeof(th_sensor);
if (sysctlbyname(thermal_zone, &th_sensor, &th_sensorlen, NULL, 0) == -1) {
perror("sysctlbyname");
goto error;
}
if (MUKTOC(th_sensor.value) >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
outwalk += sprintf(outwalk, "%.2f", MUKTOC(th_sensor.value));
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
if (sysctlbyname(thermal_zone, &th_sensor, &th_sensorlen, NULL, 0) == -1) {
perror("sysctlbyname");
goto error;
}
if (MUKTOC(th_sensor.value) >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
outwalk += sprintf(outwalk, "%.2f", MUKTOC(th_sensor.value));
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
int sysctl_rslt;
size_t sysctl_size = sizeof(sysctl_rslt);
if (sysctlbyname(thermal_zone, &sysctl_rslt, &sysctl_size, NULL, 0))
goto error;
int sysctl_rslt;
size_t sysctl_size = sizeof(sysctl_rslt);
if (sysctlbyname(thermal_zone, &sysctl_rslt, &sysctl_size, NULL, 0))
goto error;
if (TZ_AVG(sysctl_rslt) >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
if (TZ_AVG(sysctl_rslt) >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
#elif defined(__OpenBSD__)
struct sensordev sensordev;
struct sensor sensor;
size_t sdlen, slen;
int dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
struct sensordev sensordev;
struct sensor sensor;
size_t sdlen, slen;
int dev, numt, mib[5] = {CTL_HW, HW_SENSORS, 0, 0, 0};
sdlen = sizeof(sensordev);
slen = sizeof(sensor);
sdlen = sizeof(sensordev);
slen = sizeof(sensor);
for (dev = 0; ; dev++) {
for (dev = 0;; dev++) {
mib[2] = dev;
if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
if (errno == ENXIO)
continue;
if (errno == ENOENT)
break;
goto error;
if (errno == ENXIO)
continue;
if (errno == ENOENT)
break;
goto error;
}
/* 'path' is the node within the full path (defaults to acpitz0). */
if (BEGINS_WITH(sensordev.xname, thermal_zone)) {
mib[3] = SENSOR_TEMP;
/* Limit to temo0, but should retrieve from a full path... */
for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
mib[4] = numt;
if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
if (errno != ENOENT) {
warn("sysctl");
continue;
}
}
if ((int)MUKTOC(sensor.value) >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
outwalk += sprintf(outwalk, "%.2f", MUKTOC(sensor.value));
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
mib[3] = SENSOR_TEMP;
/* Limit to temo0, but should retrieve from a full path... */
for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
mib[4] = numt;
if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
if (errno != ENOENT) {
warn("sysctl");
continue;
}
}
if ((int)MUKTOC(sensor.value) >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
}
}
#elif defined(__NetBSD__)
int fd, rval;
bool err = false;
prop_dictionary_t dict;
prop_array_t array;
prop_object_iterator_t iter;
prop_object_iterator_t iter2;
prop_object_t obj, obj2, obj3;
fd = open("/dev/sysmon", O_RDONLY);
if (fd == -1)
outwalk += sprintf(outwalk, "%.2f", MUKTOC(sensor.value));
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
}
}
}
#elif defined(__NetBSD__)
int fd, rval;
bool err = false;
prop_dictionary_t dict;
prop_array_t array;
prop_object_iterator_t iter;
prop_object_iterator_t iter2;
prop_object_t obj, obj2, obj3;
fd = open("/dev/sysmon", O_RDONLY);
if (fd == -1)
goto error;
rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
if (rval == -1) {
err = true;
goto error_netbsd1;
}
rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
if (rval == -1) {
err = true;
goto error_netbsd1;
}
/* No drivers registered? */
if (prop_dictionary_count(dict) == 0) {
err = true;
goto error_netbsd2;
}
/* No drivers registered? */
if (prop_dictionary_count(dict) == 0) {
err = true;
goto error_netbsd2;
}
iter = prop_dictionary_iterator(dict);
if (iter == NULL) {
err = true;
goto error_netbsd2;
}
iter = prop_dictionary_iterator(dict);
if (iter == NULL) {
err = true;
goto error_netbsd2;
}
/* iterate over the dictionary returned by the kernel */
while ((obj = prop_object_iterator_next(iter)) != NULL) {
/* iterate over the dictionary returned by the kernel */
while ((obj = prop_object_iterator_next(iter)) != NULL) {
/* skip this dict if it's not what we're looking for */
if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) != strlen(thermal_zone)) ||
(strncmp(thermal_zone,
prop_dictionary_keysym_cstring_nocopy(obj),
strlen(thermal_zone)) != 0))
continue;
continue;
array = prop_dictionary_get_keysym(dict, obj);
if (prop_object_type(array) != PROP_TYPE_ARRAY) {
@ -223,48 +222,48 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
/* iterate over array of dicts specific to target sensor */
while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
obj3 = prop_dictionary_get(obj2, "cur-value");
obj3 = prop_dictionary_get(obj2, "cur-value");
float temp = MUKTOC(prop_number_integer_value(obj3));
if ((int)temp >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
float temp = MUKTOC(prop_number_integer_value(obj3));
if ((int)temp >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
outwalk += sprintf(outwalk, "%.2f", temp);
outwalk += sprintf(outwalk, "%.2f", temp);
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
if (colorful_output) {
END_COLOR;
colorful_output = false;
}
break;
break;
}
prop_object_iterator_release(iter2);
}
error_netbsd3:
prop_object_iterator_release(iter);
error_netbsd2:
prop_object_release(dict);
error_netbsd1:
close(fd);
if (err) goto error;
}
error_netbsd3:
prop_object_iterator_release(iter);
error_netbsd2:
prop_object_release(dict);
error_netbsd1:
close(fd);
if (err)
goto error;
#endif
walk += strlen("degrees");
}
walk += strlen("degrees");
}
}
free(thermal_zone);
free(thermal_zone);
OUTPUT_FULL_TEXT(buffer);
return;
OUTPUT_FULL_TEXT(buffer);
return;
error:
#endif
free(thermal_zone);
free(thermal_zone);
OUTPUT_FULL_TEXT("cant read temp");
(void)fputs("i3status: Cannot read temperature. Verify that you have a thermal zone in /sys/class/thermal or disable the cpu_temperature module in your i3status config.\n", stderr);
OUTPUT_FULL_TEXT("cant read temp");
(void)fputs("i3status: Cannot read temperature. Verify that you have a thermal zone in /sys/class/thermal or disable the cpu_temperature module in your i3status config.\n", stderr);
}

View File

@ -1,4 +1,4 @@
// vim:sw=8:sts=8:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
@ -30,7 +30,7 @@
#include "i3status.h"
static int prev_total = 0;
static int prev_idle = 0;
static int prev_idle = 0;
/*
* Reads the CPU utilization from /proc/stat and returns the usage as a
@ -38,74 +38,74 @@ static int prev_idle = 0;
*
*/
void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
const char *walk;
char *outwalk = buffer;
char buf[1024];
int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
int diff_idle, diff_total, diff_usage;
const char *walk;
char *outwalk = buffer;
char buf[1024];
int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
int diff_idle, diff_total, diff_usage;
#if defined(LINUX)
static char statpath[512];
strcpy(statpath, "/proc/stat");
if (!slurp(statpath, buf, sizeof(buf)) ||
sscanf(buf, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system, &curr_idle) != 4)
goto error;
static char statpath[512];
strcpy(statpath, "/proc/stat");
if (!slurp(statpath, buf, sizeof(buf)) ||
sscanf(buf, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system, &curr_idle) != 4)
goto error;
curr_total = curr_user + curr_nice + curr_system + curr_idle;
diff_idle = curr_idle - prev_idle;
diff_total = curr_total - prev_total;
diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0);
prev_total = curr_total;
prev_idle = curr_idle;
curr_total = curr_user + curr_nice + curr_system + curr_idle;
diff_idle = curr_idle - prev_idle;
diff_total = curr_total - prev_total;
diff_usage = (diff_total ? (1000 * (diff_total - diff_idle) / diff_total + 5) / 10 : 0);
prev_total = curr_total;
prev_idle = curr_idle;
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
size_t size;
long cp_time[CPUSTATES];
size = sizeof cp_time;
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0)
goto error;
size_t size;
long cp_time[CPUSTATES];
size = sizeof cp_time;
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0)
goto error;
#else
/* This information is taken from the boot cpu, any other cpus are currently ignored. */
long cp_time[CPUSTATES];
int mib[2];
size_t size = sizeof(cp_time);
/* This information is taken from the boot cpu, any other cpus are currently ignored. */
long cp_time[CPUSTATES];
int mib[2];
size_t size = sizeof(cp_time);
mib[0] = CTL_KERN;
mib[1] = KERN_CPTIME;
mib[0] = CTL_KERN;
mib[1] = KERN_CPTIME;
if (sysctl(mib, 2, cp_time, &size, NULL, 0))
goto error;
#endif
curr_user = cp_time[CP_USER];
curr_nice = cp_time[CP_NICE];
curr_system = cp_time[CP_SYS];
curr_idle = cp_time[CP_IDLE];
curr_total = curr_user + curr_nice + curr_system + curr_idle;
diff_idle = curr_idle - prev_idle;
diff_total = curr_total - prev_total;
diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0);
prev_total = curr_total;
prev_idle = curr_idle;
#else
if (sysctl(mib, 2, cp_time, &size, NULL, 0))
goto error;
#endif
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "usage")) {
outwalk += sprintf(outwalk, "%02d%%", diff_usage);
walk += strlen("usage");
}
curr_user = cp_time[CP_USER];
curr_nice = cp_time[CP_NICE];
curr_system = cp_time[CP_SYS];
curr_idle = cp_time[CP_IDLE];
curr_total = curr_user + curr_nice + curr_system + curr_idle;
diff_idle = curr_idle - prev_idle;
diff_total = curr_total - prev_total;
diff_usage = (diff_total ? (1000 * (diff_total - diff_idle) / diff_total + 5) / 10 : 0);
prev_total = curr_total;
prev_idle = curr_idle;
#else
goto error;
#endif
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
OUTPUT_FULL_TEXT(buffer);
return;
if (BEGINS_WITH(walk + 1, "usage")) {
outwalk += sprintf(outwalk, "%02d%%", diff_usage);
walk += strlen("usage");
}
}
OUTPUT_FULL_TEXT(buffer);
return;
error:
OUTPUT_FULL_TEXT("cant read cpu usage");
(void)fputs("i3status: Cannot read CPU usage\n", stderr);
OUTPUT_FULL_TEXT("cant read cpu usage");
(void)fputs("i3status: Cannot read CPU usage\n", stderr);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
@ -10,213 +10,208 @@
/* define fixed output-Strings */
char *season_long[5] = {
"Chaos",
"Discord",
"Confusion",
"Bureaucracy",
"The Aftermath"
};
"Chaos",
"Discord",
"Confusion",
"Bureaucracy",
"The Aftermath"};
char *season_short[5] = {
"Chs",
"Dsc",
"Cfn",
"Bcy",
"Afm"
};
"Chs",
"Dsc",
"Cfn",
"Bcy",
"Afm"};
char *day_long[5] = {
"Sweetmorn",
"Boomtime",
"Pungenday",
"Prickle-Prickle",
"Setting Orange"
};
"Sweetmorn",
"Boomtime",
"Pungenday",
"Prickle-Prickle",
"Setting Orange"};
char *day_short[5] = {
"SM",
"BT",
"PD",
"PP",
"SO"
};
"SM",
"BT",
"PD",
"PP",
"SO"};
char *holidays[10] = {
"Mungday",
"Mojoday",
"Syaday",
"Zaraday",
"Maladay",
"Chaoflux",
"Discoflux",
"Confuflux",
"Bureflux",
"Afflux"
};
"Mungday",
"Mojoday",
"Syaday",
"Zaraday",
"Maladay",
"Chaoflux",
"Discoflux",
"Confuflux",
"Bureflux",
"Afflux"};
/* A helper-struct, taking the discordian date */
struct disc_time {
int year;
int season;
int week_day;
int season_day;
int st_tibs_day;
int year;
int season;
int week_day;
int season_day;
int st_tibs_day;
};
/* Print the date *dt in format *format */
static int format_output(char *outwalk, char *format, struct disc_time *dt) {
char *orig_outwalk = outwalk;
char *i;
char *tibs_end = 0;
char *orig_outwalk = outwalk;
char *i;
char *tibs_end = 0;
for (i = format; *i != '\0'; i++) {
if (*i != '%') {
*(outwalk++) = *i;
continue;
}
switch (*(i+1)) {
/* Weekday in long and abbreviation */
case 'A':
outwalk += sprintf(outwalk, "%s", day_long[dt->week_day]);
break;
case 'a':
outwalk += sprintf(outwalk, "%s", day_short[dt->week_day]);
break;
/* Season in long and abbreviation */
case 'B':
outwalk += sprintf(outwalk, "%s", season_long[dt->season]);
break;
case 'b':
outwalk += sprintf(outwalk, "%s", season_short[dt->season]);
break;
/* Day of the season (ordinal and cardinal) */
case 'd':
outwalk += sprintf(outwalk, "%d", dt->season_day + 1);
break;
case 'e':
outwalk += sprintf(outwalk, "%d", dt->season_day + 1);
if (dt->season_day > 9 && dt->season_day < 13) {
outwalk += sprintf(outwalk, "th");
break;
}
switch (dt->season_day % 10) {
case 0:
outwalk += sprintf(outwalk, "st");
break;
case 1:
outwalk += sprintf(outwalk, "nd");
break;
case 2:
outwalk += sprintf(outwalk, "rd");
break;
default:
outwalk += sprintf(outwalk, "th");
break;
}
break;
/* YOLD */
case 'Y':
outwalk += sprintf(outwalk, "%d", dt->year);
break;
/* Holidays */
case 'H':
if (dt->season_day == 4) {
outwalk += sprintf(outwalk, "%s", holidays[dt->season]);
}
if (dt->season_day == 49) {
outwalk += sprintf(outwalk, "%s", holidays[dt->season + 5]);
}
break;
/* Stop parsing the format string, except on Holidays */
case 'N':
if (dt->season_day != 4 && dt->season_day != 49) {
return (outwalk - orig_outwalk);
}
break;
/* Newline- and Tabbing-characters */
case 'n':
outwalk += sprintf(outwalk, "\n");
break;
case 't':
outwalk += sprintf(outwalk, "\t");
break;
/* The St. Tib's Day replacement */
case '{':
tibs_end = strstr(i, "%}");
if (tibs_end == NULL) {
i++;
break;
}
if (dt->st_tibs_day) {
/* We outpt "St. Tib's Day... */
outwalk += sprintf(outwalk, "St. Tib's Day");
} else {
/* ...or parse the substring between %{ and %} ... */
*tibs_end = '\0';
outwalk += format_output(outwalk, i + 2, dt);
*tibs_end = '%';
}
/* ...and continue with the rest */
i = tibs_end;
break;
case '}':
i++;
break;
default:
/* No escape-sequence, so we just skip */
outwalk += sprintf(outwalk, "%%%c", *(i+1));
break;
}
i++;
for (i = format; *i != '\0'; i++) {
if (*i != '%') {
*(outwalk++) = *i;
continue;
}
return (outwalk - orig_outwalk);
switch (*(i + 1)) {
/* Weekday in long and abbreviation */
case 'A':
outwalk += sprintf(outwalk, "%s", day_long[dt->week_day]);
break;
case 'a':
outwalk += sprintf(outwalk, "%s", day_short[dt->week_day]);
break;
/* Season in long and abbreviation */
case 'B':
outwalk += sprintf(outwalk, "%s", season_long[dt->season]);
break;
case 'b':
outwalk += sprintf(outwalk, "%s", season_short[dt->season]);
break;
/* Day of the season (ordinal and cardinal) */
case 'd':
outwalk += sprintf(outwalk, "%d", dt->season_day + 1);
break;
case 'e':
outwalk += sprintf(outwalk, "%d", dt->season_day + 1);
if (dt->season_day > 9 && dt->season_day < 13) {
outwalk += sprintf(outwalk, "th");
break;
}
switch (dt->season_day % 10) {
case 0:
outwalk += sprintf(outwalk, "st");
break;
case 1:
outwalk += sprintf(outwalk, "nd");
break;
case 2:
outwalk += sprintf(outwalk, "rd");
break;
default:
outwalk += sprintf(outwalk, "th");
break;
}
break;
/* YOLD */
case 'Y':
outwalk += sprintf(outwalk, "%d", dt->year);
break;
/* Holidays */
case 'H':
if (dt->season_day == 4) {
outwalk += sprintf(outwalk, "%s", holidays[dt->season]);
}
if (dt->season_day == 49) {
outwalk += sprintf(outwalk, "%s", holidays[dt->season + 5]);
}
break;
/* Stop parsing the format string, except on Holidays */
case 'N':
if (dt->season_day != 4 && dt->season_day != 49) {
return (outwalk - orig_outwalk);
}
break;
/* Newline- and Tabbing-characters */
case 'n':
outwalk += sprintf(outwalk, "\n");
break;
case 't':
outwalk += sprintf(outwalk, "\t");
break;
/* The St. Tib's Day replacement */
case '{':
tibs_end = strstr(i, "%}");
if (tibs_end == NULL) {
i++;
break;
}
if (dt->st_tibs_day) {
/* We outpt "St. Tib's Day... */
outwalk += sprintf(outwalk, "St. Tib's Day");
} else {
/* ...or parse the substring between %{ and %} ... */
*tibs_end = '\0';
outwalk += format_output(outwalk, i + 2, dt);
*tibs_end = '%';
}
/* ...and continue with the rest */
i = tibs_end;
break;
case '}':
i++;
break;
default:
/* No escape-sequence, so we just skip */
outwalk += sprintf(outwalk, "%%%c", *(i + 1));
break;
}
i++;
}
return (outwalk - orig_outwalk);
}
/* Get the current date and convert it to discordian */
struct disc_time *get_ddate(struct tm *current_tm) {
static struct disc_time dt;
static struct disc_time dt;
if (current_tm == NULL)
return NULL;
if (current_tm == NULL)
return NULL;
/* We have to know, whether we have to insert St. Tib's Day, so whether it's a leap
year in gregorian calendar */
int is_leap_year = !(current_tm->tm_year % 4) &&
(!(current_tm->tm_year % 400) || current_tm->tm_year % 100);
/* We have to know, whether we have to insert St. Tib's Day, so whether it's a leap
* year in gregorian calendar */
int is_leap_year = !(current_tm->tm_year % 4) &&
(!(current_tm->tm_year % 400) || current_tm->tm_year % 100);
/* If St. Tib's Day has passed, it will be necessary to skip a day. */
int yday = current_tm->tm_yday;
/* If St. Tib's Day has passed, it will be necessary to skip a day. */
int yday = current_tm->tm_yday;
if (is_leap_year && yday == 59) {
/* On St. Tibs Day we don't have to define a date */
dt.st_tibs_day = 1;
} else {
dt.st_tibs_day = 0;
if (is_leap_year && yday > 59)
yday -= 1;
if (is_leap_year && yday == 59) {
/* On St. Tibs Day we don't have to define a date */
dt.st_tibs_day = 1;
} else {
dt.st_tibs_day = 0;
if (is_leap_year && yday > 59)
yday -= 1;
dt.season_day = yday % 73;
dt.week_day = yday % 5;
}
dt.year = current_tm->tm_year + 3066;
dt.season = yday / 73;
return &dt;
dt.season_day = yday % 73;
dt.week_day = yday % 5;
}
dt.year = current_tm->tm_year + 3066;
dt.season = yday / 73;
return &dt;
}
void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t) {
char *outwalk = buffer;
static char *form = NULL;
struct tm current_tm;
struct disc_time *dt;
set_timezone(NULL); /* Use local time. */
localtime_r(&t, &current_tm);
if ((dt = get_ddate(&current_tm)) == NULL)
return;
if (form == NULL)
if ((form = malloc(strlen(format) + 1)) == NULL)
return;
strcpy(form, format);
outwalk += format_output(outwalk, form, dt);
OUTPUT_FULL_TEXT(buffer);
char *outwalk = buffer;
static char *form = NULL;
struct tm current_tm;
struct disc_time *dt;
set_timezone(NULL); /* Use local time. */
localtime_r(&t, &current_tm);
if ((dt = get_ddate(&current_tm)) == NULL)
return;
if (form == NULL)
if ((form = malloc(strlen(format) + 1)) == NULL)
return;
strcpy(form, format);
outwalk += format_output(outwalk, form, dt);
OUTPUT_FULL_TEXT(buffer);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -22,22 +22,22 @@
#define MAX_EXPONENT 4
static const char * const iec_symbols[MAX_EXPONENT+1] = {"", "Ki", "Mi", "Gi", "Ti"};
static const char * const si_symbols[MAX_EXPONENT+1] = {"", "k", "M", "G", "T"};
static const char * const custom_symbols[MAX_EXPONENT+1] = {"", "K", "M", "G", "T"};
static const char *const iec_symbols[MAX_EXPONENT + 1] = {"", "Ki", "Mi", "Gi", "Ti"};
static const char *const si_symbols[MAX_EXPONENT + 1] = {"", "k", "M", "G", "T"};
static const char *const custom_symbols[MAX_EXPONENT + 1] = {"", "K", "M", "G", "T"};
/*
* Formats bytes according to the given base and set of symbols.
*
*/
static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char * const symbols[]) {
double size = bytes;
int exponent = 0;
while (size >= base && exponent < MAX_EXPONENT) {
size /= base;
exponent += 1;
}
return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char *const symbols[]) {
double size = bytes;
int exponent = 0;
while (size >= base && exponent < MAX_EXPONENT) {
size /= base;
exponent += 1;
}
return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
}
/*
@ -45,13 +45,13 @@ static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char
*
*/
static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
if (strcasecmp(prefix_type, "decimal") == 0) {
return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
} else if (strcasecmp(prefix_type, "custom") == 0) {
return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
} else {
return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
}
if (strcasecmp(prefix_type, "decimal") == 0) {
return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
} else if (strcasecmp(prefix_type, "custom") == 0) {
return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
} else {
return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
}
}
/*
@ -63,44 +63,44 @@ static bool below_threshold(struct statfs buf, const char *prefix_type, const ch
#else
static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
#endif
if (strcasecmp(threshold_type, "percentage_free") == 0) {
return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
} else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
} else if (strcasecmp(threshold_type, "bytes_free") == 0) {
return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
} else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
} else if (threshold_type[0] != '\0' && strncasecmp(threshold_type+1, "bytes_", strlen("bytes_")) == 0) {
uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
double factor = 1;
if (strcasecmp(threshold_type, "percentage_free") == 0) {
return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
} else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
} else if (strcasecmp(threshold_type, "bytes_free") == 0) {
return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
} else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
} else if (threshold_type[0] != '\0' && strncasecmp(threshold_type + 1, "bytes_", strlen("bytes_")) == 0) {
uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
double factor = 1;
switch (threshold_type[0]) {
case 'T':
case 't':
factor *= base;
case 'G':
case 'g':
factor *= base;
case 'M':
case 'm':
factor *= base;
case 'K':
case 'k':
factor *= base;
break;
default:
return false;
}
if (strcasecmp(threshold_type+1, "bytes_free") == 0) {
return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
} else if (strcasecmp(threshold_type+1, "bytes_avail") == 0) {
return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
}
switch (threshold_type[0]) {
case 'T':
case 't':
factor *= base;
case 'G':
case 'g':
factor *= base;
case 'M':
case 'm':
factor *= base;
case 'K':
case 'k':
factor *= base;
break;
default:
return false;
}
return false;
if (strcasecmp(threshold_type + 1, "bytes_free") == 0) {
return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
} else if (strcasecmp(threshold_type + 1, "bytes_avail") == 0) {
return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
}
}
return false;
}
/*
@ -109,97 +109,97 @@ static bool below_threshold(struct statvfs buf, const char *prefix_type, const c
*
*/
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) {
const char *walk;
char *outwalk = buffer;
bool colorful_output = false;
const char *walk;
char *outwalk = buffer;
bool colorful_output = false;
INSTANCE(path);
INSTANCE(path);
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
struct statfs buf;
struct statfs buf;
if (statfs(path, &buf) == -1)
return;
if (statfs(path, &buf) == -1)
return;
#else
struct statvfs buf;
struct statvfs buf;
if (statvfs(path, &buf) == -1)
return;
if (statvfs(path, &buf) == -1)
return;
if (format_not_mounted != NULL) {
FILE *mntentfile = setmntent("/etc/mtab", "r");
struct mntent *m;
bool found = false;
if (format_not_mounted != NULL) {
FILE *mntentfile = setmntent("/etc/mtab", "r");
struct mntent *m;
bool found = false;
while ((m = getmntent(mntentfile)) != NULL) {
if (strcmp(m->mnt_dir, path) == 0) {
found = true;
break;
}
}
endmntent(mntentfile);
if (!found) {
format = format_not_mounted;
}
while ((m = getmntent(mntentfile)) != NULL) {
if (strcmp(m->mnt_dir, path) == 0) {
found = true;
break;
}
}
endmntent(mntentfile);
if (!found) {
format = format_not_mounted;
}
}
#endif
if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
START_COLOR("color_bad");
colorful_output = true;
if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
START_COLOR("color_bad");
colorful_output = true;
}
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "free")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
walk += strlen("free");
}
if (BEGINS_WITH(walk+1, "used")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
walk += strlen("used");
}
if (BEGINS_WITH(walk+1, "total")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
walk += strlen("total");
}
if (BEGINS_WITH(walk+1, "avail")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
walk += strlen("avail");
}
if (BEGINS_WITH(walk+1, "percentage_free")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
walk += strlen("percentage_free");
}
if (BEGINS_WITH(walk+1, "percentage_used_of_avail")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
walk += strlen("percentage_used_of_avail");
}
if (BEGINS_WITH(walk+1, "percentage_used")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
walk += strlen("percentage_used");
}
if (BEGINS_WITH(walk+1, "percentage_avail")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
walk += strlen("percentage_avail");
}
if (BEGINS_WITH(walk + 1, "free")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
walk += strlen("free");
}
if (colorful_output)
END_COLOR;
if (BEGINS_WITH(walk + 1, "used")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
walk += strlen("used");
}
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
if (BEGINS_WITH(walk + 1, "total")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
walk += strlen("total");
}
if (BEGINS_WITH(walk + 1, "avail")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
walk += strlen("avail");
}
if (BEGINS_WITH(walk + 1, "percentage_free")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
walk += strlen("percentage_free");
}
if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
walk += strlen("percentage_used_of_avail");
}
if (BEGINS_WITH(walk + 1, "percentage_used")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
walk += strlen("percentage_used");
}
if (BEGINS_WITH(walk + 1, "percentage_avail")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
walk += strlen("percentage_avail");
}
}
if (colorful_output)
END_COLOR;
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <string.h>
#include <limits.h>
#include <stdio.h>
@ -16,15 +16,15 @@
#if defined(LINUX)
#include <linux/ethtool.h>
#include <linux/sockios.h>
#define PART_ETHSPEED "E: %s (%d Mbit/s)"
#define PART_ETHSPEED "E: %s (%d Mbit/s)"
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#include <net/if_media.h>
#define IFM_TYPE_MATCH(dt, t) \
(IFM_TYPE((dt)) == 0 || IFM_TYPE((dt)) == IFM_TYPE((t)))
#define IFM_TYPE_MATCH(dt, t) \
(IFM_TYPE((dt)) == 0 || IFM_TYPE((dt)) == IFM_TYPE((t)))
#define PART_ETHSPEED "E: %s (%s)"
#define PART_ETHSPEED "E: %s (%s)"
#endif
#if defined(__OpenBSD__) || defined(__NetBSD__)
@ -34,75 +34,76 @@
static int print_eth_speed(char *outwalk, const char *interface) {
#if defined(LINUX)
/* This code path requires root privileges */
int ethspeed = 0;
struct ifreq ifr;
struct ethtool_cmd ecmd;
/* This code path requires root privileges */
int ethspeed = 0;
struct ifreq ifr;
struct ethtool_cmd ecmd;
ecmd.cmd = ETHTOOL_GSET;
(void)memset(&ifr, 0, sizeof(ifr));
ifr.ifr_data = (caddr_t)&ecmd;
(void)strcpy(ifr.ifr_name, interface);
if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
return sprintf(outwalk, "%d Mbit/s", ethspeed);
} else return sprintf(outwalk, "?");
ecmd.cmd = ETHTOOL_GSET;
(void)memset(&ifr, 0, sizeof(ifr));
ifr.ifr_data = (caddr_t)&ecmd;
(void)strcpy(ifr.ifr_name, interface);
if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
return sprintf(outwalk, "%d Mbit/s", ethspeed);
} else
return sprintf(outwalk, "?");
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
char *ethspeed;
struct ifmediareq ifm;
(void)memset(&ifm, 0, sizeof(ifm));
(void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
int ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
char *ethspeed;
struct ifmediareq ifm;
(void)memset(&ifm, 0, sizeof(ifm));
(void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
int ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
/* Get the description of the media type, partially taken from
* FreeBSD's ifconfig */
const struct ifmedia_description *desc;
struct ifmedia_description ifm_subtype_descriptions[] =
IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
/* Get the description of the media type, partially taken from
* FreeBSD's ifconfig */
const struct ifmedia_description *desc;
struct ifmedia_description ifm_subtype_descriptions[] =
IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
for (desc = ifm_subtype_descriptions;
desc->ifmt_string != NULL;
desc++) {
if (IFM_TYPE_MATCH(desc->ifmt_word, ifm.ifm_active) &&
IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifm.ifm_active))
break;
}
ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
return sprintf(outwalk, "%s", ethspeed);
for (desc = ifm_subtype_descriptions;
desc->ifmt_string != NULL;
desc++) {
if (IFM_TYPE_MATCH(desc->ifmt_word, ifm.ifm_active) &&
IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifm.ifm_active))
break;
}
ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
return sprintf(outwalk, "%s", ethspeed);
#elif defined(__OpenBSD__) || defined(__NetBSD__)
char *ethspeed;
struct ifmediareq ifmr;
char *ethspeed;
struct ifmediareq ifmr;
(void) memset(&ifmr, 0, sizeof(ifmr));
(void) strlcpy(ifmr.ifm_name, interface, sizeof(ifmr.ifm_name));
(void)memset(&ifmr, 0, sizeof(ifmr));
(void)strlcpy(ifmr.ifm_name, interface, sizeof(ifmr.ifm_name));
if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
if (errno != E2BIG)
return sprintf(outwalk, "?");
}
if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
if (errno != E2BIG)
return sprintf(outwalk, "?");
}
struct ifmedia_description *desc;
struct ifmedia_description ifm_subtype_descriptions[] =
IFM_SUBTYPE_DESCRIPTIONS;
struct ifmedia_description *desc;
struct ifmedia_description ifm_subtype_descriptions[] =
IFM_SUBTYPE_DESCRIPTIONS;
for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
/*
for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
/*
* Skip these non-informative values and go right ahead to the
* actual speeds.
*/
if (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
BEGINS_WITH(desc->ifmt_string, "auto"))
continue;
if (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
BEGINS_WITH(desc->ifmt_string, "auto"))
continue;
if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmr.ifm_active))
break;
}
ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
return sprintf(outwalk, "%s", ethspeed);
if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmr.ifm_active))
break;
}
ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
return sprintf(outwalk, "%s", ethspeed);
#else
return sprintf(outwalk, "?");
return sprintf(outwalk, "?");
#endif
}
@ -111,36 +112,36 @@ static int print_eth_speed(char *outwalk, const char *interface) {
*
*/
void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
const char *walk;
const char *ip_address = get_ip_addr(interface);
char *outwalk = buffer;
const char *walk;
const char *ip_address = get_ip_addr(interface);
char *outwalk = buffer;
INSTANCE(interface);
INSTANCE(interface);
if (ip_address == NULL) {
START_COLOR("color_bad");
outwalk += sprintf(outwalk, "%s", format_down);
goto out;
if (ip_address == NULL) {
START_COLOR("color_bad");
outwalk += sprintf(outwalk, "%s", format_down);
goto out;
}
START_COLOR("color_good");
for (walk = format_up; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
START_COLOR("color_good");
for (walk = format_up; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "ip")) {
outwalk += sprintf(outwalk, "%s", ip_address);
walk += strlen("ip");
} else if (BEGINS_WITH(walk+1, "speed")) {
outwalk += print_eth_speed(outwalk, interface);
walk += strlen("speed");
}
if (BEGINS_WITH(walk + 1, "ip")) {
outwalk += sprintf(outwalk, "%s", ip_address);
walk += strlen("ip");
} else if (BEGINS_WITH(walk + 1, "speed")) {
outwalk += print_eth_speed(outwalk, interface);
walk += strlen("speed");
}
}
out:
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -18,50 +18,49 @@
*
*/
const char *get_ip_addr(const char *interface) {
static char part[512];
socklen_t len = sizeof(struct sockaddr_in);
memset(part, 0, sizeof(part));
static char part[512];
socklen_t len = sizeof(struct sockaddr_in);
memset(part, 0, sizeof(part));
struct ifaddrs *ifaddr, *addrp;
bool found = false;
struct ifaddrs *ifaddr, *addrp;
bool found = false;
getifaddrs(&ifaddr);
getifaddrs(&ifaddr);
if (ifaddr == NULL)
return NULL;
if (ifaddr == NULL)
return NULL;
/* Skip until we are at the AF_INET address of interface */
for (addrp = ifaddr;
/* Skip until we are at the AF_INET address of interface */
for (addrp = ifaddr;
(addrp != NULL &&
(strcmp(addrp->ifa_name, interface) != 0 ||
addrp->ifa_addr == NULL ||
addrp->ifa_addr->sa_family != AF_INET));
(addrp != NULL &&
(strcmp(addrp->ifa_name, interface) != 0 ||
addrp->ifa_addr == NULL ||
addrp->ifa_addr->sa_family != AF_INET));
addrp = addrp->ifa_next) {
/* Check if the interface is down */
if (strcmp(addrp->ifa_name, interface) != 0)
continue;
found = true;
if ((addrp->ifa_flags & IFF_RUNNING) == 0) {
freeifaddrs(ifaddr);
return NULL;
}
}
if (addrp == NULL) {
freeifaddrs(ifaddr);
return (found ? "no IP" : NULL);
}
int ret;
if ((ret = getnameinfo(addrp->ifa_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) {
fprintf(stderr, "i3status: getnameinfo(): %s\n", gai_strerror(ret));
freeifaddrs(ifaddr);
return "no IP";
addrp = addrp->ifa_next) {
/* Check if the interface is down */
if (strcmp(addrp->ifa_name, interface) != 0)
continue;
found = true;
if ((addrp->ifa_flags & IFF_RUNNING) == 0) {
freeifaddrs(ifaddr);
return NULL;
}
}
if (addrp == NULL) {
freeifaddrs(ifaddr);
return part;
}
return (found ? "no IP" : NULL);
}
int ret;
if ((ret = getnameinfo(addrp->ifa_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) {
fprintf(stderr, "i3status: getnameinfo(): %s\n", gai_strerror(ret));
freeifaddrs(ifaddr);
return "no IP";
}
freeifaddrs(ifaddr);
return part;
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -16,48 +16,48 @@
#include "i3status.h"
static char *get_sockname(struct addrinfo *addr) {
static char buf[INET6_ADDRSTRLEN+1];
struct sockaddr_storage local;
int ret;
int fd;
static char buf[INET6_ADDRSTRLEN + 1];
struct sockaddr_storage local;
int ret;
int fd;
if ((fd = socket(addr->ai_family, SOCK_DGRAM, 0)) == -1) {
perror("socket()");
return NULL;
}
/* Since the socket was created with SOCK_DGRAM, this is
* actually not establishing a connection or generating
* any other network traffic. Instead, as a side-effect,
* it saves the local address with which packets would
* be sent to the destination. */
if (connect(fd, addr->ai_addr, addr->ai_addrlen) == -1) {
/* We dont display the error here because most
* likely, there just is no IPv6 connectivity.
* Thus, dont spam the users console but just
* try the next address. */
(void)close(fd);
return NULL;
}
socklen_t local_len = sizeof(struct sockaddr_storage);
if (getsockname(fd, (struct sockaddr*)&local, &local_len) == -1) {
perror("getsockname()");
(void)close(fd);
return NULL;
}
memset(buf, 0, INET6_ADDRSTRLEN + 1);
if ((ret = getnameinfo((struct sockaddr*)&local, local_len,
buf, sizeof(buf), NULL, 0,
NI_NUMERICHOST)) != 0) {
fprintf(stderr, "i3status: getnameinfo(): %s\n", gai_strerror(ret));
(void)close(fd);
return NULL;
}
if ((fd = socket(addr->ai_family, SOCK_DGRAM, 0)) == -1) {
perror("socket()");
return NULL;
}
/* Since the socket was created with SOCK_DGRAM, this is
* actually not establishing a connection or generating
* any other network traffic. Instead, as a side-effect,
* it saves the local address with which packets would
* be sent to the destination. */
if (connect(fd, addr->ai_addr, addr->ai_addrlen) == -1) {
/* We dont display the error here because most
* likely, there just is no IPv6 connectivity.
* Thus, dont spam the users console but just
* try the next address. */
(void)close(fd);
return buf;
return NULL;
}
socklen_t local_len = sizeof(struct sockaddr_storage);
if (getsockname(fd, (struct sockaddr *)&local, &local_len) == -1) {
perror("getsockname()");
(void)close(fd);
return NULL;
}
memset(buf, 0, INET6_ADDRSTRLEN + 1);
if ((ret = getnameinfo((struct sockaddr *)&local, local_len,
buf, sizeof(buf), NULL, 0,
NI_NUMERICHOST)) != 0) {
fprintf(stderr, "i3status: getnameinfo(): %s\n", gai_strerror(ret));
(void)close(fd);
return NULL;
}
(void)close(fd);
return buf;
}
/*
@ -65,82 +65,82 @@ static char *get_sockname(struct addrinfo *addr) {
* The char * is statically allocated and mustn't be freed
*/
static char *get_ipv6_addr(void) {
struct addrinfo hints;
struct addrinfo *result, *resp;
static struct addrinfo *cached = NULL;
struct addrinfo hints;
struct addrinfo *result, *resp;
static struct addrinfo *cached = NULL;
/* To save dns lookups (if they are not cached locally) and creating
* sockets, we save the fd and keep it open. */
if (cached != NULL)
return get_sockname(cached);
/* To save dns lookups (if they are not cached locally) and creating
* sockets, we save the fd and keep it open. */
if (cached != NULL)
return get_sockname(cached);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
/* We use the public IPv6 of the K root server here. It doesnt matter
* which IPv6 address we use (we dont even send any packets), as long
* as its considered global by the kernel.
* NB: We dont use a hostname since that would trigger a DNS lookup.
* By using an IPv6 address, getaddrinfo() will *not* do a DNS lookup,
* but return the address in the appropriate struct. */
if (getaddrinfo("2001:7fd::1", "domain", &hints, &result) != 0) {
/* We dont display the error here because most
* likely, there just is no connectivity.
* Thus, dont spam the users console. */
return NULL;
}
for (resp = result; resp != NULL; resp = resp->ai_next) {
char *addr_string = get_sockname(resp);
/* If we could not get our own address and there is more than
* one result for resolving k.root-servers.net, we cannot
* cache. Otherwise, no matter if we got IPv6 connectivity or
* not, we will cache the (single) result and are done. */
if (!addr_string && result->ai_next != NULL)
continue;
if ((cached = malloc(sizeof(struct addrinfo))) == NULL)
return NULL;
memcpy(cached, resp, sizeof(struct addrinfo));
if ((cached->ai_addr = malloc(resp->ai_addrlen)) == NULL) {
cached = NULL;
return NULL;
}
memcpy(cached->ai_addr, resp->ai_addr, resp->ai_addrlen);
freeaddrinfo(result);
return addr_string;
}
freeaddrinfo(result);
/* We use the public IPv6 of the K root server here. It doesnt matter
* which IPv6 address we use (we dont even send any packets), as long
* as its considered global by the kernel.
* NB: We dont use a hostname since that would trigger a DNS lookup.
* By using an IPv6 address, getaddrinfo() will *not* do a DNS lookup,
* but return the address in the appropriate struct. */
if (getaddrinfo("2001:7fd::1", "domain", &hints, &result) != 0) {
/* We dont display the error here because most
* likely, there just is no connectivity.
* Thus, dont spam the users console. */
return NULL;
}
for (resp = result; resp != NULL; resp = resp->ai_next) {
char *addr_string = get_sockname(resp);
/* If we could not get our own address and there is more than
* one result for resolving k.root-servers.net, we cannot
* cache. Otherwise, no matter if we got IPv6 connectivity or
* not, we will cache the (single) result and are done. */
if (!addr_string && result->ai_next != NULL)
continue;
if ((cached = malloc(sizeof(struct addrinfo))) == NULL)
return NULL;
memcpy(cached, resp, sizeof(struct addrinfo));
if ((cached->ai_addr = malloc(resp->ai_addrlen)) == NULL) {
cached = NULL;
return NULL;
}
memcpy(cached->ai_addr, resp->ai_addr, resp->ai_addrlen);
freeaddrinfo(result);
return addr_string;
}
freeaddrinfo(result);
return NULL;
}
void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down) {
const char *walk;
char *addr_string = get_ipv6_addr();
char *outwalk = buffer;
const char *walk;
char *addr_string = get_ipv6_addr();
char *outwalk = buffer;
if (addr_string == NULL) {
START_COLOR("color_bad");
outwalk += sprintf(outwalk, "%s", format_down);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
return;
}
START_COLOR("color_good");
for (walk = format_up; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "ip")) {
outwalk += sprintf(outwalk, "%s", addr_string);
walk += strlen("ip");
}
}
if (addr_string == NULL) {
START_COLOR("color_bad");
outwalk += sprintf(outwalk, "%s", format_down);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
return;
}
START_COLOR("color_good");
for (walk = format_up; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk + 1, "ip")) {
outwalk += sprintf(outwalk, "%s", addr_string);
walk += strlen("ip");
}
}
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include "i3status.h"
#include <stdlib.h>
#include <stdio.h>
@ -7,51 +7,51 @@
#include <yajl/yajl_version.h>
void print_load(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold) {
char *outwalk = buffer;
/* Get load */
char *outwalk = buffer;
/* Get load */
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
double loadavg[3];
const char *walk;
bool colorful_output = false;
double loadavg[3];
const char *walk;
bool colorful_output = false;
if (getloadavg(loadavg, 3) == -1)
goto error;
if (getloadavg(loadavg, 3) == -1)
goto error;
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (loadavg[0] >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
if (BEGINS_WITH(walk+1, "1min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[0]);
walk += strlen("1min");
}
if (BEGINS_WITH(walk+1, "5min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[1]);
walk += strlen("5min");
}
if (BEGINS_WITH(walk+1, "15min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[2]);
walk += strlen("15min");
}
if (colorful_output)
END_COLOR;
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (loadavg[0] >= max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
if (BEGINS_WITH(walk + 1, "1min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[0]);
walk += strlen("1min");
}
return;
if (BEGINS_WITH(walk + 1, "5min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[1]);
walk += strlen("5min");
}
if (BEGINS_WITH(walk + 1, "15min")) {
outwalk += sprintf(outwalk, "%1.2f", loadavg[2]);
walk += strlen("15min");
}
if (colorful_output)
END_COLOR;
}
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
return;
error:
#endif
OUTPUT_FULL_TEXT("cant read load");
(void)fputs("i3status: Cannot read system load using getloadavg()\n", stderr);
OUTPUT_FULL_TEXT("cant read load");
(void)fputs("i3status: Cannot read system load using getloadavg()\n", stderr);
}

View File

@ -1,3 +1,4 @@
// vim:ts=4:sw=4:expandtab
#include <stdio.h>
#include <string.h>
#include <yajl/yajl_gen.h>
@ -6,30 +7,30 @@
#include "i3status.h"
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format) {
const char *walk;
char *outwalk = buffer;
struct stat st;
const bool exists = (stat(path, &st) == 0);
const char *walk;
char *outwalk = buffer;
struct stat st;
const bool exists = (stat(path, &st) == 0);
INSTANCE(path);
INSTANCE(path);
START_COLOR((exists ? "color_good" : "color_bad"));
START_COLOR((exists ? "color_good" : "color_bad"));
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "title")) {
outwalk += sprintf(outwalk, "%s", title);
walk += strlen("title");
} else if (BEGINS_WITH(walk+1, "status")) {
outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
walk += strlen("status");
}
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
if (BEGINS_WITH(walk + 1, "title")) {
outwalk += sprintf(outwalk, "%s", title);
walk += strlen("title");
} else if (BEGINS_WITH(walk + 1, "status")) {
outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
walk += strlen("status");
}
}
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
}

View File

@ -1,3 +1,4 @@
// vim:ts=4:sw=4:expandtab
#include <stdio.h>
#include <string.h>
#include <yajl/yajl_gen.h>
@ -5,29 +6,29 @@
#include "i3status.h"
void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format) {
bool running = process_runs(pidfile);
const char *walk;
char *outwalk = buffer;
bool running = process_runs(pidfile);
const char *walk;
char *outwalk = buffer;
INSTANCE(pidfile);
INSTANCE(pidfile);
START_COLOR((running ? "color_good" : "color_bad"));
START_COLOR((running ? "color_good" : "color_bad"));
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "title")) {
outwalk += sprintf(outwalk, "%s", title);
walk += strlen("title");
} else if (BEGINS_WITH(walk+1, "status")) {
outwalk += sprintf(outwalk, "%s", (running ? "yes" : "no"));
walk += strlen("status");
}
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
if (BEGINS_WITH(walk + 1, "title")) {
outwalk += sprintf(outwalk, "%s", title);
walk += strlen("title");
} else if (BEGINS_WITH(walk + 1, "status")) {
outwalk += sprintf(outwalk, "%s", (running ? "yes" : "no"));
walk += strlen("status");
}
}
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
@ -13,34 +13,34 @@ static const char *local_timezone = NULL;
static const char *current_timezone = NULL;
void set_timezone(const char *tz) {
if (!local_timezone_init) {
/* First call, initialize. */
local_timezone = getenv("TZ");
local_timezone_init = true;
}
if (tz == NULL || tz[0] == '\0') {
/* User wants localtime. */
tz = local_timezone;
}
if (tz != current_timezone) {
if (tz) {
setenv("TZ", tz, 1);
} else {
unsetenv("TZ");
}
tzset();
current_timezone = tz;
if (!local_timezone_init) {
/* First call, initialize. */
local_timezone = getenv("TZ");
local_timezone_init = true;
}
if (tz == NULL || tz[0] == '\0') {
/* User wants localtime. */
tz = local_timezone;
}
if (tz != current_timezone) {
if (tz) {
setenv("TZ", tz, 1);
} else {
unsetenv("TZ");
}
tzset();
current_timezone = tz;
}
}
void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t) {
char *outwalk = buffer;
struct tm tm;
char *outwalk = buffer;
struct tm tm;
/* Convert time and format output. */
set_timezone(tz);
localtime_r(&t, &tm);
outwalk += strftime(outwalk, 4095, format, &tm);
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
/* Convert time and format output. */
set_timezone(tz);
localtime_r(&t, &tm);
outwalk += strftime(outwalk, 4095, format, &tm);
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <time.h>
#include <string.h>
#include <stdlib.h>
@ -27,154 +27,155 @@
#include "queue.h"
void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx) {
char *outwalk = buffer;
int pbval = 1;
char *outwalk = buffer;
int pbval = 1;
/* Printing volume only works with ALSA at the moment */
if (output_format == O_I3BAR) {
char *instance;
asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
INSTANCE(instance);
free(instance);
}
/* Printing volume only works with ALSA at the moment */
if (output_format == O_I3BAR) {
char *instance;
asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
INSTANCE(instance);
free(instance);
}
#ifdef LINUX
int err;
snd_mixer_t *m;
snd_mixer_selem_id_t *sid;
snd_mixer_elem_t *elem;
long min, max, val;
int avg;
int err;
snd_mixer_t *m;
snd_mixer_selem_id_t *sid;
snd_mixer_elem_t *elem;
long min, max, val;
int avg;
if ((err = snd_mixer_open(&m, 0)) < 0) {
fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
goto out;
}
if ((err = snd_mixer_open(&m, 0)) < 0) {
fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
goto out;
}
/* Attach this mixer handle to the given device */
if ((err = snd_mixer_attach(m, device)) < 0) {
fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
snd_mixer_close(m);
goto out;
}
/* Attach this mixer handle to the given device */
if ((err = snd_mixer_attach(m, device)) < 0) {
fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
snd_mixer_close(m);
goto out;
}
/* Register this mixer */
if ((err = snd_mixer_selem_register(m, NULL, NULL)) < 0) {
fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
snd_mixer_close(m);
goto out;
}
/* Register this mixer */
if ((err = snd_mixer_selem_register(m, NULL, NULL)) < 0) {
fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
snd_mixer_close(m);
goto out;
}
if ((err = snd_mixer_load(m)) < 0) {
fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
snd_mixer_close(m);
goto out;
}
if ((err = snd_mixer_load(m)) < 0) {
fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
snd_mixer_close(m);
goto out;
}
snd_mixer_selem_id_malloc(&sid);
if (sid == NULL) {
snd_mixer_close(m);
goto out;
}
snd_mixer_selem_id_malloc(&sid);
if (sid == NULL) {
snd_mixer_close(m);
goto out;
}
/* Find the given mixer */
snd_mixer_selem_id_set_index(sid, mixer_idx);
snd_mixer_selem_id_set_name(sid, mixer);
if (!(elem = snd_mixer_find_selem(m, sid))) {
fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
snd_mixer_close(m);
snd_mixer_selem_id_free(sid);
goto out;
}
/* Find the given mixer */
snd_mixer_selem_id_set_index(sid, mixer_idx);
snd_mixer_selem_id_set_name(sid, mixer);
if (!(elem = snd_mixer_find_selem(m, sid))) {
fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
snd_mixer_close(m);
snd_mixer_selem_id_free(sid);
goto out;
}
/* Get the volume range to convert the volume later */
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
/* Get the volume range to convert the volume later */
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
snd_mixer_handle_events (m);
snd_mixer_selem_get_playback_volume (elem, 0, &val);
if (max != 100) {
float avgf = ((float)val / max) * 100;
avg = (int)avgf;
avg = (avgf - avg < 0.5 ? avg : (avg+1));
} else avg = (int)val;
snd_mixer_handle_events(m);
snd_mixer_selem_get_playback_volume(elem, 0, &val);
if (max != 100) {
float avgf = ((float)val / max) * 100;
avg = (int)avgf;
avg = (avgf - avg < 0.5 ? avg : (avg + 1));
} else
avg = (int)val;
/* Check for mute */
if (snd_mixer_selem_has_playback_switch(elem)) {
if ((err = snd_mixer_selem_get_playback_switch(elem, 0, &pbval)) < 0)
fprintf (stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
if (!pbval) {
START_COLOR("color_degraded");
fmt = fmt_muted;
}
}
/* Check for mute */
if (snd_mixer_selem_has_playback_switch(elem)) {
if ((err = snd_mixer_selem_get_playback_switch(elem, 0, &pbval)) < 0)
fprintf(stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
if (!pbval) {
START_COLOR("color_degraded");
fmt = fmt_muted;
}
}
snd_mixer_close(m);
snd_mixer_selem_id_free(sid);
snd_mixer_close(m);
snd_mixer_selem_id_free(sid);
const char *walk = fmt;
for (; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "%")) {
outwalk += sprintf(outwalk, "%%");
walk += strlen("%");
}
if (BEGINS_WITH(walk+1, "volume")) {
outwalk += sprintf(outwalk, "%d%%", avg);
walk += strlen("volume");
}
}
const char *walk = fmt;
for (; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk + 1, "%")) {
outwalk += sprintf(outwalk, "%%");
walk += strlen("%");
}
if (BEGINS_WITH(walk + 1, "volume")) {
outwalk += sprintf(outwalk, "%d%%", avg);
walk += strlen("volume");
}
}
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
char *mixerpath;
char defaultmixer[] = "/dev/mixer";
int mixfd, vol, devmask = 0;
pbval = 1;
char *mixerpath;
char defaultmixer[] = "/dev/mixer";
int mixfd, vol, devmask = 0;
pbval = 1;
if (mixer_idx > 0)
asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
else
mixerpath = defaultmixer;
if (mixer_idx > 0)
asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
else
mixerpath = defaultmixer;
if ((mixfd = open(mixerpath, O_RDWR)) < 0)
return;
if ((mixfd = open(mixerpath, O_RDWR)) < 0)
return;
if (mixer_idx > 0)
free(mixerpath);
if (mixer_idx > 0)
free(mixerpath);
if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
return;
if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
return;
if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
return;
if (ioctl(mixfd, MIXER_READ(0), &vol) == -1)
return;
if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
START_COLOR("color_degraded");
pbval = 0;
if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
START_COLOR("color_degraded");
pbval = 0;
}
const char *walk = fmt;
for (; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
const char *walk = fmt;
for (; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "%")) {
outwalk += sprintf(outwalk, "%%");
walk += strlen("%");
}
if (BEGINS_WITH(walk+1, "volume")) {
outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
walk += strlen("volume");
}
if (BEGINS_WITH(walk + 1, "%")) {
outwalk += sprintf(outwalk, "%%");
walk += strlen("%");
}
close(mixfd);
if (BEGINS_WITH(walk + 1, "volume")) {
outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
walk += strlen("volume");
}
}
close(mixfd);
#endif
out:
*outwalk = '\0';
if (!pbval)
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
*outwalk = '\0';
if (!pbval)
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <stdio.h>
#include <string.h>
#include <yajl/yajl_gen.h>
@ -51,383 +51,380 @@
#include "i3status.h"
#define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0)
#define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1)
#define WIRELESS_INFO_FLAG_HAS_SIGNAL (1 << 2)
#define WIRELESS_INFO_FLAG_HAS_NOISE (1 << 3)
#define WIRELESS_INFO_FLAG_HAS_FREQUENCY (1 << 4)
#define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0)
#define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1)
#define WIRELESS_INFO_FLAG_HAS_SIGNAL (1 << 2)
#define WIRELESS_INFO_FLAG_HAS_NOISE (1 << 3)
#define WIRELESS_INFO_FLAG_HAS_FREQUENCY (1 << 4)
#define PERCENT_VALUE(value, total) ((int)(value * 100 / (float)total + 0.5f))
typedef struct {
int flags;
char essid[IW_ESSID_MAX_SIZE + 1];
int quality;
int quality_max;
int quality_average;
int signal_level;
int signal_level_max;
int noise_level;
int noise_level_max;
int bitrate;
double frequency;
int flags;
char essid[IW_ESSID_MAX_SIZE + 1];
int quality;
int quality_max;
int quality_average;
int signal_level;
int signal_level_max;
int noise_level;
int noise_level_max;
int bitrate;
double frequency;
} wireless_info_t;
static int get_wireless_info(const char *interface, wireless_info_t *info) {
memset(info, 0, sizeof(wireless_info_t));
memset(info, 0, sizeof(wireless_info_t));
#ifdef LINUX
int skfd = iw_sockets_open();
if (skfd < 0) {
perror("iw_sockets_open");
return 0;
}
int skfd = iw_sockets_open();
if (skfd < 0) {
perror("iw_sockets_open");
return 0;
}
wireless_config wcfg;
if (iw_get_basic_config(skfd, interface, &wcfg) < 0) {
close(skfd);
return 0;
}
wireless_config wcfg;
if (iw_get_basic_config(skfd, interface, &wcfg) < 0) {
close(skfd);
return 0;
}
if (wcfg.has_essid && wcfg.essid_on) {
info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
strncpy(&info->essid[0], wcfg.essid, IW_ESSID_MAX_SIZE);
info->essid[IW_ESSID_MAX_SIZE] = '\0';
}
if (wcfg.has_essid && wcfg.essid_on) {
info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
strncpy(&info->essid[0], wcfg.essid, IW_ESSID_MAX_SIZE);
info->essid[IW_ESSID_MAX_SIZE] = '\0';
}
if (wcfg.has_freq) {
info->frequency = wcfg.freq;
info->flags |= WIRELESS_INFO_FLAG_HAS_FREQUENCY;
}
/* If the function iw_get_stats does not return proper stats, the
wifi is considered as down.
Since ad-hoc network does not have theses stats, we need to return
here for this mode. */
if (wcfg.mode == 1) {
close(skfd);
return 1;
}
/* Wireless quality is a relative value in a driver-specific range.
Signal and noise level can be either relative or absolute values
in dBm. Furthermore, noise and quality can be expressed directly
in dBm or in RCPI (802.11k), which we convert to dBm. When those
values are expressed directly in dBm, they range from -192 to 63,
and since the values are packed into 8 bits, we need to perform
8-bit arithmetic on them. Assume absolute values if everything
else fails (driver bug). */
iwrange range;
if (iw_get_range_info(skfd, interface, &range) < 0) {
close(skfd);
return 0;
}
iwstats stats;
if (iw_get_stats(skfd, interface, &stats, &range, 1) < 0) {
close(skfd);
return 0;
}
if (stats.qual.level != 0 || (stats.qual.updated & (IW_QUAL_DBM | IW_QUAL_RCPI))) {
if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
info->quality = stats.qual.qual;
info->quality_max = range.max_qual.qual;
info->quality_average = range.avg_qual.qual;
info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
}
if (stats.qual.updated & IW_QUAL_RCPI) {
if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
info->signal_level = stats.qual.level / 2.0 - 110 + 0.5;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
info->noise_level = stats.qual.noise / 2.0 - 110 + 0.5;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
}
}
else {
if ((stats.qual.updated & IW_QUAL_DBM) || stats.qual.level > range.max_qual.level) {
if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
info->signal_level = stats.qual.level;
if (info->signal_level > 63)
info->signal_level -= 256;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
info->noise_level = stats.qual.noise;
if (info->noise_level > 63)
info->noise_level -= 256;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
}
}
else {
if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
info->signal_level = stats.qual.level;
info->signal_level_max = range.max_qual.level;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
info->noise_level = stats.qual.noise;
info->noise_level_max = range.max_qual.noise;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
}
}
}
}
else {
if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
info->quality = stats.qual.qual;
info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
}
if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
info->quality = stats.qual.level;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
info->quality = stats.qual.noise;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
}
}
struct iwreq wrq;
if (iw_get_ext(skfd, interface, SIOCGIWRATE, &wrq) >= 0)
info->bitrate = wrq.u.bitrate.value;
if (wcfg.has_freq) {
info->frequency = wcfg.freq;
info->flags |= WIRELESS_INFO_FLAG_HAS_FREQUENCY;
}
/* If the function iw_get_stats does not return proper stats, the
* wifi is considered as down.
* Since ad-hoc network does not have theses stats, we need to return
* here for this mode. */
if (wcfg.mode == 1) {
close(skfd);
return 1;
}
/* Wireless quality is a relative value in a driver-specific range.
* Signal and noise level can be either relative or absolute values
* in dBm. Furthermore, noise and quality can be expressed directly
* in dBm or in RCPI (802.11k), which we convert to dBm. When those
* values are expressed directly in dBm, they range from -192 to 63,
* and since the values are packed into 8 bits, we need to perform
* 8-bit arithmetic on them. Assume absolute values if everything
* else fails (driver bug). */
iwrange range;
if (iw_get_range_info(skfd, interface, &range) < 0) {
close(skfd);
return 0;
}
iwstats stats;
if (iw_get_stats(skfd, interface, &stats, &range, 1) < 0) {
close(skfd);
return 0;
}
if (stats.qual.level != 0 || (stats.qual.updated & (IW_QUAL_DBM | IW_QUAL_RCPI))) {
if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
info->quality = stats.qual.qual;
info->quality_max = range.max_qual.qual;
info->quality_average = range.avg_qual.qual;
info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
}
if (stats.qual.updated & IW_QUAL_RCPI) {
if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
info->signal_level = stats.qual.level / 2.0 - 110 + 0.5;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
info->noise_level = stats.qual.noise / 2.0 - 110 + 0.5;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
}
} else {
if ((stats.qual.updated & IW_QUAL_DBM) || stats.qual.level > range.max_qual.level) {
if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
info->signal_level = stats.qual.level;
if (info->signal_level > 63)
info->signal_level -= 256;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
info->noise_level = stats.qual.noise;
if (info->noise_level > 63)
info->noise_level -= 256;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
}
} else {
if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
info->signal_level = stats.qual.level;
info->signal_level_max = range.max_qual.level;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
info->noise_level = stats.qual.noise;
info->noise_level_max = range.max_qual.noise;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
}
}
}
} else {
if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
info->quality = stats.qual.qual;
info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
}
if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) {
info->quality = stats.qual.level;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) {
info->quality = stats.qual.noise;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
}
}
struct iwreq wrq;
if (iw_get_ext(skfd, interface, SIOCGIWRATE, &wrq) >= 0)
info->bitrate = wrq.u.bitrate.value;
close(skfd);
return 1;
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
int s, len, inwid;
uint8_t buf[24 * 1024], *cp;
struct ieee80211req na;
char network_id[IEEE80211_NWID_LEN + 1];
int s, len, inwid;
uint8_t buf[24 * 1024], *cp;
struct ieee80211req na;
char network_id[IEEE80211_NWID_LEN + 1];
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return (0);
memset(&na, 0, sizeof(na));
strlcpy(na.i_name, interface, sizeof(na.i_name));
na.i_type = IEEE80211_IOC_SSID;
na.i_data = &info->essid[0];
na.i_len = IEEE80211_NWID_LEN + 1;
if ((inwid = ioctl(s, SIOCG80211, (caddr_t)&na)) == -1) {
close(s);
return (0);
}
if (inwid == 0) {
if (na.i_len <= IEEE80211_NWID_LEN)
len = na.i_len + 1;
else
len = IEEE80211_NWID_LEN + 1;
info->essid[len -1] = '\0';
} else {
close(s);
return (0);
}
info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
memset(&na, 0, sizeof(na));
strlcpy(na.i_name, interface, sizeof(na.i_name));
na.i_type = IEEE80211_IOC_SCAN_RESULTS;
na.i_data = buf;
na.i_len = sizeof(buf);
if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
printf("fail\n");
close(s);
return (0);
}
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return (0);
memset(&na, 0, sizeof(na));
strlcpy(na.i_name, interface, sizeof(na.i_name));
na.i_type = IEEE80211_IOC_SSID;
na.i_data = &info->essid[0];
na.i_len = IEEE80211_NWID_LEN + 1;
if ((inwid = ioctl(s, SIOCG80211, (caddr_t)&na)) == -1) {
close(s);
len = na.i_len;
cp = buf;
struct ieee80211req_scan_result *sr;
uint8_t *vp;
sr = (struct ieee80211req_scan_result *)cp;
vp = (u_int8_t *)(sr + 1);
strlcpy(network_id, (const char *)vp, sr->isr_ssid_len + 1);
if (!strcmp(network_id, &info->essid[0])) {
info->signal_level = sr->isr_rssi;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
info->noise_level = sr->isr_noise;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
info->quality = sr->isr_intval;
info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
}
return (0);
}
if (inwid == 0) {
if (na.i_len <= IEEE80211_NWID_LEN)
len = na.i_len + 1;
else
len = IEEE80211_NWID_LEN + 1;
info->essid[len - 1] = '\0';
} else {
close(s);
return (0);
}
info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
return 1;
memset(&na, 0, sizeof(na));
strlcpy(na.i_name, interface, sizeof(na.i_name));
na.i_type = IEEE80211_IOC_SCAN_RESULTS;
na.i_data = buf;
na.i_len = sizeof(buf);
if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
printf("fail\n");
close(s);
return (0);
}
close(s);
len = na.i_len;
cp = buf;
struct ieee80211req_scan_result *sr;
uint8_t *vp;
sr = (struct ieee80211req_scan_result *)cp;
vp = (u_int8_t *)(sr + 1);
strlcpy(network_id, (const char *)vp, sr->isr_ssid_len + 1);
if (!strcmp(network_id, &info->essid[0])) {
info->signal_level = sr->isr_rssi;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
info->noise_level = sr->isr_noise;
info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
info->quality = sr->isr_intval;
info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
}
return 1;
#endif
#ifdef __OpenBSD__
struct ifreq ifr;
struct ieee80211_bssid bssid;
struct ieee80211_nwid nwid;
struct ieee80211_nodereq nr;
struct ifreq ifr;
struct ieee80211_bssid bssid;
struct ieee80211_nwid nwid;
struct ieee80211_nodereq nr;
struct ether_addr ea;
struct ether_addr ea;
int s, len, ibssid, inwid;
u_int8_t zero_bssid[IEEE80211_ADDR_LEN];
int s, len, ibssid, inwid;
u_int8_t zero_bssid[IEEE80211_ADDR_LEN];
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return (0);
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return (0);
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_data = (caddr_t)&nwid;
(void)strlcpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
inwid = ioctl(s, SIOCG80211NWID, (caddr_t)&ifr);
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_data = (caddr_t)&nwid;
(void)strlcpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
inwid = ioctl(s, SIOCG80211NWID, (caddr_t)&ifr);
memset(&bssid, 0, sizeof(bssid));
strlcpy(bssid.i_name, interface, sizeof(bssid.i_name));
ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
memset(&bssid, 0, sizeof(bssid));
strlcpy(bssid.i_name, interface, sizeof(bssid.i_name));
ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
if (ibssid != 0 || inwid != 0) {
close(s);
return 0;
}
if (ibssid != 0 || inwid != 0) {
close(s);
return 0;
}
/* NWID */
{
if (nwid.i_len <= IEEE80211_NWID_LEN)
len = nwid.i_len + 1;
else
len = IEEE80211_NWID_LEN + 1;
/* NWID */
{
if (nwid.i_len <= IEEE80211_NWID_LEN)
len = nwid.i_len + 1;
else
len = IEEE80211_NWID_LEN + 1;
strncpy(&info->essid[0], nwid.i_nwid, len);
info->essid[IW_ESSID_MAX_SIZE] = '\0';
info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
}
strncpy(&info->essid[0], nwid.i_nwid, len);
info->essid[IW_ESSID_MAX_SIZE] = '\0';
info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
}
/* Signal strength */
{
memset(&zero_bssid, 0, sizeof(zero_bssid));
if (ibssid == 0 && memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) {
memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet));
/* Signal strength */
{
memset(&zero_bssid, 0, sizeof(zero_bssid));
if (ibssid == 0 && memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) {
memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet));
bzero(&nr, sizeof(nr));
bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
strlcpy(nr.nr_ifname, interface, sizeof(nr.nr_ifname));
bzero(&nr, sizeof(nr));
bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
strlcpy(nr.nr_ifname, interface, sizeof(nr.nr_ifname));
if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
if (nr.nr_max_rssi)
info->signal_level_max = IEEE80211_NODEREQ_RSSI(&nr);
else
info->signal_level = nr.nr_rssi;
if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
if (nr.nr_max_rssi)
info->signal_level_max = IEEE80211_NODEREQ_RSSI(&nr);
else
info->signal_level = nr.nr_rssi;
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
}
}
info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
}
}
}
close(s);
return 1;
close(s);
return 1;
#endif
return 0;
return 0;
}
void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
const char *walk;
char *outwalk = buffer;
wireless_info_t info;
const char *walk;
char *outwalk = buffer;
wireless_info_t info;
INSTANCE(interface);
INSTANCE(interface);
const char *ip_address = get_ip_addr(interface);
if (ip_address == NULL) {
START_COLOR("color_bad");
outwalk += sprintf(outwalk, "%s", format_down);
goto out;
}
const char *ip_address = get_ip_addr(interface);
if (ip_address == NULL) {
START_COLOR("color_bad");
outwalk += sprintf(outwalk, "%s", format_down);
goto out;
}
if (get_wireless_info(interface, &info)) {
walk = format_up;
if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY)
START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good"));
else
START_COLOR("color_good");
} else {
walk = format_down;
START_COLOR("color_bad");
if (get_wireless_info(interface, &info)) {
walk = format_up;
if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY)
START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good"));
else
START_COLOR("color_good");
} else {
walk = format_down;
START_COLOR("color_bad");
}
for (; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
for (; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk + 1, "quality")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) {
if (info.quality_max)
outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.quality, info.quality_max));
else
outwalk += sprintf(outwalk, "%d", info.quality);
} else {
*(outwalk++) = '?';
}
walk += strlen("quality");
}
if (BEGINS_WITH(walk+1, "quality")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) {
if (info.quality_max)
outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.quality, info.quality_max));
else
outwalk += sprintf(outwalk, "%d", info.quality);
} else {
*(outwalk++) = '?';
}
walk += strlen("quality");
}
if (BEGINS_WITH(walk + 1, "signal")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
if (info.signal_level_max)
outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max));
else
outwalk += sprintf(outwalk, "%d dBm", info.signal_level);
} else {
*(outwalk++) = '?';
}
walk += strlen("signal");
}
if (BEGINS_WITH(walk+1, "signal")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
if (info.signal_level_max)
outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max));
else
outwalk += sprintf(outwalk, "%d dBm", info.signal_level);
} else {
*(outwalk++) = '?';
}
walk += strlen("signal");
}
if (BEGINS_WITH(walk + 1, "noise")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
if (info.noise_level_max)
outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max));
else
outwalk += sprintf(outwalk, "%d dBm", info.noise_level);
} else {
*(outwalk++) = '?';
}
walk += strlen("noise");
}
if (BEGINS_WITH(walk+1, "noise")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
if (info.noise_level_max)
outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max));
else
outwalk += sprintf(outwalk, "%d dBm", info.noise_level);
} else {
*(outwalk++) = '?';
}
walk += strlen("noise");
}
if (BEGINS_WITH(walk + 1, "essid")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
outwalk += sprintf(outwalk, "%s", info.essid);
else
*(outwalk++) = '?';
walk += strlen("essid");
}
if (BEGINS_WITH(walk+1, "essid")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
outwalk += sprintf(outwalk, "%s", info.essid);
else
*(outwalk++) = '?';
walk += strlen("essid");
}
if (BEGINS_WITH(walk + 1, "frequency")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9);
else
*(outwalk++) = '?';
walk += strlen("frequency");
}
if (BEGINS_WITH(walk+1, "frequency")) {
if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9);
else
*(outwalk++) = '?';
walk += strlen("frequency");
}
if (BEGINS_WITH(walk+1, "ip")) {
outwalk += sprintf(outwalk, "%s", ip_address);
walk += strlen("ip");
}
if (BEGINS_WITH(walk + 1, "ip")) {
outwalk += sprintf(outwalk, "%s", ip_address);
walk += strlen("ip");
}
#ifdef LINUX
if (BEGINS_WITH(walk+1, "bitrate")) {
char br_buffer[128];
if (BEGINS_WITH(walk + 1, "bitrate")) {
char br_buffer[128];
iw_print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);
iw_print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);
outwalk += sprintf(outwalk, "%s", br_buffer);
walk += strlen("bitrate");
}
#endif
outwalk += sprintf(outwalk, "%s", br_buffer);
walk += strlen("bitrate");
}
#endif
}
out:
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
}

View File

@ -1,4 +1,4 @@
// vim:ts=8:sw=8:expandtab
// vim:ts=4:sw=4:expandtab
#include <stdbool.h>
#include <glob.h>
#include <string.h>
@ -20,30 +20,30 @@
*
*/
bool process_runs(const char *path) {
static char pidbuf[16];
static glob_t globbuf;
memset(pidbuf, 0, sizeof(pidbuf));
static char pidbuf[16];
static glob_t globbuf;
memset(pidbuf, 0, sizeof(pidbuf));
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
die("glob() failed\n");
if (globbuf.gl_pathc == 0) {
/* No glob matches, the specified path does not contain a wildcard. */
globfree(&globbuf);
if (!slurp(path, pidbuf, sizeof(pidbuf)))
return false;
return (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM);
}
for (size_t i = 0; i < globbuf.gl_pathc; i++) {
if (!slurp(globbuf.gl_pathv[i], pidbuf, sizeof(pidbuf))) {
globfree(&globbuf);
return false;
}
if (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM) {
globfree(&globbuf);
return true;
}
}
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
die("glob() failed\n");
if (globbuf.gl_pathc == 0) {
/* No glob matches, the specified path does not contain a wildcard. */
globfree(&globbuf);
if (!slurp(path, pidbuf, sizeof(pidbuf)))
return false;
return (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM);
}
for (size_t i = 0; i < globbuf.gl_pathc; i++) {
if (!slurp(globbuf.gl_pathv[i], pidbuf, sizeof(pidbuf))) {
globfree(&globbuf);
return false;
}
if (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM) {
globfree(&globbuf);
return true;
}
}
globfree(&globbuf);
return false;
return false;
}