hostname.c (901B)
1 /* see LICENSE file for copyright and license details */ 2 3 #if FEATURE_HOSTNAME_SET 4 #ifndef _DEFAULT_SOURCE 5 #define _DEFAULT_SOURCE 6 #endif 7 #endif 8 9 #include <stdio.h> 10 #include <string.h> 11 #include <unistd.h> 12 13 #include "util.h" 14 15 static void 16 usage(void) 17 { 18 eprintf("usage: %s [name]\n", argv0); 19 } 20 21 // ?man hostname: show or set hostname 22 // ?man arguments: name 23 // ?man display or configure the system hostname 24 int 25 main(int argc, char *argv[]) 26 { 27 char host[HOST_NAME_MAX + 1]; 28 29 ARGBEGIN 30 { 31 default: 32 usage(); 33 } 34 ARGEND 35 36 if (!argc) { 37 if (gethostname(host, sizeof(host)) < 0) 38 eprintf("gethostname:"); 39 puts(host); 40 } else if (argc == 1) { 41 #if FEATURE_HOSTNAME_SET 42 if (sethostname(argv[0], strlen(argv[0])) < 0) 43 eprintf("sethostname:"); 44 #else 45 eprintf("hostname: setting not supported\n"); 46 #endif 47 } else { 48 usage(); 49 } 50 51 return fshut(stdout, "<stdout>"); 52 }