bsdcontrol.rb/ext/hbsdctl.rb/feature.c

68 lines
1.5 KiB
C
Raw Normal View History

#include <ruby.h>
2024-03-07 22:04:44 +01:00
#include <libhbsdcontrol.h>
#include <fcntl.h>
#include "feature.h"
#include "context.h"
#include "glue.h"
2024-03-09 23:40:42 +01:00
#include <errno.h>
2024-03-07 22:04:44 +01:00
/*
* BSD::Control::Feature#status
*/
2024-03-07 22:04:44 +01:00
VALUE
bsdcontrol_feature_status(VALUE self, VALUE path)
2024-03-07 22:04:44 +01:00
{
int fd;
VALUE rbcontext;
hbsdctrl_feature_t *feature;
hbsdctrl_feature_state_t state;
hbsdctrl_ctx_t *ctx;
rbcontext = rb_funcall(self, rb_intern("context"), 0);
fd = bsdcontrol_open(path);
ctx = bsdcontrol_unwrap(rbcontext);
feature = bsdcontrol_find_feature(ctx, self);
errno = 0;
if (feature->hf_get(ctx, feature, &fd, &state) == RES_FAIL)
{
close(fd);
errno == 0 ? rb_raise(rb_eSystemCallError, "hf_get")
: rb_syserr_fail(errno, "hf_get");
}
else
{
const char *str;
close(fd);
str = hbsdctrl_feature_state_to_string(&state);
return ID2SYM(rb_intern(str));
}
2024-03-07 22:04:44 +01:00
}
/*
* BSD::Control::Feature#set!
*/
VALUE
bsdcontrol_feature_set(VALUE self, VALUE path, VALUE rbstate)
2024-03-08 02:08:48 +01:00
{
int fd;
VALUE rbcontext;
hbsdctrl_feature_t *feature;
hbsdctrl_ctx_t *ctx;
int state;
rbcontext = rb_funcall(self, rb_intern("context"), 0);
fd = bsdcontrol_open(path);
ctx = bsdcontrol_unwrap(rbcontext);
feature = bsdcontrol_find_feature(ctx, self);
state = NUM2INT(rbstate);
errno = 0;
if (feature->hf_apply(ctx, feature, &fd, &state) == RES_FAIL)
{
close(fd);
errno == 0 ? rb_raise(rb_eSystemCallError, "hf_apply")
: rb_syserr_fail(errno, "hf_apply");
}
else
{
return Qtrue;
2024-03-08 02:08:48 +01:00
}
}