本文整理汇总了C++中GetOpenFile函数的典型用法代码示例。如果您正苦于以下问题:C++ GetOpenFile函数的具体用法?C++ GetOpenFile怎么用?C++ GetOpenFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了GetOpenFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: bsock_getpeereid
/*
* call-seq:
* basicsocket.getpeereid => [euid, egid]
*
* Returns the user and group on the peer of the UNIX socket.
* The result is a two element array which contains the effective uid and the effective gid.
*
* Socket.unix_server_loop("/tmp/sock") {|s|
* begin
* euid, egid = s.getpeereid
*
* # Check the connected client is myself or not.
* next if euid != Process.uid
*
* # do something about my resource.
*
* ensure
* s.close
* end
* }
*
*/
static VALUE
bsock_getpeereid(VALUE self)
{
UNRUBBY_SOCKET_HACK;
#if defined(HAVE_GETPEEREID)
rb_io_t *fptr;
uid_t euid;
gid_t egid;
GetOpenFile(self, fptr);
if (getpeereid(fptr->fd, &euid, &egid) == -1)
rb_sys_fail("getpeereid");
return rb_assoc_new(UIDT2NUM(euid), GIDT2NUM(egid));
#elif defined(SO_PEERCRED) /* GNU/Linux */
rb_io_t *fptr;
struct ucred cred;
socklen_t len = sizeof(cred);
GetOpenFile(self, fptr);
if (getsockopt(fptr->fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1)
rb_sys_fail("getsockopt(SO_PEERCRED)");
return rb_assoc_new(UIDT2NUM(cred.uid), GIDT2NUM(cred.gid));
#elif defined(HAVE_GETPEERUCRED) /* Solaris */
rb_io_t *fptr;
ucred_t *uc = NULL;
VALUE ret;
GetOpenFile(self, fptr);
if (getpeerucred(fptr->fd, &uc) == -1)
rb_sys_fail("getpeerucred");
ret = rb_assoc_new(UIDT2NUM(ucred_geteuid(uc)), GIDT2NUM(ucred_getegid(uc)));
ucred_free(uc);
return ret;
#endif
}
示例2: ttymode
static VALUE
ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void *arg)
{
rb_io_t *fptr;
int status = -1;
int error = 0;
int fd[FD_PER_IO];
conmode t[FD_PER_IO];
VALUE result = Qnil;
GetOpenFile(io, fptr);
fd[0] = GetReadFD(fptr);
if (fd[0] != -1) {
if (set_ttymode(fd[0], t+0, setter, arg)) {
status = 0;
}
else {
error = errno;
fd[0] = -1;
}
}
fd[1] = GetWriteFD(fptr);
if (fd[1] != -1 && fd[1] != fd[0]) {
if (set_ttymode(fd[1], t+1, setter, arg)) {
status = 0;
}
else {
error = errno;
fd[1] = -1;
}
}
if (status == 0) {
result = rb_protect(func, io, &status);
}
GetOpenFile(io, fptr);
if (fd[0] != -1 && fd[0] == GetReadFD(fptr)) {
if (!setattr(fd[0], t+0)) {
error = errno;
status = -1;
}
}
if (fd[1] != -1 && fd[1] != fd[0] && fd[1] == GetWriteFD(fptr)) {
if (!setattr(fd[1], t+1)) {
error = errno;
status = -1;
}
}
if (status) {
if (status == -1) {
errno = error;
rb_sys_fail(0);
}
rb_jump_tag(status);
}
return result;
}
示例3: frostbitten_message_write_to_io
VALUE frostbitten_message_write_to_io(VALUE self, VALUE io) {
fb_message *message;
Data_Get_Struct(self, fb_message, message);
frostbitten_header_write_to_io(message->header, io);
uint32_t arrayLength = (uint32_t)RARRAY_LEN(message->words);
uint32_t packetSize = 0;
char** strings = frostbitten_message_generate(message, &packetSize);
packetSize += sizeof(uint32_t); // Header
packetSize += sizeof(uint32_t); // packetSize
packetSize += sizeof(uint32_t); // wordCount
rb_io_t *fptr;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
FILE *fp = rb_io_stdio_file(fptr);
fwrite((const void*)&packetSize,sizeof(uint32_t),1,fp);
fwrite((const void*)&arrayLength,sizeof(uint32_t),1,fp);
for(uint32_t i=0; i < arrayLength; i++) {
char *str = strings[i];
uint32_t len = (uint32_t)strlen(str);
fwrite((const void*)&len, sizeof(uint32_t), 1, fp);
fwrite(str, sizeof(char), strlen(str)+1, fp);
}
fflush(fp);
return self;
}
示例4: ScreenBackgroundImage
void TextWindow::ScreenBackgroundImage(int link, uint32_t v) {
if(SS.bgImage.fromFile) MemFree(SS.bgImage.fromFile);
SS.bgImage.fromFile = NULL;
if(link == 'l') {
FILE *f = NULL;
png_struct *png_ptr = NULL;
png_info *info_ptr = NULL;
char importFile[MAX_PATH] = "";
if(!GetOpenFile(importFile, PNG_EXT, PNG_PATTERN)) goto err;
f = fopen(importFile, "rb");
if(!f) goto err;
uint8_t header[8];
if (fread(header, 1, 8, f) != 8)
goto err;
if(png_sig_cmp(header, 0, 8)) goto err;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if(!png_ptr) goto err;
info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) goto err;
if(setjmp(png_jmpbuf(png_ptr))) goto err;
png_init_io(png_ptr, f);
png_set_sig_bytes(png_ptr, 8);
png_read_png(png_ptr, info_ptr,
PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_STRIP_ALPHA, NULL);
int w; w = (int)png_get_image_width(png_ptr, info_ptr);
int h; h = (int)png_get_image_height(png_ptr, info_ptr);
uint8_t **rows; rows = png_get_rows(png_ptr, info_ptr);
// Round to next-highest powers of two, since the textures require
// that. And round up to 4, to guarantee 32-bit alignment.
int rw; rw = max(4, RoundUpToPowerOfTwo(w));
int rh; rh = max(4, RoundUpToPowerOfTwo(h));
SS.bgImage.fromFile = (uint8_t *)MemAlloc(rw*rh*3);
{for(int i = 0; i < h; i++) {
memcpy(SS.bgImage.fromFile + ((h - 1) - i)*(rw*3), rows[i], w*3);
}}
SS.bgImage.w = w;
SS.bgImage.h = h;
SS.bgImage.rw = rw;
SS.bgImage.rh = rh;
SS.bgImage.scale = SS.GW.scale;
SS.bgImage.origin = SS.GW.offset.ScaledBy(-1);
err:
if(png_ptr) png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if(f) fclose(f);
}
SS.later.showTW = true;
}
示例5: ossl_ssl_setup
static VALUE
ossl_ssl_setup(VALUE self)
{
VALUE io, v_ctx, cb;
SSL_CTX *ctx;
SSL *ssl;
rb_io_t *fptr;
Data_Get_Struct(self, SSL, ssl);
if(!ssl){
v_ctx = ossl_ssl_get_ctx(self);
Data_Get_Struct(v_ctx, SSL_CTX, ctx);
ssl = SSL_new(ctx);
if (!ssl) {
ossl_raise(eSSLError, "SSL_new:");
}
DATA_PTR(self) = ssl;
io = ossl_ssl_get_io(self);
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
rb_io_check_writable(fptr);
SSL_set_fd(ssl, TO_SOCKET(FPTR_TO_FD(fptr)));
SSL_set_ex_data(ssl, ossl_ssl_ex_ptr_idx, (void*)self);
cb = ossl_sslctx_get_verify_cb(v_ctx);
SSL_set_ex_data(ssl, ossl_ssl_ex_vcb_idx, (void*)cb);
cb = ossl_sslctx_get_client_cert_cb(v_ctx);
SSL_set_ex_data(ssl, ossl_ssl_ex_client_cert_cb_idx, (void*)cb);
cb = ossl_sslctx_get_tmp_dh_cb(v_ctx);
SSL_set_ex_data(ssl, ossl_ssl_ex_tmp_dh_callback_idx, (void*)cb);
}
return Qtrue;
}
示例6: io_wait_writable
/*
* call-seq:
* io.wait_writable -> IO
* io.wait_writable(timeout) -> IO or nil
*
* Waits until IO writable is available or times out and returns self or
* nil when EOF is reached.
*/
static VALUE
io_wait_writable(int argc, VALUE *argv, VALUE io)
{
rb_io_t *fptr;
int i;
VALUE timeout;
struct timeval timerec;
struct timeval *tv;
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
rb_scan_args(argc, argv, "01", &timeout);
if (NIL_P(timeout)) {
tv = NULL;
}
else {
timerec = rb_time_interval(timeout);
tv = &timerec;
}
i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_OUT, tv);
if (i < 0)
rb_sys_fail(0);
rb_io_check_closed(fptr);
if (i & RB_WAITFD_OUT)
return io;
return Qnil;
}
示例7: rb_gsl_open_readfile
FILE* rb_gsl_open_readfile(VALUE io, int *flag)
{
rb_io_t *fptr = NULL;
FILE *fp = NULL;
char *name;
switch (TYPE(io)) {
case T_STRING:
name = RSTRING_PTR(io);
fp = fopen(name, "r");
*flag = 1;
break;
case T_FILE:
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
fp = rb_io_stdio_file(fptr);
*flag = 0;
break;
default:
rb_raise(rb_eTypeError, "argv 1 String or File expected");
break;
}
// if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file %s.", name);
if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file");
return fp;
}
示例8: io_wait_readable
static VALUE
io_wait_readable(int argc, VALUE *argv, VALUE io)
{
rb_io_t *fptr;
int i;
ioctl_arg n;
VALUE timeout;
struct timeval timerec;
struct timeval *tv;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
rb_scan_args(argc, argv, "01", &timeout);
if (NIL_P(timeout)) {
tv = NULL;
}
else {
timerec = rb_time_interval(timeout);
tv = &timerec;
}
if (rb_io_read_pending(fptr)) return Qtrue;
if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qfalse;
i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, tv);
if (i < 0)
rb_sys_fail(0);
rb_io_check_closed(fptr);
if (ioctl(fptr->fd, FIONREAD, &n)) rb_sys_fail(0);
if (n > 0) return io;
return Qnil;
}
示例9: ossl_start_ssl
static VALUE
ossl_start_ssl(VALUE self, int (*func)(), const char *funcname)
{
SSL *ssl;
rb_io_t *fptr;
int ret, ret2;
VALUE cb_state;
rb_ivar_set(self, ID_callback_state, Qnil);
Data_Get_Struct(self, SSL, ssl);
GetOpenFile(ossl_ssl_get_io(self), fptr);
for(;;){
if((ret = func(ssl)) > 0) break;
switch((ret2 = ssl_get_error(ssl, ret))){
case SSL_ERROR_WANT_WRITE:
rb_io_wait_writable(FPTR_TO_FD(fptr));
continue;
case SSL_ERROR_WANT_READ:
rb_io_wait_readable(FPTR_TO_FD(fptr));
continue;
case SSL_ERROR_SYSCALL:
if (errno) rb_sys_fail(funcname);
ossl_raise(eSSLError, "%s SYSCALL returned=%d errno=%d state=%s", funcname, ret2, errno, SSL_state_string_long(ssl));
default:
ossl_raise(eSSLError, "%s returned=%d errno=%d state=%s", funcname, ret2, errno, SSL_state_string_long(ssl));
}
}
cb_state = rb_ivar_get(self, ID_callback_state);
if (!NIL_P(cb_state))
rb_jump_tag(NUM2INT(cb_state));
return self;
}
示例10: rb_gsl_open_readfile
FILE* rb_gsl_open_readfile(VALUE io, int *flag)
{
#ifdef RUBY_1_9_LATER
rb_io_t *fptr = NULL;
#else
OpenFile *fptr = NULL;
#endif
FILE *fp = NULL;
char *name;
switch (TYPE(io)) {
case T_STRING:
name = RSTRING_PTR(io);
fp = fopen(name, "r");
*flag = 1;
break;
case T_FILE:
GetOpenFile(io, fptr);
name = fptr->path;
rb_io_check_readable(fptr);
#ifdef RUBY_1_9_LATER
fp = rb_io_stdio_file(fptr);
#else
fp = fptr->f;
#endif
*flag = 0;
break;
default:
rb_raise(rb_eTypeError, "argv 1 String or File expected");
break;
}
if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file %s.", name);
return fp;
}
示例11: bsock_shutdown
/*
* call-seq:
* basicsocket.shutdown([how]) => 0
*
* Calls shutdown(2) system call.
*
* s.shutdown(Socket::SHUT_RD) disallows further read.
*
* s.shutdown(Socket::SHUT_WR) disallows further write.
*
* s.shutdown(Socket::SHUT_RDWR) disallows further read and write.
*
* _how_ can be symbol or string:
* - :RD, :SHUT_RD, "RD" and "SHUT_RD" are accepted as Socket::SHUT_RD.
* - :WR, :SHUT_WR, "WR" and "SHUT_WR" are accepted as Socket::SHUT_WR.
* - :RDWR, :SHUT_RDWR, "RDWR" and "SHUT_RDWR" are accepted as Socket::SHUT_RDWR.
*
* UNIXSocket.pair {|s1, s2|
* s1.puts "ping"
* s1.shutdown(:WR)
* p s2.read #=> "ping\n"
* s2.puts "pong"
* s2.close
* p s1.read #=> "pong\n"
* }
*
*/
static VALUE
bsock_shutdown(int argc, VALUE *argv, VALUE sock)
{
UNRUBBY_SOCKET_HACK;
VALUE howto;
int how;
rb_io_t *fptr;
if (rb_safe_level() >= 4 && !OBJ_TAINTED(sock)) {
rb_raise(rb_eSecurityError, "Insecure: can't shutdown socket");
}
rb_scan_args(argc, argv, "01", &howto);
if (howto == Qnil)
how = SHUT_RDWR;
else {
how = rsock_shutdown_how_arg(howto);
if (how != SHUT_WR && how != SHUT_RD && how != SHUT_RDWR) {
rb_raise(rb_eArgError, "`how' should be either :SHUT_RD, :SHUT_WR, :SHUT_RDWR");
}
}
GetOpenFile(sock, fptr);
if (shutdown(fptr->fd, how) == -1)
rb_sys_fail(0);
return INT2FIX(0);
}
示例12: rb_yaram_mbox_write_unblocked
/*
* call-seq:
* #write_unblocked(string) -> integer
*
* Writes the given string to <em>ios</em> using
* the write(2) system call. It assumes that O_NONBLOCK
* is already set for the underlying file descriptor.
*
* Over ~100,000 calls it is about 0.043 seconds faster.
*
* It returns the number of bytes written.
*/
static VALUE
rb_yaram_mbox_write_unblocked(VALUE self, VALUE io, VALUE str)
{
rb_io_t *fptr;
long n;
rb_secure(4);
if (TYPE(str) != T_STRING)
str = rb_obj_as_string(str);
io = rb_io_get_write_io(io);
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
if (io_fflush(fptr) < 0)
rb_sys_fail(0);
n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
if (n == -1) {
if (errno == EWOULDBLOCK || errno == EAGAIN)
rb_mod_sys_fail(rb_mWaitWritable, "write would block");
rb_sys_fail_path(fptr->pathv);
}
return LONG2FIX(n);
}
示例13: ossl_obj2bio
BIO *
ossl_obj2bio(VALUE obj)
{
BIO *bio;
if (RB_TYPE_P(obj, T_FILE)) {
rb_io_t *fptr;
FILE *fp;
int fd;
GetOpenFile(obj, fptr);
rb_io_check_readable(fptr);
if ((fd = rb_cloexec_dup(FPTR_TO_FD(fptr))) < 0){
rb_sys_fail(0);
}
rb_update_max_fd(fd);
if (!(fp = fdopen(fd, "r"))){
int e = errno;
close(fd);
rb_syserr_fail(e, 0);
}
if (!(bio = BIO_new_fp(fp, BIO_CLOSE))){
fclose(fp);
ossl_raise(eOSSLError, NULL);
}
}
else {
StringValue(obj);
bio = BIO_new_mem_buf(RSTRING_PTR(obj), RSTRING_LENINT(obj));
if (!bio) ossl_raise(eOSSLError, NULL);
}
return bio;
}
示例14: rsock_bsock_send
/*
* call-seq:
* basicsocket.send(mesg, flags [, dest_sockaddr]) => numbytes_sent
*
* send _mesg_ via _basicsocket_.
*
* _mesg_ should be a string.
*
* _flags_ should be a bitwise OR of Socket::MSG_* constants.
*
* _dest_sockaddr_ should be a packed sockaddr string or an addrinfo.
*
* TCPSocket.open("localhost", 80) {|s|
* s.send "GET / HTTP/1.0\r\n\r\n", 0
* p s.read
* }
*/
VALUE
rsock_bsock_send(int argc, VALUE *argv, VALUE sock)
{
struct rsock_send_arg arg;
VALUE flags, to;
rb_io_t *fptr;
int n;
rb_blocking_function_t *func;
rb_secure(4);
rb_scan_args(argc, argv, "21", &arg.mesg, &flags, &to);
StringValue(arg.mesg);
if (!NIL_P(to)) {
SockAddrStringValue(to);
to = rb_str_new4(to);
arg.to = (struct sockaddr *)RSTRING_PTR(to);
arg.tolen = (socklen_t)RSTRING_LENINT(to);
func = rsock_sendto_blocking;
}
else {
func = rsock_send_blocking;
}
GetOpenFile(sock, fptr);
arg.fd = fptr->fd;
arg.flags = NUM2INT(flags);
while (rb_thread_fd_writable(arg.fd),
(n = (int)BLOCKING_REGION_FD(func, &arg)) < 0) {
if (rb_io_wait_writable(arg.fd)) {
continue;
}
rb_sys_fail("send(2)");
}
return INT2FIX(n);
}
示例15: control_block_open
static VALUE
control_block_open(int argc, VALUE *argv, VALUE cb)
{
const char *fmode;
#ifdef RUBY19
rb_io_t *fptr;
#else
OpenFile *fptr;
#endif
VALUE file, mode;
rb_aiocb_t *cbs = GetCBStruct(cb);
rb_scan_args(argc, argv, "02", &file, &mode);
fmode = NIL_P(mode) ? "r" : RSTRING_PTR(mode);
struct stat stats;
Check_Type(file, T_STRING);
cbs->io = rb_file_open(RSTRING_PTR(file), fmode);
GetOpenFile(cbs->io, fptr);
rb_io_check_readable(fptr);
if ( cbs->cb.aio_fildes == 0 && cbs->cb.aio_nbytes == 0){
#ifdef RUBY19
cbs->cb.aio_fildes = fptr->fd;
#else
cbs->cb.aio_fildes = fileno(fptr->f);
#endif
fstat(cbs->cb.aio_fildes, &stats);
control_block_nbytes_set(cb, INT2FIX(stats.st_size));
}
return cb;
}