#include #include /* The following array (i.e., SAS program) could be generated */ /* dynamically. */ char sasprogram[] = { " libname output '.'; data output.file; infile fildes05; input var1 var2; run; proc print data=output.file; run;" }; int src[2], /* File descriptors for the SAS source code. */ data[2]; /* File descriptors for the SAS data. */ int saspid; /* Process ID of the child (SAS process). */ void CloseSAS(); main(argc,argv) int argc; char **argv; { char buf[512]; /* Generic data buffer */ int counter; /* for loop counter */ /* Invoke the SAS System, and give it the SAS program to run */ if (OpenSAS(sasprogram) == -1) { fprintf(stderr, "Can't execute The SAS System!!0); exit(1); } /* Generate two columns of integers for SAS input data. */ for(counter=0;counter<5;counter++) { sprintf(buf,"%d %d0,counter, counter); DataToSAS( buf, strlen(buf) ); } /* Terminate the SAS Process, then exit. */ CloseSAS(); return(0); } int OpenSAS(sasprogram) char *sasprogram; { int fd; /* File descriptor returned from fcntl. */ pipe(src); /* The pipe for the SAS source statements. */ pipe(data); /* The pipe for the input data stream. */ if (!(saspid = fork())) { /* We're in the child. Reroute the pipes. */ /* Close stdin of the SAS process. */ close(0); /* Associate stdin with read end of the source pipe (src[0]). */ fcntl(src[0], F_DUPFD, 0); /* Close both ends of source pipe. */ close(src[0]); close(src[1]); close(5); /* Close just in case it is open. */ /* Associate read end of the data pipe with file descriptor 5. */ fd=fcntl(data[0], F_DUPFD, 5); printf("fd from fcntl=%d0,fd); /* Close both ends of the data pipe. */ close(data[0]); close(data[1]); /* Run the SAS program. */ execlp("sas", "sas", "-stdio", "-nodms", NULL); } /* We are in the parent process. Close the unwanted ends of */ /* the pipes; we are closing the read end of the pipe since */ /* both source and data will be "written to" the SAS process. */ close(src[0]); close(data[0]); /* If invoking the SAS System failed, */ if (saspid == -1) { close( src[1] ); /* close write end of both pipes. */ close( data[1] ); return(-1); /* Return with -1. */ } /* Send the SAS System the SAS source to run and close the */ /* source pipe. */ write(src[1], sasprogram, strlen(sasprogram)); close(src[1]); /* Now any data written to the pipe "data[1]" will be sent to */ /* file descriptor #5 of the SAS process. */ return(saspid); } /* Send data to the running SAS process. */ int DataToSAS(buf, len) char *buf; int len; { if (write(data[1], buf, len) != len) return(-1); else return(0); } /* Terminate the SAS process, wait for it, and print exit status. */ void CloseSAS() { int rc; close(data[1]); while (wait(&rc) != saspid); printf("the SAS process ended with rc= %d0, rc / 256); }