Category : Files from Magazines
Archive   : BYTE0288.ZIP
Filename : PWC.C
/* little demo word count program showing single chip concurrency
*/
/* works, too */
#include
#include
#include
#include "llst.h"
#include "ld.h"
#define TOGREP CommOne
#define FROMGREP CommOne
#define TOIOPROC CommOne
#define FROMIOPROC CommOne
#define MAXSTRLEN 255
#define HOSTOUT _tohost
#define TRUE 1 /* Socratic definitions */
#define FALSE 0
channel CommOne;
channel CommTwo;
extern channel *_tohost;
char *fgets();
void IOProc();
main(argc, argv)
int argc;
char *argv[];
{
char buf[MAXSTRLEN], *ptr;
int fd, inword, nw;
if((fd = open(argv[1], O_RDONLY)) <= 0) {
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(1);
}
startp(IOProc);
resetch(&CommOne);
resetch(&CommTwo);
TOIOPROC = fd;
/* CommTwo = "start\n"; */
inword = FALSE;
nw = 0;
while(fgets(buf, MAXSTRLEN, (FILE*)0)) {
for(ptr = buf; *ptr && *ptr != '\n'; ptr++) {
if(isspace(*ptr)) /* count words */
inword = FALSE;
else if (inword == FALSE) {
inword = TRUE;
++nw;
}
}
}
/* CommTwo = "done\n"; */
printf("%d words\n", nw);
close(fd);
exit(0);
}
char *fgets(str, n, file)
char *str; int n; FILE *file;
{
char *pt = str;
static int c = 0;
static char *bufptr = 0, *bend = 0;
ITEM_T *item;
if(c == EOF) { c = 0; return(char*)0; }
while(--n > 0) {
if(bufptr < bend)
c = (int) *(bufptr++);
else {
item = FROMIOPROC;
if(!item || item->bufsize <= 0)
c = EOF;
else {
bufptr = item->buf;
bend = bufptr + item->bufsize;
c = *(bufptr++);
}
}
if( c == EOF || c == '\n') {
if( c == '\n')
*(pt++) = '\n';
*pt = '\0';
if( c == EOF && pt == str) {
return((char*)0);
}
else {
return(str);
}
} else *(pt++) = c;
}
*pt = '\0';
return(str);
}
void IOProc()
{
char *malloc(), *s;
int fd, i;
ITEM_T *item;
LISTHDR_T Header;
NODE_T *CurrentNode;
for(;;) {
Header.first = Header.last = (NODE_T*)0;
for(fd=0;fd==0;) { /* wait here */
alt {
guard &CommOne: fd = FROMGREP;
break;
guard &CommTwo: s = (char*)
CommTwo; putstr(s); break;
}
}
item = (ITEM_T*) malloc(sizeof(ITEM_T));
while((item->bufsize = read(fd, item->buf,
BUFSIZ)) > 0) {
CurrentNode =
(NODE_T*)malloc(sizeof(NODE_T));
memset(CurrentNode, 0, sizeof(NODE_T));
CurrentNode->item = item;
addhead(&Header, CurrentNode);
item = (ITEM_T*) malloc(sizeof(ITEM_T));
alt {
guard &TOGREP: if((CurrentNode =
Header.last) != 0) {
TOGREP = (ITEM_T*) CurrentNode->item;
rmv(&Header, CurrentNode);
} break;
guard &CommTwo: s = (char*)
CommTwo;
putstr(s);
break;
}
}
while((CurrentNode = Header.last) != 0) {
alt { guard &TOGREP: TOGREP =
(ITEM_T*)CurrentNode->item;
rmv(&Header, CurrentNode);
break;
guard &CommTwo: s = (char*) CommTwo; putstr(s); break;
}
}
flush:
alt { guard &CommTwo: s = (char*) CommTwo;
putstr(s); goto flush;
guard &TOGREP:
TOGREP = (ITEM_T*)0; break; }
/* TOGREP = (ITEM_T*)0; /* wait here */
}
}
NODE_T *rmv(header, node)
LISTHDR_T *header; NODE_T *node;
{
if(node->prev != NULL)
node->prev->next = node->next;
else header->first = node->next;
if(node->next != NULL)
node->next->prev = node->prev;
else header->last = node->prev;
return(node);
}
void addhead(header, node)
LISTHDR_T *header; NODE_T *node;
{
if(header->first == NULL)
header->last = node;
node->next = header->first;
if(node->next != NULL)
node->next->prev = node;
node->prev = NULL;
header->first = node;
}
putstr(s)
char *s;
{
char *buf, *_msgfmt();
unsigned int packet_length;
buf = _msgfmt("%d%d%s",R_FPUTS,1,s);
send_packet(HOSTOUT,*buf,buf);
free(buf);
}
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/