Mon, 22 Aug 2011 02:07:41 +0000
Don't use strlen() when all you're trying to do is check if the string
is empty
| 2086 | 1 | /* This file is part of the Project Athena Zephyr Notification System. |
| 2 | * It contains source for asynchronous location functions. | |
| 3 | * | |
| 4 | * Created by: Marc Horowitz | |
| 5 | * | |
| 6 | * Copyright (c) 1990,1991 by the Massachusetts Institute of Technology. | |
| 7 | * For copying and distribution information, see the file | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
15435
diff
changeset
|
8 | * "mit-copyright.h". |
| 2086 | 9 | */ |
| 10 | ||
|
8791
655f64e6d1e2
[gaim-migrate @ 9553]
Christian Hammond <chipx86@chipx86.com>
parents:
7475
diff
changeset
|
11 | #include "internal.h" |
| 2086 | 12 | |
| 13 | Code_t ZRequestLocations(user, zald, kind, auth) | |
| 7261 | 14 | const char *user; |
| 10867 | 15 | ZAsyncLocateData_t *zald; |
| 2086 | 16 | ZNotice_Kind_t kind; /* UNSAFE, UNACKED, or ACKED */ |
| 17 | Z_AuthProc auth; | |
| 18 | { | |
| 19 | int retval; | |
| 20 | ZNotice_t notice; | |
|
31955
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
21 | size_t userlen, versionlen; |
| 2086 | 22 | |
| 23 | if (ZGetFD() < 0) | |
|
7475
987384816492
[gaim-migrate @ 8088]
Mark Doliner <markdoliner@pidgin.im>
parents:
7261
diff
changeset
|
24 | if ((retval = ZOpenPort((unsigned short *)0)) != ZERR_NONE) |
| 2086 | 25 | return (retval); |
| 26 | ||
| 27 | (void) memset((char *)¬ice, 0, sizeof(notice)); | |
| 28 | notice.z_kind = kind; | |
| 29 | notice.z_port = __Zephyr_port; | |
| 30 | notice.z_class = LOCATE_CLASS; | |
| 31 | notice.z_class_inst = user; | |
| 32 | notice.z_opcode = LOCATE_LOCATE; | |
| 33 | notice.z_sender = 0; | |
| 34 | notice.z_recipient = ""; | |
| 35 | notice.z_default_format = ""; | |
| 36 | notice.z_message_len = 0; | |
| 37 | ||
| 38 | if ((retval = ZSendNotice(¬ice, auth)) != ZERR_NONE) | |
| 39 | return(retval); | |
| 40 | ||
|
31955
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
41 | userlen = strlen(user) + 1; |
|
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
42 | versionlen = strlen(notice.z_version) + 1; |
|
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
43 | if ((zald->user = (char *) malloc(userlen)) == NULL) { |
| 2086 | 44 | return(ENOMEM); |
| 45 | } | |
|
31955
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
46 | if ((zald->version = (char *) malloc(versionlen)) == NULL) { |
| 2086 | 47 | free(zald->user); |
| 48 | return(ENOMEM); | |
| 49 | } | |
| 50 | zald->uid = notice.z_multiuid; | |
|
31955
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
51 | g_strlcpy(zald->user,user,userlen); |
|
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
52 | g_strlcpy(zald->version,notice.z_version,versionlen); |
| 2086 | 53 | |
| 54 | return(ZERR_NONE); | |
| 55 | } | |
| 56 | ||
| 57 | Code_t ZParseLocations(notice,zald,nlocs,user) | |
| 10867 | 58 | ZNotice_t *notice; |
| 59 | ZAsyncLocateData_t *zald; | |
| 2086 | 60 | int *nlocs; |
| 61 | char **user; | |
| 62 | { | |
| 63 | char *ptr, *end; | |
| 64 | int i; | |
| 65 | ||
| 66 | ZFlushLocations(); /* This never fails (this function is part of the | |
| 67 | library, so it is allowed to know this). */ | |
| 68 | ||
| 69 | /* non-matching protocol version numbers means the | |
| 70 | server is probably an older version--must punt */ | |
| 71 | ||
| 72 | if (zald && strcmp(notice->z_version, zald->version)) | |
| 73 | return(ZERR_VERS); | |
| 74 | ||
| 75 | if (notice->z_kind == SERVNAK) | |
| 76 | return (ZERR_SERVNAK); | |
| 77 | ||
| 78 | /* flag ACKs as special */ | |
| 79 | if (notice->z_kind == SERVACK && | |
| 80 | !strcmp(notice->z_opcode, LOCATE_LOCATE)) { | |
| 81 | *nlocs = -1; | |
| 82 | return(ZERR_NONE); | |
| 83 | } | |
| 84 | ||
| 85 | if (notice->z_kind != ACKED) | |
| 86 | return (ZERR_INTERNAL); | |
| 87 | ||
| 88 | end = notice->z_message+notice->z_message_len; | |
| 89 | ||
| 90 | __locate_num = 0; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
15435
diff
changeset
|
91 | |
| 2086 | 92 | for (ptr=notice->z_message;ptr<end;ptr++) |
| 93 | if (!*ptr) | |
| 94 | __locate_num++; | |
| 95 | ||
| 96 | __locate_num /= 3; | |
| 97 | ||
| 98 | if (__locate_num) | |
| 99 | { | |
| 100 | __locate_list = (ZLocations_t *)malloc((unsigned)__locate_num* | |
| 101 | sizeof(ZLocations_t)); | |
| 102 | if (!__locate_list) | |
| 103 | return (ENOMEM); | |
| 104 | } else { | |
| 105 | __locate_list = 0; | |
| 106 | } | |
| 107 | ||
| 108 | for (ptr=notice->z_message, i=0; i<__locate_num; i++) { | |
| 109 | unsigned int len; | |
| 110 | ||
| 111 | len = strlen (ptr) + 1; | |
| 112 | __locate_list[i].host = (char *) malloc(len); | |
| 113 | if (!__locate_list[i].host) | |
| 114 | return (ENOMEM); | |
|
31954
e5631d5590cc
Replace numerous strcpy() invocations with strlcpy() in Zephyr.
Ethan Blanton <elb@pidgin.im>
parents:
31294
diff
changeset
|
115 | g_strlcpy(__locate_list[i].host, ptr,len); |
| 2086 | 116 | ptr += len; |
| 117 | ||
| 118 | len = strlen (ptr) + 1; | |
| 119 | __locate_list[i].time = (char *) malloc(len); | |
| 120 | if (!__locate_list[i].time) | |
| 121 | return (ENOMEM); | |
|
31954
e5631d5590cc
Replace numerous strcpy() invocations with strlcpy() in Zephyr.
Ethan Blanton <elb@pidgin.im>
parents:
31294
diff
changeset
|
122 | g_strlcpy(__locate_list[i].time, ptr,len); |
| 2086 | 123 | ptr += len; |
| 124 | ||
| 125 | len = strlen (ptr) + 1; | |
| 126 | __locate_list[i].tty = (char *) malloc(len); | |
| 127 | if (!__locate_list[i].tty) | |
| 128 | return (ENOMEM); | |
|
31954
e5631d5590cc
Replace numerous strcpy() invocations with strlcpy() in Zephyr.
Ethan Blanton <elb@pidgin.im>
parents:
31294
diff
changeset
|
129 | g_strlcpy(__locate_list[i].tty, ptr,len); |
| 2086 | 130 | ptr += len; |
| 131 | } | |
| 132 | ||
| 133 | __locate_next = 0; | |
| 134 | *nlocs = __locate_num; | |
| 135 | if (user) { | |
|
31955
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
136 | size_t len; |
| 2086 | 137 | if (zald) { |
|
31955
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
138 | len = strlen(zald->user) + 1; |
|
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
139 | if ((*user = (char *) malloc(len)) == NULL) |
| 2086 | 140 | return(ENOMEM); |
|
31955
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
141 | g_strlcpy(*user,zald->user,len); |
| 2086 | 142 | } else { |
|
31955
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
143 | len = strlen(notice->z_class_inst) + 1; |
|
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
144 | if ((*user = (char *) malloc(len)) == NULL) |
| 2086 | 145 | return(ENOMEM); |
|
31955
384153346831
Fix up Zephyr g_strlcpy patch
Ethan Blanton <elb@pidgin.im>
parents:
31954
diff
changeset
|
146 | g_strlcpy(*user,notice->z_class_inst,len); |
| 2086 | 147 | } |
| 148 | } | |
| 149 | return (ZERR_NONE); | |
| 150 | } | |
| 151 | ||
| 152 | int ZCompareALDPred(notice, zald) | |
| 153 | ZNotice_t *notice; | |
| 154 | void *zald; | |
| 155 | { | |
| 156 | return(ZCompareUID(&(notice->z_multiuid), | |
| 157 | &(((ZAsyncLocateData_t *) zald)->uid))); | |
| 158 | } | |
| 159 | ||
| 160 | void ZFreeALD(zald) | |
| 10867 | 161 | ZAsyncLocateData_t *zald; |
| 2086 | 162 | { |
| 163 | if (!zald) return; | |
| 164 | ||
| 165 | if (zald->user) free(zald->user); | |
| 166 | if (zald->version) free(zald->version); | |
| 167 | (void) memset(zald, 0, sizeof(*zald)); | |
| 168 | } |