xref: /plan9-contrib/sys/src/cmd/upas/common/aux.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include "common.h"
2 
3 /*
4  *  expand a path relative to the user's mailbox directory
5  *
6  *  if the path starts with / or ./, don't change it
7  *
8  */
9 extern String *
10 mboxpath(char *path, char *user, String *to, int dot)
11 {
12 	if (*path=='/' || dot) {
13 		to = s_append(to, path);
14 	} else {
15 		to = s_append(to, MAILROOT);
16 		to = s_append(to, "/box/");
17 		to = s_append(to, user);
18 		to = s_append(to, "/");
19 		to = s_append(to, path);
20 	}
21 	return to;
22 }
23 
24 /* expand a path relative to some `.' */
25 extern String *
26 abspath(char *path, char *dot, String *to)
27 {
28 	if (*path == '/') {
29 		to = s_append(to, path);
30 	} else {
31 		to = s_append(to, dot);
32 		to = s_append(to, "/");
33 		to = s_append(to, path);
34 	}
35 	return to;
36 }
37 
38 /* return a pointer to the base component of a pathname */
39 extern char *
40 basename(char *path)
41 {
42 	char *cp;
43 
44 	cp = strrchr(path, '/');
45 	return cp==0 ? path : cp+1;
46 }
47 
48 /* append a sub-expression match onto a String */
49 extern void
50 append_match(Resub *subexp, String *sp, int se)
51 {
52 	register char *cp = subexp[se].sp;
53 	register char *ep = subexp[se].ep;
54 
55 	for (; cp < ep; cp++)
56 		s_putc(sp, *cp);
57 	s_terminate(sp);
58 }
59 
60 /*
61  *  check for shell characters in a String
62  */
63 #define CHARS "()<>{};\\'\`^&|\r\n"
64 extern int
65 shellchars(char *cp)
66 {
67 	char *sp;
68 
69 	for(sp=CHARS; *sp; sp++)
70 		if(strchr(cp, *sp))
71 			return 1;
72 	return 0;
73 }
74