hostname.c (590B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stdio.h> 3 #include <string.h> 4 #include <unistd.h> 5 6 #include "util.h" 7 8 static void 9 usage(void) 10 { 11 eprintf("usage: %s [name]\n", argv0); 12 } 13 14 int 15 main(int argc, char *argv[]) 16 { 17 char host[HOST_NAME_MAX + 1]; 18 19 argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; 20 21 if (!argc) { 22 if (gethostname(host, sizeof(host)) < 0) 23 eprintf("gethostname:"); 24 puts(host); 25 } else if (argc == 1) { 26 if (sethostname(argv[0], strlen(argv[0])) < 0) 27 eprintf("sethostname:"); 28 } else { 29 usage(); 30 } 31 32 return fshut(stdout, "<stdout>"); 33 }