forked from sneezymud/dikumud
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelplay.c
80 lines (65 loc) · 1.42 KB
/
delplay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <ctype.h>
#include "structs.h"
#define TOLOWER(c) (((c)>='A' && (c) <= 'Z') ? ((c)+('a'-'A')) : (c))
int str_cmp(char *str1, char *str2)
{
for (; *str1 || *str2; str1++, str2++)
if (TOLOWER(*str1) != TOLOWER(*str2))
return(1);
return(0);
}
void del(char *filename, int name)
{
char confirm[80];
FILE *fl;
struct char_file_u player;
int pos, num;
long end;
if (!(fl = fopen(filename, "r+")))
{
perror("list");
exit();
}
puts("Searching for player:");
for (num = 1, pos = 0;; pos++, num++)
{
fread(&player, sizeof(player), 1, fl);
if (feof(fl))
{
fprintf(stderr, "delplay: could not locate %d.\n", name);
exit();
}
if (num == name) {
printf("Confirm deletion of [%s] by typeing Yes: ", player.name);
scanf("%s", confirm);
if (str_cmp("Yes", confirm)) {
printf("Aborted delete.\n");
exit();
} else {
break;
}
}
}
/* read the last player */
fseek(fl, -sizeof(player), 2);
fread(&player, sizeof(player), 1, fl);
fseek(fl, pos*sizeof(player), 0);
fwrite(&player, sizeof(player), 1, fl);
fseek(fl, 0, 2);
end = ftell(fl);
fclose(fl);
if (truncate(filename, end-sizeof(player)))
perror("truncate");
}
main(int argc, char **argv)
{
if (argc != 3)
puts("Usage: delplay <DikuMUD player filename> <Player Number>");
else {
if (atoi(argv[2]) < 1)
puts("Illegal player number, must be >= 1");
else
del(argv[1], atoi(argv[2]));
}
}