home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 594b.lha / Dautostart / src / dautostart.c next >
C/C++ Source or Header  |  1991-09-11  |  5KB  |  267 lines

  1.  
  2. /*
  3.  *  AUTOSTART.C
  4.  *
  5.  *  AUTOSTART [-s basename] [dir]
  6.  *
  7.  *  This program runs through specified volumes and executes any scripts
  8.  *  named boot-sequence or <dir>/boot-sequence    (that is,
  9.  *  it scans all top level directories as well as root)
  10.  *
  11.  *  If first line in the boot-sequence script file is '; PRI=<priority>'
  12.  *  then it will be executed according to the specified priority
  13.  *
  14.  *  default base-name is 'boot-sequence' but may be changed with the -s
  15.  *  option.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <exec/types.h>
  22. #include <exec/nodes.h>
  23. #include <exec/memory.h>
  24. #include <dos/dos.h>
  25. #include <dos/dosextens.h>
  26. #include <clib/exec_protos.h>
  27. #include <clib/dos_protos.h>
  28. #include <clib/alib_protos.h>
  29.  
  30. typedef struct Process        Process;
  31. typedef struct FileInfoBlock    FileInfoBlock;
  32. typedef struct Node        Node;
  33. typedef struct List        List;
  34.  
  35. typedef struct SNode {
  36.     Node    sn_Node;
  37.     char    *sn_Script;
  38.     long    sn_Bytes;
  39. } SNode;
  40.  
  41. APTR SavePtr;
  42. List RunList;
  43. char *BaseName = "boot-sequence";
  44.  
  45. void myexit(void);
  46. void AutoStart(char *, short);
  47. void AutoRunScripts(void);
  48. int  AddBootFile(char *, char *, char *);
  49. char *DirectoryOf(char *);
  50.  
  51. main(ac, av)
  52. char *av[];
  53. {
  54.     short i;
  55.     short n = 0;
  56.     Process *proc = (Process *)FindTask(NULL);
  57.  
  58.     atexit(myexit);
  59.     SavePtr = proc->pr_WindowPtr;
  60.     proc->pr_WindowPtr = (APTR)-1;
  61.  
  62.     NewList(&RunList);
  63.  
  64.     for (i = 1; i < ac; ++i) {
  65.     char *ptr = av[i];
  66.     if (*ptr == '-') {
  67.         ptr += 2;
  68.         av[i] = NULL;
  69.  
  70.         switch(ptr[-1]) {
  71.         case 's':   /*  specify script base name    */
  72.         if (*ptr) {
  73.             BaseName = ptr;
  74.         } else {
  75.             BaseName = av[++i];
  76.             av[i] = NULL;
  77.         }
  78.         break;
  79.         default:
  80.         fprintf(stderr, "Bad Option: %s\n", ptr - 2);
  81.         break;
  82.         }
  83.     }
  84.     }
  85.  
  86.     for (i = 1; i < ac; ++i) {
  87.     if (av[i])
  88.         AutoStart(av[i], 1);
  89.     }
  90.  
  91.     if (n == 0) {
  92.     if (system("Info >ram:info.tmp") == 0) {
  93.         FILE *fi;
  94.         char buf[128];
  95.         short state = 0;
  96.  
  97.         if (fi = fopen("ram:info.tmp", "r")) {
  98.         while (fgets(buf, sizeof(buf), fi)) {
  99.             if (state == 0) {
  100.             if (strnicmp(buf, "Unit", 4) == 0)
  101.                 state = 1;
  102.             } else {
  103.             if (buf[0] == '\n')
  104.                 break;
  105.             AutoStart(strtok(buf, " \t\n"), 1);
  106.             }
  107.         }
  108.         fclose(fi);
  109.         }
  110.         DeleteFile("ram:info.tmp");
  111.     }
  112.     }
  113.     AutoRunScripts();
  114.     return(0);
  115. }
  116.  
  117. void
  118. myexit(void)
  119. {
  120.     Process *proc = (Process *)FindTask(NULL);
  121.  
  122.     proc->pr_WindowPtr = SavePtr;
  123.  
  124.     {
  125.     SNode *snode;
  126.     while (snode = RemHead(&RunList)) {
  127.         if (snode->sn_Bytes)
  128.         FreeMem(snode->sn_Script, snode->sn_Bytes + 1);
  129.         FreeMem(snode, sizeof(SNode) + strlen(snode->sn_Node.ln_Name) + 1);
  130.     }
  131.     }
  132. }
  133.  
  134. void
  135. AutoStart(vol, trytop)
  136. char *vol;
  137. short trytop;
  138. {
  139.     __aligned FileInfoBlock fib;
  140.     long lock;
  141.  
  142.     printf("; AutoStart %s\n", vol);
  143.  
  144.     if (trytop)
  145.     AddBootFile(vol, BaseName, NULL);
  146.  
  147.     if (lock = Lock(vol, SHARED_LOCK)) {
  148.     if (Examine(lock, &fib)) {
  149.         while (ExNext(lock, &fib)) {
  150.         if (fib.fib_DirEntryType > 0) {
  151.             if (AddBootFile(vol, fib.fib_FileName, BaseName) == 0) {
  152.             char *path;
  153.  
  154.             if (path = malloc(strlen(vol) + strlen(fib.fib_FileName) + 2)) {
  155.                 sprintf(path, "%s%s/", vol, fib.fib_FileName);
  156.                 AutoStart(path, 0);
  157.                 free(path);
  158.             }
  159.             }
  160.         }
  161.         }
  162.     }
  163.     UnLock(lock);
  164.     }
  165. }
  166.  
  167. int
  168. AddBootFile(vol, elm1, elm2)
  169. char *vol;
  170. char *elm1;
  171. char *elm2;
  172. {
  173.     static char Tmp[256];
  174.     static char Buf[256];
  175.     int n;
  176.     int r = -1;
  177.     FILE *fi;
  178.  
  179.     n = sprintf(Tmp, "%s%s", vol, elm1);
  180.     if (elm2)
  181.     n += sprintf(Tmp + n, "/%s", elm2);
  182.     sprintf(Tmp + n, ".1");
  183.  
  184.     while (fi = fopen(Tmp, "r")) {
  185.     SNode *snode = AllocMem(sizeof(SNode) + strlen(Tmp) + 1, MEMF_PUBLIC|MEMF_CLEAR);
  186.     char *ptr;
  187.  
  188.     r = 0;
  189.     snode->sn_Node.ln_Name = (char *)(snode + 1);
  190.     strcpy(snode->sn_Node.ln_Name, Tmp);
  191.  
  192.     if (fgets(Buf, sizeof(Buf), fi)) {
  193.         if (ptr = strstr(Buf, "PRI="))
  194.         snode->sn_Node.ln_Pri = atoi(ptr + 4);
  195.     }
  196.     fseek(fi, 0L, 2);
  197.     if ((snode->sn_Bytes = ftell(fi)) > 0) {
  198.         if (snode->sn_Script = AllocMem(snode->sn_Bytes + 1, MEMF_PUBLIC|MEMF_CLEAR)) {
  199.         fseek(fi, 0L, 0);
  200.         fread(snode->sn_Script, snode->sn_Bytes, 1, fi);
  201.         }
  202.     }
  203.     if (snode->sn_Script == NULL)
  204.         snode->sn_Bytes = 0;
  205.     fclose(fi);
  206.     Forbid();
  207.     Enqueue(&RunList, &snode->sn_Node);
  208.     Permit();
  209.  
  210.     if (++Tmp[strlen(Tmp)-1] == '9' + 1)
  211.         break;
  212.     }
  213.     return(r);
  214. }
  215.  
  216.  
  217. /*
  218.  *  Paste scripts together into an output file
  219.  */
  220.  
  221. void
  222. AutoRunScripts(void)
  223. {
  224.     SNode *snode;
  225.  
  226.     for (;;) {
  227.     Forbid();
  228.     snode = RemHead(&RunList);
  229.     Permit();
  230.     if (snode == NULL)
  231.         break;
  232.     if (snode->sn_Bytes) {
  233.         printf("; autostart %+-3d %s\n", snode->sn_Node.ln_Pri, snode->sn_Node.ln_Name);
  234.         printf("CD %s\n", DirectoryOf(snode->sn_Node.ln_Name));
  235.         fwrite(snode->sn_Script, snode->sn_Bytes, 1, stdout);
  236.         FreeMem(snode->sn_Script, snode->sn_Bytes + 1);
  237.     }
  238.     FreeMem(snode, sizeof(SNode) + strlen(snode->sn_Node.ln_Name) + 1);
  239.     }
  240. }
  241.  
  242. /*
  243.  *  delete last element from path
  244.  */
  245.  
  246. char *
  247. DirectoryOf(path)
  248. char *path;
  249. {
  250.     static char Tmp[256];
  251.     char *ptr;
  252.  
  253.     strcpy(Tmp, path);
  254.     for (ptr = Tmp + strlen(Tmp); ptr >= Tmp; --ptr) {
  255.     if (*ptr == '/') {
  256.         ptr[0] = 0;
  257.         break;
  258.     }
  259.     if (*ptr == ':') {
  260.         ptr[1] = 0;
  261.         break;
  262.     }
  263.     }
  264.     return(Tmp);
  265. }
  266.  
  267.