aboutsummaryrefslogtreecommitdiff
path: root/toys/lsb
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-05-04 05:14:35 -0500
committerRob Landley <rob@landley.net>2016-05-04 05:14:35 -0500
commit5c8f3d7d2b03c0c6ba8ce244783cb88b29108a4d (patch)
tree264127b8c34e0f44fa836cac586fe7ed56b5e912 /toys/lsb
parentc451397f16ecfbe05d41ac76e3d5f84dc39a6bfc (diff)
downloadtoybox-5c8f3d7d2b03c0c6ba8ce244783cb88b29108a4d.tar.gz
Hostname cleanup, as described on the list.
Diffstat (limited to 'toys/lsb')
-rw-r--r--toys/lsb/hostname.c44
1 files changed, 13 insertions, 31 deletions
diff --git a/toys/lsb/hostname.c b/toys/lsb/hostname.c
index ebfc803c..f30d1fbd 100644
--- a/toys/lsb/hostname.c
+++ b/toys/lsb/hostname.c
@@ -27,43 +27,25 @@ GLOBALS(
void hostname_main(void)
{
- const char *hostname = toys.optargs[0];
-
- if (toys.optflags & FLAG_F) {
- char *buf;
- if ((hostname = buf = readfile(TT.fname, 0, 0))) {
- size_t len = strlen(hostname);
- char *end = buf + len - 1;
-
- /* Trim trailing whitespace. */
- while (len && isspace(*end)) {
- *end-- = '\0';
- len--;
- }
- if (!len) {
- free(buf);
- hostname = NULL;
- if (!(toys.optflags & FLAG_b))
- error_exit("empty file '%s'", TT.fname);
- }
- } else if (!(toys.optflags & FLAG_b))
- error_exit("failed to read '%s'", TT.fname);
+ char *hostname = *toys.optargs;
+
+ if (TT.fname && (hostname = xreadfile(TT.fname, 0, 0))) {
+ if (!*chomp(hostname)) {
+ if (CFG_TOYBOX_FREE) free(hostname);
+ if (!(toys.optflags&FLAG_b)) error_exit("empty '%s'", TT.fname);
+ hostname = 0;
+ }
}
- if (!hostname && toys.optflags & FLAG_b) {
- /* Do nothing if hostname already set. */
- if (gethostname(toybuf, sizeof(toybuf))) perror_exit("get failed");
- if (strnlen(toybuf, sizeof(toybuf))) exit(0);
-
- /* Else set hostname to localhost. */
- hostname = "localhost";
- }
+ if (!hostname && (toys.optflags&FLAG_b))
+ if (gethostname(toybuf, sizeof(toybuf)-1) || !*toybuf)
+ hostname = "localhost";
if (hostname) {
if (sethostname(hostname, strlen(hostname)))
- perror_exit("set failed '%s'", hostname);
+ perror_exit("set '%s'", hostname);
} else {
- if (gethostname(toybuf, sizeof(toybuf))) perror_exit("get failed");
+ if (gethostname(toybuf, sizeof(toybuf)-1)) perror_exit("gethostname");
xputs(toybuf);
}
}