static int xread(int argc, char *argv[]) { IxpCFid *fid; char *file, *buf; int count; ARGBEGIN{ default: usage(); }ARGEND; file = EARGF(usage()); fid = ixp_open(client, file, P9_OREAD); if(fid == nil) fatal("Can't open file '%s': %s\n", file, ixp_errbuf()); buf = emalloc(fid->iounit); while((count = ixp_read(fid, buf, fid->iounit)) > 0) write(1, buf, count); if(count == -1) fatal("cannot read file/directory '%s': %s\n", file, ixp_errbuf()); return 0; }
static PyObject * Wmii_write(Wmii *self, PyObject *args) { const char *file; const char *data; IxpCFid *fid; if (!PyArg_ParseTuple(args, "ss", &file, &data)) { PyErr_SetString(PyExc_TypeError, "Wmii.write() takes exactly 2 arguments"); return NULL; } fid = ixp_open(self->client, file, P9_OWRITE); if(fid == NULL) { PyErr_SetObject(PyExc_IOError, PyString_FromFormat("Can't open file '%s'\n", file)); return NULL; } ixp_write(fid, data, strlen(data)); ixp_close(fid); Py_RETURN_NONE; }
static PyObject * Wmii_ls(Wmii *self, PyObject *args) { const char *file; char *buf; PyArg_ParseTuple(args, "s", &file); int count; PyObject *list; IxpCFid *fid; IxpStat stat; IxpMsg msg; fid = ixp_open(self->client, file, P9_OREAD); buf = malloc(fid->iounit); list = PyList_New(0); while( (count = ixp_read(fid, buf, fid->iounit)) > 0 ) { msg = ixp_message(buf, count, MsgUnpack); while(msg.pos < msg.end) { ixp_pstat(&msg, &stat); PyList_Append(list, PyString_FromString(stat.name)); } } ixp_close(fid); free(buf); return list; }
static int xread(int argc, char *argv[]) { IxpCFid *fid; char *file, *buf; int count; ARGBEGIN{ default: usage(); }ARGEND; if(argc == 0) usage(); file = EARGF(usage()); do { fid = ixp_open(client, file, P9_OREAD); if(fid == nil) fatal("Can't open file '%s': %r\n", file); buf = emalloc(fid->iounit); while((count = ixp_read(fid, buf, fid->iounit)) > 0) write(1, buf, count); ixp_close(fid); if(count == -1) fprint(2, "%s: cannot read file '%s': %r\n", argv0, file); } while((file = ARGF())); return 0; }
static int xawrite(int argc, char *argv[]) { IxpCFid *fid; char *file, *buf; int nbuf, i; ARGBEGIN{ default: usage(); }ARGEND; file = EARGF(usage()); fid = ixp_open(client, file, P9_OWRITE); if(fid == nil) fatal("Can't open file '%s': %r\n", file); nbuf = 1; for(i=0; i < argc; i++) nbuf += strlen(argv[i]) + (i > 0); buf = emalloc(nbuf); buf[0] = '\0'; while(argc) { strcat(buf, ARGF()); if(argc) strcat(buf, " "); } if(ixp_write(fid, buf, nbuf) == -1) fatal("cannot write file '%s': %r\n", file); ixp_close(fid); free(buf); return 0; }
char* readctl(char *key) { char *s, *p; int nkey, n; if(ctlfid == nil) { ctlfid = ixp_open(client, "ctl", OREAD); n = ixp_read(ctlfid, ctl, 1023); ectl = ctl + n; ixp_close(ctlfid); } nkey = strlen(key); p = ctl - 1; do { p++; if(!strncmp(p, key, nkey)) { p += nkey; s = strchr(p, '\n'); n = (s ? s : ectl) - p; s = freelater(emalloc(n + 1)); s[n] = '\0'; return strncpy(s, p, n); } } while((p = strchr(p, '\n'))); return ""; }
static PyObject * Wmii_read(Wmii *self, PyObject *args) { const char *file; char *readbuf; unsigned int count = 1; char *buf; unsigned int len; unsigned int size; PyObject *outstr; IxpCFid *fid; PyArg_ParseTuple(args, "s", &file); fid = ixp_open(self->client, file, P9_OREAD); readbuf = malloc(fid->iounit); len = 1; size = fid->iounit; buf = malloc(size); buf[0] = '\0'; while( (count = ixp_read(fid, readbuf, fid->iounit)) > 0 ) { while( (len+count) > size ) { size <<= 1; buf = realloc(buf, size); } strcpy(&buf[len-1], readbuf); len += count; } ixp_close(fid); outstr = PyString_FromString(buf); free(buf); free(readbuf); return outstr; }
static int xwrite(int argc, char *argv[]) { IxpCFid *fid; char *file; ARGBEGIN{ default: usage(); }ARGEND; file = EARGF(usage()); fid = ixp_open(client, file, P9_OWRITE); if(fid == nil) fatal("Can't open file '%s': %s\n", file, ixp_errbuf()); write_data(fid, file); return 0; }
/* Service Functions */ static int xappend(int argc, char *argv[]) { IxpCFid *fid; IxpStat *stat; char *file; ARGBEGIN{ default: usage(); }ARGEND; file = EARGF(usage()); fid = ixp_open(client, file, P9_OWRITE); if(fid == nil) fatal("Can't open file '%s': %s\n", file, ixp_errbuf()); stat = ixp_stat(client, file); fid->offset = stat->length; ixp_freestat(stat); free(stat); write_data(fid, file); return 0; }
static int xawrite(int argc, char *argv[]) { IxpCFid *fid; char *file, *buf, *arg; int nbuf, mbuf, len; ARGBEGIN{ default: usage(); }ARGEND; file = EARGF(usage()); fid = ixp_open(client, file, P9_OWRITE); if(fid == nil) fatal("Can't open file '%s': %s\n", file, ixp_errbuf()); nbuf = 0; mbuf = 128; buf = emalloc(mbuf); while(argc) { arg = ARGF(); len = strlen(arg); if(nbuf + len > mbuf) { mbuf <<= 1; buf = ixp_erealloc(buf, mbuf); } memcpy(buf+nbuf, arg, len); nbuf += len; if(argc) buf[nbuf++] = ' '; } if(ixp_write(fid, buf, nbuf) == -1) fatal("cannot write file '%s': %s\n", file, ixp_errbuf()); return 0; }
static int xls(int argc, char *argv[]) { IxpMsg m; Stat *stat; IxpCFid *fid; char *file, *buf; int lflag, dflag, count, nstat, mstat, i; lflag = dflag = 0; ARGBEGIN{ case 'l': lflag++; break; case 'd': dflag++; break; default: usage(); }ARGEND; file = EARGF(usage()); stat = ixp_stat(client, file); if(stat == nil) fatal("cannot stat file '%s': %s\n", file, ixp_errbuf()); if(dflag || (stat->mode&P9_DMDIR) == 0) { print_stat(stat, lflag); ixp_freestat(stat); return 0; } ixp_freestat(stat); fid = ixp_open(client, file, P9_OREAD); if(fid == nil) fatal("Can't open file '%s': %s\n", file, ixp_errbuf()); nstat = 0; mstat = 16; stat = emalloc(sizeof(*stat) * mstat); buf = emalloc(fid->iounit); while((count = ixp_read(fid, buf, fid->iounit)) > 0) { m = ixp_message(buf, count, MsgUnpack); while(m.pos < m.end) { if(nstat == mstat) { mstat <<= 1; stat = ixp_erealloc(stat, sizeof(*stat) * mstat); } ixp_pstat(&m, &stat[nstat++]); } } qsort(stat, nstat, sizeof(*stat), comp_stat); for(i = 0; i < nstat; i++) { print_stat(&stat[i], lflag); ixp_freestat(&stat[i]); } free(stat); if(count == -1) fatal("cannot read directory '%s': %s\n", file, ixp_errbuf()); return 0; }
static int xls(int argc, char *argv[]) { IxpMsg m; Stat *stat; IxpCFid *fid; char *file; char *buf; int lflag, dflag, pflag; int count, nstat, mstat, i; lflag = dflag = pflag = 0; ARGBEGIN{ case 'l': lflag++; break; case 'd': dflag++; break; case 'p': pflag++; break; default: usage(); }ARGEND; count = 0; file = EARGF(usage()); do { stat = ixp_stat(client, file); if(stat == nil) fatal("cannot stat file '%s': %r\n", file); i = strlen(file); if(file[i-1] == '/') { file[i-1] = '\0'; if(!(stat->mode&P9_DMDIR)) fatal("%s: not a directory", file); } if(dflag || (stat->mode&P9_DMDIR) == 0) { print_stat(stat, lflag, file, pflag); ixp_freestat(stat); continue; } ixp_freestat(stat); fid = ixp_open(client, file, P9_OREAD); if(fid == nil) fatal("Can't open file '%s': %r\n", file); nstat = 0; mstat = 16; stat = emalloc(mstat * sizeof *stat); buf = emalloc(fid->iounit); while((count = ixp_read(fid, buf, fid->iounit)) > 0) { m = ixp_message(buf, count, MsgUnpack); while(m.pos < m.end) { if(nstat == mstat) { mstat <<= 1; stat = erealloc(stat, mstat * sizeof *stat); } ixp_pstat(&m, &stat[nstat++]); } } ixp_close(fid); qsort(stat, nstat, sizeof *stat, comp_stat); for(i = 0; i < nstat; i++) { print_stat(&stat[i], lflag, file, pflag); ixp_freestat(&stat[i]); } free(stat); } while((file = ARGF())); if(count == -1) fatal("cannot read directory '%s': %r\n", file); return 0; }