[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug-inetutils] hostname -- new addition to Inetutils
From: |
Debarshi Ray |
Subject: |
[bug-inetutils] hostname -- new addition to Inetutils |
Date: |
Sun, 10 Aug 2008 03:14:23 +0530 |
On Alfred's request, I started to write an implementation of hostname
for GNU Inetutils. What follows is the initial version of the code.
Its fairly simple and does not support all the options yet.
/* Copyright (C) 2008 Free Software Foundation, Inc.
This file is part of GNU Inetutils.
GNU Inetutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Inetutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Inetutils; see the file COPYING. If not, write
to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA 02110-1301 USA. */
#include <config.h>
#include <argp.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libinetutils.h"
#include "xalloc.h"
int (*action) (char * name, size_t size) = gethostname;
short int hostname_dns_domain = 0;
short int hostname_short = 0;
ARGP_PROGRAM_DATA ("hostname", "2008", "Debarshi Ray");
const char args_doc[] = "[NAME]";
const char doc[] = "Show or set the system's host name.";
static struct argp_option argp_options[] = {
#define GRP 0
{"domain", 'd', NULL, 0, "DNS domain name", GRP+1},
{"file", 'F', "FILE", 0, "Read host name or NIS domain name from FILE",
GRP+1},
{"fqdn", 'f', NULL, 0, "DNS host name or FQDN", GRP+1},
{"long", 'f', NULL, OPTION_ALIAS, "DNS host name or FQDN", GRP+1},
{"short", 's', NULL, 0, "Short host name", GRP+1},
{"yp", 'y', NULL, 0, "NIS/YP domain name", GRP+1},
{"nis", 'y', NULL, OPTION_ALIAS, "NIS/YP domain name", GRP+1},
#undef GRP
{NULL}
};
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'd':
action = gethostname;
hostname_dns_domain = 1;
break;
case 'F':
break;
case 'f':
action = gethostname;
break;
case 's':
action = gethostname;
hostname_short = 1;
break;
case 'y':
action = getdomainname;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {argp_options, parse_opt, args_doc, doc};
char * get_dns_domain_name (const char * const host_name);
char * get_short_hostname (const char * const host_name);
int
main (int argc, char * argv[])
{
char * buffer;
char * name;
int status;
size_t size;
/* Parse command line */
argp_parse (&argp, argc, argv, 0, NULL, NULL);
size = sysconf (_SC_HOST_NAME_MAX);
buffer = (char *) xmalloc (sizeof (char) * (size + 1));
status = (*action) (buffer, size);
while (status == -1)
{
size += 32;
buffer = (char *) xrealloc (buffer, size);
status = (*action) (buffer, size);
}
if (hostname_dns_domain == 1)
name = get_dns_domain_name (buffer);
else if (hostname_short == 1)
name = get_short_hostname (buffer);
else
name = strdup (buffer);
printf ("%s\n", name);
free (name);
free (buffer);
return 0;
}
char *
get_dns_domain_name (const char * const host_name)
{
char * domain_name;
const char * const pos = strchr (host_name, '.');
if (pos == NULL)
domain_name = strdup ("(none)");
else
domain_name = strdup (pos+1);
return domain_name;
}
char *
get_short_hostname (const char * const host_name)
{
size_t size;
char * short_hostname;
const char * const pos = strchr (host_name, '.');
if (pos == NULL)
short_hostname = strdup (host_name);
else
{
size = pos - host_name;
short_hostname = strndup (host_name, size);
}
return short_hostname;
}
Comments?
Happy hacking,
Debarshi
- [bug-inetutils] hostname -- new addition to Inetutils,
Debarshi Ray <=
- [bug-inetutils] Re: hostname -- new addition to Inetutils, Debarshi Ray, 2008/08/12
- Re: [bug-inetutils] Re: hostname -- new addition to Inetutils, Alfred M. Szmidt, 2008/08/13
- Re: [bug-inetutils] Re: hostname -- new addition to Inetutils, Debarshi Ray, 2008/08/14
- Re: [bug-inetutils] Re: hostname -- new addition to Inetutils, Alfred M. Szmidt, 2008/08/14
- [bug-inetutils] Re: hostname -- new addition to Inetutils, Simon Josefsson, 2008/08/14
- [bug-inetutils] Re: hostname -- new addition to Inetutils, Alfred M. Szmidt, 2008/08/14
- [bug-inetutils] Re: hostname -- new addition to Inetutils, Debarshi Ray, 2008/08/14