/bin/hostname options/arguments

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

/bin/hostname options/arguments

Przemyslaw Nowaczyk
hi,
I've got a question about hostname arguments.. the hostname(1) says:
"hostname - set *or* print name of current host system" so as I understand it
something like: 'hostname -s new_name' shouldn't be allowed right? becouse we
acctually don't know what to do (set or print, assuming we're running root)..
if I'm right - here's a diff to change that behavior (with some KNF fixups)..
if not - sorry for spamming..

Index: hostname.c
===================================================================
RCS file: /cvs/src/bin/hostname/hostname.c,v
retrieving revision 1.7
diff -u -r1.7 hostname.c
--- hostname.c 2003/06/02 23:32:08 1.7
+++ hostname.c 2006/03/09 09:44:12
@@ -52,15 +52,13 @@
 #include <string.h>
 #include <unistd.h>
 
-extern char *__progname;
+__dead void usage(void);
 
-void usage(void);
-
 int
 main(int argc, char *argv[])
 {
- int ch, sflag;
- char *p, hostname[MAXHOSTNAMELEN];
+ int ch, sflag;
+ char *p, hostname[MAXHOSTNAMELEN];
 
  sflag = 0;
  while ((ch = getopt(argc, argv, "s")) != -1)
@@ -70,6 +68,7 @@
  break;
  default:
  usage();
+ /* NOTREACHED */
  }
  argc -= optind;
  argv += optind;
@@ -78,6 +77,8 @@
  usage();
 
  if (*argv) {
+ if (sflag)
+ usage();
  if (sethostname(*argv, strlen(*argv)))
  err(1, "sethostname");
  } else {
@@ -93,6 +94,8 @@
 void
 usage(void)
 {
+ extern char *__progname;
+
  (void)fprintf(stderr, "usage: %s [-s] [name-of-host]\n", __progname);
  exit(1);
 }

and one more thing.. if I'm right, shouldn't the usage args look like
usage: hostname [-s | name-of-host]?

--
Przemyslaw Nowaczyk <[hidden email]>
CS student @ Poznan University of Technology

Reply | Threaded
Open this post in threaded view
|

Re: /bin/hostname options/arguments

Przemyslaw Nowaczyk
On Thu, Mar 09, 2006 at 10:57:47AM +0100, Przemyslaw Nowaczyk wrote:

> hi,
> I've got a question about hostname arguments.. the hostname(1) says:
> "hostname - set *or* print name of current host system" so as I understand it
> something like: 'hostname -s new_name' shouldn't be allowed right? becouse we
> acctually don't know what to do (set or print, assuming we're running root)..
> if I'm right - here's a diff to change that behavior (with some KNF fixups)..
> if not - sorry for spamming..
>
> <snip>
>
> and one more thing.. if I'm right, shouldn't the usage args look like
> usage: hostname [-s | name-of-host]?
>

here're all the changes (to man page too).. could somebody verify if my idea
is right..?

Index: hostname.1
===================================================================
RCS file: /cvs/src/bin/hostname/hostname.1,v
retrieving revision 1.18
diff -u -r1.18 hostname.1
--- hostname.1 2003/12/03 14:06:08 1.18
+++ hostname.1 2006/03/09 17:49:16
@@ -38,8 +38,7 @@
 .Nd set or print name of current host system
 .Sh SYNOPSIS
 .Nm hostname
-.Op Fl s
-.Op Ar name-of-host
+.Op Fl s | Ar name-of-host
 .Sh DESCRIPTION
 The
 .Nm
Index: hostname.c
===================================================================
RCS file: /cvs/src/bin/hostname/hostname.c,v
retrieving revision 1.7
diff -u -r1.7 hostname.c
--- hostname.c 2003/06/02 23:32:08 1.7
+++ hostname.c 2006/03/09 17:49:16
@@ -52,15 +52,13 @@
 #include <string.h>
 #include <unistd.h>
 
-extern char *__progname;
+__dead void usage(void);
 
-void usage(void);
-
 int
 main(int argc, char *argv[])
 {
- int ch, sflag;
- char *p, hostname[MAXHOSTNAMELEN];
+ int ch, sflag;
+ char *p, hostname[MAXHOSTNAMELEN];
 
  sflag = 0;
  while ((ch = getopt(argc, argv, "s")) != -1)
@@ -70,6 +68,7 @@
  break;
  default:
  usage();
+ /* NOTREACHED */
  }
  argc -= optind;
  argv += optind;
@@ -78,6 +77,8 @@
  usage();
 
  if (*argv) {
+ if (sflag)
+ usage();
  if (sethostname(*argv, strlen(*argv)))
  err(1, "sethostname");
  } else {
@@ -90,9 +91,11 @@
  exit(0);
 }
 
-void
+__dead void
 usage(void)
 {
- (void)fprintf(stderr, "usage: %s [-s] [name-of-host]\n", __progname);
+ extern char *__progname;
+
+ (void)fprintf(stderr, "usage: %s [-s | name-of-host]\n", __progname);
  exit(1);
 }

--
Przemyslaw Nowaczyk <[hidden email]>
CS student @ Poznan University of Technology