aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorColin Walters2002-04-29 22:49:00 +0000
committerColin Walters2002-04-29 22:49:00 +0000
commitd99fabf003250967223de222d8a6215c63ed19a7 (patch)
treeb18040118d757bf8c43f55a87ed16fd138b3e9e2 /lib-src
parent7451222f00d7707f296d4d4fd2bafdb3e73dc830 (diff)
downloademacs-d99fabf003250967223de222d8a6215c63ed19a7.tar.gz
emacs-d99fabf003250967223de222d8a6215c63ed19a7.zip
(SCORE_FILE_PREFIX): Delete.
(main): New argument -d, for specifying directory. (usage): Document. (get_user_id): Compute (get_home_dir): Deleted. (get_prefix): New function, taken from main. (main): Check whether or not we are running setuid. Move prefix computation to get_prefix. Don't call getpwent; we don't need to any more. Instead, move it to get_user_id().
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/update-game-score.c86
1 files changed, 41 insertions, 45 deletions
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c
index 0762956a701..194997b63ab 100644
--- a/lib-src/update-game-score.c
+++ b/lib-src/update-game-score.c
@@ -49,12 +49,6 @@ Boston, MA 02111-1307, USA. */
49#define MAX_SCORES 200 49#define MAX_SCORES 200
50#define MAX_DATA_LEN 1024 50#define MAX_DATA_LEN 1024
51 51
52#ifdef HAVE_SHARED_GAME_DIR
53#define SCORE_FILE_PREFIX HAVE_SHARED_GAME_DIR
54#else
55#define SCORE_FILE_PREFIX "~/.emacs.d/games"
56#endif
57
58#if !defined (__GNUC__) || __GNUC__ < 2 52#if !defined (__GNUC__) || __GNUC__ < 2
59#define __attribute__(x) 53#define __attribute__(x)
60#endif 54#endif
@@ -67,6 +61,7 @@ usage(int err)
67 fprintf(stdout, " -h\t\tDisplay this help.\n"); 61 fprintf(stdout, " -h\t\tDisplay this help.\n");
68 fprintf(stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n"); 62 fprintf(stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
69 fprintf(stdout, " -r\t\tSort the scores in increasing order.\n"); 63 fprintf(stdout, " -r\t\tSort the scores in increasing order.\n");
64 fprintf(stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
70 exit(err); 65 exit(err);
71} 66}
72 67
@@ -94,10 +89,23 @@ int
94write_scores(const char *filename, const struct score_entry *scores, 89write_scores(const char *filename, const struct score_entry *scores,
95 int count); 90 int count);
96 91
92void lose(const char *msg, ...)
93 __attribute__ ((format (printf,1,0), noreturn));
94
95void lose(const char *msg, ...)
96{
97 va_list ap;
98 va_start(ap, msg);
99 vfprintf(stderr, msg, ap);
100 va_end(ap);
101 exit(1);
102}
103
97char * 104char *
98get_user_id(struct passwd *buf) 105get_user_id(void)
99{ 106{
100 char *name; 107 char *name;
108 struct passwd *buf = getpwuid(getuid());
101 if (!buf) 109 if (!buf)
102 { 110 {
103 int count = 1; 111 int count = 1;
@@ -106,6 +114,8 @@ get_user_id(struct passwd *buf)
106 while (tuid /= 10) 114 while (tuid /= 10)
107 count++; 115 count++;
108 name = malloc(count+1); 116 name = malloc(count+1);
117 if (!name)
118 return NULL;
109 sprintf(name, "%d", uid); 119 sprintf(name, "%d", uid);
110 return name; 120 return name;
111 } 121 }
@@ -113,45 +123,43 @@ get_user_id(struct passwd *buf)
113} 123}
114 124
115char * 125char *
116get_home_dir(struct passwd *buf) 126get_prefix(int running_suid, char *user_prefix)
117{ 127{
118 if (!buf) 128 if (!running_suid && user_prefix == NULL)
119 return NULL; 129 lose("Not using a shared game directory, and no prefix given.\n");
120 return buf->pw_dir; 130 if (running_suid)
121} 131 {
122 132#ifdef HAVE_SHARED_GAME_DIR
123void lose(const char *msg, ...) 133 return HAVE_SHARED_GAME_DIR;
124 __attribute__ ((format (printf,1,0), noreturn)); 134#else
125 135 lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.\n");
126void lose(const char *msg, ...) 136#endif
127{ 137 }
128 va_list ap; 138 return user_prefix;
129 va_start(ap, msg);
130 vfprintf(stderr, msg, ap);
131 va_end(ap);
132 exit(1);
133} 139}
134 140
135int 141int
136main(int argc, char **argv) 142main(int argc, char **argv)
137{ 143{
138 int c; 144 int c, running_suid;
139 void *lockstate; 145 void *lockstate;
140 char *scorefile, *prefix; 146 char *user_id, *scorefile, *prefix, *user_prefix = NULL;
141 struct stat buf; 147 struct stat buf;
142 struct score_entry *scores; 148 struct score_entry *scores;
143 int newscore, scorecount, reverse = 0, max = MAX_SCORES; 149 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
144 char *newdata; 150 char *newdata;
145 struct passwd *passwdbuf;
146 151
147 srand(time(0)); 152 srand(time(0));
148 153
149 while ((c = getopt(argc, argv, "hrm:")) != -1) 154 while ((c = getopt(argc, argv, "hrm:d:")) != -1)
150 switch (c) 155 switch (c)
151 { 156 {
152 case 'h': 157 case 'h':
153 usage(0); 158 usage(0);
154 break; 159 break;
160 case 'd':
161 user_prefix = optarg;
162 break;
155 case 'r': 163 case 'r':
156 reverse = 1; 164 reverse = 1;
157 break; 165 break;
@@ -167,36 +175,24 @@ main(int argc, char **argv)
167 if (optind+3 != argc) 175 if (optind+3 != argc)
168 usage(1); 176 usage(1);
169 177
170 passwdbuf = getpwuid(getuid()); 178 running_suid = (getuid() != geteuid());
171 179
172 if (!strncmp(SCORE_FILE_PREFIX, "~", 1)) 180 prefix = get_prefix(running_suid, user_prefix);
173 {
174 char *homedir = get_home_dir(passwdbuf);
175 if (!homedir)
176 lose("Unable to determine home directory\n");
177 prefix = malloc(strlen(homedir) + strlen(SCORE_FILE_PREFIX) + 1);
178 strcpy(prefix, homedir);
179 /* Skip over the '~'. */
180 strcat(prefix, SCORE_FILE_PREFIX+1);
181 }
182 else
183 prefix = strdup(SCORE_FILE_PREFIX);
184
185 if (!prefix)
186 lose("Couldn't create score file name: %s\n", strerror(errno));
187 181
188 scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2); 182 scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2);
189 if (!scorefile) 183 if (!scorefile)
190 lose("Couldn't create score file name: %s\n", strerror(errno)); 184 lose("Couldn't create score file name: %s\n", strerror(errno));
191 185
192 strcpy(scorefile, prefix); 186 strcpy(scorefile, prefix);
193 free(prefix);
194 strcat(scorefile, "/"); 187 strcat(scorefile, "/");
195 strcat(scorefile, argv[optind]); 188 strcat(scorefile, argv[optind]);
196 newscore = atoi(argv[optind+1]); 189 newscore = atoi(argv[optind+1]);
197 newdata = argv[optind+2]; 190 newdata = argv[optind+2];
198 if (strlen(newdata) > MAX_DATA_LEN) 191 if (strlen(newdata) > MAX_DATA_LEN)
199 newdata[MAX_DATA_LEN] = '\0'; 192 newdata[MAX_DATA_LEN] = '\0';
193
194 if ((user_id = get_user_id()) == NULL)
195 lose("Couldn't determine user id: %s\n", strerror(errno));
200 196
201 if (stat(scorefile, &buf) < 0) 197 if (stat(scorefile, &buf) < 0)
202 lose("Failed to access scores file \"%s\": %s\n", scorefile, 198 lose("Failed to access scores file \"%s\": %s\n", scorefile,
@@ -210,7 +206,7 @@ main(int argc, char **argv)
210 lose("Failed to read scores file \"%s\": %s\n", scorefile, 206 lose("Failed to read scores file \"%s\": %s\n", scorefile,
211 strerror(errno)); 207 strerror(errno));
212 } 208 }
213 push_score(&scores, &scorecount, newscore, get_user_id(passwdbuf), newdata); 209 push_score(&scores, &scorecount, newscore, user_id, newdata);
214 /* Limit the number of scores. If we're using reverse sorting, then 210 /* Limit the number of scores. If we're using reverse sorting, then
215 we should increment the beginning of the array, to skip over the 211 we should increment the beginning of the array, to skip over the
216 *smallest* scores. Otherwise, we just decrement the number of 212 *smallest* scores. Otherwise, we just decrement the number of