1 /*
2 Copyright (C) 2003 Cedric Cellier, Dominique Lavault
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include "user.h"
23 #include "system.h"
24 #include "memspool.h"
25 #include "log.h"
26
27 static char *user_score_file; /* score file */
28 char *user_rc_dir;
29 char *user_name;
30 unsigned user_score;
31
init_void_user()32 void init_void_user()
33 {
34 user_score = 1;
35 }
36
user_save()37 void user_save()
38 {
39 FILE *file;
40 file = fopen(user_score_file, "w+");
41 if (!file) {
42 gltv_log_warning(GLTV_LOG_MUSTSEE, "user_save: Cannot open file '%s' : %s", user_score_file, strerror(errno));
43 } else {
44 if (fwrite(&user_score, sizeof(user_score), 1, file)<1) {
45 gltv_log_warning(GLTV_LOG_MUSTSEE, "user_save: Cannot write file '%s' : %s", user_score_file, strerror(errno));
46 }
47 }
48 gltv_memspool_unregister(user_score_file);
49 gltv_memspool_unregister(user_rc_dir);
50 }
51
user_read()52 void user_read()
53 {
54 char *user_dir;
55 const char *config_dir =
56 #ifdef _WINDOWS
57 "\\.ensemblist";
58 #else
59 "/.ensemblist";
60 #endif
61 FILE *file;
62 const char *score_file =
63 #ifdef _WINDOWS
64 "\\score";
65 #else
66 "/score";
67 #endif
68 unsigned u, v, w;
69 if (!sys_get_user_name(&user_name)) {
70 gltv_log_warning(GLTV_LOG_MUSTSEE, "user_read: Cannot get user name");
71 user_name = "unknown";
72 }
73 gltv_log_warning(GLTV_LOG_DEBUG, "USER = %s",user_name);
74 if (!sys_get_user_dir(&user_dir)) {
75 gltv_log_warning(GLTV_LOG_MUSTSEE, "user_read: Cannot get user's directory");
76 init_void_user();
77 return;
78 }
79 u = strlen(user_dir);
80 v = strlen(config_dir);
81 w = strlen(score_file);
82 user_rc_dir = gltv_memspool_alloc(u+v+1);
83 user_score_file = gltv_memspool_alloc(u+v+w+1);
84 if (!user_rc_dir || !user_score_file) gltv_log_fatal("user_read: Cannot get memory\n");
85 memcpy(user_rc_dir, user_dir, u);
86 memcpy(user_rc_dir+u, config_dir, v+1);
87 if (!sys_make_dir(user_rc_dir)) {
88 gltv_log_warning(GLTV_LOG_MUSTSEE, "user_read: Cannot get user's config directory");
89 init_void_user();
90 return;
91 }
92 atexit(user_save);
93 memcpy(user_score_file, user_rc_dir, u+v);
94 memcpy(user_score_file+u+v, score_file, w+1);
95 file = fopen(user_score_file, "r");
96 if (!file) {
97 init_void_user();
98 return;
99 }
100 if (fread(&user_score, sizeof(user_score), 1, file)<1) {
101 gltv_log_warning(GLTV_LOG_MUSTSEE, "user_read: Cannot read file '%s' : %s", user_score_file, strerror(errno));
102 init_void_user();
103 return;
104 }
105 if (user_score==0) user_score = 1;
106 }
107
108
109