date and time created 88/10/18 22:03:05 by bostic
[unix-history] / usr / src / old / gettable / gettable.c
index 287ecd1..7d4fbac 100644 (file)
@@ -1,6 +1,29 @@
+/*
+ * Copyright (c) 1983 The Regents of the University of California.
+ * 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 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)gettable.c 4.1 (Bekeley) %G%";
-#endif
+static char sccsid[] = "@(#)gettable.c 5.4 (Berkeley) %G%";
+#endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/socket.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -11,7 +34,9 @@ static char sccsid[] = "@(#)gettable.c        4.1 (Bekeley) %G%";
 #include <netdb.h>
 
 #define        OUTFILE         "hosts.txt"     /* default output file */
 #include <netdb.h>
 
 #define        OUTFILE         "hosts.txt"     /* default output file */
+#define        VERFILE         "hosts.ver"     /* default version file */
 #define        QUERY           "ALL\r\n"       /* query to hostname server */
 #define        QUERY           "ALL\r\n"       /* query to hostname server */
+#define        VERSION         "VERSION\r\n"   /* get version number */
 
 #define        equaln(s1, s2, n)       (!strncmp(s1, s2, n))
 
 
 #define        equaln(s1, s2, n)       (!strncmp(s1, s2, n))
 
@@ -26,44 +51,54 @@ main(argc, argv)
        int s;
        register len;
        register FILE *sfi, *sfo, *hf;
        int s;
        register len;
        register FILE *sfi, *sfo, *hf;
-       register char *p;
        char *host;
        register struct hostent *hp;
        struct servent *sp;
        char *host;
        register struct hostent *hp;
        struct servent *sp;
+       int version = 0;
+       int beginseen = 0;
+       int endseen = 0;
 
        argv++, argc--;
 
        argv++, argc--;
+       if (**argv == '-') {
+               if (argv[0][1] != 'v')
+                       fprintf(stderr, "unknown option %s ignored\n", *argv);
+               else
+                       version++, outfile = VERFILE;
+               argv++, argc--;
+       }
        if (argc < 1 || argc > 2) {
        if (argc < 1 || argc > 2) {
-               fprintf(stderr, "usage: gettable host [ file ]\n");
+               fprintf(stderr, "usage: gettable [-v] host [ file ]\n");
                exit(1);
        }
                exit(1);
        }
-       sp = getservbyname("nicname", "tcp");
+       sp = getservbyname("hostnames", "tcp");
        if (sp == NULL) {
        if (sp == NULL) {
-               fprintf(stderr, "gettable: nicname/tcp: unknown service\n");
+               fprintf(stderr, "gettable: hostnames/tcp: unknown service\n");
                exit(3);
        }
        host = *argv;
        argv++, argc--;
        hp = gethostbyname(host);
        if (hp == NULL) {
                exit(3);
        }
        host = *argv;
        argv++, argc--;
        hp = gethostbyname(host);
        if (hp == NULL) {
-               fprintf(stderr, "gettable: %s: host unknown\n", host);
+               fprintf(stderr, "gettable: %s: ", host);
+               herror((char *)NULL);
                exit(2);
        }
        host = hp->h_name;
        if (argc > 0)
                outfile = *argv;
        sin.sin_family = hp->h_addrtype;
                exit(2);
        }
        host = hp->h_name;
        if (argc > 0)
                outfile = *argv;
        sin.sin_family = hp->h_addrtype;
-       s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0);
+       s = socket(hp->h_addrtype, SOCK_STREAM, 0);
        if (s < 0) {
                perror("gettable: socket");
                exit(4);
        }
        if (s < 0) {
                perror("gettable: socket");
                exit(4);
        }
-       if (bind(s, &sin, sizeof (sin), 0) < 0) {
+       if (bind(s, &sin, sizeof (sin)) < 0) {
                perror("gettable: bind");
                exit(5);
        }
                perror("gettable: bind");
                exit(5);
        }
-       bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
+       bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
        sin.sin_port = sp->s_port;
        sin.sin_port = sp->s_port;
-       if (connect(s, &sin, sizeof (sin), 0) < 0) {
+       if (connect(s, &sin, sizeof (sin)) < 0) {
                perror("gettable: connect");
                exit(6);
        }
                perror("gettable: connect");
                exit(6);
        }
@@ -81,22 +116,50 @@ main(argc, argv)
                close(s);
                exit(1);
        }
                close(s);
                exit(1);
        }
-       fprintf(sfo, QUERY);
+       fprintf(sfo, version ? VERSION : QUERY);
        fflush(sfo);
        while (fgets(buf, sizeof(buf), sfi) != NULL) {
                len = strlen(buf);
                buf[len-2] = '\0';
        fflush(sfo);
        while (fgets(buf, sizeof(buf), sfi) != NULL) {
                len = strlen(buf);
                buf[len-2] = '\0';
-               if (equaln(buf, "BEGIN", 5) || equaln(buf, "END", 3)) {
+               if (!version && equaln(buf, "BEGIN", 5)) {
+                       if (beginseen || endseen) {
+                               fprintf(stderr,
+                                   "gettable: BEGIN sequence error\n");
+                               exit(90);
+                       }
+                       beginseen++;
                        continue;
                }
                        continue;
                }
-               if (equaln(buf, "ERR", 3)) {
-                       fprintf(stderr, "gettable: nicname error: %s", buf);
+               if (!version && equaln(buf, "END", 3)) {
+                       if (!beginseen || endseen) {
+                               fprintf(stderr,
+                                   "gettable: END sequence error\n");
+                               exit(91);
+                       }
+                       endseen++;
                        continue;
                }
                        continue;
                }
+               if (equaln(buf, "ERR", 3)) {
+                       fprintf(stderr,
+                           "gettable: hostname service error: %s", buf);
+                       exit(92);
+               }
                fprintf(hf, "%s\n", buf);
        }
        fclose(hf);
                fprintf(hf, "%s\n", buf);
        }
        fclose(hf);
-       fprintf(stderr, "Host table received.\n");
+       if (!version) {
+               if (!beginseen) {
+                       fprintf(stderr, "gettable: no BEGIN seen\n");
+                       exit(93);
+               }
+               if (!endseen) {
+                       fprintf(stderr, "gettable: no END seen\n");
+                       exit(94);
+               }
+               fprintf(stderr, "Host table received.\n");
+       } else
+               fprintf(stderr, "Version number received.\n");
        close(s);
        fprintf(stderr, "Connection to %s closed\n", host);
        close(s);
        fprintf(stderr, "Connection to %s closed\n", host);
+       exit(0);
 }
 }