posix_compat - assign stdin/stdout device names
end_posix_compat - release stdin/stdout device names
#include <stdio.h>
The user defined function, posix_compat(), is used to encapsulate any specific setup which an application requires from startup.
Typically, a standard LINUX/UNIX application may look for certain devices to be present when it is evoked. Additionally, many STDIO runtime functions (such as printf(), getc(), etc.) will only operate if their expected device exists.
In most cases, this requirement can be satisfied by defining the following three devices through parameters for posix_compat():
Of course, other environment and run-time definitions can also be placed in this encapsulation, as needed. By transferring all server setup into this call along with the assignment of stdin and stdout, the packaging of Unison applications becomes much closer to a multi-threaded LINUX program. This approach is highly recommended because it allows application developers from the Linux world to separate the application from the underlying device drivers and the initialization process.
If required, the paired user defined function, end_posix_compat(), can be used to reverse (or undo) the actions performed by posix_compat(). Typically, this would involve closing the standard devices and freeing user assignments. Generally, servers which have been started by posix_compat() should not be canceled; however, in some cases it may be necessary.
For a typical usage, see the example below.
User defined
int posix_compat(char *cwd, char *stdinServerName, char *stdoutServerName)
{
pthread_t pid;
pthread_attr_t attr;
struct sched_param myNewPriority;
struct set_tty *set_tty;
pthread_attr_init(&attr);
myNewPriority.sched_priority = 5;
pthread_attr_setschedparam(&attr, &myNewPriority);
pthread_attr_setstacksize(&attr,1100);
if(pthread_create(&pid, &attr, &tty_shell, 0)!=0)
{
xprintf("Can't create tty_shell = %d\n", errno);
return 0;
}
if(dir_register("/dev/ttyS", pid, TYPE_SERVER)==0)
{
xprintf("Can't register '/dev/ttyS' = %d\n", errno);
return 0;
}
perprintf(stdout_init (stdoutServerName));
stdin_init (stdinServerName);
return 1;
}
int end_posix_compat()
{
stdout_close();
stdin_close();
return 1;
}
THREAD Main(void *arg)
{
...
posix_compat("", "/dev/ttyS0", "/dev/ttyS0");
// Print to stdout
printf("Start tty with posix_compat\n");
...
// Read from stdin
gets(buf);
...
}