Mercurial > pidgin
comparison src/util.c @ 4160:fcc6db86f5c7
[gaim-migrate @ 4389]
Nicola's Lichtmaier (niqueco) writes:
"Several things:
I've added some "const"s here and there.
I've declared some functions as "pure" or "const"
(helps gcc to optimize the code).
I've declared debug_printf so that gcc can check the
printf format string (this makes visible several
formatting bugs!)
I've changed minor things in util.c (why having an
useless extra call to g_strncasecmp?).
And I've added some _() as usual.
I hope some of this gets checked in, if not all."
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Tue, 31 Dec 2002 18:19:14 +0000 |
parents | a611ec77d1d2 |
children | d3c8d2b40494 |
comparison
equal
deleted
inserted
replaced
4159:3f267c74e50a | 4160:fcc6db86f5c7 |
---|---|
65 date = ctime(&tme); | 65 date = ctime(&tme); |
66 date[strlen(date) - 1] = '\0'; | 66 date[strlen(date) - 1] = '\0'; |
67 return date; | 67 return date; |
68 } | 68 } |
69 | 69 |
70 gint badchar(char c) | 70 G_GNUC_CONST static gint badchar(char c) |
71 { | 71 { |
72 switch (c) { | 72 switch (c) { |
73 case ' ': | 73 case ' ': |
74 case ',': | 74 case ',': |
75 case '(': | 75 case '(': |
153 text[cnt++] = *c; | 153 text[cnt++] = *c; |
154 c++; | 154 c++; |
155 if (!(*c)) | 155 if (!(*c)) |
156 break; | 156 break; |
157 } | 157 } |
158 } else if ((!g_strncasecmp(c, "http://", 7) || (!g_strncasecmp(c, "https://", 8)))) { | 158 } else if ((*c=='h') && (!g_strncasecmp(c, "http://", 7) || (!g_strncasecmp(c, "https://", 8)))) { |
159 t = c; | 159 t = c; |
160 while (1) { | 160 while (1) { |
161 if (badchar(*t)) { | 161 if (badchar(*t)) { |
162 | 162 |
163 if (*(t) == ',' && (*(t + 1) != ' ')) { | 163 if (*(t) == ',' && (*(t + 1) != ' ')) { |
178 break; | 178 break; |
179 t++; | 179 t++; |
180 | 180 |
181 } | 181 } |
182 } else if (!g_strncasecmp(c, "www.", 4)) { | 182 } else if (!g_strncasecmp(c, "www.", 4)) { |
183 if (g_strncasecmp(c, "www..", 5)) { | 183 if (c[4] != '.') { |
184 t = c; | 184 t = c; |
185 while (1) { | 185 while (1) { |
186 if (badchar(*t)) { | 186 if (badchar(*t)) { |
187 if (t - c == 4) { | 187 if (t - c == 4) { |
188 break; | 188 break; |
225 break; | 225 break; |
226 t++; | 226 t++; |
227 | 227 |
228 } | 228 } |
229 } else if (!g_strncasecmp(c, "ftp.", 4)) { | 229 } else if (!g_strncasecmp(c, "ftp.", 4)) { |
230 if (g_strncasecmp(c, "ftp..", 5)) { | 230 if (c[4] != '.') { |
231 t = c; | 231 t = c; |
232 while (1) { | 232 while (1) { |
233 if (badchar(*t)) { | 233 if (badchar(*t)) { |
234 if (t - c == 4) { | 234 if (t - c == 4) { |
235 break; | 235 break; |
265 if (!t) | 265 if (!t) |
266 break; | 266 break; |
267 t++; | 267 t++; |
268 | 268 |
269 } | 269 } |
270 } else if (c != cpy && !g_strncasecmp(c, "@", 1)) { | 270 } else if (c != cpy && (*c == '@')) { |
271 char *tmp; | 271 char *tmp; |
272 int flag; | 272 int flag; |
273 int len = 0; | 273 int len = 0; |
274 char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\0"; | 274 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0"; |
275 url_buf[0] = 0; | 275 url_buf[0] = 0; |
276 | 276 |
277 if (*(c - 1) == ' ' || *(c + 1) == ' ' || rindex(illegal_chars, *(c + 1)) | 277 if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1))) |
278 || *(c + 1) == 13 || *(c + 1) == 10) | |
279 flag = 0; | 278 flag = 0; |
280 else | 279 else |
281 flag = 1; | 280 flag = 1; |
282 | 281 |
283 t = c; | 282 t = c; |
341 g_free(cpy); | 340 g_free(cpy); |
342 return cnt; | 341 return cnt; |
343 } | 342 } |
344 | 343 |
345 | 344 |
346 FILE *open_gaim_log_file(char *name, int *flag) | 345 FILE *open_gaim_log_file(const char *name, int *flag) |
347 { | 346 { |
348 char *buf; | 347 char *buf; |
349 char *buf2; | 348 char *buf2; |
350 char log_all_file[256]; | 349 char log_all_file[256]; |
351 struct stat st; | 350 struct stat st; |
370 fd = fopen(log_all_file, "r"); | 369 fd = fopen(log_all_file, "r"); |
371 | 370 |
372 if (!fd) { | 371 if (!fd) { |
373 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); | 372 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); |
374 if (res < 0) { | 373 if (res < 0) { |
375 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", | 374 g_snprintf(buf, BUF_LONG, _("Unable to make directory %s for logging"), |
376 log_all_file); | 375 log_all_file); |
377 do_error_dialog(buf, NULL, GAIM_ERROR); | 376 do_error_dialog(buf, NULL, GAIM_ERROR); |
378 g_free(buf); | 377 g_free(buf); |
379 g_free(buf2); | 378 g_free(buf2); |
380 return NULL; | 379 return NULL; |
391 | 390 |
392 fd = fopen(log_all_file, "r"); | 391 fd = fopen(log_all_file, "r"); |
393 if (!fd) { | 392 if (!fd) { |
394 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); | 393 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); |
395 if (res < 0) { | 394 if (res < 0) { |
396 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", | 395 g_snprintf(buf, BUF_LONG, _("Unable to make directory %s for logging"), |
397 log_all_file); | 396 log_all_file); |
398 do_error_dialog(buf, NULL, GAIM_ERROR); | 397 do_error_dialog(buf, NULL, GAIM_ERROR); |
399 g_free(buf); | 398 g_free(buf); |
400 g_free(buf2); | 399 g_free(buf2); |
401 return NULL; | 400 return NULL; |
404 fclose(fd); | 403 fclose(fd); |
405 #else /* _WIN32 */ | 404 #else /* _WIN32 */ |
406 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs", gaim_dir); | 405 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs", gaim_dir); |
407 | 406 |
408 if( _mkdir(log_all_file) < 0 && errno != EEXIST ) { | 407 if( _mkdir(log_all_file) < 0 && errno != EEXIST ) { |
409 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", log_all_file); | 408 g_snprintf(buf, BUF_LONG, _("Unable to make directory %s for logging"), log_all_file); |
410 do_error_dialog(buf, NULL, GAIM_ERROR); | 409 do_error_dialog(buf, NULL, GAIM_ERROR); |
411 g_free(buf); | 410 g_free(buf); |
412 g_free(buf2); | 411 g_free(buf2); |
413 return NULL; | 412 return NULL; |
414 } | 413 } |
425 g_free(buf); | 424 g_free(buf); |
426 g_free(buf2); | 425 g_free(buf2); |
427 return fd; | 426 return fd; |
428 } | 427 } |
429 | 428 |
430 FILE *open_log_file(char *name, int is_chat) | 429 FILE *open_log_file(const char *name, int is_chat) |
431 { | 430 { |
432 struct stat st; | 431 struct stat st; |
433 char realname[256]; | 432 char realname[256]; |
434 struct log_conversation *l; | 433 struct log_conversation *l; |
435 FILE *fd; | 434 FILE *fd; |
448 | 447 |
449 fd = fopen(l->filename, "a"); | 448 fd = fopen(l->filename, "a"); |
450 | 449 |
451 if (flag) { /* is a new file */ | 450 if (flag) { /* is a new file */ |
452 if (logging_options & OPT_LOG_STRIP_HTML) { | 451 if (logging_options & OPT_LOG_STRIP_HTML) { |
453 fprintf(fd, "IM Sessions with %s\n", name); | 452 fprintf(fd, _("IM Sessions with %s\n"), name); |
454 } else { | 453 } else { |
455 fprintf(fd, "<HTML><HEAD><TITLE>"); | 454 fprintf(fd, "<HTML><HEAD><TITLE>"); |
456 fprintf(fd, "IM Sessions with %s", name); | 455 fprintf(fd, _("IM Sessions with %s"), name); |
457 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); | 456 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); |
458 } | 457 } |
459 } | 458 } |
460 | 459 |
461 return fd; | 460 return fd; |
464 g_snprintf(realname, sizeof(realname), "%s.log", normalize(name)); | 463 g_snprintf(realname, sizeof(realname), "%s.log", normalize(name)); |
465 fd = open_gaim_log_file(realname, &flag); | 464 fd = open_gaim_log_file(realname, &flag); |
466 | 465 |
467 if (fd && flag) { /* is a new file */ | 466 if (fd && flag) { /* is a new file */ |
468 if (logging_options & OPT_LOG_STRIP_HTML) { | 467 if (logging_options & OPT_LOG_STRIP_HTML) { |
469 fprintf(fd, "IM Sessions with %s\n", name); | 468 fprintf(fd, _("IM Sessions with %s\n"), name); |
470 } else { | 469 } else { |
471 fprintf(fd, "<HTML><HEAD><TITLE>"); | 470 fprintf(fd, "<HTML><HEAD><TITLE>"); |
472 fprintf(fd, "IM Sessions with %s", name); | 471 fprintf(fd, _("IM Sessions with %s"), name); |
473 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); | 472 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); |
474 } | 473 } |
475 } | 474 } |
476 | 475 |
477 return fd; | 476 return fd; |
485 return open_log_file(name, 2); | 484 return open_log_file(name, 2); |
486 else | 485 else |
487 return open_gaim_log_file("system", &x); | 486 return open_gaim_log_file("system", &x); |
488 } | 487 } |
489 | 488 |
490 char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789+/"; | 489 const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789+/"; |
491 | 490 |
492 /* XXX Find bug */ | 491 /* XXX Find bug */ |
493 char *tobase64(const char *text) | 492 char *tobase64(const char *text) |
494 { | 493 { |
495 char *out = NULL; | 494 char *out = NULL; |
882 | 881 |
883 /* returns a string of the form ~/.gaim, where ~ is replaced by the user's home | 882 /* returns a string of the form ~/.gaim, where ~ is replaced by the user's home |
884 * dir. Note that there is no trailing slash after .gaim. */ | 883 * dir. Note that there is no trailing slash after .gaim. */ |
885 gchar *gaim_user_dir() | 884 gchar *gaim_user_dir() |
886 { | 885 { |
887 if(gaim_home_dir()) { | 886 const gchar *hd = gaim_home_dir(); |
888 strcpy( (char*)&home_dir, gaim_home_dir() ); | 887 if(hd) { |
888 strcpy( (char*)&home_dir, hd ); | |
889 strcat( (char*)&home_dir, G_DIR_SEPARATOR_S ".gaim" ); | 889 strcat( (char*)&home_dir, G_DIR_SEPARATOR_S ".gaim" ); |
890 return (gchar*)&home_dir; | 890 return (gchar*)&home_dir; |
891 } | 891 } |
892 else { | 892 else { |
893 return NULL; | 893 return NULL; |
1217 if( result == NULL ) | 1217 if( result == NULL ) |
1218 debug_printf("gaim_mkstemp: Problem creating the template\n"); | 1218 debug_printf("gaim_mkstemp: Problem creating the template\n"); |
1219 else | 1219 else |
1220 { | 1220 { |
1221 if( (fp = fopen( result, "w+" )) == NULL ) { | 1221 if( (fp = fopen( result, "w+" )) == NULL ) { |
1222 debug_printf("Error: Couldn't fopen()in gaim_mkstemp():\n%s\n", result); | 1222 debug_printf("Error: Couldn't fopen() in gaim_mkstemp():\n%s\n", result); |
1223 } | 1223 } |
1224 } | 1224 } |
1225 #else | 1225 #else |
1226 if((fd = mkstemp(*fpath)) == -1) { | 1226 if((fd = mkstemp(*fpath)) == -1) { |
1227 debug_printf("Error: Couldn't make \"%s\", error: %d\n", *fpath, errno); | 1227 debug_printf("Error: Couldn't make \"%s\", error: %d\n", *fpath, errno); |
1261 } | 1261 } |
1262 conn = conn->next; | 1262 conn = conn->next; |
1263 } | 1263 } |
1264 | 1264 |
1265 if (gc == NULL) | 1265 if (gc == NULL) |
1266 return "Not connected to AIM"; | 1266 return _("Not connected to AIM"); |
1267 | 1267 |
1268 /* aim:goim?screenname=screenname&message=message */ | 1268 /* aim:goim?screenname=screenname&message=message */ |
1269 if (!g_strncasecmp(uri, "aim:goim?", strlen("aim:goim?"))) { | 1269 if (!g_strncasecmp(uri, "aim:goim?", strlen("aim:goim?"))) { |
1270 char *who, *what; | 1270 char *who, *what; |
1271 struct conversation *c; | 1271 struct conversation *c; |
1272 uri = uri + strlen("aim:goim?"); | 1272 uri = uri + strlen("aim:goim?"); |
1273 | 1273 |
1274 if (!(who = strstr(uri, "screenname="))) { | 1274 if (!(who = strstr(uri, "screenname="))) { |
1275 return "No screenname given."; | 1275 return _("No screenname given."); |
1276 } | 1276 } |
1277 /* spaces are encoded as +'s */ | 1277 /* spaces are encoded as +'s */ |
1278 who = who + strlen("screenname="); | 1278 who = who + strlen("screenname="); |
1279 str = g_string_new(NULL); | 1279 str = g_string_new(NULL); |
1280 while (*who && (*who != '&')) { | 1280 while (*who && (*who != '&')) { |
1307 char *who, *group; | 1307 char *who, *group; |
1308 uri = uri + strlen("aim:addbuddy?"); | 1308 uri = uri + strlen("aim:addbuddy?"); |
1309 /* spaces are encoded as +'s */ | 1309 /* spaces are encoded as +'s */ |
1310 | 1310 |
1311 if (!(who = strstr(uri, "screenname="))) { | 1311 if (!(who = strstr(uri, "screenname="))) { |
1312 return "No screenname given."; | 1312 return _("No screenname given."); |
1313 } | 1313 } |
1314 who = who + strlen("screenname="); | 1314 who = who + strlen("screenname="); |
1315 str = g_string_new(NULL); | 1315 str = g_string_new(NULL); |
1316 while (*who && (*who != '&')) { | 1316 while (*who && (*who != '&')) { |
1317 g_string_append_c(str, *who == '+' ? ' ' : *who); | 1317 g_string_append_c(str, *who == '+' ? ' ' : *who); |
1343 | 1343 |
1344 uri = uri + strlen("aim:gochat?"); | 1344 uri = uri + strlen("aim:gochat?"); |
1345 /* spaces are encoded as +'s */ | 1345 /* spaces are encoded as +'s */ |
1346 | 1346 |
1347 if (!(room = strstr(uri, "roomname="))) { | 1347 if (!(room = strstr(uri, "roomname="))) { |
1348 return "No roomname given."; | 1348 return _("No roomname given."); |
1349 } | 1349 } |
1350 room = room + strlen("roomname="); | 1350 room = room + strlen("roomname="); |
1351 str = g_string_new(NULL); | 1351 str = g_string_new(NULL); |
1352 while (*room && (*room != '&')) { | 1352 while (*room && (*room != '&')) { |
1353 g_string_append_c(str, *room == '+' ? ' ' : *room); | 1353 g_string_append_c(str, *room == '+' ? ' ' : *room); |
1359 chat = g_list_append(chat, &exch); | 1359 chat = g_list_append(chat, &exch); |
1360 serv_join_chat(gc, chat); | 1360 serv_join_chat(gc, chat); |
1361 g_free(room); | 1361 g_free(room); |
1362 g_list_free(chat); | 1362 g_list_free(chat); |
1363 } else { | 1363 } else { |
1364 return "Invalid AIM URI"; | 1364 return _("Invalid AIM URI"); |
1365 } | 1365 } |
1366 | 1366 |
1367 | 1367 |
1368 return NULL; | 1368 return NULL; |
1369 } | 1369 } |