I was recently looking at some C code and 'translating' to Python but got stuck at a particular function called _IOR
. It is defined in sys/ioctl.h
like so:
#define _IOC(inout,group,num,len) \
(inout | ((len & IOCPARM_MASK) << 16) | ((group) << 8) | (num))
#define _IOR(g,n,t) _IOC(IOC_OUT, (g), (n), sizeof(t))
And I have seen it called in these ways:
_IOR('t', 3, int)
_IOR('keys', 1, unsigned char *)
The 'keys' call is the one I need to do. Looks like it's doing a bitwise operation on a string.
I managed to find equivalent Python code to the above but it only works for a single character.
_IOC_NRBITS = 8
_IOC_TYPEBITS = 8
_IOC_SIZEBITS = 14
_IOC_DIRBITS = 2
_IOC_NRSHIFT = 0
_IOC_TYPESHIFT =(_IOC_NRSHIFT+_IOC_NRBITS)
_IOC_SIZESHIFT =(_IOC_TYPESHIFT+_IOC_TYPEBITS)
_IOC_DIRSHIFT =(_IOC_SIZESHIFT+_IOC_SIZEBITS)
_IOC_NONE = 0
_IOC_WRITE = 1
_IOC_READ = 2
def _IOC(direction,type,nr,size):
return (((direction) << _IOC_DIRSHIFT) |
((type) << _IOC_TYPESHIFT) |
((nr) << _IOC_NRSHIFT) |
((size) << _IOC_SIZESHIFT))
def _IOR(type, number, size):
return _IOC(_IOC_READ, type, number, size)
This works for single character calls.
_IOR(ord('t'), 3, 1)
But I don't know the equivalent of the second call with 'keys'. Is there a way to do this C call below in Python?
_IOR('keys', 1, unsigned char *)