Open System Services Programmer's Guide

Example 30 Creating an OSS Process Using tdm_spawnp()
#include <tdmext.h>
#include <spawn.h> /* Note 1 */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
/* Set defaults for input and output structures */
struct process_extension
procext = DEFAULT_PROCESS_EXTENSION;
struct process_extension_results
procresults = DEFAULT_PROCESS_EXTENSION_RESULTS;
struct inheritance inherit;
extern char **environ;
char *argv[10];
int main(void)
{
char buf[4096];
pid_t pid;
int status, cnt;
int fd_count, fd_map[20];
char *ptr;
/* Get parameters for the child process from the command line. */
printf("%% "); /* print prompt (printf requires %% to print %) */
while (fgets(buf, 4096, stdin) != NULL) {
ptr = buf;
buf[strlen(buf) - 1] = '\0'; /* replace newline with NULL */
/* Build the parameter list for the child process. */
argv[0] = buf;
cnt = 1;
while(*ptr) {
if(*ptr == ' ') {
*ptr = '\0';
argv[cnt++] = ptr + 1; /* one blank between parameters */
}
ptr++;
}
argv[cnt] = NULL;
fd_map[0] = 0; /* Note 2 */
fd_map[1] = 1;
fd_map[2] = 2;
fd_count = 3;
/* Request system-generated process name. */
procext.pe_name_options = _TPC_GENERATE_NAME;
/* Set up child process as new group leader. */
inherit.flags = SPAWN_SETGROUP;
inherit.pgroup = SPAWN_NEWPGROUP;
printf("trying spawn of %s\n", buf);
/* Spawn child process with supplied parameters. */
if((pid=tdm_spawnp(buf,fd_count,fd_map,&inherit,
argv,environ,&procext,&procresults))<0){
/* If unsuccessful, print diagnostic messages. */
fprintf(stderr, "pid = %o\n", pid);
fprintf(stderr, "procresults.pr_len = %o\n",
procresults.pr_len);
fprintf(stderr, "procresults.pr_pid = %o\n",
122 Managing Processes