SEXP cr_close(SEXP sc) { rconn_t *c; if (!Rf_inherits(sc, "redisConnection")) Rf_error("invalid connection"); c = (rconn_t*) EXTPTR_PTR(sc); rc_close(c); return R_NilValue; }
/* issuue one command with one key paratemer and return the result */ SEXP cr_cmd(SEXP sc, SEXP sArgs) { rconn_t *c; const char **argv = argbuf; int n, i; redisReply *reply; SEXP res; if (!Rf_inherits(sc, "redisConnection")) Rf_error("invalid connection"); c = (rconn_t*) EXTPTR_PTR(sc); if (!c) Rf_error("invalid connection (NULL)"); rc_validate_connection(c, 0); if (TYPEOF(sArgs) != STRSXP || LENGTH(sArgs) < 1) Rf_error("invalid command - must be a string"); n = LENGTH(sArgs); if (n + 1 > NARGBUF) { argv = malloc(sizeof(const char*) * (n + 2)); if (!argv) Rf_error("out of memory"); } for (i = 0; i < n; i++) argv[i] = CHAR(STRING_ELT(sArgs, i)); /* we use strings only, so no need to supply argvlen */ reply = redisCommandArgv(c->rc, n, argv, 0); if (!reply && (c->flags & RCF_RETRY)) { SEXP es = Rf_mkChar(c->rc->errstr); rc_close(c); rc_validate_connection(c, 1); if (c->rc) reply = redisCommandArgv(c->rc, 2, argv, 0); else { if (argv != argbuf) free(argv); Rf_error("%s error: %s and re-connect failed", argv[0], CHAR(es)); } } if (argv != argbuf) free(argv); if (!reply) { SEXP es = Rf_mkChar(c->rc->errstr); rc_close(c); Rf_error("%s error: %s", argv[0], CHAR(es)); } /* Rprintf("reply, type=%d\n", reply->type); */ res = rc_reply2R(reply); freeReplyObject(reply); return res; }
SEXP cr_del(SEXP sc, SEXP keys) { rconn_t *c; int n, i; const char **argv = argbuf; redisReply *reply; if (!Rf_inherits(sc, "redisConnection")) Rf_error("invalid connection"); c = (rconn_t*) EXTPTR_PTR(sc); if (!c) Rf_error("invalid connection (NULL)"); rc_validate_connection(c, 0); if (TYPEOF(keys) != STRSXP) Rf_error("invalid keys"); n = LENGTH(keys); if (n + 1 > NARGBUF) { argv = malloc(sizeof(const char*) * (n + 2)); if (!argv) Rf_error("out of memory"); } argv[0] = "DEL"; for (i = 0; i < n; i++) argv[i + 1] = CHAR(STRING_ELT(keys, i)); /* we use strings only, so no need to supply argvlen */ reply = redisCommandArgv(c->rc, n + 1, argv, 0); if (!reply && (c->flags & RCF_RETRY)) { SEXP es = Rf_mkChar(c->rc->errstr); rc_close(c); rc_validate_connection(c, 1); if (c->rc) reply = redisCommandArgv(c->rc, n + 1, argv, 0); else { if (argv != argbuf) free(argv); Rf_error("DEL error: %s and re-connect failed", CHAR(es)); } } if (argv != argbuf) free(argv); if (!reply) { SEXP es = Rf_mkChar(c->rc->errstr); rc_close(c); Rf_error("DEL error: %s", CHAR(es)); } /* Rprintf("reply, type=%d\n", reply->type); */ freeReplyObject(reply); return R_NilValue; }
SEXP cr_keys(SEXP sc, SEXP sPattern) { rconn_t *c; int n, i; const char *pattern = "*"; redisReply *reply; SEXP res; if (!Rf_inherits(sc, "redisConnection")) Rf_error("invalid connection"); c = (rconn_t*) EXTPTR_PTR(sc); if (!c) Rf_error("invalid connection (NULL)"); rc_validate_connection(c, 0); if (TYPEOF(sPattern) == STRSXP && LENGTH(sPattern) > 0) pattern = CHAR(STRING_ELT(sPattern, 0)); reply = redisCommand(c->rc, "KEYS %s", pattern); if (!reply && (c->flags & RCF_RETRY)) { rc_close(c); rc_validate_connection(c, 0); reply = redisCommand(c->rc, "KEYS %s", pattern); } if (!reply) { SEXP es = Rf_mkChar(c->rc->errstr); rc_close(c); Rf_error("KEYS error: %s", CHAR(es)); } if (reply->type != REDIS_REPLY_ARRAY) { freeReplyObject(reply); Rf_error("unexpected result type"); } res = PROTECT(Rf_allocVector(STRSXP, (n = reply->elements))); for (i = 0; i < n; i++) { if (reply->element[i]->type == REDIS_REPLY_STRING) SET_STRING_ELT(res, i, Rf_mkCharLenCE(reply->element[i]->str, reply->element[i]->len, CE_UTF8)); else if (reply->element[i]->type == REDIS_REPLY_NIL) SET_STRING_ELT(res, i, R_NaString); else { freeReplyObject(reply); Rf_error("invalid element (non-string) in the keys array"); } } freeReplyObject(reply); UNPROTECT(1); return res; }
int main(int argc, char *argv[]) { int islogin, error; char *p, *p1; struct ncp_conn_loginfo li; islogin = strcmp(__progname, "ncplogin") == 0; if (argc == 2) { if (strcmp(argv[1], "-h") == 0) { if (islogin) login_usage(); else logout_usage(); } } if (ncp_initlib()) exit(1); if (ncp_li_init(&li, argc, argv)) return 1; if (argc >= 2 && argv[argc - 1][0] == '/') { p = argv[argc - 1]; error = 1; do { if (*p++ != '/') break; p1 = strchr(p, ':'); if (p1 == NULL) break; *p1++ = 0; if (ncp_li_setserver(&li, p)) break; if (*p1 == 0) break; if (ncp_li_setuser(&li, p1)) break; error = 0; } while(0); if (error) errx(EX_DATAERR, "an error occurred while parsing '%s'", argv[argc - 1]); } if (ncp_li_readrc(&li)) return 1; if (ncp_rc) rc_close(ncp_rc); if (islogin) login(argc, argv, &li); else logout(argc, argv, &li); return 0; }
static void rconn_fin(SEXP what) { rconn_t *c = (rconn_t*) EXTPTR_PTR(what); if (c) { rc_close(c); if (c->host) { free(c->host); c->host = 0; } free(c); } }
static int rcextract(const char *archive, char **members) { RcArchive rca = rc_open_archive(archive, RC_RDONLY); RcMember m; if ( rca ) { for(m=rca->members; m; m = m->next) { if ( memberOfList(m->name, members) ) { FILE *fd = fopen(m->name, "wb"); if ( fd ) { RcObject o = rc_open(rca, m->name, m->rc_class, RC_RDONLY); char buf[8192]; size_t size = m->size; while( size > 0 ) { ssize_t n = rc_read(o, buf, sizeof(buf)); if ( n > 0 ) { if ( fwrite(buf, sizeof(char), (size_t)n, fd) != (size_t)n ) { fclose(fd); error("Failure writing %s: %s", m->name, strerror(errno)); } size -= n; } else if ( n == 0 ) { error("Premature EOF on archive %s", m->name); break; } else { error("Read error on archive %s", m->name); break; } } if ( size == 0 ) verbose("x %s\n", m->name); fclose(fd); rc_close(o); } else error("Could not open %s: %s", m->name, strerror(errno)); } } rc_close_archive(rca); return 0; } return badarchive(archive); }
int cmd_lookup(int argc, char *argv[]) { struct nb_ctx *ctx; struct sockaddr *sap; char *hostname; int error, opt; if (argc < 2) lookup_usage(); error = nb_ctx_create(&ctx); if (error) { smb_error("unable to create nbcontext", error); exit(1); } if (smb_open_rcfile() == 0) { if (nb_ctx_readrcsection(smb_rc, ctx, "default", 0) != 0) exit(1); rc_close(smb_rc); } while ((opt = getopt(argc, argv, "w:")) != EOF) { switch(opt) { case 'w': nb_ctx_setns(ctx, optarg); break; default: lookup_usage(); /*NOTREACHED*/ } } if (optind >= argc) lookup_usage(); if (nb_ctx_resolve(ctx) != 0) exit(1); hostname = argv[argc - 1]; /* printf("Looking for %s...\n", hostname);*/ error = nbns_resolvename(hostname, ctx, &sap); if (error) { smb_error("unable to resolve %s", error, hostname); exit(1); } printf("Got response from %s\n", inet_ntoa(ctx->nb_lastns.sin_addr)); printf("IP address of %s: %s\n", hostname, inet_ntoa(((struct sockaddr_in*)sap)->sin_addr)); return 0; }
int cmd_view(int argc, char *argv[]) { struct smb_ctx sctx, *ctx = &sctx; struct smb_share_info_1 *rpbuf, *ep; char *cp; u_int16_t type; int error, opt, bufsize, i, entries, total; if (argc < 2) view_usage(); if (smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_VC, SMB_ST_ANY) != 0) exit(1); if (smb_ctx_readrc(ctx) != 0) exit(1); if (smb_rc) rc_close(smb_rc); while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) { switch(opt){ case STDPARAM_ARGS: error = smb_ctx_opt(ctx, opt, optarg); if (error) exit(1); break; default: view_usage(); /*NOTREACHED*/ } } #ifdef APPLE if (loadsmbvfs()) errx(EX_OSERR, "SMB filesystem is not available"); #endif smb_ctx_setshare(ctx, "IPC$", SMB_ST_ANY); if (smb_ctx_resolve(ctx) != 0) exit(1); error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE); if (error) { smb_error("could not login to server %s", error, ctx->ct_ssn.ioc_srvname); exit(1); } printf("Share Type Comment\n"); printf("-------------------------------\n"); bufsize = 0xffe0; /* samba notes win2k bug with 65535 */ rpbuf = malloc(bufsize); error = smb_rap_NetShareEnum(ctx, 1, rpbuf, bufsize, &entries, &total); if (error && error != (SMB_ERROR_MORE_DATA | SMB_RAP_ERROR)) { smb_error("unable to list resources", error); exit(1); } for (ep = rpbuf, i = 0; i < entries; i++, ep++) { type = letohs(ep->shi1_type); cp = (char*)rpbuf + ep->shi1_remark; printf("%-12s %-10s %s\n", ep->shi1_netname, shtype[min(type, sizeof shtype / sizeof(char *) - 1)], ep->shi1_remark ? nls_str_toloc(cp, cp) : ""); } printf("\n%d shares listed from %d available\n", entries, total); free(rpbuf); return 0; }
int parse_config(char *cfgfname, const char *devfname) { int err = 0, fd; unsigned int chipsectionscnt, ctrlsectionscnt, i; struct rcfile *f; struct sim_chip *chips; struct sim_ctrl *ctrls; err = rc_open(cfgfname, "r", &f); if (err) { error("could not open configuration file (%s)", cfgfname); return (EX_NOINPUT); } /* First, try to configure simulator itself. */ if (configure_sim(devfname, f) != EX_OK) { rc_close(f); return (EINVAL); } debug("SIM CONFIGURED!\n"); /* Then create controllers' configs */ if (create_ctrls(f, &ctrls, &ctrlsectionscnt) != 0) { rc_close(f); return (ENXIO); } debug("CTRLS CONFIG READ!\n"); /* Then create chips' configs */ if (create_chips(f, &chips, &chipsectionscnt) != 0) { destroy_ctrls(ctrls); rc_close(f); return (ENXIO); } debug("CHIPS CONFIG READ!\n"); if (validate_ctrls(ctrls, ctrlsectionscnt) != 0) { destroy_ctrls(ctrls); destroy_chips(chips); rc_close(f); return (EX_SOFTWARE); } if (validate_chips(chips, chipsectionscnt, ctrls, ctrlsectionscnt) != 0) { destroy_ctrls(ctrls); destroy_chips(chips); rc_close(f); return (EX_SOFTWARE); } /* Open device */ fd = open(devfname, O_RDWR); if (fd == -1) { error("could not open simulator device file (%s)!", devfname); rc_close(f); destroy_chips(chips); destroy_ctrls(ctrls); return (EX_OSFILE); } debug("SIM CONFIG STARTED!\n"); /* At this stage, both ctrls' and chips' configs should be valid */ for (i = 0; i < ctrlsectionscnt; i++) { err = ioctl(fd, NANDSIM_CREATE_CTRL, &ctrls[i]); if (err) { if (err == EEXIST) error("Controller#%d already created\n", ctrls[i].num); else if (err == EINVAL) error("Incorrect controler number (%d)\n", ctrls[i].num); else error("Could not created controller#%d\n", ctrls[i].num); /* Errors during controller creation stops parsing */ close(fd); rc_close(f); destroy_ctrls(ctrls); destroy_chips(chips); return (ENXIO); } debug("CTRL#%d CONFIG STARTED!\n", i); } for (i = 0; i < chipsectionscnt; i++) { err = ioctl(fd, NANDSIM_CREATE_CHIP, &chips[i]); if (err) { if (err == EEXIST) error("Chip#%d for controller#%d already " "created\n", chips[i].num, chips[i].ctrl_num); else if (err == EINVAL) error("Incorrect chip number (%d:%d)\n", chips[i].num, chips[i].ctrl_num); else error("Could not create chip (%d:%d)\n", chips[i].num, chips[i].ctrl_num); error("Could not start chip#%d\n", i); destroy_chips(chips); destroy_ctrls(ctrls); close(fd); rc_close(f); return (ENXIO); } } debug("CHIPS CONFIG STARTED!\n"); close(fd); rc_close(f); destroy_chips(chips); destroy_ctrls(ctrls); return (0); }
int main(int argc, char *argv[]) { struct iovec *iov; unsigned int iovlen; struct smb_ctx sctx, *ctx = &sctx; struct stat st; #ifdef APPLE extern void dropsuid(); extern int loadsmbvfs(); #else struct xvfsconf vfc; #endif char *next, *p, *val; int opt, error, mntflags, caseopt, fd; uid_t uid; gid_t gid; mode_t dir_mode, file_mode; char errmsg[255] = { 0 }; iov = NULL; iovlen = 0; fd = 0; uid = (uid_t)-1; gid = (gid_t)-1; caseopt = 0; file_mode = 0; dir_mode = 0; #ifdef APPLE dropsuid(); #endif if (argc == 2) { if (strcmp(argv[1], "-h") == 0) { usage(); } } if (argc < 3) usage(); #ifdef APPLE error = loadsmbvfs(); #else error = getvfsbyname(smbfs_vfsname, &vfc); if (error) { if (kldload(smbfs_vfsname) < 0) err(EX_OSERR, "kldload(%s)", smbfs_vfsname); error = getvfsbyname(smbfs_vfsname, &vfc); } #endif if (error) errx(EX_OSERR, "SMB filesystem is not available"); if (smb_lib_init() != 0) exit(1); mntflags = error = 0; caseopt = SMB_CS_NONE; if (smb_ctx_init(ctx, argc, argv, SMBL_SHARE, SMBL_SHARE, SMB_ST_DISK) != 0) exit(1); if (smb_ctx_readrc(ctx) != 0) exit(1); if (smb_rc) rc_close(smb_rc); while ((opt = getopt(argc, argv, STDPARAM_OPT"c:d:f:g:l:n:o:u:w:")) != -1) { switch (opt) { case STDPARAM_ARGS: error = smb_ctx_opt(ctx, opt, optarg); if (error) exit(1); break; case 'u': { struct passwd *pwd; pwd = isdigit(optarg[0]) ? getpwuid(atoi(optarg)) : getpwnam(optarg); if (pwd == NULL) errx(EX_NOUSER, "unknown user '%s'", optarg); uid = pwd->pw_uid; break; } case 'g': { struct group *grp; grp = isdigit(optarg[0]) ? getgrgid(atoi(optarg)) : getgrnam(optarg); if (grp == NULL) errx(EX_NOUSER, "unknown group '%s'", optarg); gid = grp->gr_gid; break; } case 'd': errno = 0; dir_mode = strtol(optarg, &next, 8); if (errno || *next != 0) errx(EX_DATAERR, "invalid value for directory mode"); break; case 'f': errno = 0; file_mode = strtol(optarg, &next, 8); if (errno || *next != 0) errx(EX_DATAERR, "invalid value for file mode"); break; case '?': usage(); /*NOTREACHED*/ case 'n': { char *inp, *nsp; nsp = inp = optarg; while ((nsp = strsep(&inp, ",;:")) != NULL) { if (strcasecmp(nsp, "LONG") == 0) { build_iovec(&iov, &iovlen, "nolong", NULL, 0); } else { errx(EX_DATAERR, "unknown suboption '%s'", nsp); } } break; }; case 'o': getmntopts(optarg, mopts, &mntflags, 0); p = strchr(optarg, '='); val = NULL; if (p != NULL) { *p = '\0'; val = p + 1; } build_iovec(&iov, &iovlen, optarg, val, (size_t)-1); break; case 'c': switch (optarg[0]) { case 'l': caseopt |= SMB_CS_LOWER; break; case 'u': caseopt |= SMB_CS_UPPER; break; default: errx(EX_DATAERR, "invalid suboption '%c' for -c", optarg[0]); } break; default: usage(); } } if (optind == argc - 2) optind++; if (optind != argc - 1) usage(); realpath(argv[optind], mount_point); if (stat(mount_point, &st) == -1) err(EX_OSERR, "could not find mount point %s", mount_point); if (!S_ISDIR(st.st_mode)) { errno = ENOTDIR; err(EX_OSERR, "can't mount on %s", mount_point); } /* if (smb_getextattr(mount_point, &einfo) == 0) errx(EX_OSERR, "can't mount on %s twice", mount_point); */ if (uid == (uid_t)-1) uid = st.st_uid; if (gid == (gid_t)-1) gid = st.st_gid; if (file_mode == 0 ) file_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); if (dir_mode == 0) { dir_mode = file_mode; if (dir_mode & S_IRUSR) dir_mode |= S_IXUSR; if (dir_mode & S_IRGRP) dir_mode |= S_IXGRP; if (dir_mode & S_IROTH) dir_mode |= S_IXOTH; } /* * For now, let connection be private for this mount */ ctx->ct_ssn.ioc_opt |= SMBVOPT_PRIVATE; ctx->ct_ssn.ioc_owner = ctx->ct_sh.ioc_owner = 0; /* root */ ctx->ct_ssn.ioc_group = ctx->ct_sh.ioc_group = gid; opt = 0; if (dir_mode & S_IXGRP) opt |= SMBM_EXECGRP; if (dir_mode & S_IXOTH) opt |= SMBM_EXECOTH; ctx->ct_ssn.ioc_rights |= opt; ctx->ct_sh.ioc_rights |= opt; error = smb_ctx_resolve(ctx); if (error) exit(1); error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE); if (error) { exit(1); } fd = ctx->ct_fd; build_iovec(&iov, &iovlen, "fstype", strdup("smbfs"), -1); build_iovec(&iov, &iovlen, "fspath", mount_point, -1); build_iovec_argf(&iov, &iovlen, "fd", "%d", fd); build_iovec(&iov, &iovlen, "mountpoint", mount_point, -1); build_iovec_argf(&iov, &iovlen, "uid", "%d", uid); build_iovec_argf(&iov, &iovlen, "gid", "%d", gid); build_iovec_argf(&iov, &iovlen, "file_mode", "%d", file_mode); build_iovec_argf(&iov, &iovlen, "dir_mode", "%d", dir_mode); build_iovec_argf(&iov, &iovlen, "caseopt", "%d", caseopt); build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof errmsg); error = nmount(iov, iovlen, mntflags); smb_ctx_done(ctx); if (error) { smb_error("mount error: %s %s", error, mount_point, errmsg); exit(1); } return 0; }
int rc_parse (void) { char *fn; struct keyword *kw; fn = file_in_dir (user_rcdir, RC_FILE); rc_open (fn); g_free (fn); if(!rc) return -1; while ((token = rc_next_token ()) != TOKEN_EOF) { switch (token) { case TOKEN_KEYWORD: for (kw = keywords; kw->name; kw++) { if (strcmp (token_str, kw->name) == 0) { switch (kw->required) { case KEYWORD_INT: if (rc_expect_token (TOKEN_INT)) config_set_int (kw->config, token_int); break; case KEYWORD_BOOL: if (rc_expect_token (TOKEN_INT)) config_set_bool (kw->config, token_int); break; case KEYWORD_STRING: if (rc_expect_token (TOKEN_STRING)) config_set_string (kw->config, token_str); break; } break; } } break; case TOKEN_EOL: case TOKEN_EOF: break; default: syntax_error (); rc_skip_to_eol (); break; } /* switch */ } /* while */ rc_close (); /* Compatibility with old versions */ if (!games[Q1_SERVER].dir && games[QW_SERVER].dir) { games[Q1_SERVER].dir = g_strdup (games[QW_SERVER].dir); config_set_string ("/" CONFIG_FILE "/Game: QS/dir", games[Q1_SERVER].dir); } if (default_w_switch < 0) default_w_switch = 0; if (default_b_switch < 0) default_b_switch = 0; return 0; }
static int rc_next_token (void) { int c; int num, i; int sign = 1; if (!rc) return TOKEN_EOF; if ((c = rc_getc (rc)) == EOF) { rc_close (); return TOKEN_EOF; } tptr = token_str; while (1) { switch (state) { case STATE_SPACE: if (c == ' ' || c == '\r' || c == '\t') break; if (isdigit (c)) { sign = 1; token_int = c - '0'; state = STATE_INT; break; } if (isalnum (c) || c == '_') { *tptr++ = c; state = STATE_TOKEN; break; } switch (c) { case '\n': line++; pos = 1; return TOKEN_EOL; break; case ',': return TOKEN_COMMA; break; case '\"': state = STATE_STRING; break; case '-': sign = -1; token_int = 0; state = STATE_INT; break; case '#': state = STATE_COMMENT; break; default: unexpected_char_error (c); break; } /* switch */ break; case STATE_COMMENT: if (c == '\n') { line++; pos = 1; state = STATE_SPACE; return TOKEN_EOL; } break; case STATE_INT: if (!isdigit (c)) { rc_ungetc (c, rc); token_int = token_int * sign; state = STATE_SPACE; return TOKEN_INT; } token_int = token_int*10 + c - '0'; break; case STATE_TOKEN: if (!isalnum (c) && c != '_') { rc_ungetc (c, rc); *tptr = '\0'; state = STATE_SPACE; return TOKEN_KEYWORD; } *tptr++ = c; break; case STATE_STRING: if (c == '\"') { *tptr = '\0'; state = STATE_SPACE; return TOKEN_STRING; } if (c == '\n') { unexpected_eof_error (); rc_ungetc (c, rc); *tptr = '\0'; state = STATE_SPACE; return TOKEN_STRING; } if (c == '\\') { if ((c = rc_getc (rc)) == EOF) { *tptr = '\0'; rc_close (); return TOKEN_STRING; } if (c >= '0' && c <='7') { for (i=0, num=0; i < 3; i++) { num <<= 3; num |= c - '0'; c = rc_getc (rc); if (c < '0' || c > '7') break; } *tptr++ = num; if (c == EOF) { *tptr = '\0'; rc_close (); return TOKEN_STRING; } continue; } /* if (c >= '0' && c <='7') */ switch (c) { case 'n': *tptr++ = '\n'; break; case 'r': *tptr++ = '\r'; break; case 't': *tptr++ = '\t'; break; default: *tptr++ = c; break; } /* switch (c) */ break; } /* if (c == '\\') */ *tptr++ = c; break; } /* switch (state) */ c = rc_getc (rc); } /* while (1) */ }
SEXP cr_set(SEXP sc, SEXP keys, SEXP values) { rconn_t *c; int n, i; const char **argv = argbuf; size_t *argsz = argszbuf; redisReply *reply; if (!Rf_inherits(sc, "redisConnection")) Rf_error("invalid connection"); c = (rconn_t*) EXTPTR_PTR(sc); if (!c) Rf_error("invalid connection (NULL)"); rc_validate_connection(c, 0); if (TYPEOF(keys) != STRSXP) Rf_error("invalid keys"); n = LENGTH(keys); if (n < 1) return R_NilValue; /* FIXME: we check only the first ... in the hope that we support more formats later */ if (TYPEOF(values) != VECSXP || TYPEOF(VECTOR_ELT(values, 0)) != RAWSXP) Rf_error ("Sorry, values can only be a list of raw vectors for now"); if (LENGTH(values) != n) Rf_error("keys/values length mismatch"); if (2 * n + 1 > NARGBUF) { argv = malloc(sizeof(const char*) * (2 * n + 2)); if (!argv) Rf_error("out of memory"); argsz = malloc(sizeof(size_t) * (2 * n + 2)); if (!argsz) { free(argv); Rf_error("out of memory"); } } argv[0] = "MSET"; argsz[0] = strlen(argv[0]); for (i = 0; i < n; i++) { argv [2 * i + 1] = CHAR(STRING_ELT(keys, i)); argsz[2 * i + 1] = strlen(argv[2 * i + 1]); argv [2 * i + 2] = (char*) RAW(VECTOR_ELT(values, i)); argsz[2 * i + 2] = LENGTH(VECTOR_ELT(values, i)); } reply = redisCommandArgv(c->rc, 2 * n + 1, argv, argsz); if (!reply && (c->flags & RCF_RETRY)) { SEXP es = Rf_mkChar(c->rc->errstr); rc_close(c); rc_validate_connection(c, 1); if (c->rc) reply = redisCommandArgv(c->rc, 2 * n + 1, argv, argsz); else { if (argv != argbuf) { free(argv); free(argsz); } Rf_error("MGET error: %s and re-connect failed", CHAR(es)); } } if (argv != argbuf) { free(argv); free(argsz); } if (!reply) { SEXP es = Rf_mkChar(c->rc->errstr); rc_close(c); Rf_error("MSET error: %s", CHAR(es)); } /* Rprintf("reply, type=%d\n", reply->type); */ /* Note: the result is normally "status" - probably nothing useful we can do with that? */ freeReplyObject(reply); return R_NilValue; }
SEXP cr_get(SEXP sc, SEXP keys, SEXP asList) { rconn_t *c; int n, i, use_list = Rf_asInteger(asList); const char **argv = argbuf; redisReply *reply; SEXP res; if (!Rf_inherits(sc, "redisConnection")) Rf_error("invalid connection"); c = (rconn_t*) EXTPTR_PTR(sc); if (!c) Rf_error("invalid connection (NULL)"); rc_validate_connection(c, 0); if (TYPEOF(keys) != STRSXP) Rf_error("invalid keys"); n = LENGTH(keys); if (use_list < 0) /* asList == NA -> list for non scalar results only */ use_list = (n == 1) ? 0 : 1; if (n != 1 && !use_list) Rf_error("exaclty one key must be specified with list=FALSE"); if (n + 1 > NARGBUF) { argv = malloc(sizeof(const char*) * (n + 2)); if (!argv) Rf_error("out of memory"); } argv[0] = "MGET"; for (i = 0; i < n; i++) argv[i + 1] = CHAR(STRING_ELT(keys, i)); /* we use strings only, so no need to supply argvlen */ reply = redisCommandArgv(c->rc, n + 1, argv, 0); if (!reply && (c->flags & RCF_RETRY)) { SEXP es = Rf_mkChar(c->rc->errstr); rc_close(c); rc_validate_connection(c, 1); if (c->rc) reply = redisCommandArgv(c->rc, n + 1, argv, 0); else { if (argv != argbuf) free(argv); Rf_error("MGET error: %s and re-connect failed", CHAR(es)); } } if (argv != argbuf) free(argv); if (!reply) { SEXP es = Rf_mkChar(c->rc->errstr); rc_close(c); Rf_error("MGET error: %s", CHAR(es)); } /* Rprintf("reply, type=%d\n", reply->type); */ if (reply->type != REDIS_REPLY_ARRAY) { freeReplyObject(reply); Rf_error("unexpected result type"); } if (reply->elements != n) { freeReplyObject(reply); Rf_error("unexpected result length - should be %d but is %d", n, (int) reply->elements); } if (use_list) { int n = reply->elements; res = PROTECT(Rf_allocVector(VECSXP, n)); Rf_setAttrib(res, R_NamesSymbol, keys); for (i = 0; i < n; i++) SET_VECTOR_ELT(res, i, rc_reply2R(reply->element[i])); UNPROTECT(1); } else res = rc_reply2R(reply->element[0]); freeReplyObject(reply); return res; }
int cmd_print(int argc, char *argv[]) { struct smb_ctx sctx, *ctx = &sctx; smbfh fh; off_t offset; char buf[8192]; char *filename; char fnamebuf[256]; int error, opt, i, file, count; if (argc < 2) view_usage(); if (smb_ctx_init(ctx, argc, argv, SMBL_SHARE, SMBL_SHARE, SMB_ST_PRINTER) != 0) exit(1); if (smb_ctx_readrc(ctx) != 0) exit(1); if (smb_rc) rc_close(smb_rc); while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) { switch(opt){ case STDPARAM_ARGS: error = smb_ctx_opt(ctx, opt, optarg); if (error) exit(1); break; default: view_usage(); /*NOTREACHED*/ } } if (optind + 1 >= argc) print_usage(); filename = argv[optind + 1]; if (strcmp(filename, "-") == 0) { file = 0; /* stdin */ filename = "stdin"; } else { file = open(filename, O_RDONLY, 0); if (file < 0) { smb_error("could not open file %s\n", errno, filename); exit(1); } } if (smb_ctx_resolve(ctx) != 0) exit(1); error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE); if (error) { smb_error("could not login to server %s", error, ctx->ct_ssn.ioc_srvname); exit(1); } snprintf(fnamebuf, sizeof(fnamebuf), "%s_%s_%s", ctx->ct_ssn.ioc_user, ctx->ct_ssn.ioc_srvname, filename); error = smb_smb_open_print_file(ctx, 0, 1, fnamebuf, &fh); if (error) { smb_error("could not open print job", error); exit(1); } offset = 0; error = 0; for(;;) { count = read(file, buf, sizeof(buf)); if (count == 0) break; if (count < 0) { error = errno; smb_error("error reading input file\n", error); break; } i = smb_write(ctx, fh, offset, count, buf); if (i < 0) { error = errno; smb_error("error writing spool file\n", error); break; } if (i != count) { smb_error("incomplete write to spool file\n", 0); error = EIO; break; } offset += count; } close(file); error = smb_smb_close_print_file(ctx, fh); if (error) smb_error("an error while closing spool file\n", error); return error ? 1 : 0; }