comparison src/util.c @ 3630:9682c0e022c6

[gaim-migrate @ 3753] Yeah this will probably break a lot of shit knowing my luck. But hey, I really don't care what people thnk. committer: Tailor Script <tailor@pidgin.im>
author Rob Flynn <gaim@robflynn.com>
date Fri, 11 Oct 2002 03:14:01 +0000
parents bdd0bebd2d04
children 5e50f6746509
comparison
equal deleted inserted replaced
3629:afc5bb164c5a 3630:9682c0e022c6
20 */ 20 */
21 21
22 #ifdef HAVE_CONFIG_H 22 #ifdef HAVE_CONFIG_H
23 #include <config.h> 23 #include <config.h>
24 #endif 24 #endif
25
26 #ifndef _WIN32
25 #include <unistd.h> 27 #include <unistd.h>
28 #include <sys/time.h>
29 #include <sys/wait.h>
30 #else
31 #include <direct.h>
32 #include <io.h>
33 #endif
34
26 #include <errno.h> 35 #include <errno.h>
27 #include <stdio.h> 36 #include <stdio.h>
28 #include <stdlib.h> 37 #include <stdlib.h>
29 #include <sys/time.h>
30 #include <sys/types.h> 38 #include <sys/types.h>
31 #include <sys/stat.h> 39 #include <sys/stat.h>
32 #include <string.h> 40 #include <string.h>
33 #include <sys/wait.h>
34 #include <ctype.h> 41 #include <ctype.h>
35 #ifdef HAVE_ICONV 42 #ifdef HAVE_ICONV
36 #include <iconv.h> 43 #include <iconv.h>
37 #endif 44 #endif
38 #include <math.h> 45 #include <math.h>
39 #include "gaim.h" 46 #include "gaim.h"
40 #include "prpl.h" 47 #include "prpl.h"
41 #include "gtkspell.h" 48 #include "gtkspell.h"
49
50 #ifdef _WIN32
51 #include "win32dep.h"
52 #endif
53
54 #ifndef MAXPATHLEN
55 #define MAXPATHLEN 1024
56 #endif
57
58 static char home_dir[MAXPATHLEN];
42 59
43 char *full_date() 60 char *full_date()
44 { 61 {
45 char *date; 62 char *date;
46 time_t tme; 63 time_t tme;
332 char *buf; 349 char *buf;
333 char *buf2; 350 char *buf2;
334 char log_all_file[256]; 351 char log_all_file[256];
335 struct stat st; 352 struct stat st;
336 FILE *fd; 353 FILE *fd;
354 #ifndef _WIN32
337 int res; 355 int res;
356 #endif
338 gchar *gaim_dir; 357 gchar *gaim_dir;
339 358
340 buf = g_malloc(BUF_LONG); 359 buf = g_malloc(BUF_LONG);
341 buf2 = g_malloc(BUF_LONG); 360 buf2 = g_malloc(BUF_LONG);
342 gaim_dir = gaim_user_dir(); 361 gaim_dir = gaim_user_dir();
343 362
344 /* Dont log yourself */ 363 /* Dont log yourself */
345 strncpy(log_all_file, gaim_dir, 256); 364 strncpy(log_all_file, gaim_dir, 256);
346 365
366 #ifndef _WIN32
347 stat(log_all_file, &st); 367 stat(log_all_file, &st);
348 if (!S_ISDIR(st.st_mode)) 368 if (!S_ISDIR(st.st_mode))
349 unlink(log_all_file); 369 unlink(log_all_file);
350 370
351 fd = fopen(log_all_file, "r"); 371 fd = fopen(log_all_file, "r");
356 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", 376 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging",
357 log_all_file); 377 log_all_file);
358 do_error_dialog(buf, NULL, GAIM_ERROR); 378 do_error_dialog(buf, NULL, GAIM_ERROR);
359 g_free(buf); 379 g_free(buf);
360 g_free(buf2); 380 g_free(buf2);
361 g_free(gaim_dir);
362 return NULL; 381 return NULL;
363 } 382 }
364 } else 383 } else
365 fclose(fd); 384 fclose(fd);
366 385
367 g_snprintf(log_all_file, 256, "%s/logs", gaim_dir); 386 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs", gaim_dir);
368 387
369 if (stat(log_all_file, &st) < 0) 388 if (stat(log_all_file, &st) < 0)
370 *flag = 1; 389 *flag = 1;
371 if (!S_ISDIR(st.st_mode)) 390 if (!S_ISDIR(st.st_mode))
372 unlink(log_all_file); 391 unlink(log_all_file);
378 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", 397 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging",
379 log_all_file); 398 log_all_file);
380 do_error_dialog(buf, NULL, GAIM_ERROR); 399 do_error_dialog(buf, NULL, GAIM_ERROR);
381 g_free(buf); 400 g_free(buf);
382 g_free(buf2); 401 g_free(buf2);
383 g_free(gaim_dir);
384 return NULL; 402 return NULL;
385 } 403 }
386 } else 404 } else
387 fclose(fd); 405 fclose(fd);
388 406 #else /* _WIN32 */
389 407 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs", gaim_dir);
390 408
391 g_snprintf(log_all_file, 256, "%s/logs/%s", gaim_dir, name); 409 if( _mkdir(log_all_file) < 0 && errno != EEXIST ) {
410 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", log_all_file);
411 do_error_dialog(buf, NULL, GAIM_ERROR);
412 g_free(buf);
413 g_free(buf2);
414 return NULL;
415 }
416 #endif
417
418 g_snprintf(log_all_file, 256, "%s" G_DIR_SEPARATOR_S "logs" G_DIR_SEPARATOR_S "%s", gaim_dir, name);
392 if (stat(log_all_file, &st) < 0) 419 if (stat(log_all_file, &st) < 0)
393 *flag = 1; 420 *flag = 1;
394 421
395 debug_printf("Logging to: \"%s\"\n", log_all_file); 422 debug_printf("Logging to: \"%s\"\n", log_all_file);
396 423
397 fd = fopen(log_all_file, "a"); 424 fd = fopen(log_all_file, "a");
398 425
399 g_free(buf); 426 g_free(buf);
400 g_free(buf2); 427 g_free(buf2);
401 g_free(gaim_dir);
402 return fd; 428 return fd;
403 } 429 }
404 430
405 FILE *open_log_file(char *name, int is_chat) 431 FILE *open_log_file(char *name, int is_chat)
406 { 432 {
618 } 644 }
619 645
620 646
621 void clean_pid(void) 647 void clean_pid(void)
622 { 648 {
649 #ifndef _WIN32
623 int status; 650 int status;
624 pid_t pid, spell_pid; 651 pid_t pid, spell_pid;
625 652
626 while((pid = waitpid(-1, &status, WNOHANG)) > 0) { 653 while((pid = waitpid(-1, &status, WNOHANG)) > 0) {
627 if((spell_pid = gtkspell_running()) != 0 && pid == spell_pid) { 654 if((spell_pid = gtkspell_running()) != 0 && pid == spell_pid) {
631 if(pid < 0 && errno != ECHILD) { 658 if(pid < 0 && errno != ECHILD) {
632 char errmsg[BUFSIZ]; 659 char errmsg[BUFSIZ];
633 sprintf(errmsg, "Warning: waitpid() returned %d", pid); 660 sprintf(errmsg, "Warning: waitpid() returned %d", pid);
634 perror(errmsg); 661 perror(errmsg);
635 } 662 }
663 #endif
636 } 664 }
637 665
638 struct aim_user *find_user(const char *name, int protocol) 666 struct aim_user *find_user(const char *name, int protocol)
639 { 667 {
640 char *who = g_strdup(normalize(name)); 668 char *who = g_strdup(normalize(name));
877 lastgood += limit; 905 lastgood += limit;
878 } 906 }
879 } 907 }
880 } 908 }
881 909
910 gchar *gaim_home_dir()
911 {
912 if(g_get_home_dir())
913 return g_get_home_dir();
914 else
915 #ifndef _WIN32
916 return NULL;
917 #else
918 /* Win9x and WinME don't have a home dir */
919 return "C:";
920 #endif
921
922 }
923
882 /* returns a string of the form ~/.gaim, where ~ is replaced by the user's home 924 /* returns a string of the form ~/.gaim, where ~ is replaced by the user's home
883 * dir. this string should be freed after it's used. Note that there is no 925 * dir. Note that there is no trailing slash after .gaim. */
884 * trailing slash after .gaim. */
885 gchar *gaim_user_dir() 926 gchar *gaim_user_dir()
886 { 927 {
887 return g_strjoin(G_DIR_SEPARATOR_S, g_get_home_dir(), ".gaim", NULL); 928 if(gaim_home_dir()) {
929 strcpy( (char*)&home_dir, gaim_home_dir() );
930 strcat( (char*)&home_dir, G_DIR_SEPARATOR_S ".gaim" );
931 return (gchar*)&home_dir;
932 }
933 else {
934 return NULL;
935 }
888 } 936 }
889 937
890 /* 938 /*
891 * rcg10312000 This could be more robust, but it works for my current 939 * rcg10312000 This could be more robust, but it works for my current
892 * goal: to remove those annoying <BR> tags. :) 940 * goal: to remove those annoying <BR> tags. :)
1241 static const char *gaim_mkstemp_templ = {"gaimXXXXXX"}; 1289 static const char *gaim_mkstemp_templ = {"gaimXXXXXX"};
1242 1290
1243 FILE *gaim_mkstemp(gchar **fpath) 1291 FILE *gaim_mkstemp(gchar **fpath)
1244 { 1292 {
1245 const gchar *tmpdir; 1293 const gchar *tmpdir;
1294 #ifndef _WIN32
1246 int fd; 1295 int fd;
1296 #endif
1247 FILE *fp = NULL; 1297 FILE *fp = NULL;
1248 1298
1249 if((tmpdir = g_get_tmp_dir()) != NULL) { 1299 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) {
1250 if((*fpath = g_strdup_printf("%s/%s", tmpdir, gaim_mkstemp_templ)) != NULL) { 1300 if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, gaim_mkstemp_templ)) != NULL) {
1301 #ifdef _WIN32
1302 char* result = _mktemp( *fpath );
1303 if( result == NULL )
1304 debug_printf("gaim_mkstemp: Problem creating the template\n");
1305 else
1306 {
1307 if( (fp = fopen( result, "w+" )) == NULL ) {
1308 debug_printf("Error: Couldn't fopen()in gaim_mkstemp():\n%s\n", result);
1309 }
1310 }
1311 #else
1251 if((fd = mkstemp(*fpath)) == -1) { 1312 if((fd = mkstemp(*fpath)) == -1) {
1252 debug_printf("Error: Couldn't make \"%s\", error: %d\n", *fpath, errno); 1313 debug_printf("Error: Couldn't make \"%s\", error: %d\n", *fpath, errno);
1253 } else { 1314 } else {
1254 if((fp = fdopen(fd, "r+")) == NULL) { 1315 if((fp = fdopen(fd, "r+")) == NULL) {
1255 close(fd); 1316 close(fd);
1256 debug_printf("Error: Couldn't fdopen(), error: %d\n", errno); 1317 debug_printf("Error: Couldn't fdopen(), error: %d\n", errno);
1257 } 1318 }
1258 } 1319 }
1320 #endif
1259 if(!fp) { 1321 if(!fp) {
1260 g_free(*fpath); 1322 g_free(*fpath);
1261 *fpath = NULL; 1323 *fpath = NULL;
1262 } 1324 }
1263 } 1325 }