#include #ifndef sun #include #endif #include #define lockfile "/mclab/imc/main/etc/oneword.lock" #define wordfile "/mclab/imc/main/etc/oneword.words" #define lastuser "/mclab/imc/main/etc/oneword.lastuser" #define worduser "/mclab/imc/main/etc/oneword.worduser" #include #include #include FILE *f; int check(); void print(); void die(); int uid; int width=70; main() { struct stat sbuf; unsigned size; char word[100]; int p; int y; int w; int e; int para; struct winsize sz; char c; uid=getuid(); if(!ioctl(0,TIOCGWINSZ,&sz))width=sz.ws_col-1; if(width<40||width>100)width=70; while(1){ if (stat(wordfile,&sbuf)==-1)die(1); size=sbuf.st_size; puts("The story so far..."), print(), putchar('\n'); if(check())puts("Sorry, you were the last person to add a word."), puts("Please try again later."), exit(1); puts("Enter your word. If you want to start a new paragraph,"), puts("prefix your word with a space."), puts("If you do not want to enter a word, just press return."), gets(word), word[99]=0; if(strlen(word)>width/2){ puts("Sorry, your word is too long. It may not be added."); continue; } for(p=0;word[p]==' ';p++); /* p=first non-blank char */ if(!word[p])exit(0); for(e=p;(c=word[e])&&strchr(")\"\'!,.:;?}] ",c);e++); while(e>p&&word[e-1]==' ')e--; /* e=first non-punctuation */ for(w=e;(c=word[w])&&c==' ';w++); /* w=first non-blank char after punct */ for(y=w;c=word[y];y++) if(c<=' '||c>0x7e)w=-1; if(w==-1){ puts("Sorry, your word contains an invalid character. It may not be added."); continue; } if(e>p&&word[e-1]=='\"'&&(e-1>p&&word[e-2]==' '||e-1==p&&word[e]!=' ')) for(e--;e>p&&word[e-1]==' ';e--); /* discount opening quotes */ if(!(f=fopen(lockfile,"a")))die(2); fwrite("*",1,1,f), y=(int)ftell(f), fclose(f); if(y>1){ puts("Sorry, someone is just updating the file."), puts("Please wait then try again."), sleep(2); continue; } if (stat(wordfile,&sbuf)==-1)die(1); if (size!=sbuf.st_size) puts("Sorry, someone has updated the file."), puts("Please try again."), unlink(lockfile); else { if(!(f=fopen(wordfile,"a"))) unlink(lockfile),die(3); para=p; while(e>p)if((c=word[p++])!=' ')fputc(c,f); if(para)fputc('\n',f); else if(word[w])fputc(' ',f); while(pwidth){ words[s]=0; puts(words); for(i=0;i