ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

halt.c (748B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/syscall.h>
      3 
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <unistd.h>
      7 
      8 #include "reboot.h"
      9 #include "util.h"
     10 
     11 static void
     12 usage(void)
     13 {
     14 	eprintf("usage: %s [-pr]\n", argv0);
     15 }
     16 
     17 int
     18 main(int argc, char *argv[])
     19 {
     20 	int pflag = 0, rflag = 0;
     21 	int cmd = LINUX_REBOOT_CMD_HALT;
     22 
     23 	ARGBEGIN {
     24 	case 'p':
     25 		pflag = 1;
     26 		break;
     27 	case 'r':
     28 		rflag = 1;
     29 		break;
     30 	default:
     31 		usage();
     32 	} ARGEND;
     33 
     34 	if (argc > 0)
     35 		usage();
     36 
     37 	sync();
     38 
     39 	if (pflag && rflag)
     40 		usage();
     41 
     42 	if (pflag)
     43 		cmd = LINUX_REBOOT_CMD_POWER_OFF;
     44 	if (rflag)
     45 		cmd = LINUX_REBOOT_CMD_RESTART;
     46 
     47 	if (syscall(__NR_reboot, LINUX_REBOOT_MAGIC1,
     48 		    LINUX_REBOOT_MAGIC2, cmd, NULL) < 0)
     49 		eprintf("reboot:");
     50 	return 0;
     51 }