home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2688 < prev    next >
Encoding:
Internet Message Format  |  1991-02-08  |  3.5 KB

  1. From: jfh@rpp386.cactus.org (John F Haugh II)
  2. Newsgroups: comp.unix.wizards,alt.sources
  3. Subject: Setting the node name
  4. Message-ID: <19036@rpp386.cactus.org>
  5. Date: 7 Feb 91 15:52:27 GMT
  6.  
  7. Someone asked for a program which did this.  I just found this while
  8. cleaning out a directory.  Enjoy!
  9. --
  10. /*
  11.  * setuname.c
  12.  *
  13.  *  This program is hereby placed in the public domain.  You may do
  14.  * whatever you wish.  You may even lie and say you wrote it yourself.
  15.  * Most importantly you may sell it with your operating system so people
  16.  * won't have to reconfigure their systems to change the nodename.
  17.  *
  18.  *    John F. Haugh II    (jfh@rpp368.cactus.org)    9/29/88
  19.  *
  20.  *    Modification History
  21.  *
  22.  *    02/07/91    jfh@rpp386.cactus.org
  23.  *        Changed my mail address.
  24.  */
  25.  
  26. #include <sys/a.out.h>
  27. #include <sys/types.h>
  28. #include <sys/utsname.h>
  29. #include <fcntl.h>
  30. #include <stdio.h>
  31.  
  32. int    memfd;
  33. int    kmemfd;
  34.  
  35. struct    utsname    utsname;
  36. struct    utsname    Kutsname;
  37. char    nodename[SYS_NMLN];
  38.  
  39. #define    UTSNAME    0
  40. #define    NODENAME 1
  41.  
  42. struct    xlist    namelist[] = {
  43.     { 0, 0, 0, "_utsname" },
  44.     { 0, 0, 0, "_node" },
  45.     { 0, 0, 0, (char *) 0 }
  46. };
  47.  
  48. usage ()
  49. {
  50.     fprintf (stderr, "usage: setuname [ -n namelist ] [ -k kmem ] nodename\n");
  51.     exit (1);
  52. }
  53.  
  54. r_read (fd, buf, n)
  55. int    fd;
  56. char    *buf;
  57. int    n;
  58. {
  59.     int    i;
  60.  
  61.     if ((i = read (fd, buf, n)) == -1) {
  62.         perror ("error on read");
  63.         return (-1);
  64.     } else
  65.         return (i);
  66. }
  67.  
  68. w_write (fd, buf, n)
  69. int    fd;
  70. char    *buf;
  71. int    n;
  72. {
  73.     int    i;
  74.  
  75.     if ((i = write (fd, buf, n)) == -1) {
  76.         perror ("error on write");
  77.         return (-1);
  78.     } else
  79.         return (i);
  80. }
  81.  
  82. long    l_lseek (fd, offs, whence)
  83. int    fd;
  84. long    offs;
  85. int    whence;
  86. {
  87.     long    i;
  88.     long    lseek ();
  89.  
  90.     if ((i = lseek (fd, offs, whence)) == -1L) {
  91.         perror ("error on lseek");
  92.         return (-1);
  93.     } else
  94.         return (i);
  95. }
  96.  
  97. main (argc, argv)
  98. int    argc;
  99. char    **argv;
  100. {
  101.     char    newname[10];
  102.     char    *namefile = "/xenix";
  103.     char    *kmemfile = "/dev/kmem";
  104.     char    *name;
  105.     int    c;
  106.     extern    int    optind;
  107.     extern    char    *optarg;
  108.  
  109.     while ((c = getopt (argc, argv, "n:k:")) != EOF) {
  110.         switch (c) {
  111.             case 'k':
  112.                 kmemfile = optarg;
  113.                 break;
  114.             case 'n':
  115.                 namefile = optarg;
  116.                 break;
  117.             default:
  118.                 usage ();
  119.         }
  120.     }
  121.     if (optind == argc)
  122.         usage ();
  123.  
  124.     name = argv[optind];
  125.  
  126.     if (xlist (namefile, namelist) != 0) {
  127.         perror ("pstat: namelist");
  128.         exit (1);
  129.     }
  130.     if (namelist[UTSNAME].xl_value == 0 ||
  131.             namelist[NODENAME].xl_value == 0){
  132.         fprintf (stderr, "pstat: bad namelist: %s\n", namefile);
  133.         exit (1);
  134.     }
  135.     if ((kmemfd = open (kmemfile, O_RDWR)) < 0) {
  136.         perror ("pstat: kmemfile");
  137.         exit (1);
  138.     }
  139.     l_lseek (kmemfd, namelist[NODENAME].xl_value, 0);
  140.     r_read (kmemfd, nodename, sizeof nodename);
  141.  
  142.     l_lseek (kmemfd, namelist[UTSNAME].xl_value, 0);
  143.     r_read (kmemfd, &Kutsname, sizeof Kutsname);
  144.     uname (&utsname);
  145.  
  146.     if (memcmp (&Kutsname, &utsname, sizeof utsname) != 0) {
  147.         fprintf (stderr, "pstat: can't find utsname!\n");
  148.         exit (1);
  149.     }
  150.     printf ("changing node %.9s to %.9s:  DEL if wrong!\n", nodename, name);
  151.     sleep (10);
  152.  
  153.     strncpy (nodename, name, sizeof utsname.nodename);
  154.     l_lseek (kmemfd, namelist[NODENAME].xl_value, 0);
  155.     w_write (kmemfd, nodename, sizeof utsname.nodename);
  156.  
  157.     strncpy (utsname.nodename, name, sizeof utsname.nodename);
  158.     l_lseek (kmemfd, namelist[UTSNAME].xl_value, 0);
  159.     w_write (kmemfd, &utsname, sizeof utsname);
  160.  
  161.     exit (0);
  162. }
  163. -- 
  164. John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
  165. Ma Bell: (512) 832-8832                           Domain: jfh@rpp386.cactus.org
  166. "I've never written a device driver, but I have written a device driver manual"
  167.                 -- Robert Hartman, IDE Corp.
  168.