use library version of rmut
[unix-history] / usr / src / sbin / ifconfig / ifconfig.c
index 5cde7e4..6d52302 100644 (file)
@@ -1,18 +1,29 @@
 /*
  * Copyright (c) 1983 Regents of the University of California.
 /*
  * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  All rights reserved.\n";
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)ifconfig.c 4.17 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)ifconfig.c 4.21 (Berkeley) %G%";
+#endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/socket.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -107,7 +118,7 @@ main(argc, argv)
        int af = AF_INET;
 
        if (argc < 2) {
        int af = AF_INET;
 
        if (argc < 2) {
-               fprintf(stderr, "usage: ifconfig interface\n%s%s%s%s",
+               fprintf(stderr, "usage: ifconfig interface\n%s%s%s%s%s",
                    "\t[ af [ address [ dest_addr ] ] [ up ] [ down ]",
                            "[ netmask mask ] ]\n",
                    "\t[ metric n ]\n",
                    "\t[ af [ address [ dest_addr ] ] [ up ] [ down ]",
                            "[ netmask mask ] ]\n",
                    "\t[ metric n ]\n",
@@ -296,42 +307,44 @@ status()
                printf(" metric %d", metric);
        putchar('\n');
        if ((p = afp) != NULL) {
                printf(" metric %d", metric);
        putchar('\n');
        if ((p = afp) != NULL) {
-               (*p->af_status)();
-               return;
-       }
-       for (p = afs; p->af_name; p++) {
+               (*p->af_status)(1);
+       } else for (p = afs; p->af_name; p++) {
                ifr.ifr_addr.sa_family = p->af_af;
                ifr.ifr_addr.sa_family = p->af_af;
-               (*p->af_status)();
+               (*p->af_status)(0);
        }
 }
 
        }
 }
 
-in_status()
+in_status(force)
+       int force;
 {
        struct sockaddr_in *sin;
        char *inet_ntoa();
 
 {
        struct sockaddr_in *sin;
        char *inet_ntoa();
 
-       if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
-               if (errno != EADDRNOTAVAIL)
-                       Perror("ioctl (SIOCGIFNETMASK)");
-       } else
-               netmask.sin_addr =
-                   ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
        strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
        if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
        strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
        if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
-               if (errno == EADDRNOTAVAIL)
+               if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
+                       if (!force)
+                               return;
                        bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
                        bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
-               else
-                       Perror("ioctl (SIOCGIFADDR)");
+               else
+                       perror("ioctl (SIOCGIFADDR)");
        }
        }
-       strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
        sin = (struct sockaddr_in *)&ifr.ifr_addr;
        printf("\tinet %s ", inet_ntoa(sin->sin_addr));
        sin = (struct sockaddr_in *)&ifr.ifr_addr;
        printf("\tinet %s ", inet_ntoa(sin->sin_addr));
+       strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
+       if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
+               if (errno != EADDRNOTAVAIL)
+                       perror("ioctl (SIOCGIFNETMASK)");
+               bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
+       } else
+               netmask.sin_addr =
+                   ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
        if (flags & IFF_POINTOPOINT) {
                if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
                        if (errno == EADDRNOTAVAIL)
                            bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
                        else
        if (flags & IFF_POINTOPOINT) {
                if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
                        if (errno == EADDRNOTAVAIL)
                            bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
                        else
-                           Perror("ioctl (SIOCGIFDSTADDR)");
+                           perror("ioctl (SIOCGIFDSTADDR)");
                }
                strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
                sin = (struct sockaddr_in *)&ifr.ifr_dstaddr;
                }
                strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
                sin = (struct sockaddr_in *)&ifr.ifr_dstaddr;
@@ -341,33 +354,39 @@ in_status()
        if (flags & IFF_BROADCAST) {
                if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
                        if (errno == EADDRNOTAVAIL)
        if (flags & IFF_BROADCAST) {
                if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
                        if (errno == EADDRNOTAVAIL)
-                               return;
-                       Perror("ioctl (SIOCGIFADDR)");
+                           bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
+                       else
+                           perror("ioctl (SIOCGIFADDR)");
                }
                strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
                sin = (struct sockaddr_in *)&ifr.ifr_addr;
                }
                strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
                sin = (struct sockaddr_in *)&ifr.ifr_addr;
-               printf("broadcast %s", inet_ntoa(sin->sin_addr));
+               if (sin->sin_addr.s_addr != 0)
+                       printf("broadcast %s", inet_ntoa(sin->sin_addr));
        }
        putchar('\n');
 }
 
 
        }
        putchar('\n');
 }
 
 
-xns_status()
+xns_status(force)
+       int force;
 {
        struct sockaddr_ns *sns;
 
        close(s);
        s = socket(AF_NS, SOCK_DGRAM, 0);
        if (s < 0) {
 {
        struct sockaddr_ns *sns;
 
        close(s);
        s = socket(AF_NS, SOCK_DGRAM, 0);
        if (s < 0) {
-               if (errno == EAFNOSUPPORT)
+               if (errno == EPROTONOSUPPORT)
                        return;
                perror("ifconfig: socket");
                exit(1);
        }
        if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
                        return;
                perror("ifconfig: socket");
                exit(1);
        }
        if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
-               if (errno == EADDRNOTAVAIL || errno==EAFNOSUPPORT)
-                       return;
-               Perror("ioctl (SIOCGIFADDR)");
+               if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
+                       if (!force)
+                               return;
+                       bzero((char *)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
+               } else
+                       perror("ioctl (SIOCGIFADDR)");
        }
        strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
        sns = (struct sockaddr_ns *)&ifr.ifr_addr;
        }
        strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
        sns = (struct sockaddr_ns *)&ifr.ifr_addr;