| 1 | /* Copyright (c) 1979 Regents of the University of California */ |
| 2 | #include <retrofit.h> |
| 3 | |
| 4 | /* funny - |
| 5 | * funny accepts a line from the user and prints it out in |
| 6 | * a mildly amusing manner. |
| 7 | */ |
| 8 | |
| 9 | #define WORDLEN 200 |
| 10 | char nl[] { 015, 0}; |
| 11 | |
| 12 | char is3a; |
| 13 | |
| 14 | main(ct,av) |
| 15 | int ct; |
| 16 | char **av; |
| 17 | { |
| 18 | int cvec[2],size,order[WORDLEN],i,rubout(); |
| 19 | char *word,current[WORDLEN],myline[WORDLEN],*src; |
| 20 | |
| 21 | if (ct > 1 && av[1][0] == '-') { |
| 22 | is3a++; |
| 23 | ct--; |
| 24 | av++; |
| 25 | } |
| 26 | time(cvec); |
| 27 | srand(cvec[1]); |
| 28 | if (ct<2) |
| 29 | { |
| 30 | printf("Input a line - "); |
| 31 | size=read(0,myline,WORDLEN)-1; |
| 32 | myline[size]=0; |
| 33 | src=myline; |
| 34 | } |
| 35 | else src=av[1]; |
| 36 | signal(2,rubout); |
| 37 | word=src; |
| 38 | size=0; |
| 39 | while (*word++) size++; |
| 40 | if (size>=WORDLEN) |
| 41 | { |
| 42 | printf("%s\nLINE TOO LONG.\n",src); |
| 43 | exit(9); |
| 44 | } |
| 45 | word=src; |
| 46 | for (;;) |
| 47 | { |
| 48 | for (i=0;i<size;++i) current[i]=040; |
| 49 | pick(size,order); |
| 50 | for (i=0;i<size;++i) prch(order[i],word,current); |
| 51 | printf("\n"); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pick(s,a) |
| 56 | int s,*a; |
| 57 | { |
| 58 | int i,j,r,this[WORDLEN]; |
| 59 | for (i=0;i<s;++i) this[i]=i; |
| 60 | for (i=s-1;i>=0;--i) |
| 61 | { |
| 62 | r=rnd(i+1); |
| 63 | a[i]=this[r]; |
| 64 | this[r]=this[i]; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | rnd(i) |
| 69 | int i; |
| 70 | { |
| 71 | int r; |
| 72 | r=rand(); |
| 73 | if (r<0) r= -r; |
| 74 | return(r % i); |
| 75 | } |
| 76 | |
| 77 | prch(who,from,to) |
| 78 | int who; |
| 79 | char *from,*to; |
| 80 | { |
| 81 | int i; |
| 82 | to[who]=from[who]; |
| 83 | if (is3a) { |
| 84 | printf("\033=%c%c%c", ' '+23, ' '+who, to[who]); |
| 85 | return; |
| 86 | } |
| 87 | for (i=0;i<=who;++i) write(1,to+i,1); |
| 88 | /* |
| 89 | for (i=0;i<=who;++i) write(1,"\b",1); |
| 90 | */ |
| 91 | write(1,nl,1); |
| 92 | } |
| 93 | |
| 94 | rubout() |
| 95 | { |
| 96 | char *cp,resp[100]; |
| 97 | signal(2,1); |
| 98 | printf("\nHad enough?"); |
| 99 | read(0,resp,100); |
| 100 | cp=resp; |
| 101 | while (*cp != 012 && *cp != 'y' && *cp !='n') ++cp; |
| 102 | if (*cp == 'n') |
| 103 | { |
| 104 | signal(2,rubout); |
| 105 | return; |
| 106 | } |
| 107 | exit(9); |
| 108 | } |