summaryrefslogtreecommitdiff
path: root/lib/ZLocateU.c
blob: 57af9a56ad00ab766b81e3ca6ea8f8dec6070140 (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
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains source for the ZLocateUser function.
 *
 *	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". 
 */
/* $Header$ */

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

#include <zephyr/mit-copyright.h>

#include <zephyr/zephyr_internal.h>

Code_t ZLocateUser(user, nlocs)
    char *user;
    int *nlocs;
{
    int i, retval;
    ZNotice_t notice, retnotice;
    char *ptr, *end;
	
    retval = ZFlushLocations();

    if (retval != ZERR_NONE && retval != ZERR_NOLOCATIONS)
	return (retval);
	
    notice.z_kind = ACKED;
    notice.z_port = 0;
    notice.z_class = LOCATE_CLASS;
    notice.z_class_inst = user;
    notice.z_opcode = LOCATE_LOCATE;
    notice.z_sender = 0;
    notice.z_recipient = "";
    notice.z_default_format = "";
    notice.z_message_len = 0;

    if ((retval = ZSendNotice(&notice, ZAUTH)) != ZERR_NONE)
	return (retval);

    if ((retval = ZIfNotice(&retnotice, NULL, ZCompareUIDPred,
			    (char *)&notice.z_uid)) != ZERR_NONE)
	return (retval);

    if (retnotice.z_kind == SERVNAK) {
	ZFreeNotice(&retnotice);
	return (ZERR_SERVNAK);
    }
	
    if (retnotice.z_kind != SERVACK) {
	ZFreeNotice(&retnotice);
	return (ZERR_INTERNAL);
    }

    end = retnotice.z_message+retnotice.z_message_len;

    __locate_num = 0;
	
    for (ptr=retnotice.z_message;ptr<end;ptr++)
	if (!*ptr)
	    __locate_num++;

    __locate_num /= 3;

    __locate_list = (ZLocations_t *)malloc((unsigned)__locate_num*
					   sizeof(ZLocations_t));
    if (!__locate_list)
	return (ENOMEM);
	
    for (ptr=retnotice.z_message, i=0;i<__locate_num;i++) {
	__locate_list[i].host = (char *)malloc((unsigned)strlen(ptr)+
					       1);
	if (!__locate_list[i].host)
	    return (ENOMEM);
	(void) strcpy(__locate_list[i].host, ptr);
	ptr += strlen(ptr)+1;
	__locate_list[i].time = (char *)malloc((unsigned)strlen(ptr)+
					       1);
	if (!__locate_list[i].time)
	    return (ENOMEM);
	(void) strcpy(__locate_list[i].time, ptr);
	ptr += strlen(ptr)+1;
	__locate_list[i].tty = (char *)malloc((unsigned)strlen(ptr)+
					      1);
	if (!__locate_list[i].tty)
	    return (ENOMEM);
	(void) strcpy(__locate_list[i].tty, ptr);
	ptr += strlen(ptr)+1;
    }

    ZFreeNotice(&retnotice);
    
    __locate_next = 0;
    *nlocs = __locate_num;
	
    return (ZERR_NONE);
}