xrxs/xrxs.c

78 lines
2.7 KiB
C
Raw Normal View History

2021-06-13 17:22:58 +00:00
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include <stdio.h>
/* clang-format off */
char clca(char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; } /* char to lowercase */
char cuca(char c) { return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c; } /* char to uppercase */
int slen(char *s) { int i = 0; while(s[i] && s[++i]) { ; } return i; } /* string length */
char *st__(char *s, char (*fn)(char)) { int i = 0; char c; while((c = s[i])) s[i++] = fn(c); return s; }
char *stuc(char *s) { return st__(s, cuca); } /* string to uppercase */
char *stlc(char *s) { return st__(s, clca); } /* string to lowercase */
char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */
int scmp(char *a, char *b) { int i = 0; while(a[i] == b[i]) if(!a[i++]) return 1; return 0; } /* string compare */
char *scsw(char *s, char a, char b) { int i = 0; char c; while((c = s[i])) s[i++] = c == a ? b : c; return s; } /* string char swap */
char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */
int ssin(char *s, char *ss) { int a = 0, b = 0; while(s[a]) { if(s[a] == ss[b]) { if(!ss[b + 1]) return a - b; b++; } else b = 0; a++; } return -1; } /* string substring index */
char *ccat(char *dst, char c) { int len = slen(dst); dst[len] = c; dst[len + 1] = '\0'; return dst; }
/* clang-format on */
int i = 0;
2021-06-13 17:22:58 +00:00
void fsread(Req* r) {
char numReads[32];
i++;
sprintf(numReads, "Hello from 9p!\n%d reads\n", i);
2021-06-13 17:22:58 +00:00
readstr(r, numReads);
respond(r, nil);
}
Srv fs = {
.read = fsread,
};
void threadmain(int argc, char* argv[]) {
2021-06-13 17:22:58 +00:00
Tree* tree;
char* mtpt = nil;
char* usocket = nil;
int i;
/* if -m PATH is supplied, mount on PATH */
/* if -s NAME is supplied, create a socket for the namespace */
/* otherwise, just use srv() (for wrapping with socat or inetd) */
2021-06-13 17:22:58 +00:00
if (argc >= 3) {
for (i = 0; i < argc; i++) {
if (scmp(argv[i], "-m")) {
mtpt = argv[++i];
printf("serving on %s", mtpt);
} else if (scmp(argv[i], "-s")) {
usocket = argv[++i];
printf("serving socket namespace %s", usocket);
}
}
}
/* create a single file called 'hello'. reading it calls fsread() */
2021-06-13 17:22:58 +00:00
tree = alloctree(nil, nil, DMDIR | 0555, nil);
fs.tree = tree;
fs.foreground = 1;
createfile(tree->root, "hello", nil, 0555, nil);
if (argc >= 3) {
if (mtpt != nil && access(mtpt, AEXIST) < 0 && access(mtpt, AEXIST) < 0)
sysfatal("mountpoint %s does not exist", mtpt);
2021-06-13 17:22:58 +00:00
threadpostmountsrv(&fs, usocket, mtpt, MREPL | MCREATE);
threadexits(0);
} else {
srv(&fs);
}
}