]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/zlocate/zlocate.c
upstream tag
[1ts-debian.git] / zephyr / clients / zlocate / zlocate.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains code for the "zlocate" command.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id: zlocate.c,v 1.15 1999/01/22 23:18:36 ghudson Exp $
7  *
8  *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #include <sysdep.h>
14 #include <zephyr/zephyr.h>
15 #include <sys/socket.h>
16
17 #if !defined(lint) && !defined(SABER)
18 static const char rcsid_zlocate_c[] = "$Id: zlocate.c,v 1.15 1999/01/22 23:18:36 ghudson Exp $";
19 #endif
20
21 int numusers=0, numleft=0, parallel=0, oneline=0;
22 char *whoami;
23
24 RETSIGTYPE timeout(sig)
25 {
26   fprintf (stderr, "%s: no response from server\n", whoami);
27   exit(1);
28 }
29
30 void usage()
31 {
32    printf("Usage: %s [ -a | -d ] [ -p ] [ -1 ] user ... \n",whoami);
33    exit(1);
34 }
35
36 void print_locs(user,nlocs)
37      char *user;
38      int nlocs;
39 {
40    int one = 1, retval;
41    ZLocations_t locations;
42
43    if ((!oneline) && (numusers>1))
44      printf("\t%s:\n",user);
45
46    if ((!oneline) && (nlocs == 0))
47       printf("Hidden or not logged-in\n");
48
49    for (;nlocs;nlocs--) {
50       if ((retval = ZGetLocations(&locations,&one)) != ZERR_NONE) {
51          com_err(whoami,retval,"while getting location");
52          exit(1);
53       }
54  
55       if (oneline) {
56          printf("%s:\t%s\t%s\t%s\n",user,locations.host,locations.tty,
57                 locations.time);
58       } else {
59          printf("%-42s %-7s %s\n",locations.host, locations.tty, locations.time);
60       }
61    }
62
63    if ((!oneline) && (numusers > 1) && (numleft > 0))
64      printf("\n");
65 }
66
67 /*ARGSUSED*/
68 main(argc,argv)
69         int argc;
70         char *argv[];
71 {
72     char user[BUFSIZ],*whichuser;
73     ZAsyncLocateData_t ald;
74     int retval,i,numlocs,numfound,loc,auth,rlen;
75     ZNotice_t notice;
76 #ifdef _POSIX_VERSION
77     struct sigaction sa;
78 #endif
79    
80     whoami = argv[0];
81     auth = -1;
82
83     argv++;
84     argc--;
85
86     for (i=0; i < argc; i++)
87         if (argv[i][0] == '-')
88             switch (argv[i][1]) {
89             case 'a':
90                 if (auth != -1) usage();
91                 auth = 1;
92                 break;
93             case 'd':
94                 if (auth != -1) usage();
95                 auth = 0;
96                 break;
97             case 'p':
98                 parallel = 1;
99                 break;
100             case '1':
101                 oneline = 1;
102                 break;
103             default:
104                 usage();
105                 break;
106             }
107         else
108             numusers++;
109
110     if (numusers == 0)
111         usage();
112
113     if (auth == -1) auth = 1;
114    
115     if ((retval = ZInitialize()) != ZERR_NONE) {
116         com_err(whoami,retval,"while initializing");
117         exit(1);
118     } 
119
120     numleft = numusers;
121     numfound = 0;
122     rlen = strlen(ZGetRealm());
123
124     i = 0;
125     for (loc = 0; loc < argc; loc++) {
126         if (argv[loc][0] == '-') continue;
127
128         (void) strncpy(user,argv[loc],sizeof(user) - rlen - 2);
129         user[sizeof(user) - rlen - 2] = '\0';
130         if (!strchr(user,'@')) {
131             (void) strcat(user,"@");
132             (void) strcat(user,ZGetRealm());
133         } 
134         if (parallel) {
135             if ((retval = ZRequestLocations(user, &ald, i ? UNSAFE : UNACKED,
136                                             auth?ZAUTH:ZNOAUTH)) != ZERR_NONE) {
137                 com_err(whoami,retval,"requesting location of %s",user);
138                 exit(1);
139             }
140             i = 1;
141         } else {
142             if ((retval = ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH)) != ZERR_NONE) {
143                 com_err(whoami,retval,"while locating user %s",user);
144                 exit(1);
145             }
146             print_locs(user,numlocs);
147             numfound += numlocs;
148         }
149     }
150
151     if (parallel) {
152 #ifdef _POSIX_VERSION
153         sigemptyset(&sa.sa_mask);
154         sa.sa_flags = 0;
155         sa.sa_handler = timeout;
156         sigaction(SIGALRM, &sa, (struct sigaction *)0);
157 #else
158         signal (SIGALRM, timeout);
159 #endif
160         while (numleft-- > 0) {
161             alarm(SRV_TIMEOUT);
162             if ((retval = ZReceiveNotice(&notice, NULL)) != ZERR_NONE) {
163                 com_err(whoami,retval,"while searching notice queue");
164                 continue;
165             }
166             if ((retval = ZParseLocations(&notice, (ZAsyncLocateData_t *)NULL,
167                                           &numlocs, &whichuser)) != ZERR_NONE) {
168                 com_err(whoami,retval,"while parsing locations");
169                 continue;
170             }
171             if (numlocs >= 0) {
172                 print_locs(whichuser,numlocs);
173                 free(whichuser);
174                 numfound += numlocs;
175             }
176             ZFreeNotice(&notice);
177         }
178     }
179     return((numfound > 0) ? 0 : 1);
180 }