~ubuntu-branches/ubuntu/maverick/typespeed/maverick

« back to all changes in this revision

Viewing changes to file.c

  • Committer: Bazaar Package Importer
  • Author(s): Dafydd Harries
  • Date: 2007-12-07 05:14:13 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071207051413-zn96fxeem6wknkiz
Tags: 0.6.4-1
* New upstream release. Closes: #375136.
  - High priority due to fix for DoS attack. Closes: #454527
    (CVE-2007-6220).
  - Fixes segfault when $HOME is unset. Closes: #355887.
  - Adds Italian and French word lists, Dutch word file merged.
* New upstream maintainer.
  - Update homepage URL, debian/copyright, debian/watch.
  - This version is not network-compatible with versions prior to 0.5.2.
  - Stricter network code.
  - Improved memory management.
* High score file format has changed: install score conversion program to
  /usr/lib/typespeed and run it when package is configured.
* postinst:
  - Remove obsolete score file backup/create/restore code.
  - Add code to upgrade score files to the new text-based format.
* rules:
  - Update to new autotools build.
  - Put stamp files in debian/.
  - Support DEB_BUILD_OPTS=noopt.
* Remove unnecessary debian/install.
* Update man page installation.
* Change menu file to Games/Action section as per new menu policy.
* Bump to debhelper compat version 5.
* Bump standards version to 3.7.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   file.c - file access functions
3
 
 */
4
 
 
5
 
#include "typespeed.h"
6
 
 
7
 
#ifdef DEBIAN
8
 
#define DEBIAN__HIGHSCORE "/var/games/typespeed"
9
 
#define DEBIAN__WORDLIST "/usr/share/games/typespeed"
10
 
#endif /* DEBIAN */
11
 
 
12
 
int loadwords(char *stringi)
13
 
{
14
 
        FILE *wordfile;
15
 
        int nakki;
16
 
        char *makkara;
17
 
        int nakkimakkara;
18
 
 
19
 
        wordcount = -1;
20
 
        nakkimakkara = 0;
21
 
 
22
 
#ifdef DEBIAN
23
 
chdir(DEBIAN__WORDLIST);
24
 
#endif /* DEBIAN */
25
 
        if ((wordfile = fopen(stringi, "r")) == NULL) {
26
 
                return 1;
27
 
        }
28
 
        makkara = malloc(21 * sizeof(char));
29
 
        while ((nakki = getc(wordfile)) != EOF) {
30
 
                if (wordcount>=WORD_MAX) {
31
 
                        break;  /* don't read more than max words */
32
 
                }
33
 
                switch (nakki) {
34
 
                case 0x0d:
35
 
                        break;
36
 
                case 0x0a:
37
 
                        makkara[nakkimakkara] = 0;
38
 
 
39
 
                        /* we don't need wordfilename here.. */
40
 
 
41
 
/*                      if (wordcount == -1) {
42
 
   strcpy(wordfilename, makkara);
43
 
   } else {                           */
44
 
                        if (wordcount >= 0) {
45
 
                                word[wordcount] = malloc(21 * sizeof(char));
46
 
                                strcpy(word[wordcount], makkara);
47
 
                        }
48
 
                        wordcount++;
49
 
                        nakkimakkara = 0;
50
 
                        break;
51
 
                default:
52
 
                        if (nakkimakkara < 21) {        /*max word length 20.. + end null char */
53
 
                                makkara[nakkimakkara] = nakki;
54
 
                                nakkimakkara++;
55
 
                        }
56
 
                        break;
57
 
                }
58
 
        }
59
 
        free(makkara);
60
 
        fclose(wordfile);
61
 
        if (wordcount < 22) {   /*not enough words */
62
 
                freewords();
63
 
                return 1;
64
 
        }
65
 
        return 0;
66
 
}
67
 
 
68
 
void freewords()
69
 
{
70
 
        int i;
71
 
 
72
 
        for (i = 0; i < wordcount; i++)
73
 
                free(word[i]);
74
 
 
75
 
}
76
 
 
77
 
 
78
 
 
79
 
int choose_wordfile(int kumpi)
80
 
{
81
 
 
82
 
        struct dirent **namelist;
83
 
        FILE *fileinfo;
84
 
        int n, k, tot, cpos = 1;
85
 
        long int a;
86
 
        int *what = NULL;
87
 
        char *filena;
88
 
        char *info;
89
 
        //char *thingie[50];
90
 
        char thingie[WORDFILE_MAX][62];
91
 
        int merkk;
92
 
 
93
 
 
94
 
        filena = malloc(4 * sizeof(char));
95
 
        info = malloc(62 * sizeof(char));
96
 
 
97
 
        k = 0;
98
 
        a = 1;
99
 
 
100
 
#ifdef DEBIAN
101
 
chdir(DEBIAN__WORDLIST);
102
 
#endif /* DEBIAN */
103
 
        dcolor_set(5, NULL);
104
 
        n = scandir(".", &namelist, 0, alphasort);
105
 
        tot = n;
106
 
        if (n < 0) {
107
 
                printf("scandir failed.\n");
108
 
                exit(0);
109
 
        } else {
110
 
                what = (int *) malloc((n + 1) * sizeof(int));
111
 
                while (n--) {
112
 
                        if (strstr(namelist[n]->d_name, "words.") == namelist[n]->d_name) {
113
 
                                k++;
114
 
                                if ((fileinfo = fopen(namelist[n]->d_name, "r")) != NULL) {
115
 
                                        fgets(info, 60, fileinfo);
116
 
                                        //mvprintw(k + 1, 2, "%d. %s", k, info);
117
 
                                        strcpy(thingie[k], info);
118
 
                                        fclose(fileinfo);
119
 
                                        what[k] = n;
120
 
                                }
121
 
                        }
122
 
                }
123
 
        }
124
 
 
125
 
        dcolor_set(4, NULL);
126
 
 
127
 
        tot = k;
128
 
 
129
 
        if (k == 0) {
130
 
                mvprintw(k + 1, 5, "No wordfiles found...");
131
 
                mvprintw(k + 3, 5, "Press Enter...");
132
 
                liima_mvgetnstr(k + 3, 19, filena, 0);
133
 
                return 0;
134
 
        } else {
135
 
                mvprintw(2, 5, "Choose a wordfile (UP/DOWN/ENTER):");
136
 
                if (tot >= 17) {
137
 
                        for (k = 1; k <= 17; k++) {
138
 
                                mvprintw(k + 3, 5, "%s", thingie[k]);
139
 
                        }
140
 
                } else {
141
 
                        for (k = 1; k <= tot; k++) {
142
 
                                mvprintw(k + 3, 5, "%s", thingie[k]);
143
 
                        }
144
 
                }
145
 
                k = 1;
146
 
                mvprintw(k + 3, 1, "->");
147
 
        }
148
 
 
149
 
        noecho();
150
 
 
151
 
        while (a) {
152
 
 
153
 
                /* and thank's for xmunkki, he made my day and
154
 
                   "solved" this, what i didn't get right (or did
155
 
                   but i used 2 extra variables.. :) */
156
 
 
157
 
                merkk = getch();
158
 
                switch (merkk) {
159
 
                case 'A':
160
 
                        {
161
 
                                if (cpos > 1) {
162
 
                                        cpos--;
163
 
                                        if (k > 1)
164
 
                                                k--;
165
 
                                } else {
166
 
                                        if (k > 1) {
167
 
                                                k--;
168
 
                                                mvprintw(20, 5, "");
169
 
                                                deleteln();
170
 
                                                mvprintw(4, 5, "");
171
 
                                                insertln();
172
 
                                                mvprintw(4, 5, "%s", thingie[k]);
173
 
                                        }
174
 
                                }
175
 
                        }
176
 
                        break;
177
 
                case 'B':
178
 
                        {
179
 
                                if ((cpos < 17) && (cpos < tot)) {
180
 
                                        cpos++;
181
 
                                        if (k < tot)
182
 
                                                k++;
183
 
                                } else {
184
 
                                        if (k < tot) {
185
 
                                                k++;
186
 
                                                mvprintw(4, 5, "");
187
 
                                                deleteln();
188
 
                                                mvprintw(20, 5, "");
189
 
                                                insertln();
190
 
                                                mvprintw(20, 5, "%s", thingie[k]);
191
 
                                        }
192
 
                                }
193
 
                        }
194
 
                        break;
195
 
                case 32:
196
 
                case 10:{
197
 
                                a = 0;
198
 
                        }
199
 
                        break;
200
 
                }
201
 
                //mvprintw(1,20,"merkk: %d  k: %d cpos: %d",merkk,k,cpos);
202
 
                mvprintw(cpos + 4, 1, "  ");
203
 
                mvprintw(cpos + 2, 1, "  ");
204
 
                mvprintw(cpos + 3, 1, "->");
205
 
 
206
 
        }
207
 
 
208
 
        echo();
209
 
        dcolor_set(2, NULL);
210
 
        a = 1;
211
 
 
212
 
        switch (kumpi) {
213
 
        case 2:{
214
 
                        loadhighscores(namelist[what[k]]->d_name);
215
 
                        dcolor_set(4, NULL);
216
 
                        mvprintw(16, 3, "Do you want to RESET this [y/n] ? ");
217
 
                        noecho();
218
 
                        do {
219
 
                                merkk = getch();
220
 
                                merkk = clower(merkk);
221
 
                        } while ((merkk != 'y') && (merkk != 'n'));
222
 
                        echo();
223
 
                        a = 0;
224
 
                        if (merkk == 'y') {
225
 
                                a = resetscorefile(namelist[what[k]]->d_name);
226
 
                        } else {
227
 
                                break;
228
 
                        }
229
 
                }
230
 
        case 1:{
231
 
                        loadhighscores(namelist[what[k]]->d_name);
232
 
                        dcolor_set(4, NULL);
233
 
                        mvprintw(16, 3, "Press any key to continue...");
234
 
                        do {
235
 
                        } while ((merkk = getch()) == ERR);
236
 
                        a = 0;
237
 
                        break;
238
 
                }
239
 
        case 0:{
240
 
                        a = loadwords(namelist[what[k]]->d_name);
241
 
                        break;
242
 
                }
243
 
        }
244
 
 
245
 
        if (a != 0) {
246
 
                mvprintw(3, 5, "Something went wrong. Contact your mother.");
247
 
                do {
248
 
                } while ((merkk = getch()) == ERR);
249
 
        }
250
 
        usedwordfile = malloc(sizeof(namelist[what[k]]->d_name));
251
 
        strcpy(usedwordfile, namelist[what[k]]->d_name);
252
 
 
253
 
        free(info);
254
 
        free(filena);
255
 
        for (k = 0; k < tot; k++) {
256
 
                free(namelist[k]);
257
 
        }
258
 
        /* cores if we free this(?) free(namelist); */
259
 
        free(what);
260
 
        //free(thingie);
261
 
        dcolor_set(2, NULL);
262
 
        return 0;
263
 
}
264
 
 
265
 
int resetscorefile(char *sanafile)
266
 
{
267
 
 
268
 
        FILE *highfile;
269
 
        int x, rvalue = 1;
270
 
        char *filetys;
271
 
 
272
 
        int epong[10] =
273
 
        {700, 600, 500, 400, 300, 200, 100, 50, 0, 0};
274
 
        int efidelity[10] =
275
 
        {8, 7, 6, 5, 4, 3, 2, 1, 0, 0};
276
 
        float ecps[10] =
277
 
        {7, 6, 5, 4, 3, 2, 1, 0.5, 0, 0};
278
 
        float etcps[10] =
279
 
        {7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 1, 0, 0};
280
 
        float etyporatio[10] =
281
 
        {10, 10, 10, 10, 10, 10, 10, 10, 0, 0};
282
 
        char efaim[10][21] =
283
 
        {"3l33t", "Pro", "VeryGood", "Good", "Average", "NoGood", "ADP-Pro", "Loser", "None", "None"};
284
 
 
285
 
 
286
 
#ifdef DEBIAN
287
 
chdir(DEBIAN__HIGHSCORE);
288
 
#endif /* DEBIAN */
289
 
        filetys = (char *) malloc(sizeof(char) * (strlen(sanafile) + strlen(".high") + 1));
290
 
        strcpy(filetys, "");
291
 
        strcat(filetys, "high.");
292
 
        strcat(filetys, sanafile);
293
 
 
294
 
        if ((highfile = fopen(filetys, "w")) != NULL) {
295
 
                for (x = 0; x < 10; x++) {
296
 
                        fwrite(&epong[x], sizeof(epong[x]), 1, highfile);
297
 
                        fwrite(&ecps[x], sizeof(ecps[x]), 1, highfile);
298
 
                        fwrite(&efidelity[x], sizeof(efidelity[x]), 1, highfile);
299
 
                        fwrite(&etcps[x], sizeof(etcps[x]), 1, highfile);
300
 
                        fwrite(&etyporatio[x], sizeof(etyporatio[x]), 1, highfile);
301
 
                        fwrite(efaim[x], sizeof(efaim[x]), 1, highfile);
302
 
                }
303
 
 
304
 
                fclose(highfile);
305
 
                rvalue = 0;
306
 
        } else {
307
 
                rvalue = 1;
308
 
        }
309
 
 
310
 
        free(filetys);
311
 
 
312
 
        return rvalue;
313
 
}
314
 
 
315
 
void loadhighscores(char *sanafile)
316
 
{
317
 
        FILE *highfile;
318
 
        int x;
319
 
        char *filetys;
320
 
        struct list {
321
 
                int pong;
322
 
                int fidelity;
323
 
                float cps;
324
 
                float tcps;
325
 
                float typoratio;
326
 
                char faim[21];
327
 
        } high;
328
 
#ifdef DEBIAN
329
 
chdir(DEBIAN__HIGHSCORE);
330
 
#endif /* DEBIAN */
331
 
        filetys = (char *) malloc(sizeof(char) * (strlen(sanafile) + strlen(".high") + 1));
332
 
        strcpy(filetys, "");
333
 
        strcat(filetys, "high.");
334
 
        strcat(filetys, sanafile);
335
 
        clear();
336
 
        drawscreen();
337
 
        dcolor_set(4, NULL);
338
 
 
339
 
        high.pong = 0;
340
 
        high.fidelity = 0;
341
 
        high.cps = 0;
342
 
        high.tcps = 0;
343
 
        high.typoratio = 0;
344
 
        strcpy(high.faim, "                    ");
345
 
 
346
 
        if ((highfile = fopen(filetys, "r")) != NULL) {
347
 
                mvprintw(1, 0,
348
 
                         "Top10   (score)(name)                   (level)   (cps)  (tcps) (typoinfos)");
349
 
                dcolor_set(5, NULL);
350
 
                for (x = 1; x <= 10; x++) {
351
 
                        fread(&high.pong, sizeof(int), 1, highfile);
352
 
                        fread(&high.cps, sizeof(float), 1, highfile);
353
 
                        fread(&high.fidelity, sizeof(int), 1, highfile);
354
 
                        fread(&high.tcps, sizeof(float), 1, highfile);
355
 
                        fread(&high.typoratio, sizeof(float), 1, highfile);
356
 
                        fread(high.faim, sizeof(char), 21, highfile);
357
 
                        mvprintw(x + 2, 3, "%d.", x);
358
 
                        mvprintw(x + 2, 8, "%d", high.pong);
359
 
                        mvprintw(x + 2, 15, "%s", high.faim);
360
 
                        level(high.pong);
361
 
                        if (high.fidelity>=0 && high.fidelity<11) mvprintw(x + 2, 40, "%s", rankki[high.fidelity]);
362
 
                        dcolor_set(5, NULL);
363
 
                        now.level = 0;  /* Why here?? */
364
 
                        mvprintw(x + 2, 50, "%2.3f", high.cps);
365
 
                        mvprintw(x + 2, 57, "%2.3f", high.tcps);
366
 
                        mvprintw(x + 2, 64, "%2.2f%%", high.typoratio);
367
 
                        if (high.pong != 0) {
368
 
/*                      typorankkaus(high.typoratio); */
369
 
                                mvprintw(x + 2, 71, "%s", typorank[typorankkaus(high.typoratio)]);
370
 
                        } else {
371
 
                                mvprintw(x + 2, 71, "None");
372
 
                        }
373
 
                        dcolor_set(5, NULL);
374
 
 
375
 
 
376
 
                }
377
 
                fclose(highfile);
378
 
        } else {
379
 
                mvprintw(4, 3, "No Highscores");
380
 
        }
381
 
        dcolor_set(2, NULL);
382
 
        free(filetys);
383
 
}
384
 
 
385
 
 
386
 
/*int addtop10(char *sanaf, int pointsgot, int levelgot, float speedgot, float truecps, float typosgot) */
387
 
int addtop10(char *sanaf, stats_struct * stat)
388
 
{
389
 
 
390
 
        FILE *highf;
391
 
        int x, madeit = 0;
392
 
        char *filet;
393
 
        char name[21];
394
 
        struct list {
395
 
                int pong[11];
396
 
                int fidelity[11];
397
 
                float cps[11];
398
 
                float tcps[11];
399
 
                float typoratio[11];
400
 
                char faim[11][21];
401
 
        } high;
402
 
        filet = (char *) malloc(sizeof(char) * (strlen(sanaf) +
403
 
                                                strlen(".high")
404
 
                                                + 1));
405
 
 
406
 
#ifdef DEBIAN
407
 
chdir(DEBIAN__HIGHSCORE);
408
 
#endif /* DEBIAN */
409
 
        dcolor_set(5, NULL);
410
 
        strcpy(filet, "");
411
 
        strcat(filet, "high.");
412
 
        strcat(filet, sanaf);
413
 
        for (x = 0; x < 10; x++) {
414
 
                high.pong[x] = 0;
415
 
                high.cps[x] = 0;
416
 
                high.typoratio[x] = 0;
417
 
                high.tcps[x] = 0;
418
 
                high.fidelity[x] = 0;
419
 
                strcpy(high.faim[x], "                    ");
420
 
        }
421
 
 
422
 
        if ((highf = fopen(filet, "r")) == NULL) {
423
 
                resetscorefile(sanaf);
424
 
        }
425
 
        if ((highf = fopen(filet, "r")) != NULL) {
426
 
                for (x = 0; x < 10; x++) {
427
 
                        fread(&high.pong[x], sizeof(int), 1, highf);
428
 
                        fread(&high.cps[x], sizeof(float), 1, highf);
429
 
                        fread(&high.fidelity[x], sizeof(int), 1, highf);
430
 
                        fread(&high.tcps[x], sizeof(float), 1, highf);
431
 
                        fread(&high.typoratio[x], sizeof(float), 1, highf);
432
 
                        fread(&high.faim[x], sizeof(char), 21, highf);
433
 
                }
434
 
                fclose(highf);
435
 
        }
436
 
        if ((highf = fopen(filet, "w")) != NULL) {
437
 
                for (x = 0; x < 10; x++) {
438
 
                        /* was >= now >, now changed system to get top10 */
439
 
                        if ((stat->score > high.pong[x]) && (madeit != 1)) {
440
 
 
441
 
                                fwrite(&(stat->score), sizeof(stat->score), 1, highf);
442
 
                                fwrite(&(stat->speed), sizeof(stat->speed), 1, highf);
443
 
                                fwrite(&(stat->level), sizeof(stat->level), 1, highf);
444
 
                                fwrite(&(stat->totalspeed), sizeof(stat->totalspeed), 1, highf);
445
 
                                fwrite(&(stat->ratio), sizeof(stat->ratio), 1, highf);
446
 
 
447
 
                                clear();
448
 
                                drawscreen();
449
 
 
450
 
                                dcolor_set(5, NULL);
451
 
                                mvprintw(3, 2,
452
 
                                         "You managed to get to top10!");
453
 
                                mvprintw(5, 2, "Enter your name:");
454
 
                                liima_mvgetnstr(5, 19, name, 20);
455
 
                                dcolor_set(2, NULL);
456
 
 
457
 
                                fwrite(name, sizeof(name), 1, highf);
458
 
                                madeit = 1;
459
 
 
460
 
                                fwrite(&high.pong[x],
461
 
                                       sizeof(high.pong[x]), 1, highf);
462
 
                                fwrite(&high.cps[x], sizeof(high.cps[x]),
463
 
                                       1, highf);
464
 
                                fwrite(&high.fidelity[x],
465
 
                                       sizeof(high.fidelity[x]),
466
 
                                       1, highf);
467
 
                                fwrite(&high.tcps[x],
468
 
                                       sizeof(high.tcps[x]), 1, highf);
469
 
                                fwrite(&high.typoratio[x],
470
 
                                       sizeof(high.typoratio[x]),
471
 
                                       1, highf);
472
 
                                fwrite(high.faim[x],
473
 
                                       sizeof(high.faim[x]), 1, highf);
474
 
 
475
 
                        } else {
476
 
 
477
 
                                if ((madeit == 1) && (x == 9)) {
478
 
                                } else {
479
 
                                        fwrite(&high.pong[x],
480
 
                                               sizeof(high.pong[x]),
481
 
                                               1, highf);
482
 
                                        fwrite(&high.cps[x],
483
 
                                               sizeof(high.cps[x]),
484
 
                                               1, highf);
485
 
                                        fwrite(&high.fidelity[x],
486
 
                                               sizeof(high.fidelity[x]),
487
 
                                               1, highf);
488
 
                                        fwrite(&high.tcps[x],
489
 
                                               sizeof(high.tcps[x]),
490
 
                                               1, highf);
491
 
                                        fwrite(&high.typoratio[x],
492
 
                                               sizeof(high.typoratio[x]),
493
 
                                               1, highf);
494
 
                                        fwrite(high.faim[x],
495
 
                                               sizeof(high.faim[x]),
496
 
                                               1, highf);
497
 
                                }
498
 
                        }
499
 
                }
500
 
                fclose(highf);
501
 
        }
502
 
        dcolor_set(2, NULL);
503
 
        free(filet);
504
 
        return 0;
505
 
}
506
 
 
507
 
void readconfig()
508
 
{
509
 
 
510
 
        FILE *conffile;
511
 
        char wordhakemisto[1025];
512
 
        char tmp[10];
513
 
        char *userhome;
514
 
 
515
 
        userhome = malloc((2+strlen(getenv("HOME"))+strlen(LOCALCONF))*sizeof(char));
516
 
        strcpy(userhome,getenv("HOME"));
517
 
        strcat(userhome,"/");
518
 
        strcat(userhome,LOCALCONF);
519
 
        hakemisto = malloc(1026 * sizeof(char));
520
 
        getcwd(hakemisto, 1024);
521
 
        conffile = fopen(CONFIGFILE, "r");
522
 
        if (conffile != NULL) {
523
 
                fgets(wordhakemisto,1024,conffile);
524
 
                fgets(tmp,6,conffile);
525
 
                while (iscntrl(wordhakemisto[strlen(wordhakemisto)-1])) wordhakemisto[strlen(wordhakemisto)-1] = '\0';
526
 
                if (!strcmp(tmp, "cheat"))
527
 
                        opt.cheat = 1;
528
 
                fclose(conffile);
529
 
                chdir(wordhakemisto);
530
 
        }
531
 
        conffile = fopen(userhome, "r");
532
 
        if (conffile != NULL) {
533
 
                fgets(wordhakemisto,1024,conffile);
534
 
                fgets(tmp,6,conffile);
535
 
                while (iscntrl(wordhakemisto[strlen(wordhakemisto)-1])) wordhakemisto[strlen(wordhakemisto)-1] = '\0';
536
 
                if (!strcmp(tmp, "cheat"))
537
 
                        opt.cheat = 1;
538
 
                fclose(conffile);
539
 
                chdir(wordhakemisto);
540
 
        }
541
 
        free(userhome);
542
 
}
543
 
 
544
 
int makescorefiles(void) {
545
 
 
546
 
        struct dirent **namelist;
547
 
        int n;
548
 
 
549
 
#ifdef DEBIAN
550
 
chdir(DEBIAN__WORDLIST);
551
 
#endif /* DEBIAN */
552
 
        n = scandir(".", &namelist, 0, alphasort);
553
 
        if (n < 0) {
554
 
                printf("scandir failed.\n");
555
 
                exit(1);
556
 
        } else {
557
 
                printf("\nReseting/Making scorefiles to all found wordfiles...\n\n");
558
 
                while (n--) {
559
 
                        if (strstr(namelist[n]->d_name, "words.") == namelist[n]->d_name) {
560
 
                                resetscorefile(namelist[n]->d_name);
561
 
                                printf("Make scorefile for wordfile: %s\n",namelist[n]->d_name);
562
 
                                }
563
 
                        }
564
 
        }
565
 
        return 0;
566
 
}
567