home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 182.lha / CShell_v3.0a / comm1.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  16KB  |  788 lines

  1. /*
  2.  * COMM1.C
  3.  *
  4.  * Matthew Dillon, August 1986
  5.  *
  6.  * Version 2.07M by Steve Drew 10-Sep-87
  7.  *
  8.  * Version 3.00A by Carlo Borreo & Cesare Dieni 30-Oct-88
  9.  *
  10.  */
  11.  
  12. extern char *v_passed, *v_gotofwd, *v_cwd, *v_lasterr;
  13.  
  14. #define DIR_SHORT 0x01
  15. #define DIR_FILES 0x02
  16. #define DIR_DIRS  0x04
  17.  
  18. extern int has_wild;
  19. char cwd[256];
  20.  
  21. /*
  22.     Parse the options specified in sw[]
  23.     Setting a bit for each one found
  24. */
  25.  
  26. get_opt(sw,count)
  27. char *sw;
  28. int *count;
  29. {
  30. register char *c,*s;
  31. unsigned int l,i = 0;
  32.  
  33. options=0;
  34. while((++i < ac) && (av[i][0] == '-')) {
  35.     for (c = av[i]+1; *c ; c++) {
  36.         for(l = 0,s = sw;*s && *s != *c; ++s) ++l;
  37.         if (*s) options |= (1 << l);
  38.         }
  39.     }
  40. *count = i;
  41. return options;
  42. }
  43.  
  44. do_sleep()
  45. {
  46. int i;
  47.  
  48. if (ac == 2) {
  49.     i = atoi(av[1]);
  50.     while (i > 0) { Delay (100L); i -= 2; if (CHECKBREAK()) break; }
  51.     }
  52. return 0;
  53. }
  54.  
  55. do_protect()
  56. {
  57. register long mask=0xf;
  58. register char *s, *p, *flags="DEWRAPSH";
  59. register unsigned int i;
  60. for (s=av[--ac]; *s; s++)
  61.     if (p=index(flags,Toupper(*s))) mask^=(1 << (p-flags));
  62.     else ierror(av[ac],500);
  63. for (i=1; i<ac; i++) if (!SetProtection(av[i],mask)) pError(av[i]);
  64. return 0;
  65. }
  66.  
  67. do_filenote()
  68. {
  69. char *note=av[--ac];
  70. register unsigned int i;
  71.  
  72. for (i=1; i<ac; i++) if (!SetComment(av[i], note)) pError(av[i]);
  73. return 0;
  74. }
  75.  
  76. do_cat()
  77. {
  78. FILE *fopen(), *fi;
  79. register unsigned int lctr;
  80. unsigned int i;
  81. char buf[256];
  82.  
  83. get_opt("n",&i);
  84. for (; i<ac; i++)
  85.     if (fi = fopen (av[i], "r")) {
  86.         lctr=0;
  87.         while (fgets(buf,256,fi) && !dobreak()) {
  88.             if (options) printf("%4d ",++lctr);
  89.             printf("%s",buf);
  90.             }
  91.         fclose (fi);
  92.         }
  93.     else pError(av[i]);
  94. }
  95.  
  96. do_info()
  97. {
  98. BPTR lock;
  99. struct InfoData *info;
  100. unsigned int size, free;
  101. char *p,*s,*state;
  102. APTR original=Myprocess->pr_WindowPtr;
  103. struct DirectoryEntry *de_head=NULL, *de;
  104.  
  105. info=(struct InfoData *)AllocMem((long)sizeof(struct InfoData),MEMF_PUBLIC);
  106. AddDADevs(&de_head, DLF_DEVICES | DLF_DISKONLY );
  107. Myprocess->pr_WindowPtr = (APTR)(-1);
  108. printf ("Unit  Size  Bytes  Used Blk/By-Free Full Errs  Status    Name\n");
  109. for (de=de_head; de; de=de->de_Next) {
  110.     printf("%-5s",de->de_Name);
  111.     if (lock=Lock(de->de_Name,ACCESS_READ)) {
  112.         if (Info(lock, info)) {
  113.         s = get_pwd(lock);
  114.         if (p=index(s,':')) *p = '\0';
  115.         size = ((info->id_NumBlocks + 2)* info->id_BytesPerBlock)/ 1024;
  116.         free = (((info->id_NumBlocks-info->id_NumBlocksUsed))*
  117.                info->id_BytesPerBlock)/ 1024;
  118.         switch(info->id_DiskState) {
  119.             case ID_WRITE_PROTECTED: state="Read Only "; break;
  120.             case ID_VALIDATED:     state="Read/Write"; break;
  121.             case ID_VALIDATING:     state="Validating"; break;
  122.             }
  123.         printf("%4d%c%6ld%7ld%7ld%4d%c%4ld%%%4ld  %s %s\n",
  124.             (size>1024) ? ((size+512) >> 10) : size,
  125.             (size>1024) ? 'M' : 'K',
  126.             info->id_BytesPerBlock,
  127.             info->id_NumBlocksUsed,
  128.             info->id_NumBlocks-info->id_NumBlocksUsed,
  129.             (free>1024) ? ((free+512) >> 10) : free,
  130.             (free>1024) ? 'M' : 'K',
  131.             (info->id_NumBlocksUsed * 100)/info->id_NumBlocks,
  132.             info->id_NumSoftErrors,
  133.             state,
  134.             s);
  135.         }
  136.         else pError (de->de_Name);
  137.         UnLock(lock);
  138.         }
  139.     else puts("  No disk present");
  140.     }
  141. FreeDAList(&de_head);
  142. Myprocess->pr_WindowPtr = original;
  143. FreeMem(info,(long)sizeof(struct InfoData));
  144. }
  145.  
  146. /* things shared with display_file */
  147.  
  148. char lspec[128];
  149. int filecount, col;
  150. long bytes, blocks;
  151.  
  152. /*
  153.  * the args passed to do_dir will never be expanded
  154.  */
  155. do_dir()
  156. {
  157.    void display_file();
  158.    int i;
  159.  
  160.    col = filecount = 0;
  161.    bytes = blocks = 0L;
  162.    *lspec = '\0';
  163.  
  164.    get_opt("sfd",&i);
  165.  
  166.    if (ac == i) {
  167.       ++ac;
  168.       av[i] = "";
  169.    }
  170.    if (!(options & (DIR_FILES | DIR_DIRS)))  options|=(DIR_FILES | DIR_DIRS);
  171.  
  172.    for (; i < ac; ++i) {
  173.       char **eav;
  174.       int c,eac;
  175.       if (!(eav = expand(av[i], &eac)))
  176.       continue;
  177.       QuickSort(eav, eac);
  178.       for(c=0;c < eac && !breakcheck();++c) display_file(options,eav[c]);
  179.       free_expand (eav);
  180.       if (CHECKBREAK()) break;
  181.    }
  182.    if (col)  printf("\n");
  183.    if (filecount > 1) {
  184.       blocks += filecount; /* account for dir blocks */
  185.       printf (" %ld Blocks, %ld Bytes used in %d files\n", blocks, bytes, filecount);
  186.    }
  187.    return (0);
  188. }
  189.  
  190. void
  191. display_file(options,filestr)
  192. int options;
  193. char *filestr;
  194. {
  195.    long atol();
  196.    int isadir,slen;
  197.    char sc;
  198.    char *c,*s,*fi;
  199.    BPTR lock;
  200.  
  201. /*    if current dir different from lspec then
  202.     look for ':' or '/' if found lock it and get_pwd.
  203.     else then use cwd.
  204. */
  205.    for(s = c = filestr; *c; ++c) if (*c == ':' || *c == '/') s = c;
  206.    if (*s == ':') ++s;
  207.    sc = *s;
  208.    *s = '\0';
  209.    c = filestr;
  210.    if (!*c) c = cwd;
  211.    if (strcmp (c, &lspec))  {
  212.     strcpy(lspec, c);
  213.     if (col) printf("\n");
  214.     if (lock=Lock(c,SHARED_LOCK)) {
  215.         printf("Directory of %s\n", get_pwd(lock));
  216.         UnLock(lock);
  217.         }
  218.     col = 0;
  219.     }
  220.    *s = sc;
  221.    if (sc == '/') s++;
  222.    slen = strlen(s);
  223.    fi = s + slen + 1;
  224.    isadir = (fi[12] =='D');
  225.  
  226.    if (!(((options & DIR_FILES) && !isadir) ||
  227.       ((options & DIR_DIRS) &&  isadir)))
  228.       return;
  229.    if (isadir) printf ("\23333m");
  230.    if (options & DIR_SHORT) {
  231.     if (col==3 && slen>18) { printf("\n"); col = 0; }
  232.     if (slen>18) { printf(" %-37s",s); col+= 2; }
  233.         else { printf(" %-18s",s); col++; }
  234.     if (col > 3) { printf("\n"); col=0; }
  235.     }
  236.    else printf("   %-24s %s",s ,fi);
  237.    if (isadir) printf("\2330m");
  238.    fflush(stdout);
  239.    fi[16] = fi[21] = '\0';
  240.    bytes  += atol(fi+10);
  241.    blocks += atol(fi+17);
  242.    filecount++;
  243.    return;
  244. }
  245.  
  246. do_quit()
  247. {
  248. if (Src_stack) {
  249.     Quit = 1;
  250.     return(do_return());
  251.     }
  252. main_exit(0);
  253. }
  254.  
  255. do_echo(str)
  256. register char *str;
  257. {
  258. char nl=1;
  259.  
  260. for (; *str && *str != ' '; ++str);
  261. if (*str==' ') ++str;
  262. if (av[1] && !strcmp(av[1],"-n")) {
  263.     nl = 0;
  264.     str += 2;
  265.     if (*str==' ') ++str;
  266.     }
  267. printf("%s",str);
  268. if (nl) printf("\n");
  269. fflush(stdout);
  270. return 0;
  271. }
  272.  
  273. do_source(str)
  274. char *str;
  275. {
  276.    register FILE *fi;
  277.    char buf[256];
  278.  
  279.    if (Src_stack == MAXSRC) {
  280.       ierror(NULL,217);
  281.       return(-1);
  282.    }
  283.    if ((fi = fopen (av[1], "r")) == 0) {
  284.       ierror(av[1], 205);
  285.       return(-1);
  286.    }
  287.    set_var(LEVEL_SET, v_passed, next_word(next_word(str)));
  288.    ++H_stack;
  289.    Src_pos[Src_stack] = 0;
  290.    Src_base[Src_stack] = (long)fi;
  291.    ++Src_stack;
  292.    while (fgets (buf, 256, fi)) {
  293.       int len = strlen(buf);
  294.       buf[len-1] = '\0';
  295.       Src_pos[Src_stack - 1] += len;
  296.       if (Verbose && !forward_goto)
  297.       fprintf(stderr,"%s\n",buf);
  298.       exec_command (buf);
  299.       if (CHECKBREAK())
  300.       break;
  301.    }
  302.    --H_stack;
  303.    --Src_stack;
  304.    if (forward_goto)
  305.       ierror(NULL,501);
  306.    forward_goto = 0;
  307.    unset_level(LEVEL_LABEL + Src_stack);
  308.    unset_var(LEVEL_SET, v_gotofwd);
  309.    unset_var(LEVEL_SET, v_passed);
  310.    fclose (fi);
  311.    return (0);
  312. }
  313.  
  314. /*
  315.  * return ptr to string that contains full cwd spec.
  316.  */
  317. char *get_pwd(flock)
  318. BPTR flock;
  319. {
  320. static char pwdstr[130];
  321. PathName(flock, pwdstr, 128L);
  322. return pwdstr;
  323. }
  324.  
  325. /*
  326.  * set process cwd name and $_cwd, if str != NULL also print it.
  327.  */
  328. do_pwd(str)
  329. char *str;
  330. {
  331. char *ptr;
  332.  
  333. if (Myprocess->pr_CurrentDir == 0)
  334.     attempt_cd(":"); /* if we just booted 0 = root lock */
  335. strcpy(cwd,get_pwd(Myprocess->pr_CurrentDir,1));
  336. if (str) puts(cwd);
  337. set_var(LEVEL_SET, v_cwd, cwd);
  338. /* put the current dir name in our CLI task structure */
  339. CtoBStr(cwd, Mycli->cli_SetName, 128L);
  340.  
  341. /*
  342. ptr=(char *)((ULONG)Mycli->cli_SetName << 2);
  343. ptr[0] = strlen(cwd);
  344. movmem(cwd,ptr+1,(int)ptr[0]);
  345. return 0;
  346. */
  347. }
  348.  
  349. /*
  350.  * CD
  351.  *
  352.  * CD(str, 0)      -do CD operation.
  353.  *
  354.  *    standard operation: breakup path by '/'s and process independantly
  355.  *    x:    -reset cwd base
  356.  *    ..    -remove last cwd element
  357.  *    N     -add N or /N to cwd
  358.  */
  359.  
  360. do_cd(str)
  361. char *str;
  362. {
  363.    char sc, *ptr;
  364.    int err=0;
  365.  
  366.    str = next_word(str);
  367.    if (*str == '\0') {
  368.       puts(cwd);
  369.       return(0);
  370.    }
  371.    str[strlen(str)+1] = '\0';        /* add second \0 on end */
  372.    while (*str) {
  373.       for (ptr = str; *ptr && *ptr != '/' && *ptr != ':'; ++ptr);
  374.       switch (*ptr) {
  375.       case ':':
  376.       sc = ptr[1];
  377.       ptr[1] = '\0';
  378.       err = attempt_cd(str);
  379.       ptr[1] = sc;
  380.       break;
  381.       case '\0':
  382.       case '/':
  383.       *ptr = '\0';
  384.       if (strcmp(str, "..") == 0 || str == ptr)
  385.          str = "/";
  386.       if (*str) err = attempt_cd(str);
  387.       break;
  388.       }
  389.       if (err) break;
  390.       str = ptr + 1;
  391.    }
  392.    do_pwd(NULL);      /* set $_cwd */
  393.    return(err);
  394. }
  395.  
  396. attempt_cd(str)
  397. char *str;
  398. {
  399. BPTR oldlock, filelock;
  400.  
  401. if (filelock=Lock(str, ACCESS_READ)) {
  402.     if (isdir(str)) {
  403.         if (oldlock=CurrentDir(filelock)) UnLock(oldlock);
  404.         return (0);
  405.         }
  406.     UnLock(filelock);
  407.     ierror(str, 212);
  408.     }
  409. else ierror(str, 205);
  410. return -1;
  411. }
  412.  
  413. do_mkdir()
  414. {
  415. register unsigned int i;
  416. BPTR lock;
  417.  
  418. for (i=1; i<ac; ++i) {
  419.     if (exists(av[i])) ierror(av[i],203);
  420.     else if (lock=CreateDir(av[i])) UnLock (lock);
  421.     else pError(av[i]);
  422.     }
  423. return 0;
  424. }
  425.  
  426. do_mv()
  427. {
  428. char *dest, buf[256];
  429. int dirflag;
  430. register unsigned int i;
  431.  
  432. dirflag=isdir(dest=av[--ac]);
  433. if (ac>3 && !dirflag) { ierror(dest, 507); return (-1); }
  434. for (i=1; i<ac; ++i) {
  435.     strcpy(buf, dest);
  436.     if (dirflag) TackOn(buf, BaseName(av[i]));
  437.     if (Rename(av[i],buf)==0)
  438.         { pError(av[i]); return -1; }
  439.     }
  440. return 0;
  441. }
  442.  
  443. int dirstoo;
  444.  
  445. all_args(args, action, dirsflag)
  446. char *args;
  447. int (*action)();
  448. {
  449. unsigned int i;
  450.  
  451. get_opt(args, &i);
  452. dirstoo=dirsflag;
  453. for (; i<ac && !dobreak(); ++i)
  454.     if (isdir(av[i])) {
  455.         if (options & 1) recurse(av[i], action);
  456.             else if (dirstoo) (*action)(av[i]);
  457.         }
  458.     else (*action)(av[i]);
  459. return 0;
  460. }
  461.  
  462. char *searchstring;
  463.  
  464. search_file(s)
  465. char *s;
  466. {
  467. FILE *fopen(), *fi;
  468. unsigned int lctr, len=strlen(searchstring);
  469. int yesno;
  470. char buf[256], lowbuf[256], first;
  471. register char *p, *l, *t;
  472.  
  473. if (!(options & 2)) for (t=searchstring; *t=Toupper(*t); t++);
  474. first=*searchstring;
  475. if ((fi=fopen(s, "r"))==NULL) { pError(s); return; }
  476. lctr=0;
  477. if (!(options & 32)) printf("Examining %s...\n",s);
  478. while (fgets(buf,256,fi) && !dobreak()) {
  479.     lctr++;
  480.     if (options & 4) yesno=compare_ok(searchstring,buf);
  481.     else {
  482.         yesno=0;
  483.         p=buf;
  484.         if (!(options & 2)) {
  485.             l=lowbuf;            /* p is already =buf */
  486.             while (*l++=Toupper(*p++));    /* lowbuf=upper(buf) */
  487.             p=lowbuf;
  488.             }
  489.         while (p=index(p,first))
  490.             if (!strncmp(p++,searchstring,len)) { yesno=1; break; }
  491.         }
  492.     if (yesno ^ ((options & 16)!=0) ) {
  493.             /* default: print line numbers */
  494.         if (!(options & 8)) printf("%4d ",lctr);
  495.         printf("%s",buf);
  496.         }
  497.     }
  498. fclose (fi);
  499. }
  500.  
  501. do_search()
  502. {
  503. searchstring=av[--ac];
  504. all_args("rcwneq", search_file, 0);
  505. return 0;
  506. }
  507.  
  508. rm_file(file)
  509. char *file;
  510. {
  511. if (has_wild) printf(" %s...",file);
  512. if (options & 2) SetProtection(file,0L);
  513. if (!DeleteFile(file)) pError (file); else if (has_wild) printf("Deleted\n");
  514. }
  515.  
  516. do_rm()
  517. {
  518. all_args("rp", rm_file, 1);
  519. return 0;
  520. }
  521.  
  522. recurse(name, action)
  523. char *name;
  524. int (*action)();
  525. {
  526. register BPTR lock, cwd;
  527. register FIB *fib=(FIB *)AllocMem((long)sizeof(FIB),MEMF_PUBLIC);
  528. char *namecopy=malloc(256);
  529.  
  530. if (name[0] =='\0') return;
  531. namecopy[0]=0;
  532. if (lock=Lock(name,ACCESS_READ)) {
  533.     cwd =CurrentDir(lock);
  534.     if (Examine(lock, fib))
  535.     while (ExNext(lock, fib) && !CHECKBREAK()) {
  536.         if (*namecopy) { (*action)(namecopy); namecopy[0]=0; }
  537.         if (fib->fib_DirEntryType>=0) recurse(fib->fib_FileName,action);
  538.         else strcpy(namecopy,fib->fib_FileName);
  539.         }
  540.     if (*namecopy) (*action)(namecopy);
  541.     UnLock(CurrentDir(cwd));
  542.     if (dirstoo) (*action)(name);
  543.     }
  544. else pError(name);
  545. free(namecopy);
  546. FreeMem(fib, (long)sizeof(FIB));
  547. }
  548.  
  549. do_history()
  550. {
  551. register struct HIST *hist;
  552. int i = H_tail_base;
  553. int len = (av[1]) ? strlen(av[1]) : 0;
  554.  
  555. for (hist = H_tail; hist && !dobreak(); hist = hist->prev)
  556.     if (len == 0 || !strncmp(av[1], hist->line, len))
  557.         printf("%3d %s", i++, hist->line);
  558. return 0;
  559. }
  560.  
  561. do_mem()
  562. {
  563. long cfree, ffree;
  564. extern long AvailMem();
  565.  
  566. Forbid();
  567. cfree = AvailMem (MEMF_CHIP);
  568. ffree = AvailMem (MEMF_FAST);
  569. Permit();
  570. if (ffree) printf ("FAST memory: %ld\nCHIP memory: %ld\n", ffree, cfree);
  571. printf("Total  Free: %ld\n", cfree+ffree);
  572. return 0;
  573. }
  574.  
  575. /*
  576.  * foreach var_name  ( str str str str... str ) commands
  577.  * spacing is important (unfortunately)
  578.  *
  579.  * ac=0    1 2 3 4 5 6 7
  580.  * foreach i ( a b c ) echo $i
  581.  * foreach i ( *.c ) "echo -n "file ->";echo $i"
  582.  */
  583.  
  584. do_foreach()
  585. {
  586.    register int i, cstart, cend;
  587.    register char *cstr, *vname;
  588.    char **fav;
  589.  
  590.    cstart = i = (*av[2] == '(') ? 3 : 2;
  591.    while (i < ac) {
  592.       if (*av[i] == ')')
  593.       break;
  594.       ++i;
  595.    }
  596.    if (i == ac) {
  597.       fprintf (stderr,"')' expected\n");
  598.       return (-1);
  599.    }
  600.    ++H_stack;
  601.    cend = i;
  602.  
  603.    vname = strcpy(malloc(strlen(av[1])+1), av[1]);
  604.    fav = (char **)malloc(sizeof(char *) * (ac));
  605.    cstr = compile_av (av, cend + 1, ac, ' ');
  606.  
  607.    for (i = cstart; i < cend; ++i) {
  608.      fav[i] = av[i];
  609.    }
  610.  
  611.    for (i = cstart; i < cend; ++i) {
  612.       set_var (LEVEL_SET, vname, fav[i]);
  613.       if (CHECKBREAK())
  614.       break;
  615.       exec_command (cstr);
  616.    }
  617.    --H_stack;
  618.    free (fav);
  619.    free (cstr);
  620.    unset_var (LEVEL_SET, vname);
  621.    free (vname);
  622.    return (0);
  623. }
  624.  
  625. do_forever(str)
  626. char *str;
  627. {
  628.    int rcode = 0;
  629.    char *ptr = next_word(str);
  630.  
  631.    ++H_stack;
  632.    for (;;) {
  633.       if (CHECKBREAK()) {
  634.       rcode = 20;
  635.       break;
  636.       }
  637.       if (exec_command (ptr) < 0) {
  638.       str = get_var(LEVEL_SET, v_lasterr);
  639.       rcode = (str) ? atoi(str) : 20;
  640.       break;
  641.       }
  642.    }
  643.    --H_stack;
  644.    return (rcode);
  645. }
  646.  
  647. extern struct Window *w;
  648. extern struct IntuitionBase *IntuitionBase;
  649.  
  650. do_window()
  651. {
  652. long x, y, maxwidth, maxheight, arg[5];
  653. unsigned int i;
  654. struct Screen *screen;
  655. struct Window *window;
  656.  
  657. get_opt("slfbaq", &i);
  658. if (options & 1)
  659.     SizeWindow(w, (long)(w->MinWidth-w->Width), (long)(w->MinHeight-w->Height));
  660. if (options & 2) {
  661.     x=-w->LeftEdge;
  662.     y=-w->TopEdge;
  663.     MoveWindow(w,x,y);
  664.     x=IntuitionBase->ActiveScreen->Width -w->Width;
  665.     y=IntuitionBase->ActiveScreen->Height-w->Height;
  666.     SizeWindow(w,x,y);
  667.     }
  668. if (options & 4) WindowToFront(w);
  669. if (options & 8) WindowToBack(w);
  670. if (options & 16) ActivateWindow(w);
  671. if(ac >= 5) {
  672.     Errno = 0;
  673.     for(i=1; i<5; i++) {
  674.         arg[i] = myatol(av[i]);
  675.         if (Errno || arg[i] < 0) {
  676.             ierror(av[i], 500);
  677.             return 20;
  678.             }
  679.         }
  680.     maxwidth = w->WScreen->Width;
  681.     maxheight= w->WScreen->Height;
  682.     if (arg[3] > maxwidth - arg[1] || arg[4] > maxheight- arg[2]) {
  683.         ierror(NULL, 500);
  684.         return 20;
  685.         }
  686.     x = -w->LeftEdge;
  687.     y = -w->TopEdge;
  688.     MoveWindow(w, x, y);
  689.     x = arg[3] - w->Width;
  690.     y = arg[4] - w->Height;
  691.     SizeWindow(w, x, y);
  692.     x = arg[1];
  693.     y = arg[2];
  694.     MoveWindow(w, x, y);
  695.     }
  696. if(options & 32) {
  697.     for (screen=IntuitionBase->FirstScreen; screen; screen=screen->NextScreen) {
  698.         printf("\nScreen \"%s\" (%dx%d):\n",
  699.             screen->Title,
  700.             screen->Width,
  701.             screen->Height
  702.             );
  703.         for (window=screen->FirstWindow; window; window=window->NextWindow) {
  704.         printf("\tWindow\t\"%s\" (%dx%d)\n",
  705.             window->Title,
  706.             window->Width,
  707.             window->Height
  708.             );
  709.         }
  710.         }
  711.     return 0;
  712.     }
  713. printf("\014");
  714. return 0;
  715. }
  716.  
  717. setsystemtime(ds)
  718. struct DateStamp *ds;
  719. {
  720. struct timerequest tr;
  721. long secs= ds->ds_Days*86400 + ds->ds_Minute*60 + ds->ds_Tick/TICKS_PER_SECOND;
  722.  
  723. if (OpenDevice(TIMERNAME, UNIT_VBLANK, &tr, 0L)) {
  724.     fprintf(stderr,"Clock error: can't open timer device\n");
  725.     return;
  726.     }
  727. tr.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  728. tr.tr_node.io_Message.mn_Node.ln_Pri = 0L;
  729. tr.tr_node.io_Message.mn_Node.ln_Name = NULL;
  730. tr.tr_node.io_Message.mn_ReplyPort = NULL;
  731. tr.tr_node.io_Command = TR_SETSYSTIME;
  732. tr.tr_time.tv_secs = secs;
  733. tr.tr_time.tv_micro = 0L;
  734. if (DoIO (&tr)) fprintf(stderr,"Clock error: can't talk to timer device\n");
  735. CloseDevice (&tr);
  736. }
  737.  
  738. char tday[10];
  739.  
  740. char *dates(dss)
  741. struct DateStamp *dss;
  742. {
  743. static char timestr[40];
  744. char tdate[10], ttime[10];
  745. struct DateTime dt;
  746. struct DateStamp *myds=&(dt.dat_Stamp);
  747.  
  748. dt.dat_Format=FORMAT_DOS;
  749. dt.dat_StrDay=tday;
  750. dt.dat_StrDate=tdate;
  751. dt.dat_StrTime=ttime;
  752. dt.dat_Flags=NULL;
  753. myds->ds_Days=dss->ds_Days;
  754. myds->ds_Minute=dss->ds_Minute;
  755. myds->ds_Tick=dss->ds_Tick;
  756. StamptoStr(&dt);
  757. sprintf(timestr,"%s %s\n",tdate,ttime);
  758. timestr[18]='\n';
  759. timestr[19]='\0';    /* protection against bad timestamped files */
  760. return timestr;
  761. }
  762.  
  763. date()
  764. {
  765. struct DateStamp dss;
  766. register unsigned short i;
  767. struct DateTime dt;
  768.  
  769. dt.dat_Format=FORMAT_DOS;
  770. if (ac==1) {
  771.     DateStamp(&dss);
  772.     printf("%s %s",tday,dates(&dss));
  773.     }
  774. else {
  775.     DateStamp(& (dt.dat_Stamp));
  776.     for (i=1; i<ac; i++) {
  777.         dt.dat_StrDate=NULL;
  778.         dt.dat_StrTime=NULL;
  779.         dt.dat_Flags=DTF_FUTURE;
  780.         if (index(av[i],':')) dt.dat_StrTime=av[i];
  781.             else dt.dat_StrDate=av[i];
  782.         if (StrtoStamp(&dt)) ierror(av[i],500);
  783.         }
  784.     setsystemtime( & (dt.dat_Stamp) );
  785.     }
  786. return 0;
  787. }
  788.