Wed, 16 Oct 2002 19:57:03 +0000
[gaim-migrate @ 3850]
Warning fixes and WIN32 ifdef removals
|
3717
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
1 | /* $Id: common.c 3850 2002-10-16 19:57:03Z hermanator $ */ |
| 2846 | 2 | |
| 3 | /* | |
| 4 | * (C) Copyright 2001 Wojtek Kaniewski <wojtekka@irc.pl>, | |
| 5 | * Robert J. Woźny <speedy@ziew.org> | |
| 6 | * | |
| 7 | * This program is free software; you can redistribute it and/or modify | |
| 8 | * it under the terms of the GNU General Public License Version 2 as | |
| 9 | * published by the Free Software Foundation. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 19 | */ | |
| 20 | ||
| 21 | #include <stdio.h> | |
| 22 | #include <stdlib.h> | |
| 3630 | 23 | #ifndef _WIN32 |
| 2846 | 24 | #include <unistd.h> |
| 25 | #include <sys/socket.h> | |
| 26 | #include <netinet/in.h> | |
| 27 | #include <arpa/inet.h> | |
| 3630 | 28 | #include <netdb.h> |
| 2846 | 29 | #include <sys/ioctl.h> |
| 3630 | 30 | #include <pwd.h> |
| 2846 | 31 | #include <sys/wait.h> |
| 3630 | 32 | #endif |
| 2846 | 33 | #include <sys/time.h> |
| 34 | #include <errno.h> | |
| 35 | #ifndef _AIX | |
| 36 | # include <string.h> | |
| 37 | #endif | |
| 38 | #include <stdarg.h> | |
| 39 | #include <time.h> | |
| 40 | #ifdef sun | |
| 41 | #include <sys/filio.h> | |
| 42 | #endif | |
| 43 | #include "libgg.h" | |
| 44 | #include "config.h" | |
| 3466 | 45 | #include <glib.h> |
| 2846 | 46 | |
|
3717
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
47 | #ifdef _WIN32 |
|
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
48 | #include "win32dep.h" |
|
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
49 | #endif |
|
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
50 | |
| 2846 | 51 | /* |
| 52 | * gg_debug() | |
| 53 | * | |
| 54 | * wyrzuca komunikat o danym poziomie, o ile użytkownik sobie tego życzy. | |
| 55 | * | |
| 56 | * - level - poziom wiadomości, | |
| 57 | * - format... - treść wiadomości (printf-alike.) | |
| 58 | * | |
| 59 | * niczego nie zwraca. | |
| 60 | */ | |
| 61 | void gg_debug(int level, char *format, ...) | |
| 62 | { | |
| 63 | va_list ap; | |
| 64 | ||
| 65 | if ((gg_debug_level & level)) { | |
| 66 | va_start(ap, format); | |
| 67 | vprintf(format, ap); | |
| 68 | va_end(ap); | |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | /* | |
| 73 | * gg_alloc_sprintf() | |
| 74 | * | |
| 75 | * robi dokładnie to samo, co sprintf(), tyle że alokuje sobie wcześniej | |
| 76 | * miejsce na dane. powinno działać na tych maszynach, które mają funkcję | |
| 77 | * vsnprintf() zgodną z C99, jak i na wcześniejszych. | |
| 78 | * | |
| 79 | * - format, ... - parametry takie same jak w innych funkcjach *printf() | |
| 80 | * | |
| 81 | * zwraca zaalokowany buforek, który wypadałoby później zwolnić, lub NULL | |
| 82 | * jeśli nie udało się wykonać zadania. | |
| 83 | */ | |
| 84 | char *gg_alloc_sprintf(char *format, ...) | |
| 85 | { | |
| 86 | va_list ap; | |
| 87 | char *buf = NULL, *tmp; | |
| 88 | int size = 0, res; | |
| 89 | ||
| 90 | va_start(ap, format); | |
| 91 | ||
| 3630 | 92 | if ((size = g_vsnprintf(buf, 0, format, ap)) < 1) { |
| 2846 | 93 | size = 128; |
| 94 | do { | |
| 95 | size *= 2; | |
| 96 | if (!(tmp = realloc(buf, size))) { | |
| 97 | free(buf); | |
| 98 | return NULL; | |
| 99 | } | |
| 100 | buf = tmp; | |
| 3630 | 101 | res = g_vsnprintf(buf, size, format, ap); |
| 2846 | 102 | } while (res == size - 1); |
| 103 | } else { | |
| 104 | if (!(buf = malloc(size + 1))) | |
| 105 | return NULL; | |
| 106 | } | |
| 107 | ||
| 3630 | 108 | g_vsnprintf(buf, size + 1, format, ap); |
| 2846 | 109 | |
| 110 | va_end(ap); | |
| 111 | ||
| 112 | return buf; | |
| 113 | } | |
| 114 | ||
| 115 | /* | |
| 116 | * gg_get_line() | |
| 117 | * | |
| 118 | * podaje kolejną linię z bufora tekstowego. psuje co bezpowrotnie, dzieląc | |
| 119 | * na kolejne stringi. zdarza się, nie ma potrzeby pisania funkcji dublującej | |
| 120 | * bufor żeby tylko mieć nieruszone dane wejściowe, skoro i tak nie będą nam | |
| 121 | * poźniej potrzebne. obcina `\r\n'. | |
| 122 | * | |
| 123 | * - ptr - wskaźnik do zmiennej, która przechowuje aktualną pozycję | |
| 124 | * w przemiatanym buforze. | |
| 125 | * | |
| 126 | * wskaźnik do kolejnej linii tekstu lub NULL, jeśli to już koniec bufora. | |
| 127 | */ | |
| 128 | char *gg_get_line(char **ptr) | |
| 129 | { | |
| 130 | char *foo, *res; | |
| 131 | ||
| 132 | if (!ptr || !*ptr || !strcmp(*ptr, "")) | |
| 133 | return NULL; | |
| 134 | ||
| 135 | res = *ptr; | |
| 136 | ||
| 137 | if (!(foo = strchr(*ptr, '\n'))) | |
| 138 | *ptr += strlen(*ptr); | |
| 139 | else { | |
| 140 | *ptr = foo + 1; | |
| 141 | *foo = 0; | |
| 142 | if (res[strlen(res) - 1] == '\r') | |
| 143 | res[strlen(res) - 1] = 0; | |
| 144 | } | |
| 145 | ||
| 146 | return res; | |
| 147 | } | |
| 148 | ||
| 149 | /* | |
| 150 | * gg_connect() | |
| 151 | * | |
| 152 | * łączy się z serwerem. pierwszy argument jest typu (void *), żeby nie | |
| 153 | * musieć niczego inkludować w libgg.h i nie psuć jakiś głupich zależności | |
| 154 | * na dziwnych systemach. | |
| 155 | * | |
| 156 | * - addr - adres serwera (struct in_addr *), | |
| 157 | * - port - port serwera, | |
| 158 | * - async - ma być asynchroniczne połączenie? | |
| 159 | * | |
| 160 | * zwraca połączonego socketa lub -1 w przypadku błędu. zobacz errno. | |
| 161 | */ | |
| 162 | int gg_connect(void *addr, int port, int async) | |
| 163 | { | |
| 3630 | 164 | int sock, ret, one = 1; |
| 2846 | 165 | struct sockaddr_in sin; |
| 166 | struct in_addr *a = addr; | |
| 167 | ||
| 168 | gg_debug(GG_DEBUG_FUNCTION, "** gg_connect(%s, %d, %d);\n", inet_ntoa(*a), port, async); | |
| 169 | ||
| 170 | if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { | |
|
3717
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
171 | gg_debug(GG_DEBUG_MISC, "-- socket() failed. errno = %d (%s)\n", errno, strerror(errno)); |
| 2846 | 172 | return -1; |
| 173 | } | |
| 174 | ||
| 175 | if (async) { | |
| 176 | if (ioctl(sock, FIONBIO, &one) == -1) { | |
| 177 | gg_debug(GG_DEBUG_MISC, "-- ioctl() failed. errno = %d (%s)\n", errno, strerror(errno)); | |
| 178 | return -1; | |
| 179 | } | |
| 180 | } | |
| 181 | ||
| 182 | sin.sin_port = htons(port); | |
| 183 | sin.sin_family = AF_INET; | |
| 184 | sin.sin_addr.s_addr = a->s_addr; | |
| 185 | ||
| 3630 | 186 | if ((ret = connect(sock, (struct sockaddr*) &sin, sizeof(sin))) == -1) { |
| 2846 | 187 | if (errno && (!async || errno != EINPROGRESS)) { |
| 188 | gg_debug(GG_DEBUG_MISC, "-- connect() failed. errno = %d (%s)\n", errno, strerror(errno)); | |
| 189 | return -1; | |
| 190 | } | |
| 191 | gg_debug(GG_DEBUG_MISC, "-- connect() in progress\n"); | |
| 192 | } | |
| 193 | ||
| 194 | return sock; | |
| 195 | } | |
| 196 | ||
| 197 | /* | |
| 198 | * gg_read_line() | |
| 199 | * | |
| 200 | * czyta jedną linię tekstu z socketa. | |
| 201 | * | |
| 202 | * - sock - socket, | |
| 203 | * - buf - wskaźnik bufora, | |
| 204 | * - length - długość bufora. | |
| 205 | * | |
| 206 | * olewa błędy. jeśli na jakiś trafi, potraktuje go jako koniec linii. | |
| 207 | */ | |
| 208 | void gg_read_line(int sock, char *buf, int length) | |
| 209 | { | |
| 210 | int ret; | |
| 211 | ||
| 212 | gg_debug(GG_DEBUG_FUNCTION, "** gg_read_line(...);\n"); | |
| 213 | ||
| 214 | for (; length > 1; buf++, length--) { | |
| 215 | do { | |
| 216 | if ((ret = read(sock, buf, 1)) == -1 && errno != EINTR) { | |
| 217 | *buf = 0; | |
| 218 | return; | |
| 219 | } | |
| 220 | } while (ret == -1 && errno == EINTR); | |
| 221 | ||
| 222 | if (*buf == '\n') { | |
| 223 | buf++; | |
| 224 | break; | |
| 225 | } | |
| 226 | } | |
| 227 | ||
| 228 | *buf = 0; | |
| 229 | return; | |
| 230 | } | |
| 231 | ||
| 232 | /* | |
| 233 | * gg_chomp() | |
| 234 | * | |
| 235 | * ucina "\r\n" lub "\n" z końca linii. | |
| 236 | * | |
| 237 | * - line - ofiara operacji plastycznej. | |
| 238 | * | |
| 239 | * niczego nie zwraca. | |
| 240 | */ | |
| 241 | void gg_chomp(char *line) | |
| 242 | { | |
| 243 | if (!line || strlen(line) < 1) | |
| 244 | return; | |
| 245 | ||
| 246 | if (line[strlen(line) - 1] == '\n') | |
| 247 | line[strlen(line) - 1] = 0; | |
| 248 | if (line[strlen(line) - 1] == '\r') | |
| 249 | line[strlen(line) - 1] = 0; | |
| 250 | } | |
| 251 | ||
| 252 | ||
| 253 | /* | |
| 254 | * gg_urlencode() // funkcja wewnętrzna | |
| 255 | * | |
| 256 | * zamienia podany tekst na ciąg znaków do formularza http. przydaje się | |
| 257 | * przy szukaniu userów z dziwnymi znaczkami. | |
| 258 | * | |
| 259 | * - str - ciąg znaków do poprawki. | |
| 260 | * | |
| 261 | * zwraca zaalokowany bufor, który wypadałoby kiedyś zwolnić albo NULL | |
| 262 | * w przypadku błędu. | |
| 263 | */ | |
| 3466 | 264 | char *gg_urlencode(const char *str) |
| 2846 | 265 | { |
| 3466 | 266 | const char *p, hex[] = "0123456789abcdef"; |
| 267 | char *q, *buf; | |
| 268 | ||
| 2846 | 269 | int size = 0; |
| 270 | ||
| 271 | if (!str) | |
| 3466 | 272 | str = ""; |
| 2846 | 273 | |
| 274 | for (p = str; *p; p++, size++) { | |
| 275 | if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9'))) | |
| 276 | size += 2; | |
| 277 | } | |
| 278 | ||
| 3466 | 279 | buf = g_new(char, size + 1); |
| 2846 | 280 | |
| 281 | for (p = str, q = buf; *p; p++, q++) { | |
| 282 | if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9')) | |
| 283 | *q = *p; | |
| 284 | else { | |
| 285 | *q++ = '%'; | |
| 286 | *q++ = hex[*p >> 4 & 15]; | |
| 287 | *q = hex[*p & 15]; | |
| 288 | } | |
| 289 | } | |
| 290 | ||
| 291 | *q = 0; | |
| 292 | ||
| 293 | return buf; | |
| 294 | } | |
| 295 | ||
| 296 | /* | |
| 297 | * gg_http_hash() | |
| 298 | * | |
| 299 | * funkcja, która liczy hash dla adresu e-mail i hasła. | |
| 300 | * | |
| 301 | * - email - adres email, | |
| 302 | * - password - hasło. | |
| 303 | * | |
| 304 | * zwraca hash wykorzystywany przy rejestracji i wszelkich | |
| 305 | * manipulacjach własnego wpisu w katalogu publicznym. | |
| 306 | */ | |
| 307 | ||
| 3466 | 308 | int gg_http_hash(const unsigned char *email, const unsigned char *password) |
| 2846 | 309 | { |
| 310 | unsigned int a, c; | |
| 311 | int b, i; | |
| 312 | b = (-1); | |
| 313 | ||
| 314 | i = 0; | |
| 315 | while ((c = (int) email[i++]) != 0) { | |
| 316 | a = (c ^ b) + (c << 8); | |
| 317 | b = (a >> 24) | (a << 8); | |
| 318 | } | |
| 319 | ||
| 320 | i = 0; | |
| 321 | while ((c = (int) password[i++]) != 0) { | |
| 322 | a = (c ^ b) + (c << 8); | |
| 323 | b = (a >> 24) | (a << 8); | |
| 324 | } | |
| 325 | ||
| 326 | return (b < 0 ? -b : b); | |
| 327 | } | |
| 328 | ||
| 329 | /* | |
| 330 | * Local variables: | |
| 331 | * c-indentation-style: k&r | |
| 332 | * c-basic-offset: 8 | |
| 333 | * indent-tabs-mode: notnil | |
| 334 | * End: | |
| 335 | * | |
| 336 | * vim: shiftwidth=8: | |
| 337 | */ |