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; }
/* Utility Functions */ static void write_data(IxpCFid *fid, char *name) { void *buf; long len; buf = emalloc(fid->iounit);; do { len = read(0, buf, fid->iounit); if(len >= 0 && ixp_write(fid, buf, len) != len) fatal("cannot write file '%s': %s\n", name, ixp_errbuf()); } while(len > 0); free(buf); }
static int xremove(int argc, char *argv[]) { char *file; ARGBEGIN{ default: usage(); }ARGEND; file = EARGF(usage()); if(ixp_remove(client, file) == 0) fatal("Can't remove file '%s': %s\n", file, ixp_errbuf()); 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; }
/** * Function: ixp_sendmsg * Function: ixp_recvmsg * * These functions read and write messages to and from the given * file descriptors. * * ixp_sendmsg writes the data at P<msg>->pos upto P<msg>->end. * If the call returns non-zero, all data is assured to have * been written. * * ixp_recvmsg first reads a 32 bit, little-endian length from * P<fd> and then reads a message of that length (including the * 4 byte size specifier) into the buffer at P<msg>->data, so * long as the size is less than P<msg>->size. * * Returns: * These functions return the number of bytes read or * written, or 0 on error. Errors are stored in * F<ixp_errbuf>. */ uint ixp_sendmsg(int fd, IxpMsg *msg) { int r; msg->pos = msg->data; while(msg->pos < msg->end) { r = thread->write(fd, msg->pos, msg->end - msg->pos); if(r < 1) { if(errno == EINTR) continue; werrstr("broken pipe: %s", ixp_errbuf()); return 0; } msg->pos += r; } return msg->pos - msg->data; }
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; }
static int readn(int fd, IxpMsg *msg, uint count) { uint num; int r; num = count; while(num > 0) { r = mread(fd, msg, num); if(r == -1 && errno == EINTR) continue; if(r == 0) { werrstr("broken pipe: %s", ixp_errbuf()); return count - num; } num -= r; } return count - num; }
static int xcreate(int argc, char *argv[]) { IxpCFid *fid; char *file; ARGBEGIN{ default: usage(); }ARGEND; file = EARGF(usage()); fid = ixp_create(client, file, 0777, P9_OWRITE); if(fid == nil) fatal("Can't create file '%s': %s\n", file, ixp_errbuf()); if((fid->qid.type&P9_DMDIR) == 0) write_data(fid, file); return 0; }
int main(int argc, char **argv) { int i; UpClient *upclient; IxpClient *client; signals_setup(&quit_handler); client = ixp_nsmount("wmii"); if(client == NULL) { printf("ixp_nsmount: %s\n", ixp_errbuf()); abort(); } mainloop = g_main_loop_new(NULL, FALSE); upclient = up_client_new(); sb_init(&sb, client); sb_add(&sb, &sbe_ac); for(i = 0; i < MAX_BATTERIES; i++) { sb_add(&sb, &sbe_batteries[i]); } up_client_enumerate_devices_sync(upclient, NULL, NULL); g_signal_connect(upclient, "device-added", G_CALLBACK(device_added_cb), NULL); g_signal_connect(upclient, "device-removed", G_CALLBACK(device_removed_cb), NULL); g_signal_connect(upclient, "device-changed", G_CALLBACK(device_changed_cb), NULL); g_signal_connect(upclient, "changed", G_CALLBACK(changed_cb), NULL); update_sb(upclient); g_main_loop_run(mainloop); sb_finish(&sb); ixp_unmount(client); }
/* 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 errfmt(Fmt *f) { return fmtstrcpy(f, ixp_errbuf()); }
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; }
int main(int argc, char **argv) { int n, nfds, res; struct itimerspec timerits; struct epoll_event events[MAX_EVENTS]; struct epoll_event timerevent; IxpClient* client; struct sb sb; signals_setup(&quit_handler); struct sb_entry sbe_sda = { .sbe_path = "/rbar/60_sda", .sbe_private = "sda", .sbe_init = &init_block, .sbe_update = &update_block, .sbe_foreground = 0xbbbbbb, .sbe_background = 0x444444, .sbe_border = 0x555555, }; struct sb_entry sbe_sdb = { .sbe_path = "/rbar/61_sdb", .sbe_private = "sdb", .sbe_init = &init_block, .sbe_update = &update_block, .sbe_foreground = 0xbbbbbb, .sbe_background = 0x444444, .sbe_border = 0x555555, }; struct sb_entry sbe_sdc = { .sbe_path = "/rbar/62_sdc", .sbe_private = "sdc", .sbe_init = &init_block, .sbe_update = &update_block, .sbe_foreground = 0xbbbbbb, .sbe_background = 0x444444, .sbe_border = 0x555555, }; int epollfd = epoll_create1(EPOLL_CLOEXEC); if(epollfd == -1) { perror("epoll_create"); abort(); } int timerfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC); if(timerfd == -1) { perror("timerfd_create"); abort(); } timerevent.events = EPOLLIN; timerevent.data.fd = timerfd; timerits.it_interval.tv_sec = 0; timerits.it_interval.tv_nsec = 250 * 1000 * 1000; timerits.it_value.tv_sec = timerits.it_interval.tv_sec; timerits.it_value.tv_nsec = timerits.it_interval.tv_nsec; client = ixp_nsmount("wmii"); if(client == NULL) { printf("ixp_nsmount: %s\n", ixp_errbuf()); abort(); } res = epoll_ctl(epollfd, EPOLL_CTL_ADD, timerfd, &timerevent); if(res == -1) { perror("epoll_ctl"); abort(); } res = timerfd_settime(timerfd, 0, &timerits, NULL); if(res == -1) { perror("timerfd_settime"); abort(); } sb_init(&sb, client); sb_add(&sb, &sbe_sda); sb_add(&sb, &sbe_sdb); sb_add(&sb, &sbe_sdc); while(1) { nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); if(nfds == -1) { if(errno != EINTR) { perror("epoll_wait"); abort(); } } if(should_quit) { break; } for (n = 0; n < nfds; n++) { if(events[n].data.fd == timerfd) { uint64_t x; read(timerfd, &x, sizeof(x)); sb_update(&sb); } } } sb_finish(&sb); ixp_unmount(client); return 0; }
void rerrstr(char *buf, int n) { strncpy(buf, ixp_errbuf(), n); }