-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanf.c
More file actions
95 lines (88 loc) · 2.29 KB
/
anf.c
File metadata and controls
95 lines (88 loc) · 2.29 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (C) 2014 AliReza Forouzandeh Nezhad (aka alireza7991)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <anf.h>
char *getval(char *a) {
while(*a!='=') { a++; }
return strdup(a+1);
}
char *getkey(char *s) {
char *a=strdup(s);
char *b=a;
while(*a!='\0') { if(*a=='=') { *a='\0'; break; } a++; }
return b;
}
unsigned isparam(char *s) {
while(*s!='\0') { if(*s=='=') { return 1; } s++; } return 0;
}
void do_switch(unsigned char s,anf_stat *b) {
unsigned i,f;
// small credit to sean bright (http://www.seanbright.com) for using sizeof instead of while loop
for(i=0;i<sizeof(anf_ext)/sizeof(anf_ext[0]);i++) {
if(anf_ext[i].s==s) { anf_ext[i].func(b); f=1; }
}
if(!f) {
#ifdef ANF_INTERACTIVE
printf("invalid switch\n");
#endif
exit(EINVALIDSWITCH);
}
}
void hwset(char *path,char *val) {
int f=open(path,O_RDWR);
if(!f) {
#ifdef ANF_INTERACTIVE
perror("hwset");
#endif
exit(EOPENFAILED);
}
register unsigned s=strlen(val);
register int rstat=write(f,val,strlen(val));
if(rstat!=s) {
#ifdef ANF_INTERACTIVE
printf("hwset : write %s at %s failed\n",val,path);
#endif
exit(EWRITEFAILED);
}
fsync(f); // be sure data is written
close(f);
}
int main(int argc,char **argv) {
unsigned char nswitch=NULL;
unsigned i;
for(i=1;i<argc;i++) {
if(*argv[i]=='-') {
nswitch=argv[i][1];
break;
}
}
if(!nswitch) {
#ifdef ANF_INTERACTIVE
printf("no switch has been used!\n");
#endif
exit(ENOSWITCH);
}
anf_stat a;
a.c=argc;
a.vec=argv;
do_switch(nswitch,&a);
}