2010-05-23 03:02:12 +04:00
|
|
|
/* Copyight ©2007-2010 Kris Maglione <maglione.k at Gmail>
|
2007-03-26 08:48:00 +04:00
|
|
|
* See LICENSE file for license details.
|
|
|
|
*/
|
2007-07-01 15:08:30 +04:00
|
|
|
#define IXP_NO_P9_
|
|
|
|
#define IXP_P9_STRUCTS
|
2010-05-30 23:18:15 +04:00
|
|
|
#include <dirent.h>
|
2010-08-13 04:10:13 +04:00
|
|
|
#include <errno.h>
|
2010-06-06 13:07:24 +04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
2007-03-26 08:48:00 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2010-08-13 04:10:13 +04:00
|
|
|
#include <sys/stat.h>
|
2010-05-28 08:29:31 +04:00
|
|
|
#include <sys/signal.h>
|
2007-03-26 08:48:00 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2010-06-06 13:07:24 +04:00
|
|
|
#include <wchar.h>
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
#include <ixp.h>
|
2010-05-22 06:52:47 +04:00
|
|
|
#include <stuff/util.h>
|
2010-05-30 23:29:30 +04:00
|
|
|
#include <bio.h>
|
2007-07-01 15:08:30 +04:00
|
|
|
#include <fmt.h>
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2021-10-15 23:12:16 +03:00
|
|
|
char *argv0;
|
|
|
|
|
2010-06-06 13:07:24 +04:00
|
|
|
static IxpClient* client;
|
|
|
|
static Biobuf* outbuf;
|
|
|
|
static bool binary;
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
static void
|
2007-05-25 01:42:33 +04:00
|
|
|
usage(void) {
|
2010-06-06 13:07:24 +04:00
|
|
|
lprint(1,
|
|
|
|
"usage: %s [-a <address>] [-b] {create | ls [-dlp] | read | remove | write} <file>\n"
|
2009-10-13 06:07:51 +04:00
|
|
|
" %s [-a <address>] xwrite <file> <data>\n"
|
|
|
|
" %s -v\n", argv0, argv0, argv0);
|
2007-03-26 08:48:00 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-07-01 15:08:30 +04:00
|
|
|
static int
|
|
|
|
errfmt(Fmt *f) {
|
|
|
|
return fmtstrcpy(f, ixp_errbuf());
|
|
|
|
}
|
|
|
|
|
2010-06-06 13:07:24 +04:00
|
|
|
static bool
|
|
|
|
flush(IxpCFid *fid, char *in, int len, bool binary) {
|
|
|
|
static mbstate_t state;
|
|
|
|
static char buf[IXP_MAX_MSG];
|
|
|
|
static char *out = buf, *outend = buf + sizeof buf;
|
|
|
|
char *inend;
|
|
|
|
wchar_t w;
|
|
|
|
Rune r;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if(binary)
|
|
|
|
return ixp_write(fid, in, len) == len;
|
|
|
|
|
|
|
|
inend = in + len;
|
|
|
|
do {
|
|
|
|
if(in == nil || out + UTFmax > outend) {
|
|
|
|
if(ixp_write(fid, buf, out - buf) != out - buf)
|
|
|
|
return false;
|
|
|
|
out = buf;
|
|
|
|
}
|
|
|
|
if(in == nil) {
|
|
|
|
state = (mbstate_t){0};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch((res = mbrtowc(&w, in, inend - in, &state))) {
|
|
|
|
case -1:
|
|
|
|
return false;
|
|
|
|
case 0:
|
|
|
|
case -2:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
in += res;
|
|
|
|
r = w < Runemax ? w : Runesync;
|
|
|
|
out += runetochar(out, &r);
|
|
|
|
}
|
|
|
|
} while(in < inend);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
unflush(int fd, char *in, int len, bool binary) {
|
|
|
|
static mbstate_t state;
|
|
|
|
static char buf[IXP_MAX_MSG], extra[UTFmax];
|
|
|
|
static char *out = buf, *outend = buf + sizeof buf;
|
|
|
|
static int nextra;
|
|
|
|
char *start;
|
|
|
|
Rune r;
|
|
|
|
int res, n;
|
|
|
|
|
|
|
|
if(binary)
|
|
|
|
return write(fd, in, len) == len;
|
|
|
|
|
|
|
|
if(in) {
|
|
|
|
if((n = nextra)) {
|
|
|
|
nextra = 0;
|
|
|
|
while(len > 0 && n < UTFmax && !fullrune(extra, n)) {
|
|
|
|
extra[n++] = *in++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
unflush(fd, extra, n, binary);
|
|
|
|
}
|
|
|
|
n = utfnlen(in, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
start = in;
|
|
|
|
do {
|
|
|
|
if(in == nil || out + MB_LEN_MAX > outend) {
|
|
|
|
if(write(fd, buf, out - buf) != out - buf)
|
|
|
|
return false;
|
|
|
|
out = buf;
|
|
|
|
}
|
|
|
|
if(in == nil || n == 0) {
|
|
|
|
state = (mbstate_t){0};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
in += chartorune(&r, in);
|
|
|
|
n--;
|
|
|
|
res = wcrtomb(out, r, &state);
|
|
|
|
if(res == -1)
|
|
|
|
*out++ = '?';
|
|
|
|
else
|
|
|
|
out += res;
|
|
|
|
} while(n > 0);
|
2010-06-24 18:39:42 +04:00
|
|
|
|
2010-06-06 13:07:24 +04:00
|
|
|
if(in < start + len) {
|
|
|
|
nextra = min(sizeof extra, len - (in - start));
|
|
|
|
memcpy(extra, in, nextra);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
/* Utility Functions */
|
|
|
|
static void
|
2010-06-06 13:07:24 +04:00
|
|
|
write_data(IxpCFid *fid, char *name, bool binary) {
|
|
|
|
char buf[IXP_MAX_MSG];
|
2007-05-25 00:49:45 +04:00
|
|
|
int len;
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2010-06-06 13:07:24 +04:00
|
|
|
while((len = read(0, buf, fid->iounit)) > 0)
|
|
|
|
if(!flush(fid, buf, len, binary))
|
2008-06-03 06:31:59 +04:00
|
|
|
fatal("cannot write file %q\n", name);
|
2010-06-06 13:07:24 +04:00
|
|
|
|
|
|
|
if(!binary)
|
|
|
|
flush(fid, nil, 0, binary);
|
2007-03-26 08:48:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
comp_stat(const void *s1, const void *s2) {
|
|
|
|
Stat *st1, *st2;
|
|
|
|
|
|
|
|
st1 = (Stat*)s1;
|
|
|
|
st2 = (Stat*)s2;
|
|
|
|
return strcmp(st1->name, st2->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setrwx(long m, char *s) {
|
|
|
|
static char *modes[] = {
|
|
|
|
"---", "--x", "-w-",
|
|
|
|
"-wx", "r--", "r-x",
|
|
|
|
"rw-", "rwx",
|
|
|
|
};
|
|
|
|
strncpy(s, modes[m], 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2007-07-17 00:52:35 +04:00
|
|
|
modestr(uint mode) {
|
2007-03-26 08:48:00 +04:00
|
|
|
static char buf[16];
|
|
|
|
|
|
|
|
buf[0]='-';
|
|
|
|
if(mode & P9_DMDIR)
|
|
|
|
buf[0]='d';
|
|
|
|
buf[1]='-';
|
|
|
|
setrwx((mode >> 6) & 7, &buf[2]);
|
|
|
|
setrwx((mode >> 3) & 7, &buf[5]);
|
|
|
|
setrwx((mode >> 0) & 7, &buf[8]);
|
|
|
|
buf[11] = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-10-13 06:07:51 +04:00
|
|
|
static char*
|
|
|
|
timestr(time_t val) {
|
2007-03-26 08:48:00 +04:00
|
|
|
static char buf[32];
|
|
|
|
|
2009-10-13 06:07:51 +04:00
|
|
|
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M", localtime(&val));
|
2007-03-26 08:48:00 +04:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-02-03 23:06:26 +03:00
|
|
|
print_stat(Stat *s, int lflag, char *file, int pflag) {
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
slash = "";
|
|
|
|
if(pflag)
|
|
|
|
slash = "/";
|
|
|
|
else
|
|
|
|
file = "";
|
|
|
|
|
2007-04-06 23:29:24 +04:00
|
|
|
if(lflag)
|
2010-06-06 13:07:24 +04:00
|
|
|
Blprint(outbuf, "%s %s %s %5llud %s %s%s%s\n",
|
|
|
|
modestr(s->mode), s->uid, s->gid, s->length,
|
|
|
|
timestr(s->mtime), file, slash, s->name);
|
2007-03-26 08:48:00 +04:00
|
|
|
else {
|
|
|
|
if((s->mode&P9_DMDIR) && strcmp(s->name, "/"))
|
2010-06-06 13:07:24 +04:00
|
|
|
Blprint(outbuf, "%s%s%s/\n", file, slash, s->name);
|
2007-03-26 08:48:00 +04:00
|
|
|
else
|
2010-06-06 13:07:24 +04:00
|
|
|
Blprint(outbuf, "%s%s%s\n", file, slash, s->name);
|
2007-03-26 08:48:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Service Functions */
|
|
|
|
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)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("Can't open file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2010-06-06 13:07:24 +04:00
|
|
|
write_data(fid, file, binary);
|
2007-07-03 18:16:14 +04:00
|
|
|
ixp_close(fid);
|
2007-03-26 08:48:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xawrite(int argc, char *argv[]) {
|
|
|
|
IxpCFid *fid;
|
2008-02-03 23:06:26 +03:00
|
|
|
char *file, *buf;
|
|
|
|
int nbuf, i;
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
|
|
|
file = EARGF(usage());
|
|
|
|
fid = ixp_open(client, file, P9_OWRITE);
|
|
|
|
if(fid == nil)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("Can't open file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2008-08-25 20:47:56 +04:00
|
|
|
nbuf = 1;
|
2008-02-03 23:06:26 +03:00
|
|
|
for(i=0; i < argc; i++)
|
2008-05-31 20:00:39 +04:00
|
|
|
nbuf += strlen(argv[i]) + (i > 0);
|
2008-02-03 23:06:26 +03:00
|
|
|
buf = emalloc(nbuf);
|
|
|
|
buf[0] = '\0';
|
2007-03-26 08:48:00 +04:00
|
|
|
while(argc) {
|
2008-02-03 23:06:26 +03:00
|
|
|
strcat(buf, ARGF());
|
2007-03-26 08:48:00 +04:00
|
|
|
if(argc)
|
2008-02-03 23:06:26 +03:00
|
|
|
strcat(buf, " ");
|
2007-03-26 08:48:00 +04:00
|
|
|
}
|
|
|
|
|
2010-06-06 13:07:24 +04:00
|
|
|
if(!(flush(fid, buf, nbuf, binary) && (binary || flush(fid, 0, 0, binary))))
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("cannot write file '%s': %r\n", file);
|
2007-07-03 18:16:14 +04:00
|
|
|
ixp_close(fid);
|
|
|
|
free(buf);
|
2007-03-26 08:48:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xcreate(int argc, char *argv[]) {
|
|
|
|
IxpCFid *fid;
|
|
|
|
char *file;
|
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
|
|
|
file = EARGF(usage());
|
2007-03-27 01:21:05 +04:00
|
|
|
fid = ixp_create(client, file, 0777, P9_OWRITE);
|
2007-03-26 08:48:00 +04:00
|
|
|
if(fid == nil)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("Can't create file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
if((fid->qid.type&P9_DMDIR) == 0)
|
2010-06-06 13:07:24 +04:00
|
|
|
write_data(fid, file, binary);
|
2007-07-03 18:16:14 +04:00
|
|
|
ixp_close(fid);
|
2007-03-26 08:48:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xremove(int argc, char *argv[]) {
|
|
|
|
char *file;
|
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
|
|
|
file = EARGF(usage());
|
2008-01-14 23:42:21 +03:00
|
|
|
do {
|
2008-01-29 01:58:23 +03:00
|
|
|
if(!ixp_remove(client, file))
|
2010-06-06 13:07:24 +04:00
|
|
|
lprint(2, "%s: Can't remove file '%s': %r\n", argv0, file);
|
2008-01-14 23:42:21 +03:00
|
|
|
}while((file = ARGF()));
|
2007-03-26 08:48:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xread(int argc, char *argv[]) {
|
|
|
|
IxpCFid *fid;
|
|
|
|
char *file, *buf;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
2008-01-14 23:42:21 +03:00
|
|
|
if(argc == 0)
|
|
|
|
usage();
|
2007-03-26 08:48:00 +04:00
|
|
|
file = EARGF(usage());
|
2008-01-14 23:42:21 +03:00
|
|
|
do {
|
|
|
|
fid = ixp_open(client, file, P9_OREAD);
|
|
|
|
if(fid == nil)
|
|
|
|
fatal("Can't open file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2008-01-14 23:42:21 +03:00
|
|
|
buf = emalloc(fid->iounit);
|
2011-09-20 07:45:42 +04:00
|
|
|
while((count = ixp_read(fid, buf, fid->iounit)) > 0) {
|
2010-06-06 13:07:24 +04:00
|
|
|
unflush(1, buf, count, binary);
|
2011-09-20 07:45:42 +04:00
|
|
|
if (!binary && count < fid->iounit)
|
|
|
|
unflush(1, 0, 0, binary);
|
|
|
|
}
|
2010-06-06 13:07:24 +04:00
|
|
|
if(!binary)
|
|
|
|
unflush(1, 0, 0, binary);
|
2008-01-14 23:42:21 +03:00
|
|
|
ixp_close(fid);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2008-01-14 23:42:21 +03:00
|
|
|
if(count == -1)
|
2010-06-06 13:07:24 +04:00
|
|
|
lprint(2, "%s: cannot read file '%s': %r\n", argv0, file);
|
2009-03-30 07:01:07 +04:00
|
|
|
} while((file = ARGF()));
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xls(int argc, char *argv[]) {
|
2007-07-01 15:08:30 +04:00
|
|
|
IxpMsg m;
|
2007-03-26 08:48:00 +04:00
|
|
|
Stat *stat;
|
|
|
|
IxpCFid *fid;
|
2007-04-22 08:07:23 +04:00
|
|
|
char *file;
|
2008-01-18 01:50:35 +03:00
|
|
|
char *buf;
|
2008-02-03 23:06:26 +03:00
|
|
|
int lflag, dflag, pflag;
|
|
|
|
int count, nstat, mstat, i;
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2008-02-03 23:06:26 +03:00
|
|
|
lflag = dflag = pflag = 0;
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
case 'l':
|
|
|
|
lflag++;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag++;
|
|
|
|
break;
|
2008-02-03 23:06:26 +03:00
|
|
|
case 'p':
|
|
|
|
pflag++;
|
|
|
|
break;
|
2007-03-26 08:48:00 +04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
2009-10-02 00:42:54 +04:00
|
|
|
count = 0;
|
2007-03-26 08:48:00 +04:00
|
|
|
file = EARGF(usage());
|
2008-02-03 23:06:26 +03:00
|
|
|
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;
|
|
|
|
}
|
2007-03-26 08:48:00 +04:00
|
|
|
ixp_freestat(stat);
|
|
|
|
|
2008-02-03 23:06:26 +03:00
|
|
|
fid = ixp_open(client, file, P9_OREAD);
|
|
|
|
if(fid == nil)
|
|
|
|
fatal("Can't open file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2008-02-03 23:06:26 +03:00
|
|
|
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++]);
|
2007-03-26 08:48:00 +04:00
|
|
|
}
|
|
|
|
}
|
2008-02-03 23:06:26 +03:00
|
|
|
ixp_close(fid);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2008-02-03 23:06:26 +03:00
|
|
|
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()));
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
if(count == -1)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("cannot read directory '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-03 23:06:26 +03:00
|
|
|
static int
|
|
|
|
xnamespace(int argc, char *argv[]) {
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
|
|
|
path = ixp_namespace();
|
|
|
|
if(path == nil)
|
|
|
|
fatal("can't find namespace: %r\n");
|
2010-10-08 00:31:41 +04:00
|
|
|
Blprint(outbuf, "%s", path);
|
2008-02-03 23:06:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-30 23:18:15 +04:00
|
|
|
static int
|
|
|
|
xproglist(int argc, char *argv[]) {
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *de;
|
2010-08-17 03:38:17 +04:00
|
|
|
struct stat st;
|
2010-08-13 04:10:13 +04:00
|
|
|
char *dir, *cwd;
|
|
|
|
int i;
|
2010-05-30 23:18:15 +04:00
|
|
|
|
|
|
|
quotefmtinstall();
|
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
2010-08-13 04:10:13 +04:00
|
|
|
i = 7, cwd = nil;
|
|
|
|
do
|
|
|
|
cwd = erealloc(cwd, 1<<i);
|
|
|
|
while(!getcwd(cwd, 1<<i) && errno == ERANGE);
|
|
|
|
|
2010-05-30 23:18:15 +04:00
|
|
|
while((dir = ARGF()))
|
2010-06-06 13:07:24 +04:00
|
|
|
/* Don't use Blprint. wimenu expects UTF-8. */
|
2010-08-13 04:10:13 +04:00
|
|
|
if(!chdir(cwd) && !chdir(dir) && (d = opendir(dir))) {
|
|
|
|
while((de = readdir(d))) {
|
2010-08-17 03:38:17 +04:00
|
|
|
stat(de->d_name, &st);
|
|
|
|
if(S_ISREG(st.st_mode) && !access(de->d_name, X_OK))
|
2010-05-30 23:29:30 +04:00
|
|
|
Bprint(outbuf, "%q\n", de->d_name);
|
2010-08-13 04:10:13 +04:00
|
|
|
}
|
2010-05-30 23:18:15 +04:00
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2008-01-22 07:21:37 +03:00
|
|
|
static int
|
|
|
|
xsetsid(int argc, char *argv[]) {
|
|
|
|
char *av0;
|
2010-05-30 23:18:15 +04:00
|
|
|
bool dofork;
|
2008-01-22 07:21:37 +03:00
|
|
|
|
|
|
|
av0 = nil;
|
2010-05-30 23:18:15 +04:00
|
|
|
dofork = false;
|
2008-01-22 07:21:37 +03:00
|
|
|
ARGBEGIN{
|
|
|
|
case '0':
|
|
|
|
av0 = EARGF(usage());
|
|
|
|
break;
|
2010-05-30 23:18:15 +04:00
|
|
|
case 'f':
|
|
|
|
dofork = true;
|
|
|
|
break;
|
2008-01-22 07:21:37 +03:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
if(av0 == nil)
|
|
|
|
av0 = argv[0];
|
2009-03-12 00:20:08 +03:00
|
|
|
if(av0 == nil)
|
|
|
|
return 1;
|
2008-01-22 07:21:37 +03:00
|
|
|
|
|
|
|
setsid();
|
2010-05-30 23:18:15 +04:00
|
|
|
if(dofork)
|
|
|
|
switch(fork()) {
|
|
|
|
case 0: break;
|
|
|
|
case -1: fatal("can't fork: %r\n");
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-22 07:21:37 +03:00
|
|
|
execvp(av0, argv);
|
|
|
|
fatal("setsid: can't exec: %r");
|
|
|
|
return 1; /* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2007-03-28 01:39:00 +04:00
|
|
|
typedef struct exectab exectab;
|
|
|
|
struct exectab {
|
|
|
|
char *cmd;
|
|
|
|
int (*fn)(int, char**);
|
2008-02-03 23:06:26 +03:00
|
|
|
} fstab[] = {
|
2008-01-14 23:42:21 +03:00
|
|
|
{"cat", xread},
|
2007-03-28 01:39:00 +04:00
|
|
|
{"create", xcreate},
|
2008-01-22 07:21:37 +03:00
|
|
|
{"ls", xls},
|
|
|
|
{"read", xread},
|
2007-03-28 01:39:00 +04:00
|
|
|
{"remove", xremove},
|
2007-05-25 00:03:15 +04:00
|
|
|
{"rm", xremove},
|
2008-01-22 07:21:37 +03:00
|
|
|
{"write", xwrite},
|
|
|
|
{"xwrite", xawrite},
|
2008-02-03 23:06:26 +03:00
|
|
|
{0, }
|
|
|
|
}, utiltab[] = {
|
|
|
|
{"namespace", xnamespace},
|
2009-10-30 10:06:14 +03:00
|
|
|
{"ns", xnamespace},
|
2010-05-30 23:18:15 +04:00
|
|
|
{"proglist", xproglist},
|
2008-02-03 23:06:26 +03:00
|
|
|
{"setsid", xsetsid},
|
|
|
|
{0, }
|
2007-03-28 01:39:00 +04:00
|
|
|
};
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
2009-10-25 21:47:45 +03:00
|
|
|
char *address;
|
2007-03-28 01:39:00 +04:00
|
|
|
exectab *tab;
|
|
|
|
int ret;
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2010-06-18 02:23:25 +04:00
|
|
|
IXP_ASSERT_VERSION;
|
|
|
|
|
2010-06-06 13:07:24 +04:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
binary = utf8locale();
|
|
|
|
|
2008-06-03 06:31:59 +04:00
|
|
|
quotefmtinstall();
|
2007-07-01 15:08:30 +04:00
|
|
|
fmtinstall('r', errfmt);
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
address = getenv("WMII_ADDRESS");
|
|
|
|
|
|
|
|
ARGBEGIN{
|
2010-06-06 13:07:24 +04:00
|
|
|
case 'b':
|
|
|
|
binary = true;
|
|
|
|
break;
|
2007-03-26 08:48:00 +04:00
|
|
|
case 'v':
|
2010-06-06 13:07:24 +04:00
|
|
|
lprint(1, "%s-" VERSION ", " COPYRIGHT "\n", argv0);
|
2007-03-26 08:48:00 +04:00
|
|
|
exit(0);
|
|
|
|
case 'a':
|
|
|
|
address = EARGF(usage());
|
2007-03-26 23:46:37 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2007-03-26 08:48:00 +04:00
|
|
|
}ARGEND;
|
|
|
|
|
2008-03-08 20:14:09 +03:00
|
|
|
if(argc < 1)
|
|
|
|
usage();
|
|
|
|
|
2010-05-30 23:29:30 +04:00
|
|
|
outbuf = Bfdopen(1, OWRITE);
|
|
|
|
|
2008-02-03 23:06:26 +03:00
|
|
|
for(tab=utiltab; tab->cmd; tab++)
|
2010-05-30 23:29:30 +04:00
|
|
|
if(!strcmp(*argv, tab->cmd)) {
|
|
|
|
ret = tab->fn(argc, argv);
|
|
|
|
goto done;
|
|
|
|
}
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2008-02-03 23:06:26 +03:00
|
|
|
if(address && *address)
|
|
|
|
client = ixp_mount(address);
|
|
|
|
else
|
|
|
|
client = ixp_nsmount("wmii");
|
2007-03-26 08:48:00 +04:00
|
|
|
if(client == nil)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("can't mount: %r\n");
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2010-05-28 08:29:31 +04:00
|
|
|
signal(SIGPIPE, SIG_DFL);
|
|
|
|
|
2008-02-03 23:06:26 +03:00
|
|
|
for(tab=fstab; tab->cmd; tab++)
|
2008-01-14 23:42:21 +03:00
|
|
|
if(strcmp(*argv, tab->cmd) == 0) break;
|
2007-03-28 01:39:00 +04:00
|
|
|
if(tab->cmd == 0)
|
2007-03-26 08:48:00 +04:00
|
|
|
usage();
|
|
|
|
|
2007-03-28 01:39:00 +04:00
|
|
|
ret = tab->fn(argc, argv);
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
ixp_unmount(client);
|
2010-05-30 23:29:30 +04:00
|
|
|
done:
|
|
|
|
Bterm(outbuf);
|
2007-03-26 08:48:00 +04:00
|
|
|
return ret;
|
|
|
|
}
|
2008-02-03 23:06:26 +03:00
|
|
|
|