-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetuser.c
68 lines (53 loc) · 1.32 KB
/
setuser.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
#include <stdio.h>
#include "bbs.h"
#define DOTPASSWDS BBSHOME"/.PASSWDS"
#define PASSWDSBAK BBSHOME"/PASSWDS"
#define TMPFILE BBSHOME"/tmp/tmpfile"
unsigned short rev(unsigned short i);
struct userec cuser;
main(argc, argv)
int argc;
char **argv;
{
FILE *foo1, *foo2;
int cnum,i,match;
if( argc < 3 || strspn(argv[1], "01") != 32) {
puts("Usage: setuser perm_32bit user1 [user2...]");
exit(1);
}
if( ((foo1=fopen(DOTPASSWDS, "r")) == NULL)
|| ((foo2=fopen(TMPFILE,"w"))== NULL) ){
puts("file opening error");
exit(1);
}
while( (cnum=fread( &cuser, sizeof(cuser), 1, foo1))>0 ) {
for (match =0, i = 2; i < argc; i++)
if(!strcmp(cuser.userid, argv[i])) {
match = 1;
break;
}
if (match)
cuser.userlevel = rev(strtoul(argv[1], 0, 2));
fwrite( &cuser, sizeof(cuser), 1, foo2);
}
fclose(foo1);
fclose(foo2);
if(f_mv(DOTPASSWDS,PASSWDSBAK)==-1){
puts("replace fails");
exit(1);
}
unlink(DOTPASSWDS);
f_mv(TMPFILE,DOTPASSWDS);
unlink("tmpfile");
return 0;
}
unsigned short rev(unsigned short i)
{
unsigned j, k;
for (k = i & 1, j = 1; j < 8 * sizeof(i); j++) {
i >>= 1;
k <<= 1;
k += i & 1;
}
return k;
}