summaryrefslogtreecommitdiff
path: root/clients/zlocate/zlocate.c
blob: a0e514f49a9dc213d7ac78f332cf8fbad5ec6d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains code for the "zlocate" command.
 *
 *	Created by:	Robert French
 *
 *	$Source$
 *	$Author$
 *
 *	Copyright (c) 1987 by the Massachusetts Institute of Technology.
 *	For copying and distribution information, see the file
 *	"mit-copyright.h". 
 */

#include <zephyr/mit-copyright.h>

#include <zephyr/zephyr.h>
#include <string.h>

#ifndef lint
static char rcsid_zlocate_c[] = "$Header$";
#endif lint

char *whoami;

void usage()
{
   printf("Usage: %s [ -a | -d ] user ... \n",whoami);
   exit(1);
}

main(argc,argv)
	int argc;
	char *argv[];
{
	int retval,numlocs,i,one,ourargc,found,auth;
	char bfr[BUFSIZ],user[BUFSIZ];
	ZLocations_t locations;
	
	whoami = argv[0];
	auth = -1;

	if (argc < 2) usage();

	if ((retval = ZInitialize()) != ZERR_NONE) {
		com_err(whoami,retval,"while initializing");
		exit(1);
	} 

	argv++;
	argc--;

	for (i=0; argv[i]; i++)
	  if (argv[i][0] == '-')
	    switch (argv[i][1]) {
		  case 'a':
		    if (auth != -1) usage();
		    auth = 1;
		    break;
		  case 'd':
		    if (auth != -1) usage();
		    auth = 0;
		    break;
		  default:
		    usage();
		    break;
	    }

	one = 1;
	found = 0;
	ourargc = argc - ((auth == -1)?0:1);
	
	if (auth == -1) auth = 1;

	for (;argc--;argv++) {
		if ((*argv)[0] == '-') continue;
		(void) strcpy(user,*argv);
		if (!index(user,'@')) {
			(void) strcat(user,"@");
			(void) strcat(user,ZGetRealm());
		} 
		if ((retval = ZNewLocateUser(user,&numlocs,
					     (auth?ZAUTH:ZNOAUTH)))
		    != ZERR_NONE) {
			(void) sprintf(bfr,"while locating user %s",user);
			com_err(whoami,retval,bfr);
			continue;
		}
		if (ourargc > 1)
			printf("\t%s:\n",user);
		if (!numlocs) {
			printf("Hidden or not logged-in\n");
			if (argc)
				printf("\n");
			continue;
		}
		for (i=0;i<numlocs;i++) {
			if ((retval = ZGetLocations(&locations,&one))
			    != ZERR_NONE) {
				com_err(whoami,retval,
					"while getting location");
				continue;
			}
			if (one != 1) {
				printf("%s: internal failure while getting location\n",whoami);
				exit(1);
			}
			/* just use printf; make the field widths one
			 * smaller to deal with the extra separation space.
			 */
			printf("%-*s %-*s %s\n",
			       42, locations.host,
			       7, locations.tty,
			       locations.time);
			found++;
		}
		if (argc)
			printf("\n");
		(void) ZFlushLocations();
	}
	if (!found)
	    exit(1);
	exit(0);
}