ref: 123101f0a15381355138315c6215cf81e959784d
parent: dafd86d6327602cb4c66d80f4e64488c03cd01e8
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Tue Jun 28 11:57:51 EDT 2022
reformat, build with -O2
--- a/9gc.c
+++ b/9gc.c
@@ -16,7 +16,8 @@
#include "c9.h"
#include "parg.h"
-enum {
+enum
+{
Msize = 8192,
Rootfid = 0,
@@ -31,7 +32,8 @@
typedef struct C9aux C9aux;
typedef struct REntry REntry;
-struct C9aux {
+struct C9aux
+{
C9ctx c;
int f;
int flags;
@@ -56,9 +58,9 @@
a = ctx->aux;
*err = 0;
- for (n = 0; n < size; n += r) {
- if ((r = read(a->f, a->rdbuf+n, size-n)) <= 0) {
- if (errno == EINTR)
+ for(n = 0; n < size; n += r){
+ if((r = read(a->f, a->rdbuf+n, size-n)) <= 0){
+ if(errno == EINTR)
continue;
a->flags |= Disconnected;
close(a->f);
@@ -75,11 +77,11 @@
uint32_t n;
int w;
- for (n = 0; n < a->wroff; n += w) {
- if ((w = write(a->f, a->wrbuf+n, a->wroff-n)) <= 0) {
- if (errno == EINTR)
+ for(n = 0; n < a->wroff; n += w){
+ if((w = write(a->f, a->wrbuf+n, a->wroff-n)) <= 0){
+ if(errno == EINTR)
continue;
- if (errno != EPIPE) /* remote end closed */
+ if(errno != EPIPE) /* remote end closed */
perror("write");
return -1;
}
@@ -96,8 +98,8 @@
C9aux *a;
a = ctx->aux;
- if (a->wroff + size > sizeof(a->wrbuf)) {
- if (wrsend(a) != 0 || a->wroff + size > sizeof(a->wrbuf))
+ if(a->wroff + size > sizeof(a->wrbuf)){
+ if(wrsend(a) != 0 || a->wroff + size > sizeof(a->wrbuf))
return NULL;
}
b = a->wrbuf + a->wroff;
@@ -116,39 +118,43 @@
static int
dial(char *s)
{
- struct addrinfo *r, *a, hint = {.ai_flags = AI_ADDRCONFIG, .ai_family = AF_UNSPEC, 0};
+ struct addrinfo hint = {
+ .ai_flags = AI_ADDRCONFIG,
+ .ai_family = AF_UNSPEC,
+ 0
+ }, *r, *a;
char host[64], *port;
int e, f, yes;
- if (strncmp(s, "udp!", 4) == 0) {
+ if(strncmp(s, "udp!", 4) == 0){
hint.ai_socktype = SOCK_DGRAM;
hint.ai_protocol = IPPROTO_UDP;
- } else if (strncmp(s, "tcp!", 4) == 0) {
+ }else if(strncmp(s, "tcp!", 4) == 0){
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;
- } else {
+ }else{
fprintf(stderr, "invalid dial string: %s\n", s);
return -1;
}
- if ((port = strchr(s+4, '!')) == NULL) {
+ if((port = strchr(s+4, '!')) == NULL){
fprintf(stderr, "invalid dial string: %s\n", s);
return -1;
}
- if (snprintf(host, sizeof(host), "%.*s", (int)(port-s-4), s+4) >= (int)sizeof(host)) {
+ if(snprintf(host, sizeof(host), "%.*s", (int)(port-s-4), s+4) >= (int)sizeof(host)){
fprintf(stderr, "host name too large: %s\n", s);
return -1;
}
port++;
- if ((e = getaddrinfo(host, port, &hint, &r)) != 0){
+ if((e = getaddrinfo(host, port, &hint, &r)) != 0){
fprintf(stderr, "%s: %s\n", gai_strerror(e), s);
return -1;
}
f = -1;
yes = 1;
- for (a = r; a != NULL; a = a->ai_next) {
- if ((f = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) < 0)
+ for(a = r; a != NULL; a = a->ai_next){
+ if((f = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) < 0)
continue;
- if (connect(f, a->ai_addr, a->ai_addrlen) == 0) {
+ if(connect(f, a->ai_addr, a->ai_addrlen) == 0){
setsockopt(f, SOL_TCP, TCP_NODELAY, &yes, sizeof(yes));
break;
}
@@ -165,9 +171,9 @@
{
int i, j;
- for (i = j = 0; i < sz; i++) {
+ for(i = j = 0; i < sz; i++){
d[j] = d[i];
- if (d[j] > 31 || d[j] == '\t' || d[j] == '\n')
+ if(d[j] > 31 || d[j] == '\t' || d[j] == '\n')
j++;
}
write(1, d, j);
@@ -180,9 +186,10 @@
C9tag tag;
const char *path[2];
char buf[64];
+ int n;
a = ctx->aux;
- switch (r->type) {
+ switch(r->type){
case Rversion:
c9attach(ctx, &tag, Rootfid, C9nofid, "none", NULL);
break;
@@ -199,13 +206,14 @@
break;
case Rread:
- if (chatoff >= skipuntil)
+ if(chatoff >= skipuntil)
output(r->read.data, r->read.size);
chatoff += r->read.size;
/* fallthrough */
case Ropen:
- if ((a->flags & Joined) == 0 && printjoin) {
- c9write(ctx, &tag, Chatfid, 0, buf, snprintf(buf, sizeof(buf), "JOIN %s to chat\n", nick));
+ if((a->flags & Joined) == 0 && printjoin){
+ n = snprintf(buf, sizeof(buf), "JOIN %s to chat\n", nick);
+ c9write(ctx, &tag, Chatfid, 0, buf, n);
a->flags |= Joined;
}
c9read(ctx, &tag, Chatfid, chatoff, chatoff < skipuntil ? skipuntil-chatoff : Msize);
@@ -230,7 +238,7 @@
const char *path[2];
a = ctx->aux;
- switch (r->type) {
+ switch(r->type){
case Rversion:
c9attach(ctx, &tag, Rootfid, C9nofid, "none", NULL);
break;
@@ -251,27 +259,27 @@
case Rread:
r->read.data[r->read.size] = 0;
- for (s = (char*)r->read.data;;) {
- if ((s = strstr(s, "chat")) == NULL)
+ for(s = (char*)r->read.data;;){
+ if((s = strstr(s, "chat")) == NULL)
break;
- for (b = s; b != (char*)r->read.data && *b != '\n'; b--);
- if (*b == '\n')
+ for(b = s; b != (char*)r->read.data && *b != '\n'; b--);
+ if(*b == '\n')
b++;
- if ((s = strchr(s, '\n')) == NULL)
+ if((s = strchr(s, '\n')) == NULL)
s = (char*)&r->read.data[r->read.size];
else
*s++ = 0;
- if (strstr(b, "tlssrv") == NULL && (e = strchr(b, ' ')) != NULL) {
+ if(strstr(b, "tlssrv") == NULL && (e = strchr(b, ' ')) != NULL){
*e = 0;
fallback:
close(a->f);
- if ((a->f = dial(b)) < 0)
+ if((a->f = dial(b)) < 0)
exit(1);
a->flags = 0;
ctx->r = ctxchatR;
a->wroff = 0;
c9version(ctx, &tag, Msize);
- if (wrsend(a) != 0)
+ if(wrsend(a) != 0)
exit(1);
return;
}
@@ -289,7 +297,7 @@
}
}
-__attribute__ ((format (printf, 1, 2)))
+__attribute__((format(printf, 1, 2)))
static void
ctxerror(const char *fmt, ...)
{
@@ -307,9 +315,9 @@
C9aux *c;
int f;
- if ((f = dial(s)) < 0)
+ if((f = dial(s)) < 0)
return NULL;
- if ((c = calloc(1, sizeof(*c))) == NULL) {
+ if((c = calloc(1, sizeof(*c))) == NULL){
close(f);
return NULL;
}
@@ -330,26 +338,26 @@
C9tag tag;
int i;
- for (i = 0; i < 10; i++) {
-// if ((a = srv(regsrv)) == NULL) {
- if ((a = srv(chatsrv)) != NULL) {
+ for(i = 0; i < 10; i++){
+// if((a = srv(regsrv)) == NULL){
+ if((a = srv(chatsrv)) != NULL){
a->c.r = ctxchatR;
c9version(&a->c, &tag, Msize);
wrsend(a);
return a;
}
-// } else {
+// }else{
// break;
// }
sleep(10);
}
- if (a == NULL)
+ if(a == NULL)
return NULL;
a->c.r = ctxregistryR;
c9version(&a->c, &tag, Msize);
wrsend(a);
- while (c9proc(&a->c) == 0 && a->c.r == ctxregistryR)
+ while(c9proc(&a->c) == 0 && a->c.r == ctxregistryR)
wrsend(a);
return a;
@@ -373,10 +381,10 @@
FD_SET(0, &e);
memset(&t, 0, sizeof(t));
t.tv_sec = 10;
- for (;;) {
+ for(;;){
errno = 0;
- if (select(a->f + 1, &r, NULL, &e, &t) < 0 || FD_ISSET(a->f, &e) || FD_ISSET(0, &e)) {
- if (errno == EINTR)
+ if(select(a->f + 1, &r, NULL, &e, &t) < 0 || FD_ISSET(a->f, &e) || FD_ISSET(0, &e)){
+ if(errno == EINTR)
continue;
return -1;
}
@@ -384,25 +392,25 @@
}
ctx = &a->c;
- if (FD_ISSET(a->f, &r)) {
+ if(FD_ISSET(a->f, &r)){
c9proc(ctx);
- } else if (FD_ISSET(0, &r)) {
+ }else if(FD_ISSET(0, &r)){
s = (char*)a->rdbuf;
sz = sz0 = sprintf(s, "%s → ", nick);
- for (;;) {
- if ((n = read(0, s+sz, sizeof(a->rdbuf)-sz)) > 0)
+ for(;;){
+ if((n = read(0, s+sz, sizeof(a->rdbuf)-sz)) > 0)
sz += n;
else
exit(0);
- if (s[sz-1] != '\n'){
+ if(s[sz-1] != '\n'){
s[sz-1] = '\n';
- } else {
- if (s[sz0] != '\n')
+ }else{
+ if(s[sz0] != '\n')
c9write(ctx, &tag, Chatfid, 0, s, sz);
break;
}
}
- } else {
+ }else{
const char *path[] = {NULL};
c9walk(ctx, &tag, Rootfid, Rootfid, path);
}
@@ -420,10 +428,10 @@
parg_init(&ps);
noecho = 0;
- while ((c = parg_getopt(&ps, argc, argv, "hjedc:")) >= 0) {
- switch (c) {
+ while((c = parg_getopt(&ps, argc, argv, "hjedc:")) >= 0){
+ switch(c){
case 1:
- if (nick != NULL) {
+ if(nick != NULL){
fprintf(stderr, "only one nickname can be specified\n");
return 1;
}
@@ -453,26 +461,26 @@
}
}
- if (nick == NULL) {
+ if(nick == NULL){
fprintf(stderr, "no nickname specified\n");
return 1;
}
- if (noecho && tcgetattr(0, &t) == 0) {
+ if(noecho && tcgetattr(0, &t) == 0){
t.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &t);
}
- for (;;) {
- if ((a = registry()) == NULL)
+ for(;;){
+ if((a = registry()) == NULL)
return 1;
while (chatrw(a) == 0 && wrsend(a) == 0);
- if (a->flags & (Disconnected|Error)) {
+ if(a->flags & (Disconnected|Error)){
a->flags &= ~(Disconnected|Error);
skipuntil = chatoff;
chatoff = 0;
free(a);
- } else {
+ }else{
printf("exiting\n");
exit(1);
}
--- a/9pex.c
+++ b/9pex.c
@@ -33,6 +33,10 @@
uint32_t crc32(const void *data, int len);
+typedef struct Fid Fid;
+typedef struct Id Id;
+typedef struct Tag Tag;
+
enum
{
Canrd = 1<<0,
@@ -39,7 +43,7 @@
Canwr = 1<<1,
};
-typedef struct
+struct Fid
{
char *path; /* full path */
char *name; /* base name */
@@ -53,20 +57,21 @@
uint32_t iounit;
int fd; /* set when it's an opened file */
-}Fid;
+};
-typedef struct
+struct Tag
{
C9tag tag;
-}Tag;
+};
-typedef struct
+struct Id
{
char *name;
uint32_t id;
-}Id;
+};
-static char *t2s[] = {
+static char *t2s[] =
+{
[Tversion-Tversion] = "Tversion",
[Tauth-Tversion] = "Tauth",
[Tattach-Tversion] = "Tattach",
@@ -82,7 +87,8 @@
[Twstat-Tversion] = "Twstat",
};
-static char *modes[] = {
+static char *modes[] =
+{
"read", "write", "rdwr", "exec",
};
@@ -117,13 +123,13 @@
static Id *uids, *gids;
static int numuids, numgids;
-__attribute__ ((format (printf, 1, 2)))
+__attribute__((format(printf, 1, 2)))
static void
trace(const char *fmt, ...)
{
va_list ap;
- if (debug == 0)
+ if(debug == 0)
return;
va_start(ap, fmt);
@@ -141,7 +147,7 @@
FD_ZERO(&r);
FD_SET(in, &r);
FD_ZERO(&w);
- if (rdonly == 0)
+ if(rdonly == 0)
FD_SET(out, &w);
FD_ZERO(&e);
FD_SET(in, &e);
@@ -148,10 +154,11 @@
FD_SET(out, &e);
memset(&t, 0, sizeof(t));
t.tv_usec = 1000;
- for (;;) {
+ for(;;){
errno = 0;
- if (select(max(in, out) + 1, &r, &w, &e, block ? NULL : &t) < 0 || FD_ISSET(in, &e) || FD_ISSET(out, &e)) {
- if (errno == EINTR)
+ if(select(max(in, out) + 1, &r, &w, &e, block ? NULL : &t) < 0 ||
+ FD_ISSET(in, &e) || FD_ISSET(out, &e)){
+ if(errno == EINTR)
continue;
return -1;
}
@@ -159,9 +166,9 @@
}
fl = 0;
- if (FD_ISSET(in, &r))
+ if(FD_ISSET(in, &r))
fl |= Canrd;
- if (FD_ISSET(out, &w))
+ if(FD_ISSET(out, &w))
fl |= Canwr;
return fl;
@@ -173,19 +180,19 @@
uint32_t n;
int w;
- if (wrend == 0)
+ if(wrend == 0)
return 0;
- for (n = 0; n < wrend; n += w) {
+ for(n = 0; n < wrend; n += w){
errno = 0;
- if ((w = write(out, wrbuf+n, wrend-n)) <= 0) {
- if (errno == EINTR)
+ if((w = write(out, wrbuf+n, wrend-n)) <= 0){
+ if(errno == EINTR)
continue;
- if (errno != EPIPE) /* remote end closed */
+ if(errno != EPIPE) /* remote end closed */
perror("write");
return -1;
}
}
- if (debug >= 2)
+ if(debug >= 2)
trace("<- %d bytes, %d left\n", wrend, wroff-wrend);
memmove(wrbuf, wrbuf+wrend, wroff-wrend);
wroff = wroff - wrend;
@@ -198,8 +205,8 @@
{
int i;
- for (i = 0; i < numtags; i++) {
- if (tags[i] != NULL && tags[i]->tag == tag)
+ for(i = 0; i < numtags; i++){
+ if(tags[i] != NULL && tags[i]->tag == tag)
return 1;
}
@@ -209,16 +216,16 @@
static int
statpath(char *path, struct stat *st, char **err)
{
- if (stat(path, st) == 0)
+ if(stat(path, st) == 0)
return 0;
- if (errno == EACCES)
+ if(errno == EACCES)
*err = Eperm;
- else if (errno == ENOMEM)
+ else if(errno == ENOMEM)
*err = Enomem;
- else if (errno == ENOTDIR)
+ else if(errno == ENOTDIR)
*err = Ewalknodir;
- else if (errno == ENOENT)
+ else if(errno == ENOENT)
*err = Enotfound;
else
*err = strerror(errno);
@@ -235,11 +242,12 @@
qid->version = crc32(&st->st_ctim, sizeof(st->st_ctim));
qid->type = C9qtfile;
fmt = st->st_mode & S_IFMT;
- if (fmt == S_IFDIR)
+ if(fmt == S_IFDIR)
qid->type |= C9qtdir;
- if ((st->st_mode & 0222) != 0 && (fmt == S_IFCHR || fmt == S_IFCHR || fmt == S_IFSOCK || fmt == S_IFIFO))
+ if((st->st_mode & 0222) != 0 &&
+ (fmt == S_IFCHR || fmt == S_IFCHR || fmt == S_IFSOCK || fmt == S_IFIFO))
qid->type |= C9qtappend;
- if (iounit != NULL)
+ if(iounit != NULL)
*iounit = st->st_blksize;
}
@@ -250,19 +258,19 @@
struct stat st;
int i;
- for (i = 0; i < numfids; i++) {
- if (fids[i] != NULL && fids[i]->fid == fid) {
+ for(i = 0; i < numfids; i++){
+ if(fids[i] != NULL && fids[i]->fid == fid){
*err = Edupfid;
return NULL;
}
}
- if (statpath(path, &st, err) != 0)
+ if(statpath(path, &st, err) != 0)
return NULL;
- if ((f = calloc(1, sizeof(*f))) == NULL || (f->path = strdup(path)) == NULL) {
+ if((f = calloc(1, sizeof(*f))) == NULL || (f->path = strdup(path)) == NULL){
nomem:
- if (f != NULL) {
+ if(f != NULL){
free(f->path);
free(f);
}
@@ -271,21 +279,21 @@
}
f->fd = -1;
f->name = strrchr(f->path, '/');
- if (f->name == NULL)
+ if(f->name == NULL)
f->name = f->path;
else
f->name++;
- if (f->name[0] == 0)
+ if(f->name[0] == 0)
fprintf(stderr, "%s -> empty file name\n", f->path);
- for (i = 0; i < numfids; i++) {
- if (fids[i] == NULL) {
+ for(i = 0; i < numfids; i++){
+ if(fids[i] == NULL){
fids[i] = f;
break;
}
}
- if (i >= numfids) {
- if ((newfids = realloc(fids, (numfids+1)*sizeof(*fids))) == NULL)
+ if(i >= numfids){
+ if((newfids = realloc(fids, (numfids+1)*sizeof(*fids))) == NULL)
goto nomem;
fids = newfids;
fids[numfids++] = f;
@@ -302,8 +310,8 @@
{
int i;
- for (i = 0; i < numfids; i++) {
- if (fids[i] != NULL && fids[i]->fid == fid) {
+ for(i = 0; i < numfids; i++){
+ if(fids[i] != NULL && fids[i]->fid == fid){
return fids[i];
}
}
@@ -319,12 +327,12 @@
Fid *f;
int i;
- for (i = 0; i < numfids; i++) {
+ for(i = 0; i < numfids; i++){
f = fids[i];
- if (f != NULL && f->fid == fid) {
- if (f->dir != NULL)
+ if(f != NULL && f->fid == fid){
+ if(f->dir != NULL)
closedir(f->dir);
- else if (f->fd >= 0)
+ else if(f->fd >= 0)
close(f->fd);
free(f->path);
free(f);
@@ -346,14 +354,14 @@
m = mode & 0xf;
stm = st->st_mode & 0777;
*err = Eperm;
- if (((stm & 0111) == 0 || (stm & 0444) == 0) && m == C9exec) /* executing needs rx */
+ if(((stm & 0111) == 0 || (stm & 0444) == 0) && m == C9exec) /* executing needs rx */
return 0;
- if ((stm & 0222) == 0 && (m == C9write || m == C9rdwr)) /* writing needs w */
+ if((stm & 0222) == 0 && (m == C9write || m == C9rdwr)) /* writing needs w */
return 0;
- if ((stm & 0444) == 0 && m != C9write) /* reading needs r */
+ if((stm & 0444) == 0 && m != C9write) /* reading needs r */
return 0;
fmt = st->st_mode & S_IFMT;
- if (fmt == S_IFDIR && ((stm & 0111) == 0 || (stm & 0444) == 0)) /* dirs need rx */
+ if(fmt == S_IFDIR && ((stm & 0111) == 0 || (stm & 0444) == 0)) /* dirs need rx */
return 0;
*err = NULL;
@@ -368,17 +376,17 @@
struct stat st;
int i, plen, ellen;
- if ((f = findfid(fid, err)) == NULL)
+ if((f = findfid(fid, err)) == NULL)
return NULL;
- if (el[0] == NULL) { /* nwname = 0 */
+ if(el[0] == NULL){ /* nwname = 0 */
qids[0] = NULL;
- if (fid == nfid)
+ if(fid == nfid)
return f;
return newfid(nfid, f->path, err);
}
- if ((f->qid.type & C9qtdir) == 0) { /* has to be a dir */
+ if((f->qid.type & C9qtdir) == 0){ /* has to be a dir */
*err = Ewalknodir;
return NULL;
}
@@ -385,7 +393,7 @@
p = strdup(f->path);
f = NULL;
- for (i = 0; el[i] != NULL; i++) {
+ for(i = 0; el[i] != NULL; i++){
plen = strlen(p);
ellen = strlen(el[i]);
path = malloc(plen + 1 + ellen + 1);
@@ -394,16 +402,16 @@
memmove(path+plen+1, el[i], ellen);
path[plen+1+ellen] = 0;
- if (!rootescape) {
+ if(!rootescape){
real = realpath(path, NULL);
free(path);
- if (real == NULL)
+ if(real == NULL)
break;
- if (strlen(real) < rootlen) { /* don't escape root */
+ if(strlen(real) < rootlen){ /* don't escape root */
free(real);
real = strdup(rootpath);
}
- } else {
+ }else{
real = path;
}
@@ -410,9 +418,9 @@
free(p);
p = real;
- if (statpath(p, &st, err) != 0)
+ if(statpath(p, &st, err) != 0)
break;
- if (el[i+1] != NULL && !hasperm(&st, C9read, err))
+ if(el[i+1] != NULL && !hasperm(&st, C9read, err))
break;
qids[i] = &walkqids[i];
@@ -420,11 +428,11 @@
}
qids[i] = NULL;
- if (el[i] == NULL) { /* could walk all the way */
+ if(el[i] == NULL){ /* could walk all the way */
f = newfid(nfid, p, err);
- if (f != NULL && f->name[0] == '/' && f->name[1] == 0) /* root */
+ if(f != NULL && f->name[0] == '/' && f->name[1] == 0) /* root */
f->name = "/";
- } else if (i != 0) { /* didn't fail on the first one */
+ }else if(i != 0){ /* didn't fail on the first one */
*err = NULL;
}
free(p);
@@ -438,19 +446,19 @@
int omode;
omode = O_NOFOLLOW;
- if ((mode & 3) == C9read)
+ if((mode & 3) == C9read)
omode |= O_RDONLY;
- else if ((mode & 3) == C9write)
+ else if((mode & 3) == C9write)
omode |= O_WRONLY;
- else if ((mode & 3) == C9rdwr)
+ else if((mode & 3) == C9rdwr)
omode |= O_RDWR;
- if (mode & C9trunc)
+ if(mode & C9trunc)
omode |= O_TRUNC;
#ifdef __linux__
- if (mode & C9rclose)
+ if(mode & C9rclose)
omode |= O_TMPFILE;
#endif
- if ((qidtype & C9qtappend) != 0)
+ if((qidtype & C9qtappend) != 0)
omode |= O_APPEND;
return omode;
@@ -462,33 +470,33 @@
struct stat st;
int omode;
- if (stat(f->path, &st) != 0 || !hasperm(&st, mode, err))
+ if(stat(f->path, &st) != 0 || !hasperm(&st, mode, err))
goto error;
stat2qid(&st, &f->qid, &f->iounit);
- if ((f->qid.type & C9qtdir) != 0) {
- if ((f->dir = opendir(f->path)) == NULL) {
+ if((f->qid.type & C9qtdir) != 0){
+ if((f->dir = opendir(f->path)) == NULL){
*err = strerror(errno);
return -1;
}
f->fd = dirfd(f->dir);
- } else {
+ }else{
omode = mode2unix(mode, f->qid.type);
f->fd = open(f->path, omode);
}
- if (f->fd < 0)
+ if(f->fd < 0)
goto error;
f->mode = mode;
return 0;
error:
- if (*err == NULL)
+ if(*err == NULL)
*err = strerror(errno);
- if (f->dir != NULL)
+ if(f->dir != NULL)
closedir(f->dir);
- else if (f->fd >= 0)
+ else if(f->fd >= 0)
close(f->fd);
f->dir = NULL;
@@ -503,15 +511,15 @@
Id *newuids;
int i;
- for (i = 0; i < numuids; i++) {
- if (uids[i].id == uid)
+ for(i = 0; i < numuids; i++){
+ if(uids[i].id == uid)
return uids[i].name;
}
- if ((p = getpwuid(uid)) == NULL) {
+ if((p = getpwuid(uid)) == NULL){
*err = strerror(errno);
return NULL;
}
- if ((newuids = realloc(uids, sizeof(*uids)*(numuids+1))) == NULL) {
+ if((newuids = realloc(uids, sizeof(*uids)*(numuids+1))) == NULL){
*err = Enomem;
return NULL;
}
@@ -529,15 +537,15 @@
Id *newgids;
int i;
- for (i = 0; i < numgids; i++) {
- if (gids[i].id == gid)
+ for(i = 0; i < numgids; i++){
+ if(gids[i].id == gid)
return gids[i].name;
}
- if ((g = getgrgid(gid)) == NULL) {
+ if((g = getgrgid(gid)) == NULL){
*err = strerror(errno);
return NULL;
}
- if ((newgids = realloc(gids, sizeof(*gids)*(numgids+1))) == NULL) {
+ if((newgids = realloc(gids, sizeof(*gids)*(numgids+1))) == NULL){
*err = Enomem;
return NULL;
}
@@ -561,14 +569,14 @@
stout->mtime = st->st_ctim.tv_sec;
fmt = st->st_mode & S_IFMT;
- if (fmt == S_IFDIR)
+ if(fmt == S_IFDIR)
stout->mode |= C9stdir;
- if (fmt == S_IFCHR || fmt == S_IFCHR || fmt == S_IFSOCK || fmt == S_IFIFO)
+ if(fmt == S_IFCHR || fmt == S_IFCHR || fmt == S_IFSOCK || fmt == S_IFIFO)
stout->mode |= C9stappend;
stout->mode |= st->st_mode & 0x1ff;
- if ((stout->uid = uid2str(st->st_uid, err)) == NULL)
+ if((stout->uid = uid2str(st->st_uid, err)) == NULL)
return -1;
- if ((stout->gid = gid2str(st->st_gid, err)) == NULL)
+ if((stout->gid = gid2str(st->st_gid, err)) == NULL)
return -1;
return 0;
@@ -580,11 +588,11 @@
struct stat st;
int r;
- if (f->fd >= 0)
+ if(f->fd >= 0)
r = fstat(f->fd, &st);
else
r = stat(f->path, &st);
- if (r != 0) {
+ if(r != 0){
*err = strerror(errno);
return -1;
}
@@ -600,14 +608,14 @@
used(c);
*err = 0;
- for (n = 0; n < size; n += r) {
+ for(n = 0; n < size; n += r){
errno = 0;
- if ((r = read(in, rdbuf+n, size-n)) <= 0) {
- if (r == EINTR)
+ if((r = read(in, rdbuf+n, size-n)) <= 0){
+ if(r == EINTR)
continue;
- if (r == 0) {
+ if(r == 0){
eof = 1;
- } else {
+ }else{
*err = 1;
perror("ctxread");
}
@@ -624,8 +632,8 @@
uint8_t *b;
used(c);
- if (wroff + size > wrbufsz) {
- if (wrsend() != 0 || wroff + size > wrbufsz)
+ if(wroff + size > wrbufsz){
+ if(wrsend() != 0 || wroff + size > wrbufsz)
return NULL;
}
b = wrbuf + wroff;
@@ -642,7 +650,7 @@
return 0;
}
-__attribute__ ((format (printf, 1, 2)))
+__attribute__((format(printf, 1, 2)))
static void
ctxerror(const char *fmt, ...)
{
@@ -657,12 +665,12 @@
static int
s9do(C9error e, char **err)
{
- if (e == 0) {
+ if(e == 0){
*err = NULL;
return 0;
}
- switch (e) {
+ switch(e){
case C9Einit: *err = "c9: initialization failed"; break;
case C9Ever: *err = "c9: protocol version doesn't match"; break;
case C9Epkt: *err = "c9: incoming packet error"; break;
@@ -689,15 +697,15 @@
long dirpos[16];
int i, num, res;
- if (size > c->msize - 12) /* make sure it fits */
+ if(size > c->msize - 12) /* make sure it fits */
size = c->msize - 12;
- if (f->dir == NULL) { /* a file */
- if ((p = malloc(size)) == NULL) {
+ if(f->dir == NULL){ /* a file */
+ if((p = malloc(size)) == NULL){
*err = Enomem;
return -1;
}
- if ((n = pread(f->fd, p, size, offset)) < 0) {
+ if((n = pread(f->fd, p, size, offset)) < 0){
*err = strerror(errno);
free(p);
return -1;
@@ -704,7 +712,7 @@
}
res = s9do(s9read(c, tag, p, n), err);
free(p);
- if (res != 0)
+ if(res != 0)
return -1;
trace("<- Rread tag=%d count=%zd data=...\n", tag, n);
return 0;
@@ -711,11 +719,11 @@
}
/* dir */
- if (offset != f->diroffset) {
- if (offset == 0) {
+ if(offset != f->diroffset){
+ if(offset == 0){
rewinddir(f->dir);
f->diroffset = 0;
- } else {
+ }else{
*err = Ebadoffset;
return -1;
}
@@ -723,28 +731,31 @@
res = 0;
num = 0;
- for (i = 0; i < nelem(c9st); i++) {
- dirpos[i] = telldir(f->dir); /* so we can rewind in case another stat doesn't fit */
+ for(i = 0; i < nelem(c9st); i++){
+ /* so we can rewind in case another stat doesn't fit */
+ dirpos[i] = telldir(f->dir);
errno = 0;
- if ((e = readdir(f->dir)) == NULL && errno != 0) {
+ if((e = readdir(f->dir)) == NULL && errno != 0){
*err = strerror(errno);
res = -1;
goto done;
}
- if (e == NULL) /* eof */
+ if(e == NULL) /* eof */
break;
- if (e->d_name[0] == '.' && (e->d_name[1] == 0 || ((e->d_name[1] == '.' && e->d_name[2] == 0))))
+ if(e->d_name[0] == '.' &&
+ (e->d_name[1] == 0 || ((e->d_name[1] == '.' && e->d_name[2] == 0))))
continue;
- if (fstatat(f->fd, e->d_name, &st, 0) != 0) {
+ if(fstatat(f->fd, e->d_name, &st, 0) != 0){
*err = strerror(errno);
- if (fstatat(f->fd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) { /* broken symlink, try to stat the link itself */
+ if(fstatat(f->fd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0){
+ /* broken symlink, try to stat the link itself */
res = -1;
goto done;
}
}
- if (stat2c9stat(e->d_name, &st, &c9st[i], err) != 0) {
+ if(stat2c9stat(e->d_name, &st, &c9st[i], err) != 0){
res = -1;
goto done;
}
@@ -753,16 +764,16 @@
}
i = num;
- if (s9do(s9readdir(c, tag, c9stp, &num, &f->diroffset, size), err) != 0) {
+ if(s9do(s9readdir(c, tag, c9stp, &num, &f->diroffset, size), err) != 0){
res = -1;
goto done;
}
trace("<- Rread tag=%d count=%"PRIu64" data=...\n", tag, f->diroffset - offset);
- if (i != num)
+ if(i != num)
seekdir(f->dir, dirpos[num]);
done:
- for (i = 0; i < num; i++)
+ for(i = 0; i < num; i++)
free(c9stp[i]->name);
return res;
}
@@ -779,26 +790,28 @@
trace("-> %s tag=%d", t2s[t->type-Tversion], t->tag);
err = NULL;
- if (hastag(t->tag)) {
+ if(hastag(t->tag)){
err = Eduptag;
- } else {
- switch (t->type){
+ }else{
+ switch(t->type){
case Tversion:
trace("\n");
- if (s9do(s9version(c), &err) == 0)
+ if(s9do(s9version(c), &err) == 0)
trace("<- Rversion\n");
break;
case Tauth:
- trace(" afid=%d uname=\"%s\" aname=\"%s\"\n", t->auth.afid, t->auth.uname, t->auth.aname);
+ trace(" afid=%d uname=\"%s\" aname=\"%s\"\n",
+ t->auth.afid, t->auth.uname, t->auth.aname);
err = Enoauth;
break;
case Tattach:
- trace(" afid=%d fid=%d uname=\"%s\" aname=\"%s\"\n", t->attach.afid, t->fid, t->attach.uname, t->attach.aname);
- if (t->attach.afid != C9nofid) {
+ trace(" afid=%d fid=%d uname=\"%s\" aname=\"%s\"\n",
+ t->attach.afid, t->fid, t->attach.uname, t->attach.aname);
+ if(t->attach.afid != C9nofid){
err = Eunknownfid;
- } else if ((f = newfid(t->fid, rootpath, &err)) != NULL) {
+ }else if((f = newfid(t->fid, rootpath, &err)) != NULL){
f->name = "/";
- if (s9do(s9attach(c, t->tag, &f->qid), &err) == 0)
+ if(s9do(s9attach(c, t->tag, &f->qid), &err) == 0)
trace("<- Rattach\n");
}
break;
@@ -805,33 +818,37 @@
case Tflush:
trace(" oldtag=%d\n", t->flush.oldtag);
/* FIXME flush it for realz */
- if (s9do(s9flush(c, t->tag), &err) == 0)
+ if(s9do(s9flush(c, t->tag), &err) == 0)
trace("<- Rflush tag=%d\n", t->tag);
break;
case Twalk:
trace(" fid=%d newfid=%d", t->fid, t->walk.newfid);
- for (i = 0; t->walk.wname[i] != NULL; i++)
+ for(i = 0; t->walk.wname[i] != NULL; i++)
trace(" \"%s\"", t->walk.wname[i]);
trace("\n");
walk(t->fid, t->walk.newfid, t->walk.wname, qids, &err);
- if (err == NULL && s9do(s9walk(c, t->tag, qids), &err) == 0) {
+ if(err == NULL && s9do(s9walk(c, t->tag, qids), &err) == 0){
trace("<- Rwalk tag=%d ", t->tag);
- for (i = 0; qids[i] != NULL; i++)
- trace("qid=[path=%"PRIu64" type=0x%02x version=%"PRIu32"] ", qids[i]->path, qids[i]->type, qids[i]->version);
+ for(i = 0; qids[i] != NULL; i++)
+ trace("qid=[path=%"PRIu64" type=0x%02x version=%"PRIu32"] ",
+ qids[i]->path, qids[i]->type, qids[i]->version);
trace("\n");
}
break;
case Topen:
trace(" fid=%d mode=0x%02x\n", t->fid, t->open.mode);
- if ((f = findfid(t->fid, &err)) != NULL) {
- if (f->fd >= 0)
+ if((f = findfid(t->fid, &err)) != NULL){
+ if(f->fd >= 0)
err = Ebotch;
- else if (t->open.mode != C9read && t->open.mode != C9exec)
+ else if(t->open.mode != C9read && t->open.mode != C9exec)
err = Eperm;
- else if (t->open.mode != C9read && (f->qid.type & C9qtdir) != 0)
+ else if(t->open.mode != C9read && (f->qid.type & C9qtdir) != 0)
err = Eisdir;
- else if (openfid(f, t->open.mode, &err) == 0 && s9do(s9open(c, t->tag, &f->qid, f->iounit), &err) == 0)
- trace("<- Ropen tag=%d qid=[path=%"PRIu64" type=0x%02x version=%"PRIu32"] iounit=%d\n", t->tag, f->qid.path, f->qid.type, f->qid.version, f->iounit);
+ else if(openfid(f, t->open.mode, &err) == 0 &&
+ s9do(s9open(c, t->tag, &f->qid, f->iounit), &err) == 0){
+ trace("<- Ropen tag=%d qid=[path=%"PRIu64" type=0x%02x version=%"PRIu32"] iounit=%d\n",
+ t->tag, f->qid.path, f->qid.type, f->qid.version, f->iounit);
+ }
}
break;
case Tcreate:
@@ -840,10 +857,10 @@
break;
case Tread:
trace(" fid=%d offset=%"PRIu64" count=%"PRIu32"\n", t->fid, t->read.offset, t->read.size);
- if ((f = findfid(t->fid, &err)) != NULL) {
- if ((f->dir == NULL && f->fd < 0) || (f->mode & 0xf) == C9write)
+ if((f = findfid(t->fid, &err)) != NULL){
+ if((f->dir == NULL && f->fd < 0) || (f->mode & 0xf) == C9write)
err = Ebotch;
- else if (readf(c, t->tag, f, t->read.offset, t->read.size, &err) != 0)
+ else if(readf(c, t->tag, f, t->read.offset, t->read.size, &err) != 0)
trace("readf failed\n");
}
break;
@@ -853,7 +870,7 @@
break;
case Tclunk:
trace(" fid=%d\n", t->fid);
- if (delfid(t->fid, &err) == 0 && s9do(s9clunk(c, t->tag), &err) == 0)
+ if(delfid(t->fid, &err) == 0 && s9do(s9clunk(c, t->tag), &err) == 0)
trace("<- Rclunk tag=%d\n", t->tag);
break;
case Tremove:
@@ -862,8 +879,11 @@
break;
case Tstat:
trace(" fid=%d\n", t->fid);
- if ((f = findfid(t->fid, &err)) != NULL && statfid(f, &st, &err) == 0 && s9do(s9stat(c, t->tag, &st), &err) == 0)
+ if((f = findfid(t->fid, &err)) != NULL &&
+ statfid(f, &st, &err) == 0 &&\
+ s9do(s9stat(c, t->tag, &st), &err) == 0){
trace("<- Rstat tag=%d ...\n", t->tag);
+ }
break;
case Twstat:
trace("...\n");
@@ -872,8 +892,8 @@
}
}
- if (err != NULL) {
- if (s9do(s9error(c, t->tag, err), &err2) == 0)
+ if(err != NULL){
+ if(s9do(s9error(c, t->tag, err), &err2) == 0)
trace("<- Rerror tag=%d \"%s\"\n", t->tag, err);
else
fprintf(stderr, "s9error: %s\n", err2);
@@ -888,19 +908,27 @@
used(s);
n = 0;
- for (i = 0; i < numfids; i++) {
+ for(i = 0; i < numfids; i++){
f = fids[i];
- if (f == NULL)
+ if(f == NULL)
continue;
fprintf(stderr, "fid %u ", f->fid);
- if (f->dir != NULL)
+ if(f->dir != NULL)
fprintf(stderr, "open mode=dir ");
- else if (f->fd >= 0)
- fprintf(stderr, "open mode=%s%s%s ", modes[(f->mode & 0xf)], (f->mode & C9trunc) ? ",trunc" : "", (f->mode & C9rclose) ? ",rclose" : "");
+ else if(f->fd >= 0){
+ fprintf(stderr, "open mode=%s%s%s ",
+ modes[(f->mode & 0xf)],
+ (f->mode & C9trunc) ? ",trunc" : "",
+ (f->mode & C9rclose) ? ",rclose" : "");
+ }
- fprintf(stderr, "qid=[path=%"PRIu64" type=0x%02x version=%"PRIu32"] iounit=%d ", f->qid.path, f->qid.type, f->qid.version, f->iounit);
+ fprintf(stderr, "qid=[path=%"PRIu64" type=0x%02x version=%"PRIu32"] iounit=%d ",
+ f->qid.path,
+ f->qid.type,
+ f->qid.version,
+ f->iounit);
fprintf(stderr, " %s %s\n", f->path, f->name);
n++;
}
@@ -925,10 +953,10 @@
debug = 0;
dir = NULL;
- while ((c = parg_getopt(&ps, argc, argv, "deh")) >= 0) {
- switch (c) {
+ while((c = parg_getopt(&ps, argc, argv, "deh")) >= 0){
+ switch(c){
case 1:
- if (dir != NULL) {
+ if(dir != NULL){
fprintf(stderr, "only one dir can be specified\n");
return 1;
}
@@ -955,12 +983,12 @@
}
}
- if (dir == NULL) {
+ if(dir == NULL){
fprintf(stderr, "no dir specified\n");
return 1;
}
- if ((rootpath = realpath(dir, NULL)) == NULL) {
+ if((rootpath = realpath(dir, NULL)) == NULL){
trace("%s: %s\n", dir, strerror(errno));
return 1;
}
@@ -1004,22 +1032,22 @@
err = NULL;
rdonly = block = 1; /* at first we wait until the client sends in data */
- for (; !eof;) {
- if ((can = canrw(rdonly, block)) < 0)
+ for(; !eof;){
+ if((can = canrw(rdonly, block)) < 0)
break;
- if ((can & Canrd) != 0) { /* if there is data, process it */
- if (s9do(s9proc(&ctx), &err) != 0)
+ if((can & Canrd) != 0){ /* if there is data, process it */
+ if(s9do(s9proc(&ctx), &err) != 0)
break;
/* give it a chance to receive all the data first */
rdonly = 1;
block = 0;
- } else if (block == 0) { /* got all the data */
- if (rdonly != 0) { /* wait until we can send OR we get more data */
+ }else if(block == 0){ /* got all the data */
+ if(rdonly != 0){ /* wait until we can send OR we get more data */
rdonly = 0;
block = 1;
}
- } else if (rdonly == 0 && (can & Canwr) != 0) { /* can send */
- if (wrsend() != 0) /* send all the data */
+ }else if(rdonly == 0 && (can & Canwr) != 0){ /* can send */
+ if(wrsend() != 0) /* send all the data */
break;
rdonly = 1; /* and go back to reading */
block = 1;
@@ -1026,14 +1054,14 @@
}
}
- if (err != NULL)
+ if(err != NULL)
trace("s9proc: %s\n", err);
- for (i = 0; i < numfids; i++) {
- if ((f = fids[i]) != NULL) {
- if (f->dir != NULL)
+ for(i = 0; i < numfids; i++){
+ if((f = fids[i]) != NULL){
+ if(f->dir != NULL)
closedir(f->dir);
- else if (f->fd >= 0)
+ else if(f->fd >= 0)
close(f->fd);
free(f->path);
free(f);
@@ -1040,10 +1068,10 @@
}
}
- for (i = 0; i < numuids; i++)
+ for(i = 0; i < numuids; i++)
free(uids[i].name);
free(uids);
- for (i = 0; i < numgids; i++)
+ for(i = 0; i < numgids; i++)
free(gids[i].name);
free(gids);
--- a/build.sh
+++ b/build.sh
@@ -1,5 +1,5 @@
#!/bin/sh
set -e
set -x
-${CC:-gcc} -std=c99 -DC9_NO_SERVER -O0 -g -Wall -Wextra -Wshadow $CFLAGS c9/*.c parg/*.c -Ic9 -Iparg 9gc.c -o 9gc || rm -f 9gc
-${CC:-gcc} -std=c99 -DC9_NO_CLIENT -O0 -g -Wall -Wextra -Wshadow $CFLAGS c9/*.c parg/*.c -Ic9 -Iparg 9pex.c crc32.c -o 9pex || rm -f 9pex
+${CC:-gcc} -std=c99 -DC9_NO_SERVER -O2 -g -Wall -Wextra -Wshadow $CFLAGS c9/*.c parg/*.c -Ic9 -Iparg 9gc.c -o 9gc || rm -f 9gc
+${CC:-gcc} -std=c99 -DC9_NO_CLIENT -O2 -g -Wall -Wextra -Wshadow $CFLAGS c9/*.c parg/*.c -Ic9 -Iparg 9pex.c crc32.c -o 9pex || rm -f 9pex
--- a/c9/c9.h
+++ b/c9/c9.h
@@ -151,12 +151,12 @@
*/
struct C9stat
{
+ C9qid qid; /* Same as qid[0]. */
uint64_t size; /* Size of the file (in bytes). */
char *name; /* Name of the file. */
char *uid; /* Owner of the file. */
char *gid; /* Group of the file. */
char *muid; /* The user who modified the file last. */
- C9qid qid; /* Same as qid[0]. */
uint32_t mode; /* Permissions. See C9st* and C9perm. */
uint32_t atime; /* Last access time. */
uint32_t mtime; /* Last modification time. */
--- a/crc32.c
+++ b/crc32.c
@@ -1,7 +1,8 @@
#define _DEFAULT_SOURCE
#include <stdint.h>
-static uint32_t table[256] = {
+static uint32_t table[256] =
+{
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
@@ -45,7 +46,7 @@
d = data;
c = 0xffffffff;
- for (i = 0; i < len; i++)
+ for(i = 0; i < len; i++)
c = table[(c & 0xff) ^ d[i]] ^ (c >> 8);
return ~c;