Skip to content

Commit

Permalink
serctl: use iolists for device name
Browse files Browse the repository at this point in the history
  • Loading branch information
msantos committed Sep 26, 2014
1 parent 74d95ea commit 344fa14
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ the system C interface.

serctl:open(Path) -> {ok, FD} | {error, posix()}

Types Path = list() | {fd, FD}
Types Path = iodata() | {fd, FD}
FD = resource()

Opens the serial device (e.g., "/dev/ttyUSB0").
Expand Down
12 changes: 8 additions & 4 deletions c_src/serctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,22 @@ load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
static ERL_NIF_TERM
nif_open(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
char buf[MAXPATHLEN];
ErlNifBinary dev;
SRLY_STATE *sp = NULL;


if (enif_get_string(env, argv[0], buf, sizeof(buf), ERL_NIF_LATIN1) < 1)
if (!enif_inspect_iolist_as_binary(env, argv[0], (ErlNifBinary *)&dev))
return enif_make_badarg(env);

sp = enif_alloc_resource(SRLY_STATE_RESOURCE, sizeof(SRLY_STATE));
if (sp == NULL)
return error_tuple(env, ENOMEM);

sp->fd = open(buf, O_RDWR|O_NOCTTY|O_NONBLOCK);
if (!enif_realloc_binary(&dev, dev.size+1))
return enif_make_badarg(env);

dev.data[dev.size-1] = '\0';

sp->fd = open((char *)dev.data, O_RDWR|O_NOCTTY|O_NONBLOCK);

if (sp->fd < 0 || isatty(sp->fd) != 1) {
int err = errno;
Expand Down

0 comments on commit 344fa14

Please sign in to comment.