7024361e780c97d4a0b9dc6df4319cccea765293
[oweals/busybox.git] / miscutils / runlevel.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Prints out the previous and the current runlevel.
4  *
5  * Version: @(#)runlevel  1.20  16-Apr-1997  MvS
6  *
7  * This file is part of the sysvinit suite,
8  * Copyright 1991-1997 Miquel van Smoorenburg.
9  *
10  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11  *
12  * initially busyboxified by Bernhard Reutner-Fischer
13  */
14 #include "libbb.h"
15
16 int runlevel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
17 int runlevel_main(int argc UNUSED_PARAM, char **argv)
18 {
19         struct utmp *ut;
20         char prev;
21
22         if (argv[1]) utmpname(argv[1]);
23
24         setutent();
25         while ((ut = getutent()) != NULL) {
26                 if (ut->ut_type == RUN_LVL) {
27                         prev = ut->ut_pid / 256;
28                         if (prev == 0) prev = 'N';
29                         printf("%c %c\n", prev, ut->ut_pid % 256);
30                         if (ENABLE_FEATURE_CLEAN_UP)
31                                 endutent();
32                         return 0;
33                 }
34         }
35
36         puts("unknown");
37
38         if (ENABLE_FEATURE_CLEAN_UP)
39                 endutent();
40         return 1;
41 }