summaryrefslogtreecommitdiff
path: root/src/skaembutils/s6-nice.c
blob: 38017f6d18f9e1b072b129f0532e06892bb327a0 (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
/* ISC license. */

#include <unistd.h>
#include <errno.h>

#include <skalibs/sgetopt.h>
#include <skalibs/types.h>
#include <skalibs/strerr.h>
#include <skalibs/exec.h>

#define USAGE "s6-nice [ -I | -i ] [ -n value ] prog..."

int main (int argc, char const *const *argv)
{
  int incr = 10 ;
  int strict = 0 ;
  PROG = "s6-nice" ;
  {
    subgetopt l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "Iin:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'I' : strict = 0 ; break ;
        case 'i' : strict = 1 ; break ;
        case 'n': if (!int_scan(l.arg, &incr)) strerr_dieusage(100, USAGE) ; break ;
        default : strerr_dieusage(100, USAGE) ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) strerr_dieusage(100, USAGE) ;

  errno = 0 ;
  if ((nice(incr) < 0) && errno)
  {
    char fmt[1+UINT_FMT] ;
    fmt[int_fmt(fmt, incr)] = 0 ;
    if (strict) strerr_diefu2sys(111, "nice to ", fmt) ;
    else strerr_warnwu2sys("nice to ", fmt) ;
  }
  xexec(argv) ;
}