From 9d81d9d6ab7969be1c152dba9b83537d5d2ebbb2 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Sun, 13 Jun 2021 11:22:58 -0600 Subject: [PATCH] first commit -- hello from 9p --- .clang-format | 23 +++++++++++++++++++++ .gitignore | 2 ++ build.sh | 6 ++++++ mkfile | 10 +++++++++ xrxs.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100755 build.sh create mode 100644 mkfile create mode 100644 xrxs.c diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a699815 --- /dev/null +++ b/.clang-format @@ -0,0 +1,23 @@ +TabWidth: 2 +IndentWidth: 2 +ContinuationIndentWidth: 2 +UseTab: Never +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: true +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +PenaltyReturnTypeOnItsOwnLine: 255 +IndentCaseLabels: true +SpaceBeforeParens: ControlStatements +AlignAfterOpenBracket: AlwaysBreak +BinPackArguments: false +BinPackArguments: false +PointerAlignment: Left +BreakBeforeBraces: Attach +SortIncludes: false +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AlignEscapedNewlines: Left + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a826c87 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +xrxs diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..d7308df --- /dev/null +++ b/build.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +clang-format -i ./*.c + +mk o.xrxs +mv o.xrxs xrxs diff --git a/mkfile b/mkfile new file mode 100644 index 0000000..6653757 --- /dev/null +++ b/mkfile @@ -0,0 +1,10 @@ +<$PLAN9/src/mkhdr + +SHORTLIB=9p + +TARG=xrxs + +OFILES=\ + xrxs.$O\ + +<$PLAN9/src/mkone \ No newline at end of file diff --git a/xrxs.c b/xrxs.c new file mode 100644 index 0000000..eb01a61 --- /dev/null +++ b/xrxs.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include <9p.h> +#include + +/* 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 */ + +void fsread(Req* r) { + static int i = 0; + char numReads[16]; + i = i + 1; + sprintf(numReads, "%d reads\n", i); + readstr(r, "Hello from 9P!\n"); + readstr(r, numReads); + respond(r, nil); +} + +Srv fs = { + .read = fsread, +}; + +void threadmain(int argc, char** argv) { + Tree* tree; + char* mtpt = argv[1]; + + if (argc < 1) + sysfatal("supply a mountpoint"); + + tree = alloctree(nil, nil, DMDIR | 0555, nil); + fs.tree = tree; + fs.foreground = 1; + createfile(tree->root, "hello", nil, 0555, nil); + + if (mtpt && access(mtpt, AEXIST) < 0 && access(mtpt, AEXIST) < 0) + sysfatal("mountpoint %s does not exist", mtpt); + + threadpostmountsrv(&fs, nil, mtpt, MREPL | MCREATE); + threadexits(0); +} \ No newline at end of file