--- os2sdk/demos/apps/cpgrep/cpgrep.c 2018/08/09 12:25:13 1.1 +++ os2sdk/demos/apps/cpgrep/cpgrep.c 2018/08/09 12:26:03 1.1.1.2 @@ -1,26 +1,21 @@ -/* cpgrep -* -* HISTORY: -* 23-Mar-87 danl exit(x) -> DOSEXIT(1, x) -* -*/ - +/* cpgrep - string searches + * + * Created by Microsoft Corp. 1986 + * + * + */ + +#include +#define INCL_DOSPROCESS +#define INCL_DOSSEMAPHORES +#define INCL_DOSQUEUES +#define INCL_DOSMEMMGR +#define INCL_DOSMISC +#include #include #include #include -int far pascal DOSCREATETHREAD(void (far *)(),int far *,int far *); -int far pascal DOSGETMACHINEMODE(char far *); -int far pascal DOSQHANDTYPE(int,int far *,int far *); -int far pascal DOSREAD(int,char far *,int,int far *); -int far pascal DOSSEMCLEAR(long far *); -int far pascal DOSSEMREQUEST(long far *,long); -int far pascal DOSSEMSET(long far *); -int far pascal DOSSEMWAIT(long far *,long); -int far pascal DOSSLEEP(long); -int far pascal DOSWRITE(int,char far *,int,int far *); -int far pascal DOSEXIT(int, int); - #define BEGLINE 0x40 #define DEBUG 0x08 #define ENDLINE 0x80 @@ -55,30 +50,30 @@ char *bufptr[] = { filbuf, filbuf + FI char outbuf[OUTBUFLEN*2]; char *obuf[] = { outbuf, outbuf + OUTBUFLEN }; char *optr[] = { outbuf, outbuf + OUTBUFLEN }; -int ocnt[] = { OUTBUFLEN, OUTBUFLEN }; +USHORT ocnt[] = { OUTBUFLEN, OUTBUFLEN }; int oi = 0; char transtab[TRTABLEN] = { 0 }; STRINGNODE *stringlist[TRTABLEN/2]; -int arrc; /* I/O return code for DOSREAD */ -int awrc; /* I/O return code for DOSWRITE */ +USHORT arrc; /* I/O return code for DosRead */ +USHORT awrc; /* I/O return code for DosWrite */ int casesen = 1; /* Assume case-sensitivity */ -int cbread; /* Bytes read by DOSREAD */ -int cbwrite; /* Bytes written by DOSWRITE */ +USHORT cbread; /* Bytes read by DosRead */ +USHORT cbwrite; /* Bytes written by DosWrite */ int clists = 1; /* One is first available index */ int flags; /* Flags */ int lineno; /* Current line number */ char pmode; /* Protected mode flag */ -long readdone; /* Async read done semaphore */ -long readpending; /* Async read pending semaphore */ +LONG readdone; /* Async read done semaphore */ +LONG readpending; /* Async read pending semaphore */ int status = 1; /* Assume failure */ char *t2buf; /* Async read buffer */ -int t2buflen; /* Async read buffer length */ -int t2fd; /* Async read file */ +USHORT t2buflen; /* Async read buffer length */ +HFILE t2fd; /* Async read file */ char *t3buf; /* Async write buffer */ -int t3buflen; /* Async write buffer length */ -int t3fd; /* Async write file */ -long writedone; /* Async write done semaphore */ -long writepending; /* Async write pending semaphore */ +USHORT t3buflen; /* Async write buffer length */ +HFILE t3fd; /* Async write file */ +LONG writedone; /* Async write done semaphore */ +LONG writepending; /* Async write pending semaphore */ char target[MAXSTRLEN]; /* Last string added */ int targetlen; /* Length of last string added */ @@ -123,7 +118,7 @@ int n; /* Length of string */ if((t = malloc(sizeof(STRINGNODE) + n + (n & 1))) == NULL) { /* If allocation fails */ fprintf(stderr,"Out of memory\n"); - DOSEXIT(1, 2); /* Print error message and die */ + DosExit( EXIT_PROCESS, 2); /* Print error message and die */ } if(n & 1) ++t; /* END of string word-aligned */ strncpy(t,s,n); /* Copy string text */ @@ -144,7 +139,7 @@ register int n; /* Length of string */ { fprintf(stderr,"Internal error\n"); /* Error message */ - DOSEXIT(1, 2); /* Error exit */ + DosExit( EXIT_PROCESS, 2); /* Error exit */ } node->s_must = n; /* Set new length */ memcpy(s_text(node),s,n); /* Copy new text */ @@ -173,7 +168,7 @@ int n; /* Length of string */ { /* If too many string lists */ fprintf(stderr,"Too many string lists\n"); /* Error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } stringlist[i] = NULL; /* Initialize */ transtab[*s] = i; /* Set pointer to new list */ @@ -364,10 +359,10 @@ void dumpstrings() } -int openfile(name) +HFILE openfile(name) char *name; /* File name */ { - int fd; /* File descriptor */ + HFILE fd; /* File descriptor */ if((fd = open(name,0)) == -1) /* If error opening file */ { @@ -380,101 +375,97 @@ char *name; /* File name */ void far thread2() /* Read thread */ { - while(DOSSEMREQUEST((long far *) &readpending,-1L) == 0) + while(DosSemRequest( &readpending, -1L) == 0) { /* While there is work to do */ - arrc = DOSREAD(t2fd,(char far *) t2buf,t2buflen,(int far *) &cbread); + arrc = DosRead( t2fd, t2buf, t2buflen, &cbread); /* Do the read */ - DOSSEMCLEAR((long far *) &readdone); - /* Signal read completed */ + DosSemClear( &readdone); /* Signal read completed */ } - fprintf(stderr,"Thread 2: DOSSEMREQUEST failed\n"); + fprintf(stderr,"Thread 2: DosSemRequest failed\n"); /* Print error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } void far thread3() /* Write thread */ { - while(DOSSEMREQUEST((long far *) &writepending,-1L) == 0) + while(DosSemRequest((long far *) &writepending,-1L) == 0) { /* While there is work to do */ - awrc = DOSWRITE(t3fd,(char far *) t3buf,t3buflen,(int far *) &cbwrite); + awrc = DosWrite(t3fd, t3buf, t3buflen, &cbwrite); /* Do the write */ - DOSSEMCLEAR((long far *) &writedone); - /* Signal write completed */ + DosSemClear( &writedone); /* Signal write completed */ } - fprintf(stderr,"Thread 3: DOSSEMREQUEST failed\n"); + fprintf(stderr,"Thread 3: DosSemRequest failed\n"); /* Print error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } void startread(fd,buffer,buflen) -int fd; /* File handle */ +HFILE fd; /* File handle */ char *buffer; /* Buffer */ -int buflen; /* Buffer length */ +USHORT buflen; /* Buffer length */ { if(pmode) /* If protected mode */ { - if(DOSSEMREQUEST((long far *) &readdone,-1L) != 0) + if(DosSemRequest( &readdone, -1L) != 0) { /* If we fail to get the semaphore */ - fprintf(stderr,"DOSSEMREQUEST failed\n"); + fprintf(stderr,"DosSemRequest failed\n"); /* Error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } t2fd = fd; /* Set parameters for read */ t2buf = buffer; t2buflen = buflen; - DOSSEMCLEAR((long far *) &readpending); - /* Wake thread 2 for read */ - DOSSLEEP(0L); /* Yield the CPU */ + DosSemClear( &readpending); /* Wake thread 2 for read */ + DosSleep(0L); /* Yield the CPU */ } - else arrc = DOSREAD(fd,(char far *) buffer,buflen,(int far *) &cbread); + else arrc = DosRead( fd, buffer, buflen, &cbread); } int finishread() { - if(pmode && DOSSEMWAIT((long far *) &readdone,-1L) != 0) + if(pmode && DosSemWait( &readdone, -1L) != 0) { /* If protected mode and wait fails */ - fprintf(stderr,"DOSSEMWAIT failed\n"); + fprintf(stderr,"DosSemWait failed\n"); /* Print error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } return((arrc == 0)? cbread: -1); /* Return number of bytes read */ } void startwrite(fd,buffer,buflen) -int fd; /* File handle */ +HFILE fd; /* File handle */ char *buffer; /* Buffer */ -int buflen; /* Buffer length */ +USHORT buflen; /* Buffer length */ { if(pmode) /* If protected mode */ { - if(DOSSEMREQUEST((long far *) &writedone,-1L) != 0) + if(DosSemRequest( &writedone, -1L) != 0) { /* If we fail to get the semaphore */ - fprintf(stderr,"DOSSEMREQUEST failed\n"); + fprintf(stderr,"DosSemRequest failed\n"); /* Error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } t3fd = fd; /* Set parameters for write */ t3buf = buffer; t3buflen = buflen; - DOSSEMCLEAR((long far *) &writepending); - /* Wake thread 3 for read */ - DOSSLEEP(0L); /* Yield the CPU */ + DosSemClear( &writepending); /* Wake thread 3 for read */ + DosSleep(0L); /* Yield the CPU */ } - else awrc = DOSWRITE(fd,(char far *) buffer,buflen,(int far *) &cbwrite); + else awrc = DosWrite(fd, buffer, buflen, &cbwrite); } int finishwrite() { - if(pmode && DOSSEMWAIT((long far *) &writedone,-1L) != 0) + if(pmode && DosSemWait( &writedone, -1L) != 0) { /* If protected mode and wait fails */ - fprintf(stderr,"DOSSEMWAIT failed\n"); + fprintf(stderr,"DosSemWait failed\n"); /* Print error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } return((awrc == 0)? cbwrite: -1); /* Return number of bytes written */ } @@ -482,25 +473,25 @@ int finishwrite() void write1nobuf(buffer,buflen) char *buffer; /* Buffer */ -register int buflen; /* Buffer length */ +USHORT buflen; /* Buffer length */ { int cb; /* Count of bytes written */ - if(DOSWRITE(1,(char far *) buffer,buflen,(int far *) &cb) != 0 || - cb != buflen) /* If write fails */ + if( DosWrite(1, buffer, buflen, &cb) != 0 || cb != buflen) + /* If write fails */ { fprintf(stderr,"write error %d\n",awrc); /* Print error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } } void write1buf(buffer,buflen) char *buffer; /* Buffer */ -register int buflen; /* Buffer length */ +USHORT buflen; /* Buffer length */ { - register int cb; /* Byte count */ + USHORT cb; /* Byte count */ while(buflen > 0) /* While bytes remain */ { @@ -508,7 +499,7 @@ register int buflen; /* Buffer length { fprintf(stderr,"write error %d\n",awrc); /* Print error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } if((cb = ocnt[oi]) == 0) /* If buffer full */ { @@ -545,7 +536,7 @@ void flush1buf() { fprintf(stderr,"write error %d\n",awrc); /* Print error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } } } @@ -686,7 +677,7 @@ char *name; /* File name */ void qgrep(name,fd) char *name; /* File name */ -int fd; /* File descriptor */ +HFILE fd; /* File descriptor */ { register int cb; /* Byte count */ register char *cp; /* Buffer pointer */ @@ -700,7 +691,7 @@ int fd; /* File descriptor */ bufi = 0; /* Initialize buffer index */ cp = bufptr[0]; /* Initialize to start of buffer */ finishread(); /* Make sure no I/O activity */ - arrc = DOSREAD(fd,(char far *) cp,FILBUFLEN,(int far *) &cbread); + arrc = DosRead( fd, cp, FILBUFLEN, &cbread); /* Do first read synchronously */ while((cb = finishread()) + taillen > 0) { /* While search incomplete */ @@ -758,7 +749,7 @@ int verbose; /* Verbose message flag * while(*opt != 0) fprintf(stderr,"%s\n",*opt++); /* Print option list */ } - DOSEXIT(1, 2); /* Error exit */ + DosExit( EXIT_PROCESS, 2); /* Error exit */ } @@ -767,22 +758,24 @@ int argc; char **argv; { register char *cp; - int fd; + HFILE fd; FILE *fi; char filnam[FILNAMLEN]; + USHORT handType; + USHORT handAttrib; int i; char *inpfile = NULL; - int j; + int j; char *seplist = " \t"; int strcnt; char *strfile = NULL; long start; /* Start time */ int (*add)(); - int t2stk[STKLEN]; /* Read thread stack */ - int t3stk[STKLEN]; /* Write thread stack */ + BYTE t2stk[2*STKLEN]; /* Read thread stack */ + BYTE t3stk[2*STKLEN]; /* Write thread stack */ long time(); /* Time and date in seconds */ - DOSGETMACHINEMODE((char far *) &pmode); + DosGetMachineMode((char far *) &pmode); flags = 0; for(i = 1; i < argc && argv[i][0] == '-'; ++i) { @@ -793,7 +786,7 @@ char **argv; if(i == argc - 1) { fprintf(stderr,"File name missing after -%c\n",argv[i][1]); - DOSEXIT(1, 2); + DosExit( EXIT_PROCESS, 2); } if(argv[i++][1] == 'i') inpfile = argv[i]; else strfile = argv[i]; @@ -893,18 +886,18 @@ char **argv; /* Get start time if timer on */ if(pmode) /* Initialize semaphores and threads */ { - DOSSEMCLEAR((long far *) &readdone); - DOSSEMCLEAR((long far *) &writedone); - DOSSEMSET((long far *) &readpending); - DOSSEMSET((long far *) &writepending); - if(DOSCREATETHREAD(thread2,(int far *) &fd, - (int far *) t2stk + STKLEN) != 0 || - DOSCREATETHREAD(thread3,(int far *) &fd, - (int far *) t3stk + STKLEN) != 0) + TID threadId; + + DosSemClear( &readdone); + DosSemClear( &writedone); + DosSemSet( &readpending); + DosSemSet( &writepending); + if(DosCreateThread(thread2, &threadId, t2stk + 2*STKLEN) != 0 || + DosCreateThread(thread3, &threadId, t3stk + 2*STKLEN) != 0) { /* If thread creation fails */ fprintf(stderr,"Failed to create child threads\n"); /* Print error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } } setmode(fileno(stdout),O_BINARY); @@ -916,7 +909,7 @@ char **argv; if((fd = open(strfile,0)) == -1) { /* If open fails */ fprintf(stderr,"Cannot read strings from %s\n",strfile); - DOSEXIT(1, 2); /* Print message and die */ + DosExit( EXIT_PROCESS, 2); /* Print message and die */ } for(cp = filbuf, j = 0; (j = read(fd,cp,FILBUFLEN*2 - j)) > 0; cp += j); /* Read strings file into buffer */ @@ -934,20 +927,20 @@ char **argv; if((strcnt = (*add)(cp,j,seplist)) == 0) { /* If no strings */ fprintf(stderr,"No search strings\n"); - DOSEXIT(1, 2); /* Print error message and die */ + DosExit( EXIT_PROCESS, 2); /* Print error message and die */ } /* * Check type of handle for std. out. */ - if(DOSQHANDTYPE(fileno(stdout),(int far *) &j,(int far *) &fd) != 0) + if(DosQHandType(fileno(stdout), &handType, &handAttrib) != 0) { /* If error */ fprintf(stderr,"Standard output bad handle\n"); /* Print error message */ - DOSEXIT(1, 2); /* Die */ + DosExit( EXIT_PROCESS, 2); /* Die */ } - if(j != 0 && (fd & ISCOT)) /* If handle is console output */ - { + if(handType != 0 && (handAttrib & ISCOT)) + { /* If handle is console output */ write1 = write1nobuf; /* Use unbuffered output */ flush1 = flush1nobuf; } @@ -968,7 +961,7 @@ char **argv; { /* If open fails */ fprintf(stderr,"Cannot read file list from %s\r\n",inpfile); /* Error message */ - DOSEXIT(1, 2); /* Error exit */ + DosExit( EXIT_PROCESS, 2); /* Error exit */ } while(fgets(filnam,FILNAMLEN,fi) != NULL) { /* While there are names */ @@ -997,5 +990,5 @@ char **argv; (*flush1)(); if(flags & TIMER) fprintf(stderr,"%ld seconds\n",time(NULL) - start); /* Print elapsed time if timer on */ - DOSEXIT(1, status); + DosExit( EXIT_PROCESS, status); }