]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/hgfs/util.c
upas/fs: fix more locking bugs, remove debugging clutter, remove planb mbox code
[plan9front.git] / sys / src / cmd / hgfs / util.c
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include "dat.h"
5 #include "fns.h"
6
7 ulong
8 hashstr(char *s)
9 {
10         ulong h, t;
11         char c;
12
13         h = 0;
14         while(c = *s++){
15                 t = h & 0xf8000000;
16                 h <<= 5;
17                 h ^= t>>27;
18                 h ^= (ulong)c;
19         }
20         return h;
21 }
22
23 int
24 getworkdir(char *work, char *path)
25 {
26         char buf[MAXPATH], *s;
27
28         if(path != nil){
29                 snprint(work, MAXPATH, "%s", path);
30                 cleanname(work);
31         } else if(getwd(work, MAXPATH) == nil)
32                 return -1;
33         for(;;){
34                 snprint(buf, sizeof(buf), "%s/.hg", work);
35                 if(access(buf, AEXIST) == 0)
36                         return 0;
37                 if(path != nil)
38                         break;
39                 if((s = strrchr(work, '/')) == nil)
40                         break;
41                 *s = 0;
42         }
43         return -1;
44 }
45
46 int
47 readfile(char *path, char *buf, int nbuf)
48 {
49         int fd, n;
50
51         n = 0;
52         if((fd = open(path, OREAD)) >= 0){
53                 if((n = read(fd, buf, nbuf-1)) < 0)
54                         n = 0;
55                 close(fd);
56         }
57         buf[n] = '\0';
58         return n;
59 }