halt.c (905B)
1 /* see LICENSE file for copyright and license details */ 2 3 #ifndef _XOPEN_SOURCE 4 #define _XOPEN_SOURCE 500 /* sync */ 5 #endif 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 11 #include "reboot.h" 12 #include "util.h" 13 14 static void 15 usage(void) 16 { 17 eprintf("usage: %s [-pr]\n", argv0); 18 } 19 20 // ?man halt: halt or reboot 21 // ?man halt, poweroff, or reboot the machine 22 int 23 main(int argc, char *argv[]) 24 { 25 int pflag = 0, rflag = 0; 26 int how = REBOOT_HALT; 27 28 ARGBEGIN 29 { 30 // ?man -p: preserve file attributes 31 case 'p': 32 pflag = 1; 33 break; 34 // ?man -r: operate recursively 35 case 'r': 36 rflag = 1; 37 break; 38 default: 39 usage(); 40 } 41 ARGEND; 42 43 if (argc > 0) 44 usage(); 45 46 sync(); 47 48 if (pflag && rflag) 49 usage(); 50 51 if (pflag) 52 how = REBOOT_POWEROFF; 53 if (rflag) 54 how = REBOOT_RESTART; 55 56 if (machine_reboot(how) < 0) 57 eprintf("reboot:"); 58 return 0; 59 }