tsp@killer.UUCP (06/09/87)
The following functions are quick hacks to get Larry Wall's cdiff.c
running under Minix. [I couldn't find them on any diskette.]
I have done very little testing with each, so
feel free to examine the code and make the appropriate changes.
Included are:
char * ctime(long_time_ptr)
FILE * popen(sh_cmd, mode)
int pclose(popen_file_ptr)
FILE * fdopen(open_fd, mode)
Tom Poindexter uucp: ihnp4!killer!tsp
Compuserve: 76117,1453
------cut here------------
echo x - popen.c
gres '^X' '' > popen.c << '/'
X/* popen and pclose for minix */
X/* needs fdopen.c (missing from minix stdio) */
X/* this code was found in an old unix programming guide */
X/* and hacked for stdio */
X
X#include <stdio.h>
X#include <signal.h>
X
X#define READ 0
X#define WRITE 1
X#define tst(a,b) (mode == READ ? (b) : (a))
X
Xstatic int popen_pid;
X
XFILE *popen(cmd, type)
Xchar *cmd;
Xchar *type;
X{
X int p[2];
X int mode;
X FILE *fdopen();
X
X switch (*type) {
X case 'r':
X mode = READ;
X break;
X case 'w':
X mode = WRITE;
X break;
X default:
X return (NULL);
X }
X
X if (pipe(p) < 0)
X return (NULL);
X
X if ((popen_pid = fork()) == 0) {
X close(tst(p[WRITE],p[READ]));
X close(tst(0,1));
X dup (tst(p[READ],p[WRITE]));
X close(tst(p[READ],p[WRITE]));
X execl("/bin/sh", "sh","-c",cmd,(char *) 0); /* normal try at sh */
X execl("/usr/bin/sh","sh","-c",cmd,(char *) 0); /* 2nd try */
X exit(1); /* oops !! */
X }
X
X if (popen_pid == -1)
X return (NULL);
X
X close(tst(p[READ],p[WRITE]));
X
X return( fdopen(tst(p[WRITE],p[READ]),type) );
X}
X
X
Xint pclose(f)
XFILE *f;
X{
X int r, (*hstat)(), (*istat)(), (*qstat)();
X int status;
X
X fclose(f);
X
X istat = signal(SIGINT, SIG_IGN);
X qstat = signal(SIGQUIT,SIG_IGN);
X hstat = signal(SIGHUP, SIG_IGN);
X
X while ((r = wait(&status)) != popen_pid && r != -1) ;
X
X if (r == -1)
X status = -1;
X
X signal(SIGINT, istat);
X signal(SIGQUIT,qstat);
X signal(SIGHUP, hstat);
X
X return(status);
X }
X
X/* finis */
X
X
X
X
X
/
echo x - fdopen.c
gres '^X' '' > fdopen.c << '/'
X/* fdopen - YAH (yet another hack) hacked from fopen.c */
X
X#include <stdio.h>
X
X#define PMODE 0644
X
X
XFILE *fdopen(fd,mode)
Xint fd;
Xchar *mode;
X{
X register int i;
X FILE *fp;
X char *malloc();
X int flags = 0;
X
X for (i = 0; _io_table[i] != 0 ; i++)
X if ( i >= NFILES )
X return(NULL);
X
X switch(*mode){
X
X case 'w':
X flags |= WRITEMODE;
X break;
X
X case 'a':
X flags |= WRITEMODE;
X lseek(fd,0L,2);
X break;
X
X case 'r':
X flags |= READMODE;
X break;
X
X default:
X return(NULL);
X }
X
X
X if (( fp = (FILE *) malloc (sizeof( FILE))) == NULL )
X return(NULL);
X
X
X fp->_count = 0;
X fp->_fd = fd;
X fp->_flags = flags;
X fp->_buf = malloc( BUFSIZ );
X if ( fp->_buf == NULL )
X fp->_flags |= UNBUFF;
X else
X fp->_flags |= IOMYBUF;
X
X fp->_ptr = fp->_buf;
X _io_table[i] = fp;
X return(fp);
X}
/
echo x - ctime.c
gres '^X' '' > ctime.c << '/'
X/* ctime.c yah (yet another hack) from date.c */
X
X
Xint days_per_month[] =
X { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Xchar *months[] =
X { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
X "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
Xchar *days[] =
X { "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed" };
X
Xstruct {
X int year, month, day, hour, min, sec;
X} tm;
X
Xlong s_p_min;
Xlong s_p_hour;
Xlong s_p_day;
Xlong s_p_year;
X
Xchar t_buf[26];
X
Xchar *ctime(t)
Xlong *t;
X{
X long tt;
X
X s_p_min = 60L;
X s_p_hour = 60L * 60L;
X s_p_day = 60L * 60L * 24L;
X s_p_year = 60L * 60L * 60L * 365L;
X
X tt = *t;
X cv_time(tt);
X
X sprintf(t_buf,"%s %s %02d %02d:%02d:%02d %04d\n", days[(tt / s_p_day) % 7],
X months[tm.month], tm.day, tm.hour, tm.min, tm.sec, tm.year);
X return(t_buf);
X}
X
Xcv_time(t)
Xlong t;
X{
X tm.year = 0;
X tm.month = 0;
X tm.day = 1;
X tm.hour = 0;
X tm.min = 0;
X tm.sec = 0;
X while (t >= s_p_year) {
X if (((tm.year + 2) % 4) == 0)
X t -= s_p_day;
X tm.year += 1;
X t -= s_p_year;
X }
X if (((tm.year + 2) % 4) == 0)
X days_per_month[1]++;
X tm.year += 1970;
X while ( t >= (days_per_month[tm.month] * s_p_day))
X t -= days_per_month[tm.month++] * s_p_day;
X while (t >= s_p_day) {
X t -= s_p_day;
X tm.day++;
X }
X while (t >= s_p_hour) {
X t -= s_p_hour;
X tm.hour++;
X }
X while (t >= s_p_min) {
X t -= s_p_min;
X tm.min++;
X }
X tm.sec = (int) t;
X}
X
X/* finis */
/