2005-11-18 18:54:58 +03:00
|
|
|
/*
|
2006-01-20 17:20:24 +03:00
|
|
|
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
2005-11-18 18:54:58 +03:00
|
|
|
* See LICENSE file for license details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-01-05 18:54:14 +03:00
|
|
|
#include <unistd.h>
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
#include "../libixp2/ixp.h"
|
|
|
|
|
|
|
|
#include <cext.h>
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
static IXPClient c = { 0 };
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
static char *version[] = {
|
2005-12-21 18:18:11 +03:00
|
|
|
"wmiir - window manager improved remote - " VERSION "\n"
|
2006-01-20 17:20:24 +03:00
|
|
|
" (C)opyright MMIV-MMVI Anselm R. Garbe\n", 0
|
2005-11-18 18:54:58 +03:00
|
|
|
};
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
static void
|
|
|
|
usage()
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-21 18:18:11 +03:00
|
|
|
fprintf(stderr, "%s",
|
2006-01-05 18:54:14 +03:00
|
|
|
"usage: wmiir [-a <server address>] [-v] <command>\n"
|
2005-12-21 18:18:11 +03:00
|
|
|
" -a server address (default: $WMIIR_SOCKET)\n"
|
|
|
|
" -v version info\n"
|
2006-01-05 18:54:14 +03:00
|
|
|
"valid commands:\n"
|
|
|
|
" create <file> -- creates file and writes data from stdin to file\n"
|
|
|
|
" read <file/dir> -- prints file/directory contents\n"
|
|
|
|
" write <file> -- writes data from stdin to file\n"
|
|
|
|
" remove <file> -- removes file\n");
|
2005-12-21 18:18:11 +03:00
|
|
|
exit(1);
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
2006-01-05 18:54:14 +03:00
|
|
|
static void
|
|
|
|
write_data(unsigned int fid)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-01-05 18:54:14 +03:00
|
|
|
void *data = cext_emallocz(c.fcall.iounit);
|
|
|
|
unsigned long long offset = 0;
|
|
|
|
size_t len = 0;
|
2005-12-21 18:18:11 +03:00
|
|
|
|
2006-01-05 18:54:14 +03:00
|
|
|
while((len = read(0, data, c.fcall.iounit)) > 0) {
|
2005-12-21 18:18:11 +03:00
|
|
|
if(ixp_client_write
|
2006-01-05 18:54:14 +03:00
|
|
|
(&c, fid, offset, len, data) != len) {
|
2005-12-21 18:18:11 +03:00
|
|
|
fprintf(stderr, "wmiir: cannot write file: %s\n", c.errstr);
|
2006-01-05 18:54:14 +03:00
|
|
|
break;
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
2006-01-05 18:54:14 +03:00
|
|
|
offset += len;
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
2006-01-05 18:54:14 +03:00
|
|
|
free(data);
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
static int
|
2006-01-05 18:54:14 +03:00
|
|
|
xcreate(char *file)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-21 18:18:11 +03:00
|
|
|
unsigned int fid;
|
2006-01-05 18:54:14 +03:00
|
|
|
char *p = strrchr(file, '/');
|
2005-12-21 18:18:11 +03:00
|
|
|
|
|
|
|
fid = c.root_fid << 2;
|
|
|
|
/* walk to bottom-most directory */
|
|
|
|
*p = 0;
|
2006-01-05 18:54:14 +03:00
|
|
|
if(ixp_client_walk(&c, fid, file) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot walk to '%s': %s\n", file, c.errstr);
|
2006-01-04 18:23:09 +03:00
|
|
|
return -1;
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
|
|
|
/* create */
|
|
|
|
p++;
|
2006-01-04 18:23:09 +03:00
|
|
|
if(ixp_client_create(&c, fid, p, (unsigned int) 0xff, IXP_OWRITE) == -1) {
|
2006-01-05 18:54:14 +03:00
|
|
|
fprintf(stderr, "wmiir: cannot create file '%s': %s\n", file, c.errstr);
|
2006-01-04 18:23:09 +03:00
|
|
|
return -1;
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
2006-01-05 18:54:14 +03:00
|
|
|
write_data(fid);
|
2006-01-04 18:23:09 +03:00
|
|
|
return ixp_client_close(&c, fid);
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
static int
|
2006-01-05 18:54:14 +03:00
|
|
|
xwrite(char *file)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-01-05 18:54:14 +03:00
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
/* open */
|
|
|
|
unsigned int fid = c.root_fid << 2;
|
2006-01-05 18:54:14 +03:00
|
|
|
if(ixp_client_open(&c, fid, file, IXP_OWRITE) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot open file '%s': %s\n", file, c.errstr);
|
2006-01-04 18:23:09 +03:00
|
|
|
return -1;
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
2006-01-05 18:54:14 +03:00
|
|
|
write_data(fid);
|
2006-01-04 18:23:09 +03:00
|
|
|
return ixp_client_close(&c, fid);
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
static void
|
2006-01-05 18:54:14 +03:00
|
|
|
print_dir(void *result, unsigned int msize)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-21 18:18:11 +03:00
|
|
|
void *p = result;
|
|
|
|
static Stat stat, zerostat = { 0 };
|
|
|
|
unsigned int len = 0;
|
2006-01-05 21:22:01 +03:00
|
|
|
unsigned short size;
|
2005-12-21 18:18:11 +03:00
|
|
|
do {
|
2006-01-07 18:50:05 +03:00
|
|
|
p = ixp_dec_stat(p, &stat, &size);
|
2006-01-05 21:22:01 +03:00
|
|
|
len += size + sizeof(unsigned short);
|
2005-12-21 18:18:11 +03:00
|
|
|
if(stat.qid.type == IXP_QTDIR)
|
|
|
|
fprintf(stdout, "%s/\n", stat.name);
|
|
|
|
else
|
|
|
|
fprintf(stdout, "%s\n", stat.name);
|
|
|
|
stat = zerostat;
|
|
|
|
}
|
|
|
|
while(len < msize);
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
static int
|
2006-01-05 18:54:14 +03:00
|
|
|
xread(char *file)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-01-04 18:23:09 +03:00
|
|
|
unsigned int fid = c.root_fid << 2;
|
|
|
|
int count, is_directory = 0;
|
2005-12-21 18:18:11 +03:00
|
|
|
static unsigned char result[IXP_MAX_MSG];
|
2006-01-05 18:54:14 +03:00
|
|
|
unsigned long long offset = 0;
|
2005-12-21 18:18:11 +03:00
|
|
|
|
|
|
|
/* open */
|
2006-01-05 18:54:14 +03:00
|
|
|
if(ixp_client_open(&c, fid, file, IXP_OREAD) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot open file '%s': %s\n", file, c.errstr);
|
2006-01-04 18:23:09 +03:00
|
|
|
return -1;
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
|
|
|
is_directory = !c.fcall.nwqid || (c.fcall.qid.type == IXP_QTDIR);
|
2006-01-05 18:54:14 +03:00
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
/* read */
|
2006-01-05 18:54:14 +03:00
|
|
|
while((count = ixp_client_read(&c, fid, offset, result, IXP_MAX_MSG)) > 0) {
|
|
|
|
if(is_directory)
|
|
|
|
print_dir(result, count);
|
|
|
|
else {
|
|
|
|
unsigned int i;
|
|
|
|
for(i = 0; i < count; i++)
|
|
|
|
putchar(result[i]);
|
|
|
|
}
|
|
|
|
offset += count;
|
|
|
|
}
|
2006-01-04 18:23:09 +03:00
|
|
|
if(count == -1) {
|
2006-01-05 18:54:14 +03:00
|
|
|
fprintf(stderr, "wmiir: cannot read file/directory '%s': %s\n", file, c.errstr);
|
2006-01-04 18:23:09 +03:00
|
|
|
return -1;
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
2006-01-04 18:23:09 +03:00
|
|
|
return ixp_client_close(&c, fid);
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
static int
|
2006-01-05 18:54:14 +03:00
|
|
|
xremove(char *file)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-21 18:18:11 +03:00
|
|
|
unsigned int fid;
|
|
|
|
|
|
|
|
/* remove */
|
|
|
|
fid = c.root_fid << 2;
|
2006-01-05 18:54:14 +03:00
|
|
|
if(ixp_client_remove(&c, fid, file) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot remove file '%s': %s\n", file, c.errstr);
|
2006-01-04 18:23:09 +03:00
|
|
|
return -1;
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
|
|
|
return 0;
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-01-05 18:54:14 +03:00
|
|
|
int i = 0;
|
|
|
|
char *cmd, *file, *sockfile = getenv("WMIIR_SOCKET");
|
2005-12-21 18:18:11 +03:00
|
|
|
|
|
|
|
/* command line args */
|
2006-01-05 18:54:14 +03:00
|
|
|
if(argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
|
|
|
|
switch (argv[i][1]) {
|
|
|
|
case 'v':
|
|
|
|
fprintf(stderr, "%s", version[0]);
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
if(i + 1 < argc)
|
|
|
|
sockfile = argv[++i];
|
|
|
|
else
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd = argv[argc - 2];
|
|
|
|
file = argv[argc - 1];
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
if(!sockfile) {
|
2005-12-28 17:00:22 +03:00
|
|
|
fprintf(stderr, "%s", "wmiir: error: WMIIR_SOCKET environment not set\n");
|
2005-12-21 18:18:11 +03:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
/* open socket */
|
2006-01-04 18:23:09 +03:00
|
|
|
if(ixp_client_init(&c, sockfile) == -1) {
|
2005-12-21 18:18:11 +03:00
|
|
|
fprintf(stderr, "wmiir: %s\n", c.errstr);
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-01-05 18:54:14 +03:00
|
|
|
|
|
|
|
if(!strncmp(cmd, "create", 7))
|
|
|
|
xcreate(file);
|
|
|
|
else if(!strncmp(cmd, "write", 6))
|
|
|
|
xwrite(file);
|
|
|
|
else if(!strncmp(cmd, "read", 5))
|
|
|
|
xread(file);
|
|
|
|
else if(!strncmp(cmd, "remove", 7))
|
|
|
|
xremove(file);
|
|
|
|
else
|
|
|
|
usage();
|
2005-12-21 18:18:11 +03:00
|
|
|
|
|
|
|
/* close socket */
|
|
|
|
ixp_client_deinit(&c);
|
|
|
|
|
2006-01-05 18:54:14 +03:00
|
|
|
return 0;
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|