xref: /plan9/sys/src/cmd/upas/send/skipequiv.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1 #include "common.h"
2 #include "send.h"
3 
4 #define isspace(c) ((c)==' ' || (c)=='\t' || (c)=='\n')
5 
6 /*
7  *  skip past all systems in equivlist
8  */
9 extern char*
skipequiv(char * base)10 skipequiv(char *base)
11 {
12 	char *sp;
13 	static Biobuf *fp;
14 
15 	while(*base){
16 		sp = strchr(base, '!');
17 		if(sp==0)
18 			break;
19 		*sp = '\0';
20 		if(lookup(base, "equivlist", &fp, 0, 0)==1){
21 			/* found or us, forget this system */
22 			*sp='!';
23 			base=sp+1;
24 		} else {
25 			/* no files or system is not found, and not us */
26 			*sp='!';
27 			break;
28 		}
29 	}
30 	return base;
31 }
32 
33 static int
okfile(char * cp,Biobuf * fp)34 okfile(char *cp, Biobuf *fp)
35 {
36 	char *buf;
37 	int len;
38 	char *bp, *ep;
39 	int c;
40 
41 	len = strlen(cp);
42 	Bseek(fp, 0, 0);
43 
44 	/* one iteration per system name in the file */
45 	while(buf = Brdline(fp, '\n')) {
46 		ep = &buf[Blinelen(fp)];
47 		for(bp=buf; bp < ep;){
48 			while(isspace(*bp) || *bp==',')
49 				bp++;
50 			if(strncmp(bp, cp, len) == 0) {
51 				c = *(bp+len);
52 				if(isspace(c) || c==',')
53 					return 1;
54 			}
55 			while(bp < ep && (!isspace(*bp)) && *bp!=',')
56 				bp++;
57 		}
58 	}
59 
60 	/* didn't find it, prohibit forwarding */
61 	return 0;
62 }
63 
64 /* return 1 if name found in one of the files
65  *	  0 if name not found in one of the files
66  *	  -1 if neither file exists
67  */
68 extern int
lookup(char * cp,char * local,Biobuf ** lfpp,char * global,Biobuf ** gfpp)69 lookup(char *cp, char *local, Biobuf **lfpp, char *global, Biobuf **gfpp)
70 {
71 	static String *file = 0;
72 
73 	if (local) {
74 		if (file == 0)
75 			file = s_new();
76 		abspath(local, UPASLIB, s_restart(file));
77 		if (*lfpp != 0 || (*lfpp = sysopen(s_to_c(file), "r", 0)) != 0) {
78 			if (okfile(cp, *lfpp))
79 				return 1;
80 		} else
81 			local = 0;
82 	}
83 	if (global) {
84 		abspath(global, UPASLIB, s_restart(file));
85 		if (*gfpp != 0 || (*gfpp = sysopen(s_to_c(file), "r", 0)) != 0) {
86 			if (okfile(cp, *gfpp))
87 				return 1;
88 		} else
89 			global = 0;
90 	}
91 	return (local || global)? 0 : -1;
92 }
93