home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / shell / csh531s.lha / comm1.c next >
Encoding:
C/C++ Source or Header  |  1993-06-20  |  46.3 KB  |  2,103 lines

  1. /*
  2.  * COMM1.C
  3.  *
  4.  * Matthew Dillon, August 1986
  5.  *
  6.  * Version 2.07M by Steve Drew 10-Sep-87
  7.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  8.  * Version 5.00L by Urban Mueller 17-Feb-91
  9.  * Version 5.20L by Andreas M. Kirchwitz (Fri, 13 Mar 1992)
  10.  *
  11.  */
  12.  
  13. #include "shell.h"
  14.  
  15. /* comm1.c */
  16. static void display_file(char *filestr);
  17. static int search_file( long mask, char *s, char *fullpath );
  18. static int rm_file    ( long mask, char *s, char *fullpath );
  19. static int quicksearch(char *name, int nocasedep, char *pattern);
  20. static void setsystemtime(struct DateStamp *ds);
  21. static int found( char *lstart, int lnum, int loffs, char *name, char left );
  22.  
  23. void lformat( char *s, char *d, FILEINFO *info );
  24.  
  25. extern int has_wild;
  26.  
  27. int
  28. do_sleep( void )
  29. {
  30.     int i;
  31.  
  32.     if (ac == 2) for (i=atoi(av[1]); i>0 && !CHECKBREAK(); i--) Delay(50);
  33.     return 0;
  34. }
  35.  
  36. /* AMK: if you change this, you must change do_chmod() also!! */
  37. int
  38. do_protect( void )
  39. {
  40.     static char flags[]="DEWRAPSH";
  41.     char *s, *p;
  42.     long setmask=0, clrmask=0xFF, mask;
  43.     int  i, mode=0, stat;
  44.     DPTR *dp;
  45.  
  46.     for (s=strupr(av[--ac]); *s; s++) {
  47.         if (*s=='=') { mode=0; continue; }
  48.         if (*s=='+') { mode=1; clrmask=FIBF_ARCHIVE; continue; }
  49.         if (*s=='-') { mode=2; clrmask=FIBF_ARCHIVE; continue; }
  50.  
  51.         if (*s=='X') *s='E';
  52.         if (p=index(flags, *s)) {
  53.             if( mode==0 ) setmask|= 1<<(p-flags), clrmask=0xFF;
  54.             if( mode==1 ) setmask|= 1<<(p-flags);
  55.             if( mode==2 ) clrmask|= 1<<(p-flags);
  56.         } else {
  57.             ierror(av[ac],500);
  58.             return 20;
  59.         }
  60.     }
  61.  
  62.     for (i=1; i<ac; i++) {
  63.         if( (dp=dopen(av[i],&stat))) {
  64.             mask = dp->fib->fib_Protection ^ 0x0F;
  65.             mask&=~clrmask;
  66.             mask|= setmask;
  67.             dclose(dp);
  68.             if( !SetProtection( av[i], mask ^ 0x0F))
  69.                 pError(av[i]);
  70.         } else
  71.             pError(av[i]);
  72.     }
  73.     return 0;
  74. }
  75.  
  76. /* AMK: same as do_protect, but flags now as first argument */
  77. int
  78. do_chmod( void )
  79. {
  80.     static char flags[]="DEWRAPSH";
  81.     char *s, *p;
  82.     long setmask=0, clrmask=0xFF, mask;
  83.     int  i, mode=0, stat;
  84.     DPTR *dp;
  85.  
  86.     for (s=strupr(av[1]); *s; s++) {       /* AMK: changed */
  87.         if (*s=='=') { mode=0; continue; }
  88.         if (*s=='+') { mode=1; clrmask=FIBF_ARCHIVE; continue; }
  89.         if (*s=='-') { mode=2; clrmask=FIBF_ARCHIVE; continue; }
  90.  
  91.         if (*s=='X') *s='E';
  92.         if (p=index(flags, *s)) {
  93.             if( mode==0 ) setmask|= 1<<(p-flags), clrmask=0xFF;
  94.             if( mode==1 ) setmask|= 1<<(p-flags);
  95.             if( mode==2 ) clrmask|= 1<<(p-flags);
  96.         } else {
  97.             ierror(av[1],500);     /* AMK: changed */
  98.             return 20;
  99.         }
  100.     }
  101.  
  102.     for (i=2; i<ac; i++) {                 /* AMK: changed */
  103.         if( (dp=dopen(av[i],&stat))) {
  104.             mask = dp->fib->fib_Protection ^ 0x0F;
  105.             mask&=~clrmask;
  106.             mask|= setmask;
  107.             dclose(dp);
  108.             if( !SetProtection( av[i], mask ^ 0x0F))
  109.                 pError(av[i]);
  110.         } else
  111.             pError(av[i]);
  112.     }
  113.     return 0;
  114. }
  115.  
  116. int
  117. do_filenote( void )
  118. {
  119.     DPTR *dp;
  120.     char *note;
  121.     int i, stat;
  122.  
  123.     if( options&1 ) {
  124.         for( i=1; i<ac && !dobreak(); i++ )
  125.             if( dp=dopen( av[i], &stat )) {
  126.                 printf( "%-12s %s\n", av[i],dp->fib->fib_Comment );
  127.                 dclose( dp );
  128.             }
  129.     } else {
  130.         note=av[--ac];
  131.         for (i=1; i<ac; i++) if (!SetComment(av[i], note)) pError(av[i]);
  132.     }
  133.     return 0;
  134. }
  135.  
  136. int
  137. do_cat( void )
  138. {
  139.     FILE *fi;
  140.     int lctr, i, docr=0;
  141.     char buf[256], *l;
  142.  
  143.     prepscroll(0);
  144.     if (ac<=1) {
  145.         if (has_wild) { printf("No files matching\n"); return 20; }
  146.         lctr=0;
  147.         while (fgets(buf,256,stdin) && !dobreak()) {
  148.             if (options) printf("%4d ",++lctr);
  149.             quickscroll();
  150.             l=buf+strlen( buf )-1; docr=1;
  151.             if( l>=buf && *l=='\n' ) docr=0;
  152.             fputs(buf,stdout);
  153.         }
  154.     } else {
  155.         for (i=1; i<ac; i++)
  156.             if (fi = fopen (av[i], "r")) {
  157.                 lctr=0;
  158.                 while (fgets(buf,256,fi) && !dobreak()) {
  159.                     if (options&1) printf("%4d ",++lctr);
  160.                     quickscroll();
  161.                     l=buf+strlen( buf )-1; docr=1;
  162.                     if( l>=buf && *l=='\n' ) docr=0;
  163.                     fputs(buf,stdout); fflush(stdout);
  164.                 }
  165.                 fclose (fi);
  166.             } else
  167.                 pError(av[i]);
  168.     }
  169.     if( docr && isconsole(Output()))
  170.         putchar('\n');
  171.     return 0;
  172. }
  173.  
  174.  
  175.  
  176. char *add_simple_device(char *list,char *dev)
  177. {
  178.     char *new = NULL;
  179.  
  180.     if (list) {
  181.         if (new = malloc(strlen(dev)+strlen(list)+2)) {        /* null byte + \n */
  182.             strcpy(new,list);
  183.             strcat(new,dev);
  184.             strcat(new,"\n");
  185.             free(list);
  186.         }
  187.         else
  188.             new = list;
  189.     }
  190.     else {
  191.         if (new = malloc(strlen(dev)+2)) {            /* null byte + \n */
  192.             strcpy(new,dev);
  193.             strcat(new,"\n");
  194.         }
  195.     }
  196.  
  197.     return(new);
  198. }
  199.  
  200.  
  201.  
  202. void
  203. get_drives(char *buf)
  204. {
  205.     struct DosList *dl;
  206.     ULONG flags = LDF_DEVICES|LDF_READ;
  207.     char devname[256];
  208.     char **dev_list=NULL;
  209.     long i,dev_num=0;
  210.  
  211.     buf[0]=0;
  212.     if (dl=LockDosList(flags)) {
  213.         while (dl=NextDosEntry(dl,flags)) {
  214.             if (dl->dol_Task) {
  215.                 BtoCStr(devname,dl->dol_Name,254L);  /* 256 - '\0' + ':' */
  216.                 strcat(devname,":");
  217.                 add_array_list(&dev_list,&dev_num,devname);
  218.             }
  219.         }
  220.         UnLockDosList(flags);
  221.     }
  222.  
  223.     QuickSort(dev_list,dev_num);
  224.  
  225.     for(i=0; i<dev_num; i++) {
  226.         if (IsFileSystem(dev_list[i])) {
  227.             if (buf[0])
  228.                 strcat(buf,"\240");
  229.             strcat(buf,dev_list[i]);
  230.         }
  231.     }
  232.  
  233.     free_array_list(dev_list,dev_num);
  234. }
  235.  
  236. static char infobuf[100];
  237. static char namebuf[32];  /* AMK: old size was 12, too small for drive names */
  238.  
  239.  
  240. /* AMK: find last occurence of a character in a string */
  241. char *strlast(char *s,char c)
  242. {
  243.   char *p=NULL;
  244.   while(*s) {
  245.     if(*s==c)
  246.       p=s;
  247.     s++;
  248.   }
  249.   return(p);
  250. }
  251.  
  252. #if 0
  253. /* AMK: just another itok() */
  254. char *
  255. itok42( int i )
  256. {
  257.     static char buf[16], which;
  258.     char *exp=" KMG", *ptr= buf+(which=8-which);
  259.  
  260.     if (i<=10238976) {
  261.         sprintf( ptr,"%d%c",(i/1024),'K');
  262.         return ptr;
  263.     }
  264.  
  265.     do
  266.         i=(i+512)/1024, exp++;
  267.     while( i>1024 );
  268.     sprintf( ptr,"%d%c",i,*exp);
  269.  
  270.     return ptr;
  271. }
  272. #endif
  273.  
  274. char *
  275. drive_name( char *name )
  276. {
  277.     struct DosList *dl;
  278.     struct MsgPort *proc= (struct MsgPort *)DeviceProc( (void*)name );
  279.     ULONG flags = LDF_DEVICES|LDF_READ;
  280.     char devname[256];
  281.     char **dev_list=NULL;
  282.     long i,dev_num=0;
  283.  
  284.     /* AMK: we want no self-modifying code */
  285.     strncpy( namebuf, name, 31 );   /* AMK: 30 chars device name + ':' */
  286.     namebuf[31] = '\0';             /* AMK: null-terminated */
  287.     if (dl=LockDosList(flags)) {
  288.         while (dl=NextDosEntry(dl,flags)) {
  289.             if (dl->dol_Task) {
  290.                 BtoCStr(devname,dl->dol_Name,254L);  /* 256 - '\0' + ':' */
  291.                 strcat(devname,":");
  292.                 add_array_list(&dev_list,&dev_num,devname);
  293.             }
  294.         }
  295.         UnLockDosList(flags);
  296.     }
  297.  
  298.     QuickSort(dev_list,dev_num);
  299.  
  300.     for(i=0; i<dev_num; i++) {
  301.         if (IsFileSystem(dev_list[i])) {
  302.             if ((struct MsgPort *)DeviceProc(dev_list[i])==proc)
  303.                 strcpy(namebuf,dev_list[i]);
  304.         }
  305.     }
  306.  
  307.     free_array_list(dev_list,dev_num);
  308.  
  309.     return namebuf;
  310. }
  311.  
  312. int
  313. do_info( void )
  314. {
  315.     struct DosList *dl;
  316.     ULONG flags = LDF_DEVICES|LDF_READ;
  317.     char devname[256];
  318.     char **dev_list=NULL;
  319.     long i,dev_num=0;
  320.  
  321.     if (options&2)
  322.         puts("Unit     Size  Block  Type   Used   Free Full Errs  Status    Name");
  323.     else
  324.         puts("Unit     Size  Bytes  Used Blk/Byte-Free Full Errs  Status    Name");
  325.  
  326.     if( ac==1 ) {
  327.         if (dl=LockDosList(flags)) {
  328.             while (dl=NextDosEntry(dl,flags)) {
  329.                 if (dl->dol_Task) {
  330.                     BtoCStr(devname,dl->dol_Name,254L);  /* 256 - '\0' + ':' */
  331.                     strcat(devname,":");
  332.                     add_array_list(&dev_list,&dev_num,devname);
  333.                 }
  334.             }
  335.             UnLockDosList(flags);
  336.         }
  337.  
  338.         QuickSort(dev_list,dev_num);
  339.  
  340.         for(i=0; !dobreak() && i<dev_num; i++) {
  341.             if (IsFileSystem(dev_list[i])) {
  342.                 oneinfo(dev_list[i],0);
  343.             }
  344.         }
  345.  
  346.         free_array_list(dev_list,dev_num);
  347.     }
  348.     else {
  349.         for( i=1; i<ac; i++ )
  350.             oneinfo( drive_name( av[i] ), 0 );
  351.     }
  352.  
  353.     return 0;
  354. }
  355.  
  356.  
  357.  
  358. /* these defines are new in OS 3.x */
  359. #ifndef ID_FASTDIR_DOS_DISK
  360. #define ID_FASTDIR_DOS_DISK (0x444F5304L)
  361. #endif
  362. #ifndef ID_FASTDIR_FFS_DISK
  363. #define ID_FASTDIR_FFS_DISK (0x444F5305L)
  364. #endif
  365.  
  366.  
  367.  
  368. /* AMK: new mode==6 to suppress output if disk is not present */
  369. char *
  370. oneinfo( char *name, int mode )
  371. {
  372.     struct InfoData *info;
  373.     struct DevProc *devproc;
  374.     struct DeviceList *dl;
  375.     BPTR lock;
  376.     long size, free, freebl, blocks;
  377.     char buf[130], *state, *type;
  378.     char *fmt="%s\240%s\240%d\240%d\240%d\240%s\240%d%%\240%d\240%s\240%s";
  379.  
  380.     Myprocess->pr_WindowPtr = (APTR)(-1);
  381.  
  382.     if (!name) name="";
  383.  
  384.     if (mode<=1 || mode>=5)
  385.         strcpy(infobuf,"");
  386.     else
  387.         strcpy(infobuf,"0");
  388.  
  389.     info=(struct InfoData *)SAllocMem(sizeof(struct InfoData),MEMF_PUBLIC|MEMF_CLEAR);
  390.  
  391.     if (devproc=GetDeviceProc(name,NULL)) {
  392.         if (DoPkt(devproc->dvp_Port,ACTION_DISK_INFO,MKBADDR(info),NULL,NULL,NULL,NULL)==DOSTRUE) {
  393.             BOOL go_on = FALSE;
  394.             char *spclfmt;
  395. #if 0
  396.             NameFromLock(lock, buf, 128L);
  397.             if (p=strlast(buf,':')) *p = '\0';
  398.             /* AMK: we want the last occurence of ':', not the first;
  399.                     ':' and '/' are legal path name components !!
  400.             if (p=index(buf,':')) *p = '\0';
  401.             */
  402. #endif
  403.             switch (mode) {
  404.                 case 0:
  405.                     if (options&1)
  406.                         spclfmt = "";
  407.                     else
  408.                         spclfmt = "%-7s %s\n";
  409.                     break;
  410.                 case 1:
  411.                     spclfmt = "%s\240%s\n";
  412.                     break;
  413.                 case 2:
  414.                 case 3:
  415.                 case 4:
  416.                     spclfmt = "0";
  417.                     break;
  418.                 case 5:
  419.                     spclfmt = "";
  420.                     break;
  421.                 default:
  422.                     spclfmt = "";
  423.                     break;
  424.             }
  425.  
  426.             switch (info->id_DiskType) {
  427.                 case ID_UNREADABLE_DISK:
  428.                     sprintf(infobuf,spclfmt,name,"Unreadable disk");
  429.                     break;
  430.                 case ID_NOT_REALLY_DOS:
  431.                     sprintf(infobuf,spclfmt,name,"Not a DOS disk");
  432.                     break;
  433.                 case ID_KICKSTART_DISK:
  434.                     sprintf(infobuf,spclfmt,name,"Kickstart disk");
  435.                     break;
  436.                 case (0x42555359L):  /* 'BUSY' */
  437.                     sprintf(infobuf,spclfmt,name,"Disk is busy");
  438.                     break;
  439.                 case ID_NO_DISK_PRESENT:
  440.                     sprintf(infobuf,spclfmt,name,"No disk present");
  441.                     break;
  442.                 default:
  443.                     sprintf(infobuf,spclfmt,name,"Unknown disk type");
  444.                     go_on = TRUE;
  445.                     break;
  446.             }
  447.  
  448.             if (go_on && (lock=Lock(name,ACCESS_READ))) {
  449.                 UnLock(lock);
  450.                 /* note:  we call Lock() to be sure that the volume is readable */
  451.  
  452.                 switch(info->id_DiskType) {
  453.                     case ID_MSDOS_DISK:       type=" MSDOS"; break;
  454.                     case ID_DOS_DISK:         type="   OFS"; break;
  455.                     case ID_FFS_DISK:         type="   FFS"; break;
  456.                     case ID_INTER_DOS_DISK:   type="IN/OFS"; break;
  457.                     case ID_INTER_FFS_DISK:   type="  INTL"; break;
  458.                     case ID_FASTDIR_DOS_DISK: type="DC/OFS"; break;
  459.                     case ID_FASTDIR_FFS_DISK: type="  DCFS"; break;
  460.                     default:                  type="   n/a"; break;
  461.                 }
  462.  
  463.                 strcpy(buf,"n/a");
  464.                 if (dl = (struct DeviceList *)BADDR(info->id_VolumeNode))
  465.                     BtoCStr(buf,dl->dl_Name,100L);
  466.  
  467.                 switch(info->id_DiskState) {
  468.                     case ID_WRITE_PROTECTED: state="Read Only "; break;
  469.                     case ID_VALIDATED:       state="Read/Write"; break;
  470.                     case ID_VALIDATING:      state="Validating"; break;
  471.                     default:                 state="Unknown   "; break;
  472.                 }
  473.  
  474. #if 0
  475.                 size   = (info->id_NumBlocks + 2) * info->id_BytesPerBlock;
  476. #endif
  477.                 size   = info->id_NumBlocks * info->id_BytesPerBlock;
  478.                 freebl = info->id_NumBlocks - info->id_NumBlocksUsed;
  479.                 free   = freebl * info->id_BytesPerBlock;
  480.                 blocks = info->id_NumBlocks;
  481. #if 0
  482.                 printf("percents: %s... %ld, %ld, %ld, %ld, %ld\n",name,
  483.                     ((info->id_NumBlocksUsed+2) * 100)/blocks,
  484.                     (info->id_NumBlocksUsed * 100)/(blocks+2),
  485.                     (((info->id_NumBlocksUsed * 1000)/blocks)+5)/10,
  486.                     ((((info->id_NumBlocksUsed+2) * 1000)/blocks)+5)/10,
  487.                     (((info->id_NumBlocksUsed * 1000)/(blocks+2))+5)/10
  488.                 );
  489. #endif
  490.  
  491.                 if (mode==0 && options&2) {
  492.                     fmt="%-7s%6s%6d %6s %6s %6s %3d%%%4d  %10s %s\n";
  493.                     sprintf(infobuf,fmt,
  494.                         name,
  495.                         itok( size ),
  496.                         info->id_BytesPerBlock,
  497.                         type,
  498.                         itok( info->id_NumBlocksUsed*info->id_BytesPerBlock ),
  499.                         itok( free ),
  500.                         (blocks) ? (int)((( (double)info->id_NumBlocksUsed/(double)blocks ) * 1000.0 + 5.0) / 10.0) : 0,
  501. #if 0
  502.                         (blocks) ? ((info->id_NumBlocksUsed * 1000)/blocks + 5) / 10 : 0,
  503. #endif
  504.                         info->id_NumSoftErrors,
  505.                         state,
  506.                         buf);
  507.                 }
  508.                 else if (mode<=1) {
  509.                     if (mode==0) fmt="%-7s%6s%6d%7d%7d %6s%4d%%%4d  %s %s\n";
  510.                     sprintf(infobuf,fmt,
  511.                         name,
  512.                         itok( size ),
  513.                         info->id_BytesPerBlock,
  514.                         info->id_NumBlocksUsed,
  515.                         freebl,
  516.                         itok( free ),
  517.                         (blocks) ? (int)((( (double)info->id_NumBlocksUsed/(double)blocks ) * 1000.0 + 5.0) / 10.0) : 0,
  518.                         info->id_NumSoftErrors,
  519.                         state,
  520.                         buf);
  521.                 }
  522.                 else if (mode==2) sprintf(infobuf,"%d",free);
  523.                 else if (mode==3) sprintf(infobuf,"%d",freebl);
  524.                 else if (mode==4) sprintf(infobuf,"%s",itok(free));
  525.                 else if (mode==5) sprintf(infobuf,"%s:",buf);
  526.             }
  527.         }
  528.         else
  529.             pError(name);
  530.         FreeDeviceProc(devproc);
  531.     }
  532.  
  533. #if 0
  534.     else {
  535.         if (mode==1) {
  536.             struct MsgPort *devtask;
  537.             sprintf(infobuf,"%s\240No disk present\n",name);
  538.             if (devtask=(struct MsgPort *)DeviceProc(name)) {
  539.                 struct InfoData *infodata;
  540.                 infodata = SAllocMem(sizeof(struct InfoData),MEMF_PUBLIC|MEMF_CLEAR);
  541.                 if (DoPkt(devtask,ACTION_DISK_INFO,MKBADDR(infodata),NULL,NULL,NULL,NULL)) {
  542.                     switch (infodata->id_DiskType) {
  543.                     case ID_UNREADABLE_DISK:
  544.                         sprintf(infobuf,"%s\240Unreadable disk\n",name);
  545.                         break;
  546.                     case ID_NOT_REALLY_DOS:
  547.                         sprintf(infobuf,"%s\240Not a DOS disk\n",name);
  548.                         break;
  549.                     case ID_KICKSTART_DISK:
  550.                         sprintf(infobuf,"%s\240Kickstart disk\n",name);
  551.                         break;
  552.                     case (0x42555359L):  /* 'BUSY' */
  553.                         sprintf(infobuf,"%s\240Busy\n",name);
  554.                         break;
  555.                     case ID_NO_DISK_PRESENT:
  556.                     default:
  557.                         sprintf(infobuf,"%s\240No disk present\n",name);
  558.                         break;
  559.                     }
  560.                 }
  561.                 FreeMem(infodata,sizeof(struct InfoData));
  562.             }
  563.         }
  564.         else if (mode==0) {
  565.             if (options&1) {
  566.                 sprintf(infobuf,"");
  567.             }
  568.             else {
  569.                 struct MsgPort *devtask;
  570.                 sprintf(infobuf,"%-7s No disk present\n",name);
  571.                 if (devtask=(struct MsgPort *)DeviceProc(name)) {
  572.                     struct InfoData *infodata;
  573.                     infodata = SAllocMem(sizeof(struct InfoData),MEMF_PUBLIC|MEMF_CLEAR);
  574.                     if (DoPkt(devtask,ACTION_DISK_INFO,MKBADDR(infodata),NULL,NULL,NULL,NULL)) {
  575.                         switch (infodata->id_DiskType) {
  576.                         case ID_UNREADABLE_DISK:
  577.                             sprintf(infobuf,"%-7s Unreadable disk\n",name);
  578.                             break;
  579.                         case ID_NOT_REALLY_DOS:
  580.                             sprintf(infobuf,"%-7s Not a DOS disk\n",name);
  581.                             break;
  582.                         case ID_KICKSTART_DISK:
  583.                             sprintf(infobuf,"%-7s Kickstart disk\n",name);
  584.                             break;
  585.                         case (0x42555359L):  /* 'BUSY' */
  586.                             sprintf(infobuf,"%-7s Disk is busy\n",name);
  587.                             break;
  588.                             case ID_NO_DISK_PRESENT:
  589.                         default:
  590.                             sprintf(infobuf,"%-7s No disk present\n",name);
  591.                             break;
  592.                         }
  593.                     }
  594.                     FreeMem(infodata,sizeof(struct InfoData));
  595.                 }
  596.             }
  597.         }
  598.         else if (mode==5) sprintf(infobuf,"");
  599.         else              sprintf(infobuf,"0");
  600.     }
  601. #endif
  602.  
  603.     if (mode==0) printf("%s",infobuf);
  604.  
  605.     FreeMem(info,sizeof(struct InfoData));
  606.     Myprocess->pr_WindowPtr = (APTR)o_noreq;
  607.  
  608.     return infobuf;
  609. }
  610.  
  611.  
  612. /* things shared with display_file */
  613.  
  614. #define DIR_SHORT 0x1
  615. #define DIR_FILES 0x2
  616. #define DIR_DIRS  0x4
  617. #define DIR_NOCOL 0x8
  618. #define DIR_NAMES 0x10
  619. #define DIR_HIDE  0x20
  620. #define DIR_LEN   0x40
  621. #define DIR_TIME  0x80
  622. #define DIR_BACK  0x100
  623. #define DIR_UNIQ  0x200
  624. #define DIR_IDENT 0x400
  625. #define DIR_CLASS 0x800
  626. #define DIR_QUIET 0x1000
  627. #define DIR_AGE   0x2000
  628. #define DIR_VIEW  0x4000
  629. #define DIR_NOTE  0x8000
  630. #define DIR_PATH  0x10000
  631. #define DIR_LFORM 0x20000
  632. #define DIR_BOT   0x40000
  633. #define DIR_TOP   0x80000
  634. #define DIR_LINK  0x100000
  635.  
  636. static char *lastpath = NULL;
  637. static int filecount, dircount, col, colw, wwidth;
  638. static long bytes, blocks;
  639.  
  640. /* the args passed to do_dir will never be expanded */
  641.  
  642. extern expand_err;
  643. extern int w_width;
  644.  
  645. static struct DateStamp Stamp;
  646. static char *LineBuf, *LinePos, LastWasDir, *LFormat, _LFormat[80], NoTitles;
  647.  
  648. int
  649. do_dir( void )
  650. {
  651.     int i=1, c, eac, reverse, nump=ac, retcode=0;
  652.     char **eav=NULL, **av1=NULL, **av2=NULL, inter=isconsole(Output());
  653.     char linebuf[200];
  654.     int (*func)(), ac1, ac2, factor=0;
  655.  
  656.     LineBuf=LinePos=linebuf;
  657.     LastWasDir=NoTitles=0;
  658.     colw=-1;
  659.  
  660.     LFormat=_LFormat;
  661.  
  662.     if( options&DIR_CLASS ) options|=DIR_IDENT;
  663.     if( !(options & (DIR_FILES | DIR_DIRS))) options|= DIR_FILES | DIR_DIRS;
  664.  
  665.     DateStamp( &Stamp );
  666.  
  667.     col = filecount = dircount = bytes = blocks = 0L;
  668.     if (lastpath) free(lastpath);
  669.     lastpath=NULL;
  670.  
  671.     wwidth=77;
  672.     if( inter )
  673.         wwidth=w_width;
  674.  
  675.     if( options&DIR_SHORT )
  676.         strcpy(LFormat," %-18n%19m");
  677.     else if( options&DIR_PATH )
  678.         strcpy(LFormat," %-50p %7s %d"), NoTitles=1;
  679.     else {
  680.         if ( options&DIR_NOTE )
  681.             strcpy(LFormat,"  %-30n %o");
  682.         else if ( options&DIR_LINK )
  683.             strcpy(LFormat,"  %-30n %L");
  684.         else {
  685. #if 1
  686.             strcpy(LFormat,"  ");
  687. #else
  688.             strcpy(LFormat,"  %-30n ");
  689. #endif
  690.             if( options&DIR_HIDE )
  691.                 strcat(LFormat, "%e");
  692.             strcat(LFormat,"%c%I%f ");
  693.             if( options&DIR_VIEW )
  694.                 strcat(LFormat,"%10v  ");
  695.             else
  696.                 strcat(LFormat,"%8s  ");
  697. #if 0
  698.             if( !(options&DIR_QUIET) )
  699.                 strcat(LFormat,options&DIR_VIEW?"%5b ":"%4b ");
  700. #endif
  701.             if( options&DIR_IDENT )
  702.                 strcat(LFormat,"%k");
  703.             else if( options&DIR_AGE )
  704.                 strcat(LFormat,"%a");
  705.             else
  706.                 strcat(LFormat,"%d %t");
  707. #if 1
  708.             strcat(LFormat,"  %N");
  709. #endif
  710.         }
  711.     }
  712.  
  713.     if( options&DIR_LFORM ) {
  714.         char *fmtstr;
  715.         if (fmtstr=get_var(LEVEL_SET,v_dirformat)) {
  716.             strncpy(LFormat,fmtstr,79);    /* copy to _LFormat[80] */
  717.             LFormat[79]=0;
  718.         }
  719.         else if (ac>1)
  720.             LFormat=av[i++];
  721.         else {
  722.             show_usage(NULL);
  723.             return 20;
  724.         }
  725.     }
  726.  
  727.     if( ac == i) ++nump, av[i]="";
  728.     if( options&DIR_UNIQ) {
  729.         if( ac-i!=2 )  { show_usage(NULL); return 20; }
  730.         i=0, nump=3;
  731.     }
  732.  
  733.     prepscroll(0);
  734.     for( ; i<nump && !CHECKBREAK(); ++i ) {
  735.         if( options&DIR_UNIQ ) {
  736.             switch( i ) {
  737.                 case 0: av1=expand( av[ac-2], &ac1 );
  738.                         av2=expand( av[ac-1], &ac2 );
  739.                         eav=without( av1, ac1, av2, ac2, &eac, 1 );
  740.                         break;
  741.                 case 1: printf("\nCommon files\n");
  742.                         eav=and( av1, ac1, av2, ac2, &eac, 1 );
  743.                         break;
  744.                 case 2: printf("\n");
  745.                         eav=without( av2, ac2, av1, ac1, &eac, 1 );
  746.                         break;
  747.             }
  748.             col = filecount = dircount = bytes = blocks = 0L;
  749.             if (lastpath) free(lastpath);
  750.             lastpath=NULL;
  751.  
  752.         /* AMK: enhanced handling of non-matching patterns */
  753.         } else if (!(eav = expand(av[i], &eac))) {
  754.             if (IoError) {
  755.                 ierror(av[i],IoError);
  756.                 retcode=5;
  757.             }
  758.             else {
  759.                 if (strlen(av[i])>0)
  760.                     fprintf(stderr,"%s: No match.\n",av[i]);
  761.             }
  762.             continue;
  763.         }
  764.  
  765.         reverse= ( options&DIR_BACK ) ? 1 : 0;
  766.         func=cmp;
  767.         if( options & DIR_TIME) func=datecmp_csh;
  768.         if( options & DIR_LEN ) func=sizecmp;
  769.         if( options & DIR_CLASS)func=classcmp;
  770.         if( options & DIR_BOT ) factor=-99999999;
  771.         if( options & DIR_TOP ) factor= 99999999;
  772.         DirQuickSort(eav, eac, func, reverse, factor);
  773.         for(c=0; c<eac && !CHECKBREAK(); ++c) {
  774.             if( options & DIR_HIDE ) {
  775.                 char *b=FilePart(eav[c]);
  776.                 int  l=strlen(b)-5;
  777.                 FILEINFO *info =(FILEINFO *)eav[c] - 1;
  778.                 if(*b=='.'|| (l>=0 && !strcmp(b+l,".info"))||(info->flags&128))
  779.                     continue;
  780.             }
  781.             if (options & DIR_NAMES) {
  782.                 if(options&(((FILEINFO *)eav[c])->size<0? DIR_DIRS: DIR_FILES))
  783.                     puts(eav[c]);
  784.             } else
  785.                 display_file(eav[c]);
  786.         }
  787.  
  788.         if (col) { quickscroll(); puts(LinePos=LineBuf); col=0; }
  789.  
  790.         if( LastWasDir )
  791.             printf(o_lolite), LastWasDir=0;
  792.  
  793.         if (options&DIR_UNIQ || (filecount>1 && i==nump-1)) {
  794.             blocks += filecount-dircount; /* account for dir blocks */
  795.             quickscroll();
  796.             printf(" %ld Blocks, %s Bytes used in %d files\n",
  797.                 blocks, itoa(bytes), filecount);
  798.         }
  799.         if( options&DIR_UNIQ )
  800.             free(eav);
  801.         else
  802.             free_expand (eav);
  803.     }
  804.     if (lastpath) free(lastpath);
  805.     lastpath=NULL;
  806.  
  807.     if( options&DIR_UNIQ )
  808.         free_expand( av1 ), free_expand( av2 );
  809.  
  810.     return retcode;
  811. }
  812.  
  813. static int MultiCol=-1;
  814.  
  815. static char
  816. pathcomp( char *s1, char *s2 )
  817. {
  818.     char ret, *t, c;
  819.  
  820.     t=FilePart( s2 ); c=*t; *t=0;
  821.     ret=stricmp( s1, s2 );
  822.     *t=c;
  823.     return ret;
  824. }
  825.  
  826. static void
  827. display_file( char *filestr )
  828. {
  829.     /* struct InfoData *id=AllocMem( sizeof(struct InfoData), 0); */
  830.     int isadir, len, collen;
  831.     char sc, *base, buf[130], *hilite;
  832.     FILEINFO *info;
  833.     BPTR thislock;
  834.  
  835.     base=FilePart(filestr);
  836.     sc = *base;
  837.     *base = 0;
  838.     /* if (thislock==NULL) return; */
  839.     if( !NoTitles ) {
  840.         if( !lastpath || pathcomp( filestr, lastpath)) {
  841.             if(!(thislock=Lock(filestr,SHARED_LOCK)))
  842.                 return;
  843.             if (col) { quickscroll(); puts(LinePos=LineBuf); col=0; }
  844.             quickscroll();
  845.             NameFromLock(thislock, buf, 128L);
  846.             if( LastWasDir )
  847.                 printf(o_lolite), LastWasDir=0;
  848.             printf("Directory of %s\n", buf );
  849.             /* Info( thislock, id ); */
  850.             /* itok((id->id_NumBlocks-id->id_NumBlocksUsed)*id->id_BytesPerBlock));*/
  851.             /* FreeMem( id, sizeof(struct InfoData)); */
  852.             lastpath = salloc(256);
  853.             strcpy(lastpath,filestr);
  854.             /*lastpath=filestr;*/
  855.             UnLock(thislock);
  856.         }
  857.     }
  858.     *base    = sc;
  859.  
  860.     info   = (FILEINFO *)filestr - 1;
  861.     isadir = info->size<0;
  862.  
  863.     if( !(options & (isadir ? DIR_DIRS : DIR_FILES)))
  864.         return;
  865.  
  866.     hilite="";
  867.     if (isadir!=LastWasDir && !(options & DIR_NOCOL))
  868.         hilite=isadir ? o_hilite : o_lolite, LastWasDir=isadir;
  869.  
  870.     lformat(LFormat, buf, info);
  871.  
  872.     if( MultiCol==-1 ) {
  873.         quickscroll();
  874.         printf("%s%s",hilite,buf);
  875.     } else {
  876.         len=strlen(buf);
  877.         if( col+len>wwidth ) {
  878.             quickscroll();
  879.             puts(LineBuf);
  880.             LinePos=LineBuf; col=0;
  881.         }
  882.         if( MultiCol )
  883.             colw=MultiCol;
  884.         else if( colw==-1 )
  885.             colw=len;
  886.         collen= (len+colw-1)-(len+colw-1)%colw;
  887.         col+=collen;
  888.         LinePos+=sprintf(LinePos,"%s%-*s",hilite,collen,buf);
  889.     }
  890.  
  891.     if(info->size>0)
  892.         bytes  += info->size;
  893.     blocks += info->blocks;
  894.     filecount++;
  895. }
  896.  
  897. static char linebuf[140];
  898. static long dlen, dblocks;
  899.  
  900. static int
  901. count( long mask, char *s, char *path )
  902. {
  903.     FIB *fib=(FIB*)s-1;
  904.     dlen+=fib->fib_Size;
  905.     dblocks+=fib->fib_NumBlocks+1;
  906.     return 0;
  907. }
  908.  
  909. void
  910. lformat( char *s, char *d, FILEINFO *info )
  911. {
  912.     long mi=0;
  913.     char buf[120], *w, *t, *class;
  914.     DPTR *dp;
  915.     int stat, i, form, sign, cut, size=info->size;
  916.     char *(*func)(int num);
  917.  
  918.     MultiCol=-1;
  919.     while( *s ) {
  920.         if( *s!='%' ) { *d++=*s++; continue; }
  921.         sign=1; form=0; cut=0; s++;
  922.         if( *s=='-' ) s++, sign=-1;
  923.         if( *s=='.' ) s++, cut=1;
  924.         while( *s>='0' && *s<='9' ) form=10*form+*s++-'0';
  925.         w=buf; w[0]=0; w[1]=0;
  926.         switch( *s ) {
  927.         case 'p': strcpy(w,(char *)(info+1));             break;
  928.         case 'b': sprintf(w,size>=0 ? "%d":"", info->blocks); break;
  929.         case 's': sprintf(w,size>=0 ? "%d":"<Dir>",size); break;
  930.         case 'i': *w= size>=0 ? '-' : 'd';                break;
  931.         case 'r':
  932.         case 'u':
  933.             if( *s=='r' ) func=itoa; else func=itok;
  934.             strcpy( w,size>=0 ? (*func)(size) : "<Dir>");
  935.             break;
  936.         case 'n':
  937.         case 'q':
  938.             strcpy(w,FilePart((char *)(info+1)));
  939.             if( *s=='q' && size<0 ) strcat(w,"/");
  940.             break;
  941.         case 'l':
  942.             if( info->flags & INFO_COMMENT ) *w='\n';
  943.             break;
  944.         case 'c':
  945.             *w= info->flags & INFO_COMMENT ? 'c' : '-';
  946.             break;
  947.         case 'e':
  948.             *w= info->flags & INFO_INFO ? 'i' : '-';
  949.             break;
  950.         case '+':
  951.             *w= info->flags & INFO_INFO ? '+' : ' ';
  952.             break;
  953.         case 'L':
  954.         case 'N':
  955.             if (*s=='N')
  956.                 strcpy(w,FilePart((char *)(info+1)));
  957.             else
  958.                 strcpy(w,"");
  959.             if (info->type==ST_SOFTLINK ||
  960.                 info->type==ST_LINKDIR  ||
  961.                 info->type==ST_LINKFILE)
  962.             {
  963.                 BPTR lock;
  964.                 if (lock=Lock((char *)(info+1),ACCESS_READ)) {
  965.                     strcat(w," -> ");
  966.                     NameFromLock(lock,w+strlen(w),80);
  967.                     UnLock(lock);
  968.                 }
  969.             }
  970.             break;
  971.         case 'I':
  972.             switch (info->type) {
  973.                 case ST_SOFTLINK:
  974.                     *w='S';
  975.                     break;
  976.                 case ST_LINKDIR:
  977.                 case ST_LINKFILE:
  978.                     *w='H';
  979.                     break;
  980.                 case ST_PIPEFILE:
  981.                     *w='P';
  982.                     break;
  983.                 default:
  984.                     *w='-';
  985.                     break;
  986.             }
  987.             break;
  988.         case 'f':
  989.             for (i=7; i>=0; i--)
  990.                 *w++ = (info->flags^15) & (1L<<i) ? "hsparwed"[7-i] : '-';
  991.             *w=0;
  992.             break;
  993.         case 'a':
  994.             if( Stamp.ds_Days!=0 ) {
  995.                 mi =Stamp.ds_Days*1440 + Stamp.ds_Minute;
  996.                 mi-=info->date.ds_Days*1440 + info->date.ds_Minute;
  997.             }
  998.             sprintf(w,mi>=0?"%4d days %02d:%02d":"Future    ",
  999.                       mi/1440,mi/60%60,mi%60);
  1000.             break;
  1001.         case 'o':
  1002.             if( dp=dopen( (char *)(info+1), &stat )) {
  1003.                 strcpy( w, dp->fib->fib_Comment );
  1004.                 dclose( dp );
  1005.             }
  1006.             break;
  1007.         case 'v':
  1008.         case 'w':
  1009.             if( *s=='v' ) func=itoa; else func=itok;
  1010.             dlen=dblocks=0;
  1011.             if( size<0 ) {
  1012.                 newrecurse( SCAN_DIR|SCAN_FILE|SCAN_RECURSE,
  1013.                             (char *)(info+1),count);
  1014.                 strcpy( w, (*func)(dlen));
  1015.                 info->size=size=dlen; info->blocks=dblocks;
  1016.             } else
  1017.                 strcpy( w, (*func)(size));
  1018.             break;
  1019.         case 'k':
  1020.             if( *info->class!=1 )
  1021.                 strcpy(w,info->class);
  1022.             else if( class=getclass((char *)(info+1)))
  1023.                 if( w=index(strncpy(w,class,50),0xA0) )
  1024.                     *w=0;
  1025.             break;
  1026.         case 'x':
  1027.         case 'd':
  1028.             sprintf(w,"%9s",dates(&info->date,*s=='x'));
  1029.             if(t=index(w,' ')) *t=0;
  1030.             break;
  1031.         case 't':
  1032.             sprintf(w,"%8s", next_word(dates(&info->date,0)));
  1033.             break;
  1034.         case 'm': MultiCol=form; form=0;      break;
  1035.         case '%': *w=*++s;                    break;
  1036.         case  0 : *w='%';                     break;
  1037.         default : *w='%';  *w++=*s; *w=0;     break;
  1038.         }
  1039.         if( cut ) buf[form]=0;
  1040.         *d=0; s++;
  1041.         d+=sprintf(d,sign<0?"%-*s":"%*s",form,buf);
  1042.     }
  1043.     if( MultiCol==-1 ) { *d++='\n'; }
  1044.     *d=0;
  1045. }
  1046.  
  1047.  
  1048.  
  1049. int
  1050. do_quit( void )
  1051. {
  1052.     if (Src_stack) {
  1053.         Quit = 1;
  1054.         return(do_return());
  1055.     }
  1056.     main_exit(0);
  1057.     return 0;
  1058. }
  1059.  
  1060. int
  1061. do_echo( void )
  1062. {
  1063.     char *args=compile_av(av,1,ac,' ',0);
  1064.     fprintf( (options&2)?stderr:stdout, (options&1)?"%s":"%s\n",args );
  1065.     free(args);
  1066.     return 0;
  1067. }
  1068.  
  1069.  
  1070. static int
  1071. breakcheckd(void)
  1072. {
  1073.     int ret=!o_nobreak && SetSignal(0L,0L) & SIGBREAKF_CTRL_D;
  1074.     if( ret )
  1075.         fprintf(stderr,"^D\n");
  1076.     return ret;
  1077. }
  1078.  
  1079. /* gets a line from file, joining lines if they end in '\' */
  1080.  
  1081. #define MAXLINE 512
  1082.  
  1083. static int
  1084. srcgets(char **buf, int *buflen, FILE *file)
  1085. {
  1086.     char *bufptr=*buf, *p, *new, concat=0, cont;
  1087.     int   totlen=0, len;
  1088.  
  1089.     do {
  1090.         if( totlen+MAXLINE > *buflen ) {
  1091.             new=salloc(*buflen *= 2);
  1092.             memcpy( new, *buf, 1+bufptr-*buf );
  1093.             bufptr+= new-*buf;
  1094.             free(*buf);
  1095.             *buf=new;
  1096.         }
  1097.         if (fgets(bufptr, MAXLINE, file)==NULL) {
  1098.             if( concat )
  1099.                 fprintf(stderr,"Source: missing '}'\n");
  1100.             else if (bufptr != *buf)
  1101.                 fprintf(stderr,"Source: file ends in '\\'\n");
  1102.             return -1;
  1103.         }
  1104.         len= strlen( bufptr );
  1105.         totlen+= len;
  1106.  
  1107.         cont=0;
  1108.  
  1109.         p=bufptr+len-1;
  1110.         if(  p>=bufptr && *p=='\n') *p--=0;
  1111.         if(  p< bufptr   ) ;
  1112.         else if( *p=='\\') *p--=0, cont=1;
  1113.         else if( *p=='{' ) concat++;
  1114.         else if( *p=='}' ) {
  1115.             if( concat>0 ) {
  1116.                 concat--;
  1117.                 if( concat ) *++p='\n';
  1118.             }
  1119.         } else if( concat ) *++p='\n';
  1120.         bufptr=++p;
  1121.     } while( cont || concat );
  1122.     *bufptr=0;
  1123.     return totlen;
  1124. }
  1125.  
  1126.  
  1127.  
  1128. int
  1129. do_source( char *str )
  1130. {
  1131.     FILE *fi;
  1132.     char *buf;
  1133.     ROOT *root;
  1134.     int  retcode, len, bufsize=512+MAXLINE;
  1135.     int j;
  1136.     char *ptr;
  1137.  
  1138.     if (Src_stack == MAXSRC) {
  1139.         ierror(NULL,217);
  1140.         return -1;
  1141.     }
  1142.  
  1143.     if ((fi = fopen (av[1], "r")) == 0)
  1144.         { pError(av[1]); return -1;    }
  1145.  
  1146.     push_locals(root=(ROOT *)salloc( sizeof(ROOT)));
  1147.     buf=salloc(bufsize);
  1148.  
  1149.     set_var(LEVEL_SET | LEVEL_LOCAL, v_passed, next_word(next_word(str)));
  1150.  
  1151.  
  1152.     /*
  1153.      * now create a bunch of positional parameters , $0, $1 etc
  1154.      */
  1155.     j = 0;
  1156.     ptr = next_word (str);
  1157.     while (*ptr) {
  1158.         char *var;
  1159.         char p[6];
  1160.         char npos[6];
  1161.  
  1162.         var = ptr;
  1163.         sprintf (p, "%d", j);
  1164.         /*
  1165.          * term var str
  1166.          */
  1167.         ptr = next_word (ptr);
  1168.  
  1169.         /* printf("%s\n" , ptr); */
  1170.  
  1171.         if (*ptr)
  1172.             *(ptr - 1) = '\0';
  1173.  
  1174.         set_var (LEVEL_SET | LEVEL_LOCAL, p, var);
  1175.         /*
  1176.          * now set up what should be $# ( number of positionals )
  1177.          */
  1178. #if 0
  1179.         sprintf (p, "_N");    /* should be "#" but csh barfs :( */
  1180. #endif
  1181.         sprintf (p, "#");    /* hackin' */
  1182.         sprintf (npos, "%d", j++);
  1183.         set_var (LEVEL_SET | LEVEL_LOCAL, p, npos);
  1184.     }
  1185.  
  1186.  
  1187.     Src_pos  [Src_stack] = 0;
  1188.     Src_abort[Src_stack] = 0;
  1189.     Src_base [Src_stack] = fi;
  1190.     Src_if[Src_stack]=If_stack;
  1191.     ++Src_stack;
  1192.     while ((len=srcgets(&buf, &bufsize, fi))>=0&& !dobreak()&& !breakcheckd()){
  1193.         Src_pos[Src_stack-1] += len;
  1194.         if (Verbose&VERBOSE_SOURCE && !forward_goto)
  1195.             if( Verbose&VERBOSE_HILITE )
  1196.                 fprintf(stderr,"%s)%s%s\n",o_hilite,buf,o_lolite);
  1197.             else
  1198.                 fprintf(stderr,")%s\n",buf);
  1199.         retcode=execute(buf);
  1200.         if( retcode>=o_failat || Src_abort[Src_stack-1] )
  1201.             break;
  1202.         retcode=0;
  1203.     }
  1204.     --Src_stack;
  1205.     if( If_stack>Src_if[Src_stack] )
  1206.         If_stack=Src_if[Src_stack], disable=If_stack && If_base[If_stack-1];
  1207.  
  1208.     if (forward_goto) ierror(NULL,501);
  1209.     forward_goto = 0;
  1210.     unset_level(LEVEL_LABEL+ Src_stack);
  1211.     unset_var(LEVEL_SET, v_gotofwd);
  1212.     unset_var(LEVEL_SET, v_passed);
  1213.     fclose (fi);
  1214.  
  1215.     pop_locals();
  1216.     free(buf);
  1217.     free(root);
  1218.  
  1219.     return retcode;
  1220. }
  1221.  
  1222. /* set process cwd name and $_cwd, if str != NULL also print it. */
  1223.  
  1224. void
  1225. set_cwd(void)
  1226. {
  1227.     char pwd[130];
  1228.  
  1229.     NameFromLock(Myprocess->pr_CurrentDir, pwd, 128L);
  1230.     set_var(LEVEL_SET, v_cwd, pwd);
  1231.     /* put the current dir name in our CLI task structure */
  1232.     CtoBStr(pwd, Mycli->cli_SetName, 128L);
  1233. }
  1234.  
  1235. int
  1236. do_pwd( void )
  1237. {
  1238.     set_cwd();
  1239.     puts( get_var( LEVEL_SET, v_cwd ));
  1240.     return 0;
  1241. }
  1242.  
  1243.  
  1244. /*
  1245.  * CD
  1246.  *
  1247.  * CD(str, 0)      -do CD operation.
  1248.  *
  1249.  */
  1250.  
  1251. extern int qcd_flag;
  1252.  
  1253. static char lastqcd[80];
  1254. static FILE *qcdfile;
  1255. static int  NumDirs;
  1256.  
  1257. static int
  1258. countfunc( long mask, char *file, char *fullpath )
  1259. {
  1260.     fprintf( qcdfile, "%s\n", fullpath );
  1261.     fprintf( stdout, "\r Directories: %d", ++NumDirs );
  1262.     fflush ( stdout );
  1263.     return 0;
  1264. }
  1265.  
  1266. int
  1267. do_cd(void)
  1268. {
  1269.     BPTR oldlock, filelock;
  1270.     char buf[100], *old, *str=av[1];
  1271.     int  i=1, repeat;
  1272.  
  1273.     if( options & 1 ) {
  1274.         if( !o_csh_qcd ) { fprintf(stderr,"$_qcd unset\n"); return 20; }
  1275.         if( !(qcdfile=fopen( o_csh_qcd, "w" )))
  1276.             { fprintf(stderr,"Can't open output\n"); return 20; }
  1277.         for( ; i<ac && !dobreak(); i++ ) {
  1278.             NumDirs=0;
  1279.             printf("%s\n",av[i]);
  1280.             newrecurse( SCAN_DIRENTRY | SCAN_RECURSE, av[i], countfunc );
  1281.             printf("\n");
  1282.         }
  1283.         fclose(qcdfile);
  1284.         return 0;
  1285.     }
  1286.  
  1287.     if ( !str || !(*str) ) {    /* old "!*str" causes enforcer hit! */
  1288.         printf("%s\n", get_var( LEVEL_SET, v_cwd ));
  1289.         return 0;
  1290.     }
  1291.  
  1292.     if (filelock=Lock(str,ACCESS_READ)) {
  1293.         lastqcd[0]=0;
  1294.         if (!isdir(str)) { UnLock(filelock); ierror(str,212); return 20; }
  1295.     } else {
  1296.         repeat= !strncmp( lastqcd, str, 79 );
  1297.         strncpy( lastqcd, str, 79);
  1298.  
  1299.         if( !quick_cd( buf, av[i], repeat) )
  1300.             { fprintf(stderr,"Object not found %s\n",str); return 20; }
  1301.         if (!(filelock=Lock(buf,ACCESS_READ)))
  1302.             { pError(buf); return 20; }
  1303.     }
  1304.     if (oldlock=CurrentDir(filelock)) UnLock(oldlock);
  1305.     if( !(old=get_var(LEVEL_SET, v_cwd)) )
  1306.         old="";
  1307.     set_var(LEVEL_SET, v_lcd, old);
  1308.     set_cwd();
  1309.  
  1310.     return 0;
  1311. }
  1312.  
  1313. char *
  1314. quick_cd( char *buf, char *name, int repeat )
  1315. {
  1316.     if( !o_csh_qcd || !exists(o_csh_qcd))
  1317.         return NULL;
  1318.     qcd_flag=repeat ? 2 : 1;
  1319.     strcpy(buf,name);
  1320.     if( quicksearch( o_csh_qcd, 1, buf)!=2 )
  1321.         return NULL;
  1322.     return buf;
  1323. }
  1324.  
  1325.  
  1326. /* AMK: mkdir now builds path to destination directory if neccessary */
  1327. int
  1328. do_mkdir( void )
  1329. {
  1330.     int i;
  1331.     BPTR lock;
  1332.     char *p,c;
  1333.  
  1334.     for (i=1; i<ac; ++i) {
  1335.  
  1336.         /* Are there any sub-directories to build? */
  1337.         if( options&1 && (p=strchr(av[i],'/'))) {
  1338.             do {
  1339.                 c = *p;
  1340.                 *p = '\0';
  1341.                 if (lock=CreateDir(av[i]))
  1342.                     UnLock(lock);
  1343.                 *p = c;
  1344.             } while(p=strchr(++p,'/'));
  1345.         }
  1346.  
  1347.         /* Okay, sub-directories should exist, now try normal mkdir. */
  1348.         if (exists(av[i]))
  1349.             ierror(av[i],203);
  1350.         else if (lock=CreateDir(av[i]))
  1351.             UnLock (lock);
  1352.         else
  1353.             pError(av[i]);
  1354.  
  1355.     }
  1356.  
  1357.     return 0;
  1358. }
  1359.  
  1360. int
  1361. do_mv( void )
  1362. {
  1363.     char *dest, buf[256];
  1364.     int dirflag, i, len;
  1365.  
  1366.     dirflag=isdir(dest=av[--ac]);
  1367.     if (ac>3 && !dirflag) { ierror(dest, 507); return (-1); }
  1368.     for (i=1; i<ac; ++i) {
  1369.         strcpy(buf, dest);
  1370.  
  1371.         /* source: remove trailing slash */
  1372.         if ((len=strlen(av[i]))>1 && av[i][len-1]=='/' && av[i][len-2]!=':' && av[i][len-2]!='/')
  1373.             av[i][len-1] = '\0';
  1374.  
  1375.         /* destination: remove trailing slash */
  1376.         if ((len=strlen(buf))>1 && buf[len-1]=='/' && buf[len-2]!=':' && buf[len-2]!='/')
  1377.             buf[len-1] = '\0';
  1378.  
  1379.         if (dirflag && stricmp(av[i],buf))
  1380.              AddPart(buf, FilePart(av[i]), 255L);
  1381.  
  1382.         if (Rename(av[i], buf)==0) {
  1383.             pError(av[i]);
  1384.             if (!(options&1))
  1385.                 return -1;
  1386.         }
  1387.         else {
  1388.             clear_archive_bit( buf );
  1389.             if (options&2) {
  1390.                 /*printf("Renaming %s as %s\n",av[i],buf);*/
  1391.                 printf(" %s...%s\n",av[i],dirflag?"moved":"renamed");
  1392.             }
  1393.         }
  1394.     }
  1395.     return 0;
  1396. }
  1397.  
  1398. static char *searchstring;
  1399. static char docr;
  1400.  
  1401. #if 0
  1402. int
  1403. all_args( int (*action)FUNCARG(long,char*,char*), int dirsflag )
  1404. {
  1405.     int  i;
  1406.     long mask= SCAN_FILE;
  1407.  
  1408.     if( options&1 )
  1409.         mask |= SCAN_RECURSE;
  1410.     if( dirsflag )
  1411.         mask |= SCAN_DIR;
  1412.  
  1413.     for ( i=1; i<ac && !dobreak(); ++i)
  1414.         if (isdir(av[i])) {
  1415.             if (options & 1)
  1416.                 newrecurse(mask, av[i], action);
  1417.             if (dirsflag)
  1418.                 (*action)(SCAN_DIR,av[i],av[i]);
  1419.         } else
  1420.             (*action)(SCAN_FILE,av[i],av[i]);
  1421.     if(docr) printf("\n"),docr=0;
  1422.     dobreak();
  1423.     return 0;
  1424. }
  1425. #endif
  1426.  
  1427. /* this is all_args() but with a user definable range from n to m */
  1428. int
  1429. all_args_n2m( int (*action)FUNCARG(long,char*,char*), int dirsflag, int aa_from, int aa_to )
  1430. {
  1431.     int  i;
  1432.     long mask= SCAN_FILE;
  1433.  
  1434.     if( options&1 )
  1435.         mask |= SCAN_RECURSE;
  1436.     if( dirsflag )
  1437.         mask |= SCAN_DIR;
  1438.  
  1439.     for ( i=aa_from; i<aa_to && !dobreak(); ++i)
  1440.         if (isdir(av[i])) {
  1441.             if (options & 1)
  1442.                 newrecurse(mask, av[i], action);
  1443.             if (dirsflag)
  1444.                 (*action)(SCAN_DIR,av[i],av[i]);
  1445.         } else
  1446.             (*action)(SCAN_FILE,av[i],av[i]);
  1447.     if(docr) printf("\n"),docr=0;
  1448.     dobreak();
  1449.     return 0;
  1450. }
  1451.  
  1452. int
  1453. all_args( int (*action)FUNCARG(long,char*,char*), int dirsflag )
  1454. {
  1455.     return( all_args_n2m(action,dirsflag,1,ac) );
  1456. }
  1457.  
  1458. #define SEARCH_REC   1
  1459. #define SEARCH_CASE  2
  1460. #define SEARCH_WILD  4
  1461. #define SEARCH_NUM   8
  1462. #define SEARCH_EXCL  16
  1463. #define SEARCH_QUIET 32
  1464. #define SEARCH_VERB  64
  1465. #define SEARCH_BIN   128
  1466. #define SEARCH_FILE  256
  1467. #define SEARCH_ABORT 512
  1468. #define SEARCH_LEFT  1024
  1469. #define SEARCH_ONLY  2048
  1470.  
  1471. /* added by amk, not yet implemented */
  1472. #define SEARCH_STYLE 4096    /* alternative style, inspired by GMD */
  1473. #define SEARCH_ONCE  8192    /* only show first pattern match per file */
  1474.  
  1475. static int abort_search;
  1476. static char lowbuf[256], file_name, file_cr;
  1477.  
  1478. static int
  1479. search_file( long mask, char *s, char *fullpath )
  1480. {
  1481.     PATTERN *pat;
  1482.     FILE *fi;
  1483.     char *p, *q;
  1484.     int nocasedep, lctr, len, excl=((options & 16) !=0 ), yesno;
  1485.     char buf[256], searchit[120], first, left;
  1486.  
  1487.     if( abort_search )
  1488.         return 0;
  1489.  
  1490.     nocasedep=!(options & SEARCH_CASE);
  1491.     lctr= docr= file_name= file_cr= 0;
  1492.     if (!(options & (SEARCH_QUIET|SEARCH_FILE))) {
  1493.         if( options & SEARCH_VERB )
  1494.             printf("Examining %s ...\n",fullpath);
  1495.         else
  1496.             printf("\015Examining %s ...\033[K",fullpath), docr=1;
  1497.         fflush( stdout );
  1498.     }
  1499.  
  1500.     strcpy(searchit,searchstring);
  1501.     if (options & SEARCH_WILD) strcat(searchit,"\n");
  1502.     len=strlen(searchit);
  1503.     if (nocasedep) strupr(searchit);
  1504.     first=*searchit;
  1505.  
  1506.     if( strcmp("STDIN",s) && !(options&SEARCH_WILD) && !excl ||
  1507.                  options&SEARCH_BIN )
  1508.         if( quicksearch(s,nocasedep,searchit) )
  1509.             return 0;
  1510.  
  1511.     if( options&SEARCH_BIN )
  1512.         { fprintf(stderr,"Out of memory\n"); return 20; }
  1513.  
  1514.     if(!(pat=compare_preparse( searchit, options&SEARCH_CASE )))
  1515.         { fprintf(stderr,"Invalid pattern\n"); return 20; }
  1516.  
  1517.     fi = strcmp("STDIN",s) ?  fopen(s,"r") : stdin;
  1518.     if (fi==NULL) { pError(s); return 20; }
  1519.  
  1520.     prepscroll(0);
  1521.  
  1522.     while (fgets(buf,256,fi) && !dobreak()) {
  1523.         lctr++; left=1;
  1524.         if (options & SEARCH_WILD)
  1525.             yesno=compare_ok(pat, p=buf);
  1526.         else {
  1527.             if (nocasedep) {
  1528.                 strcpy(lowbuf,buf);
  1529.                 strupr(lowbuf);
  1530.                 p=lowbuf;
  1531.             } else
  1532.                 p=buf;
  1533.             q=p;
  1534.             while ((p=index(p,first)) && strncmp(p++,searchit,len)) ;
  1535.             yesno= (p!=NULL);
  1536.             left = --p - q;
  1537.         }
  1538.         if( yesno ^ excl )
  1539.             if(!(options&SEARCH_ONLY)|| !isalphanum(p[-1])&&!isalphanum(p[len]))
  1540.                 if( found(buf, lctr, 0, s, left ) )
  1541.                     break;
  1542.     }
  1543.     compare_free(pat);
  1544.     if (fi!=stdin) fclose (fi);
  1545.     if( file_cr ) printf("\n");
  1546.     return 0;
  1547. }
  1548.  
  1549. int qcd_flag, qcd_offs;
  1550.  
  1551. #define READCHUNK 60000
  1552.  
  1553. static int
  1554. quicksearch( char *name, int nocasedep, char *pattern )
  1555. {
  1556.     int i, ptrn=strlen(pattern);
  1557.     char ut[256], *buffer, *lend;
  1558.     char *uptab=ut, *get, c, *lpos, *lstart;
  1559.     int len, lnum, qcd=qcd_flag, repeat=(qcd==2 && qcd_offs!=0), buflen;
  1560.     int sofar, got;
  1561.     BPTR fh;
  1562.  
  1563. #ifdef AZTEC_C
  1564.     while(0) while(0) c=c=0, uptab=uptab=ut, get=get=NULL;
  1565. #endif
  1566.  
  1567.     qcd_flag=0;
  1568.     if( !(fh=Open(name,MODE_OLDFILE))) {
  1569.         i=(long)IoErr(), docr=0;
  1570.         printf("\n");
  1571.         ierror(name,i);
  1572.         return 1;
  1573.     }
  1574.     len=filesize( name );
  1575.     buflen=len+3;
  1576.     if( !(buffer=(void *)AllocMem(buflen,0))) { Close(fh); return 0; }
  1577.     sofar=0;
  1578.     do {
  1579.         got=Read( fh, (char *)buffer+sofar, READCHUNK);
  1580.         sofar+=got;
  1581.     } while( got==READCHUNK );
  1582.     Close( fh);
  1583.     if( sofar != len ) {
  1584.         FreeMem( buffer, buflen );
  1585.         pError(pattern); return 1;
  1586.     }
  1587.     if(buffer[len-1]!='\n')
  1588.         buffer[len++]='\n';
  1589.  
  1590.     if( nocasedep )
  1591.         strupr( pattern );
  1592.  
  1593.     if( !qcd )
  1594.         prepscroll(0);
  1595.  
  1596.     for( i=0; i<256; i++ ) uptab[i]=i;
  1597.     if( nocasedep ) for( i='a'; i<='z'; i++ ) uptab[i]=i-'a'+'A';
  1598. retry:
  1599.     c=*pattern, buffer[len]=c, buffer[len+1]=c;
  1600.     get= (qcd==2) ? buffer+qcd_offs : buffer;
  1601.     if( qcd==1 ) qcd_offs=0;
  1602.  
  1603.     lpos=lstart=buffer, lnum=1;
  1604.     for( ;; ) {
  1605.         do ; while( uptab[*get++]!=c );
  1606.         if( --get>=buffer + len )
  1607.             break;
  1608.         for( i=1; i<ptrn; i++ )
  1609.             if( uptab[get[i]]!=pattern[i] )
  1610.                 break;
  1611.         if( i==ptrn ) {
  1612.             for( ;lpos<get; lpos++ )
  1613.                 if( *lpos=='\n' )
  1614.                     lstart=lpos+1, lnum++;
  1615.             for( lend=lstart+1; *lend!='\n'; lend++ ) ;
  1616.             if( qcd ) {
  1617.                 if( get[-1]==':' || get[-1]=='/' ||
  1618.                       lpos==lstart && lend[-1]==':' ) {
  1619.                     char *tmp;
  1620.                     for( tmp=get+ptrn; *tmp&& *tmp!='\n'&& *tmp!='/'; tmp++ );
  1621.                     if( *tmp!='/' ) {
  1622.                         *lend=0;
  1623.                         strncpy(pattern,lstart,79);
  1624.                         qcd_offs=lend-buffer;
  1625.                         FreeMem( buffer, buflen );
  1626.                         return 2;
  1627.                     }
  1628.                 } else 
  1629.                     lend=lpos+1;
  1630.             } else {
  1631.                 *lend=0;
  1632.                 if(!(options&SEARCH_ONLY) ||
  1633.                      !isalphanum(lpos[-1])&&!isalphanum(lpos[ptrn]))
  1634.                     if(found(lstart, lnum, get-buffer, name, lpos==lstart ))
  1635.                         break;
  1636.                 *lend='\n';
  1637.             }
  1638.             get=lend+1;
  1639.         } else
  1640.             get++;
  1641.     }
  1642.     if( repeat )  { repeat=0; qcd_offs=0; goto retry; }
  1643.     if( file_cr ) { printf("\n"); quickscroll(); }
  1644.     FreeMem( buffer, buflen );
  1645.     return 1;
  1646. }
  1647.  
  1648. static int
  1649. found( char *lstart, int lnum, int loffs, char *name, char left )
  1650. {
  1651.     int fileabort=0;
  1652.  
  1653.     if( (options&SEARCH_LEFT) && !left)
  1654.         return 0;
  1655.  
  1656.     if ( docr )
  1657.         { quickscroll(); printf("\n"); docr=0; }
  1658.  
  1659.     if( options&SEARCH_FILE ) {
  1660.         file_cr=1;
  1661.         if( !file_name )
  1662.             printf("%s",name), file_name=1;
  1663.         if( options&SEARCH_NUM )
  1664.             fileabort=1;
  1665.         else
  1666.             printf(" %d",lnum);
  1667.     } else if( options & SEARCH_BIN ) {
  1668.         if (!(options & SEARCH_NUM))
  1669.             printf("Byte offset %d\n",loffs);
  1670.         else
  1671.             printf("%d\n",loffs);
  1672.         quickscroll();
  1673.     } else {
  1674.         if (!(options & SEARCH_NUM))
  1675.             printf("%4d ",lnum);
  1676.         printf((lstart[strlen(lstart)-1]=='\n')?"%s":"%s\n",lstart);
  1677.         quickscroll();
  1678.     }
  1679.     abort_search= options&SEARCH_ABORT;
  1680.     return dobreak() || fileabort || abort_search;
  1681. }
  1682.  
  1683. int
  1684. do_search( void )
  1685. {
  1686.     if(!isconsole(Output())) options |= SEARCH_VERB;
  1687.     abort_search=0;
  1688.     searchstring=av[--ac];
  1689.     all_args(search_file, 0);
  1690.     return 0;
  1691. }
  1692.  
  1693. #if 0
  1694. /* do_grep() is just do_search() with a modified all_args() */
  1695. int
  1696. do_grep( void )
  1697. {
  1698.     if(!isconsole(Output())) options |= SEARCH_VERB;
  1699.     abort_search=0;
  1700.     searchstring=av[1];
  1701.     all_args_n2m(search_file, 0, 2, ac);
  1702.     return 0;
  1703. }
  1704. #endif
  1705.  
  1706. static int
  1707. rm_file( long mask, char *file, char *fullpath )
  1708. {
  1709.     if ( *file && file[strlen(file)-1]=='/' ) file[strlen(file)-1]=0;
  1710.     if (options&16 || has_wild) printf(" %s...",fullpath);
  1711.     if (options&2 || options&4) SetProtection(file,0L);
  1712.     if (!DeleteFile(file)) {
  1713.         pError(file);
  1714.         if (options & 8)
  1715.             return 20;
  1716.     } else if (options&16 || has_wild)
  1717.         printf("Deleted\n");
  1718.     return 0;
  1719. }
  1720.  
  1721. int
  1722. do_rm( void )
  1723. {
  1724.     all_args( rm_file, 1);
  1725.     return 0;
  1726. }
  1727.  
  1728.  
  1729. int
  1730. do_history( void )
  1731. {
  1732.     HIST *hist;
  1733.     int i = H_tail_base;
  1734.     int len = av[1] ? strlen(av[1]) : 0;
  1735.     char buf[250];
  1736.  
  1737.     if( options&2 ) {
  1738.         while( safegets(buf,stdin) )
  1739.             add_history(buf);
  1740.         return 0;
  1741.     }
  1742.  
  1743.     for (hist = H_tail; hist && !dobreak(); hist = hist->prev, i++)
  1744.         if (len == 0 || !strncmp(av[1], hist->line, len))
  1745.             if( options&1 )
  1746.                 printf("%s\n", hist->line);
  1747.             else
  1748.                 printf("%3d %s\n", i, hist->line);
  1749.     return 0;
  1750. }
  1751.  
  1752. int
  1753. do_mem( void )
  1754. {
  1755.     static long clast, flast;
  1756.     long cfree, ffree, i;
  1757.     char *desc="Free", *mem;
  1758.  
  1759.     if( options&32 )
  1760.         for( i=0; i<4; i++ )
  1761.             if(mem=(char*)AllocMem(0x7fffffff,0))
  1762.                 FreeMem(mem,0x7fffffff);
  1763.  
  1764.     Forbid();
  1765.     cfree = AvailMem (MEMF_CHIP);
  1766.     ffree = AvailMem (MEMF_FAST);
  1767.     Permit();
  1768.     if( options&8 ) {
  1769.         clast=cfree, flast=ffree;
  1770.         return 0;
  1771.     }
  1772.     if( options&16 )
  1773.         cfree=clast-cfree, ffree=flast-ffree, desc="Used";
  1774.     if( options&4 ) {
  1775.         if     ( options & 1 ) printf("%ld\n",cfree);
  1776.         else if( options & 2 ) printf("%ld\n",ffree);
  1777.         else                   printf("%ld\n",cfree+ffree);
  1778.     } else {
  1779.         if     ( options & 1 ) printf("Free CHIP memory:%10s\n",itoa(cfree));
  1780.         else if( options & 2 ) printf("Free FAST memory:%10s\n",itoa(ffree));
  1781.         else {
  1782.             if(ffree) {
  1783.                 printf("FAST memory:%10s\n",itoa(ffree));
  1784.                 printf("CHIP memory:%10s\n",itoa(cfree));
  1785.             }
  1786.             printf("Total  %s:%10s\n",desc,itoa(cfree+ffree));
  1787.         }
  1788.     }
  1789.     return 0;
  1790. }
  1791.  
  1792. int
  1793. do_forline( void )
  1794. {
  1795.     char vname[33], buf[256], *cstr;
  1796.     int lctr;
  1797.     FILE *f;
  1798.  
  1799.     strcpy(vname,av[1]);
  1800.     if( !strcmp(av[2],"STDIN") )
  1801.         f=stdin;
  1802.     else 
  1803.         if(!(f=fopen(av[2],"r"))) { pError(av[2]); return 20; }
  1804.  
  1805.     lctr=0;
  1806.     ++H_stack;
  1807.     cstr = compile_av (av, 3, ac, ' ', 0);
  1808.     while (fgets(buf,256,f) && !dobreak() && !breakcheckd()) {
  1809.         buf[strlen(buf)-1]='\0';    /* remove CR */
  1810.         lctr++;
  1811.         set_var(LEVEL_SET | LEVEL_LOCAL, vname, buf);
  1812.         sprintf(buf,"%d",lctr);
  1813.         set_var(LEVEL_SET | LEVEL_LOCAL, v_linenum, buf);
  1814.         exec_command(cstr);
  1815.     }
  1816.     if( f!=stdin ) fclose(f);
  1817.     --H_stack;
  1818.     free (cstr);
  1819.     if( lctr ) {
  1820.         unset_var (LEVEL_SET, vname);
  1821.         unset_var (LEVEL_SET, v_linenum);
  1822.     }
  1823.     return 0;
  1824. }
  1825.  
  1826. int
  1827. do_fornum( void )
  1828. {
  1829.     char vname[33], buf[16];
  1830.     int n1, n2, step, i=1, verbose, runs=0;
  1831.     char *cstr;
  1832.  
  1833.     verbose=(options & 1);
  1834.     strcpy(vname,av[i++]);
  1835.     n1=myatoi(av[i++],-32767,32767); if (atoierr) return 20;
  1836.     n2=myatoi(av[i++],-32767,32767); if (atoierr) return 20;
  1837.     if (options & 2) {
  1838.         step=myatoi(av[i++],-32767,32767); if (atoierr) return 20;
  1839.     } else
  1840.         step=1;
  1841.     ++H_stack;
  1842.     cstr = compile_av (av, i, ac, ' ', 0);
  1843.     for (i=n1; (step>=0 ? i<=n2 : i>=n2) && !CHECKBREAK() && !breakcheckd();
  1844.          i+=step, runs++) {
  1845.         if (verbose) fprintf(stderr, "fornum: %d\n", i);
  1846.         sprintf(buf,"%d",i);
  1847.         set_var (LEVEL_SET | LEVEL_LOCAL, vname, buf);
  1848.         exec_command(cstr);
  1849.     }
  1850.     --H_stack;
  1851.     free (cstr);
  1852.     if( runs )
  1853.         unset_var (LEVEL_SET, vname);
  1854.     return 0;
  1855. }
  1856.  
  1857. /*
  1858.  * foreach var_name  ( str str str str... str ) commands
  1859.  * spacing is important (unfortunately)
  1860.  *
  1861.  * ac=0    1 2 3 4 5 6 7
  1862.  * foreach i ( a b c ) echo $i
  1863.  * foreach i ( *.c ) "echo -n "file ->";echo $i"
  1864.  */
  1865.  
  1866. int
  1867. do_foreach( void )
  1868. {
  1869.     int cstart, cend;
  1870.     char *cstr, vname[33];
  1871.     char **fav;
  1872.     int i=1, verbose;
  1873.  
  1874.     verbose=(options & 1);
  1875.     strcpy(vname, av[i++]);
  1876.     if (*av[i] == '(') i++;
  1877.     cstart = i;
  1878.     while (i<ac && *av[i] != ')') i++;
  1879.     if (i > ac) { fprintf(stderr,"')' expected\n"); return 20; }
  1880.     ++H_stack;
  1881.     cend = i;
  1882.  
  1883.     fav = (char **)salloc(sizeof(char *) * (ac));
  1884.     for (i = cstart; i < cend; ++i) fav[i] = av[i];
  1885.  
  1886.     cstr = compile_av (av, cend + 1, ac, ' ', 0);
  1887.  
  1888.     for (i = cstart; i<cend && !CHECKBREAK() && !breakcheckd(); ++i) {
  1889.         set_var (LEVEL_SET | LEVEL_LOCAL, vname, fav[i]);
  1890.         if (verbose) fprintf(stderr, "foreach: %s\n", fav[i]);
  1891.         execute(cstr);
  1892.     }
  1893.     --H_stack;
  1894.     free (fav);
  1895.     free (cstr);
  1896.     if( cstart<cend)
  1897.         unset_var (LEVEL_SET, vname);
  1898.     return 0;
  1899. }
  1900.  
  1901. int
  1902. do_forever( char *str )
  1903. {
  1904.     int rcode = 0;
  1905.     char *ptr = next_word( str );
  1906.  
  1907.     ++H_stack;
  1908.     for (;;) {
  1909.         if (CHECKBREAK() || breakcheckd()) { rcode = 20; break; }
  1910.         if (exec_command (ptr) > 0) {
  1911.             str = get_var(LEVEL_SET, v_lasterr);
  1912.             rcode = (str) ? atoi(str) : 20;
  1913.             break;
  1914.         }
  1915.     }
  1916.     --H_stack;
  1917.     return rcode;
  1918. }
  1919.  
  1920. extern struct IntuitionBase *IntuitionBase;
  1921.  
  1922.  
  1923. int
  1924. do_window( void )
  1925. {
  1926.     long x=-1, y=-1, w=-1, h=-1, maxwidth, maxheight, arg[5];
  1927.     int i;
  1928.  
  1929.     if(options & 32) { /* -q */
  1930.         struct Screen *scrn;
  1931.         struct Window *window;
  1932.         char buf[80];
  1933.         buf[40]=0;
  1934.         for (scrn=IntuitionBase->FirstScreen; scrn; scrn=scrn->NextScreen) {
  1935.             buf[0]=0;
  1936.             if( scrn->Title )
  1937.                 strncpy(buf,(char*)scrn->Title,40);
  1938.             printf("\nScreen \"%s\" (%d,%d,%dx%dx%d):\n",
  1939.                 buf,
  1940.                 scrn->LeftEdge,
  1941.                 scrn->TopEdge,
  1942.                 scrn->Width,
  1943.                 scrn->Height,
  1944.                 scrn->BitMap.Depth
  1945.             );
  1946.             for (window=scrn->FirstWindow; window; window=window->NextWindow) {
  1947.                 buf[0]=0;
  1948.                 if( window->Title )
  1949.                     strncpy(buf,(char*)window->Title,40);
  1950.                 printf("\tWindow\t\"%s\" (%d,%d,%dx%d)\n",
  1951.                     buf,
  1952.                     window->LeftEdge,
  1953.                     window->TopEdge,
  1954.                     window->Width,
  1955.                     window->Height
  1956.                 );
  1957.             }
  1958.         }
  1959.         return 0;
  1960.     }
  1961.  
  1962.     if( o_nowindow || !Win )
  1963.         return 20;
  1964.  
  1965.     maxwidth = Win->WScreen->Width;
  1966.     maxheight= Win->WScreen->Height;
  1967.     if( options&1 )
  1968.         x=Win->LeftEdge,y=Win->TopEdge,w=Win->MinWidth,h=Win->MinHeight;
  1969.     if( options&2 ) x=y=0, w=maxwidth, h=maxheight;
  1970.     if( options&4 ) WindowToFront(Win);
  1971.     if( options&8 ) WindowToBack(Win);
  1972.     if( options&16) ActivateWindow(Win);
  1973.     if( ac >= 5) {
  1974.         for(i=1; i<5; i++) {
  1975.             arg[i] = myatoi(av[i],0,1023); if (atoierr) return 20;
  1976.         }
  1977.         x=arg[1]; y=arg[2]; w=arg[3]; h=arg[4];
  1978.     }
  1979.     if( w!=-1 ) {
  1980.         int i;
  1981.         if ( x+w>maxwidth || y+h>maxheight ) {
  1982.             ierror(NULL, 500);
  1983.             return 20;
  1984.         }
  1985.         if( w<Win->MinWidth  ) w=Win->MinWidth;
  1986.         if( h<Win->MinHeight ) h=Win->MinHeight;
  1987.         if( Win->LeftEdge!=0 || Win->TopEdge!=0 )
  1988.             MoveWindow(Win, -Win->LeftEdge, -Win->TopEdge );
  1989.         if( Win->Width!=w || Win->Height!=h )
  1990.             SizeWindow(Win, w-Win->Width   , h-Win->Height  );
  1991.         if( x || y )
  1992.             MoveWindow(Win, x, y );
  1993.         for( i=0; i<10; i++ ) {
  1994.             if(  Win->LeftEdge==x && Win->TopEdge==y && 
  1995.                  Win->Width   ==w && Win->Height ==h )
  1996.                 break;
  1997.             Delay(5);
  1998.         }
  1999.     } else 
  2000.         Delay(20); /* pause 1/2 sec. before trying to print */
  2001.  
  2002.     Delay(10);
  2003.     printf("\014");
  2004.     return 0;
  2005. }
  2006.  
  2007. static void
  2008. setsystemtime(struct DateStamp *ds)
  2009. {
  2010.     struct timerequest tr;
  2011.     long secs= ds->ds_Days*86400+ds->ds_Minute*60+ds->ds_Tick/TICKS_PER_SECOND;
  2012.  
  2013.     if (OpenDevice(TIMERNAME, UNIT_VBLANK,(struct IORequest *)&tr, 0L)) {
  2014.         fprintf(stderr,"Clock error: can't open timer device\n");
  2015.         return;
  2016.     }
  2017.  
  2018.     tr.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  2019.     tr.tr_node.io_Message.mn_Node.ln_Pri = 0L;
  2020.     tr.tr_node.io_Message.mn_Node.ln_Name = NULL;
  2021.     tr.tr_node.io_Message.mn_ReplyPort = NULL;
  2022.     tr.tr_node.io_Command = TR_SETSYSTIME;
  2023.     tr.tr_time.tv_secs = secs;
  2024.     tr.tr_time.tv_micro = 0L;
  2025.     if (DoIO ((struct IORequest *)&tr))
  2026.         fprintf(stderr,"Clock error: can't talk to timer device\n");
  2027.     CloseDevice ((struct IORequest *)&tr);
  2028. }
  2029.  
  2030. static char tday[LEN_DATSTRING+1];    /* not sure, if +1 (null byte) is neccessary */
  2031.  
  2032. char *
  2033. dates( struct DateStamp *dss, int flags )
  2034. {
  2035.     static char timestr[2*LEN_DATSTRING+1];
  2036.     char tdate[LEN_DATSTRING+1], ttime[LEN_DATSTRING+1];
  2037.     struct DateTime dt;
  2038.     struct DateStamp *myds;
  2039.     char *dp,*tp;
  2040.  
  2041.     dt.dat_Format  = FORMAT_DOS;
  2042.     dt.dat_StrDay  = tday;
  2043.     dt.dat_StrDate = tdate;
  2044.     dt.dat_StrTime = ttime;
  2045.     dt.dat_Flags   = flags ? DTF_SUBST : 0;
  2046.     dt.dat_Stamp   = *dss;
  2047.  
  2048.     myds = &(dt.dat_Stamp);
  2049.  
  2050.     if(  myds->ds_Days<0   || myds->ds_Days>36500 ||
  2051.          myds->ds_Minute<0 || myds->ds_Minute>1440 ||
  2052.          myds->ds_Tick<0   || myds->ds_Tick>3000 || !DateToStr(&dt) )
  2053.         strcpy(tdate,"---------"), strcpy(ttime,"--------");
  2054.  
  2055. #if 0
  2056.     ttime[8] = '\0';
  2057.     sprintf(timestr,"%-9s %-8s",tdate,ttime);
  2058. #endif
  2059.  
  2060.     for (dp=tdate; dp && *dp && (*dp==' ' || *dp=='\t'); dp++)
  2061.         ;
  2062.     for (tp=ttime; tp && *tp && (*tp==' ' || *tp=='\t'); tp++)
  2063.         ;
  2064.     sprintf(timestr,"%9.9s %8.8s",dp,tp);
  2065.  
  2066.     timestr[18] = '\0';    /* protection against bad timestamped files */
  2067.     return timestr;
  2068. }
  2069.  
  2070. int
  2071. do_date( void )
  2072. {
  2073.     static long stopwatch;
  2074.     struct DateStamp dss;
  2075.     struct DateTime dt;
  2076.     long time;
  2077.     int i=1;
  2078.  
  2079.     dt.dat_Format=FORMAT_DOS;
  2080.     if (ac==1) {
  2081.         DateStamp(&dss);
  2082.         time=dss.ds_Minute*6000+2*dss.ds_Tick;   /* 2 = 100/TickPerSec   */
  2083.         if( options & 1 )
  2084.             stopwatch=time;
  2085.         else if( options&2 )
  2086.             printf( "%d.%02d\n",(time-stopwatch)/100,(time-stopwatch)%100);
  2087.         else
  2088.             printf("%s %s\n",tday,dates(&dss,0));
  2089.     } else {
  2090.         DateStamp(&dt.dat_Stamp);
  2091.         for ( ; i<ac; i++) {
  2092.             dt.dat_StrDate=NULL;
  2093.             dt.dat_StrTime=NULL;
  2094.             dt.dat_Flags=DTF_FUTURE;
  2095.             if (index(av[i],':')) dt.dat_StrTime=av[i];
  2096.                 else dt.dat_StrDate=av[i];
  2097.             if (!StrToDate(&dt)) ierror(av[i],500);
  2098.         }
  2099.         setsystemtime( & (dt.dat_Stamp) );
  2100.     }
  2101.     return 0;
  2102. }
  2103.