hostname.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * $Id: hostname.c,v 1.36 2003/07/14 21:21:01 andersen Exp $
  4. * Mini hostname implementation for busybox
  5. *
  6. * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  7. *
  8. * adjusted by Erik Andersen <andersen@codepoet.org> to remove
  9. * use of long options and GNU getopt. Improved the usage info.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <errno.h>
  26. #include <arpa/inet.h>
  27. #include <netdb.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <getopt.h>
  33. #include "busybox.h"
  34. extern char *optarg; /* in unistd.h */
  35. extern int optind, opterr, optopt; /* in unistd.h */
  36. static void do_sethostname(char *s, int isfile)
  37. {
  38. FILE *f;
  39. char buf[255];
  40. if (!s)
  41. return;
  42. if (!isfile) {
  43. if (sethostname(s, strlen(s)) < 0) {
  44. if (errno == EPERM)
  45. bb_error_msg_and_die("you must be root to change the hostname");
  46. else
  47. bb_perror_msg_and_die("sethostname");
  48. }
  49. } else {
  50. f = bb_xfopen(s, "r");
  51. while (fgets(buf, 255, f) != NULL) {
  52. if (buf[0] =='#') {
  53. continue;
  54. }
  55. chomp(buf);
  56. do_sethostname(buf, 0);
  57. }
  58. #ifdef CONFIG_FEATURE_CLEAN_UP
  59. fclose(f);
  60. #endif
  61. }
  62. }
  63. int hostname_main(int argc, char **argv)
  64. {
  65. int opt;
  66. int type = 0;
  67. struct hostent *hp;
  68. char *filename = NULL;
  69. char buf[255];
  70. char *p = NULL;
  71. if (argc < 1)
  72. bb_show_usage();
  73. while ((opt = getopt(argc, argv, "dfisF:")) > 0) {
  74. switch (opt) {
  75. case 'd':
  76. case 'f':
  77. case 'i':
  78. case 's':
  79. type = opt;
  80. break;
  81. case 'F':
  82. filename = optarg;
  83. break;
  84. default:
  85. bb_show_usage();
  86. }
  87. }
  88. /* Output in desired format */
  89. if (type != 0) {
  90. gethostname(buf, 255);
  91. hp = xgethostbyname(buf);
  92. p = strchr(hp->h_name, '.');
  93. if (type == 'f') {
  94. puts(hp->h_name);
  95. } else if (type == 's') {
  96. if (p != NULL) {
  97. *p = 0;
  98. }
  99. puts(hp->h_name);
  100. } else if (type == 'd') {
  101. if (p) puts(p + 1);
  102. } else if (type == 'i') {
  103. while (hp->h_addr_list[0]) {
  104. printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
  105. }
  106. printf("\n");
  107. }
  108. }
  109. /* Set the hostname */
  110. else if (filename != NULL) {
  111. do_sethostname(filename, 1);
  112. } else if (optind < argc) {
  113. do_sethostname(argv[optind], 0);
  114. }
  115. /* Or if all else fails,
  116. * just print the current hostname */
  117. else {
  118. gethostname(buf, 255);
  119. puts(buf);
  120. }
  121. return(0);
  122. }