summaryrefslogtreecommitdiff
path: root/lib/util.c
blob: 0c22adfe6e20d447aaf239265ba9469d15a76016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libnotify/notify.h>

#include "util.h"

void
die(const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);

  if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
    fputc(' ', stderr);
    perror(NULL);
  } else {
    fputc('\n', stderr);
  }

  exit(1);
}

void
sendnotif(const char *appname, const char *title, const char *body)
{
  notify_init(appname);

  NotifyNotification *n;
  n = notify_notification_new(title, body, NULL);

  notify_notification_show(n, NULL);

  g_object_unref(G_OBJECT(n));
  notify_uninit();
}

int
getbtnint(const char *blkbtn)
{
  char *endptr;
  int errno;
  long val;

  errno = 0;

  if (blkbtn == NULL)
    return 0;

  val = strtol(blkbtn, &endptr, 10);

  if (errno == 0 && *endptr == '\0' && val >= 1 && val <= 8)
    return (int)val;

  return 0;
}

void
spawn(const char *const argv[])
{
	if (fork() == 0) {
		dup2(STDERR_FILENO, STDOUT_FILENO);
		setsid();
		execvp(argv[0], (char * const *)argv);
		die("spawn %s failed:", (argv)[0]);
	}
}