iabdalkader
iabdalkader

Reputation: 17312

Is it possible to write to file descriptor opened in C from a Lua function?

I have a small C program that calls a Lua function and sends it a file descriptor, is it possible to write to this file descriptor from Lua ?

Upvotes: 3

Views: 1234

Answers (2)

fbox
fbox

Reputation: 23

Have a look at the C function fdopen(). It takes a file descriptor and gives you back a stream/FILE*.

If you implement a Lua C extension for fdopen() to give you back a LuaStream then you can use the normal Lua io calls on the resulting Lua Stream.

Better than just implementing a one-off function for what you want to do.

see io_open in liolib.c of the Lua 5.2.1 sources for an example of creating a Lua Stream.

Upvotes: 2

iabdalkader
iabdalkader

Reputation: 17312

I think redirecting the standard output to the socket fd, before calling the lua function, may work, but it's easier to just return the string and write it in C.

Upvotes: -1

Related Questions