/* vi: set sw=4 ts=4: */
/*
- * $Id: hostname.c,v 1.14 2000/09/27 02:43:35 kraai Exp $
+ * $Id: hostname.c,v 1.30 2001/06/26 02:06:08 bug1 Exp $
* Mini hostname implementation for busybox
*
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include "busybox.h"
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
+#include <string.h>
#include <stdio.h>
+#include <stdlib.h>
+#include "busybox.h"
-void do_sethostname(char *s, int isfile)
+static void do_sethostname(char *s, int isfile)
{
FILE *f;
char buf[255];
if (!isfile) {
if (sethostname(s, strlen(s)) < 0) {
if (errno == EPERM)
- errorMsg("you must be root to change the hostname\n");
+ error_msg_and_die("you must be root to change the hostname");
else
- perror("sethostname");
- exit(1);
+ perror_msg_and_die("sethostname");
}
} else {
f = xfopen(s, "r");
fgets(buf, 255, f);
+#ifdef BB_FEATURE_CLEAN_UP
fclose(f);
- if (buf[strlen(buf) - 1] == '\n')
- buf[strlen(buf) - 1] = 0;
- if (sethostname(buf, strlen(buf)) < 0) {
- perror("sethostname");
- exit(1);
- }
+#endif
+ chomp(buf);
+ do_sethostname(buf, 0);
}
}
char *s = NULL;
if (argc < 1)
- usage(hostname_usage);
+ show_usage();
while (--argc > 0 && **(++argv) == '-') {
while (*(++(*argv))) {
break;
case 'F':
if (--argc == 0) {
- usage(hostname_usage);
+ show_usage();
+ }
+ filename = *(++argv);
+ break;
+ case '-':
+ if (strcmp(++(*argv), "file") || --argc ==0 ) {
+ show_usage();
}
filename = *(++argv);
break;
default:
- usage(hostname_usage);
+ show_usage();
}
if (filename != NULL)
break;
if (!s)
s = buf;
*s = 0;
- printf("%s\n", buf);
+ puts(buf);
} else if (opt_domain) {
s = strchr(buf, '.');
- printf("%s\n", (s ? s + 1 : ""));
+ puts(s ? s + 1 : "");
} else if (opt_ip) {
- h = gethostbyname(buf);
- if (!h) {
- printf("Host not found\n");
- exit(1);
- }
- printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr)));
+ h = xgethostbyname(buf);
+ puts(inet_ntoa(*(struct in_addr *) (h->h_addr)));
} else {
- printf("%s\n", buf);
+ puts(buf);
}
}
return(0);