home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 118.lha / defdisk.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  8KB  |  297 lines

  1. /*
  2.  * DEFDISK.C
  3.  *
  4.  * A program to make a specified disk/directory the default system disk.
  5.  * It accomplishes this by resetting the DOS library structures to point
  6.  * to the new path. To use this program you will need to compile it as
  7.  * follows:
  8.  *        1> cc defdisk
  9.  *        1> ln defdisk.o -lc
  10.  *
  11.  * Real simple.  I used AZTEC C v3.40a, but I think that 3.30c or later would
  12.  * also work.  It should be possible to compile & build it under LATTICE as
  13.  * well, though I haven't tried.  Once the program is built, put it on your
  14.  * Workbench disk or hard disk somewhere, load up your ram: disk or hard disk
  15.  * and:
  16.  *        1> defdisk dh0:
  17.  *              or
  18.  *        1> defdisk dh0:bin
  19.  *
  20.  * Woila! the standard workbench assignments are now pointed at the hard
  21.  * disk (SYS, C, L, S, DEVS, LIBS, FONTS).  The advantage to me in using this
  22.  * is that I don't have to load the AmigaDOS assign program 7 times to
  23.  * point the system at the hard disk.  I have the following two lines at the
  24.  * beginning of my startup-sequence for the Amiga 2090 disk controller:
  25.  *
  26.  *              binddrivers
  27.  *              dh0:system/defdisk dh0:
  28.  *
  29.  * One final note, be sure that there is a directory for each of the assigns
  30.  * on the target disk (C, L, S, DEVS, LIBS, FONTS).
  31.  *
  32.  * Author: J. K. Levie
  33.  *
  34.  * Version 1.1 24-Jan-1988 - minor corrections to documentation
  35.  * Version 1.0 20-Mar-1986
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include <functions.h>
  40. #include <exec/types.h>
  41. #include <exec/exec.h>
  42. #include <libraries/dos.h>
  43. #include <libraries/dosextens.h>
  44.  
  45. extern void BtoCSTR();            /* forward ... */
  46. extern void CtoBSTR();            /* ... references */
  47.  
  48. struct DevInfo
  49. {
  50.    BPTR di_Next;
  51.    LONG di_Type;
  52.    APTR di_Task;
  53.    BPTR di_Lock;
  54.    BSTR di_Handler;
  55.    LONG di_StackSize;
  56.    LONG di_Priority;
  57.    LONG di_Startup;
  58.    BPTR di_SegList;
  59.    BPTR di_GlobVec;
  60.    BSTR di_Name;
  61. };
  62. struct DosLibrary *dosBase;
  63. struct RootNode *root;
  64. struct DosInfo *info;
  65. struct DevInfo *dev_head, *dev_ele;
  66. struct Lock *lock;
  67.  
  68. /*
  69.  * Define the logical to physical table.
  70.  */
  71. struct ASSIGNS
  72. {
  73.    char *log_name;            /* logical name to look for */
  74.    char *phy_name;            /* physical name to assign */
  75.    short success;            /* result 0=> failure, 1=> success */
  76. };
  77. struct ASSIGNS assigns[]=
  78. {
  79.    "SYS",   NULL,    0,
  80.    "C",     "c",     0,
  81.    "DEVS",  "devs",  0,
  82.    "LIBS",  "libs",  0,
  83.    "L",     "l",     0,
  84.    "S",     "s",     0,
  85.    "FONTS", "fonts", 0,
  86.    NULL,    NULL
  87. };
  88.  
  89.  
  90.  
  91.  
  92. main(argc, argv)
  93. int argc;
  94. char *argv[];
  95. {
  96.    char *ptr, *path;
  97.    register int len, n;
  98.  
  99.    /*
  100.     * See if we can get a lock on the new C directory, if we can't
  101.     * we won't even attempt to do the assigns.
  102.     */
  103.    path=argv[1];
  104.    if(argc!=2 || *path=='?')        /* correct arg cnt?  useage? */
  105.    {
  106.       printf("Usage: defdisk dev:[dir.../dir]\n");
  107.       exit(20);
  108.    }
  109.    /*
  110.     * Now we are ready to fill in the logical name table.  Handle the 
  111.     * Sys logical first since it will not have anything appended.  Then
  112.     * see if we will need to append a path separator to the user
  113.     * specified path.  We stop appending when we see the "T" logical.
  114.     */
  115.    assigns[0].phy_name=path;        /* Sys is special case */
  116.    len=1+strlen(path);            /* find length of physical name */
  117.    if(path[strlen(path)-1]!=':')    /* path specify the root of volume? */
  118.    {
  119.       len++;                /* room for path separator */
  120.       ptr=AllocMem((long)(len+1), MEMF_PUBLIC);
  121.       strcpy(ptr, path);        /* put user specified path in ... */
  122.       strcat(ptr, "/");            /* ... and path separator */
  123.       path=ptr;                /* point to new path */
  124.    }
  125.    /*
  126.     * Now fill in the rest of the table.  Allocate memory for the
  127.     * complete physical name and the directory specified in the table
  128.     * entry.  Then concatenate the path and the directory and set up
  129.     * the new pointer.
  130.     */
  131.    for(n=1; assigns[n].log_name[0]!=NULL; n++)
  132.    {
  133.       ptr=AllocMem((long)(len+strlen(assigns[n].log_name)), MEMF_PUBLIC);
  134.       strcpy(ptr, path);
  135.       strcat(ptr, assigns[n].phy_name);
  136.       assigns[n].phy_name=ptr;
  137.    }
  138.    /*
  139.     * Convert the logical names to B strings.
  140.     */
  141.    for(n=0; assigns[n].log_name!=NULL; n++) CtoBSTR(assigns[n].log_name);
  142.  
  143.    /*
  144.     * Now for a little paranoia.  I don't want to point the system
  145.     * somewhere that doesn't contain a C directory.
  146.     */
  147.    lock=Lock(assigns[1].phy_name, SHARED_LOCK);
  148.    if(lock==NULL)            /* are we sane? */
  149.    {
  150.       /*
  151.        * NO!  Makes no sense to set the system volume to something that
  152.        * doesn't contain a c directory.
  153.        */
  154.       printf("defdisk-F-%s does not contain a C directory\n",
  155.               assigns[1].phy_name);
  156.       exit(20);
  157.    }
  158.    UnLock(lock);            /* Okay unlock the c directory */
  159.  
  160.    /*
  161.     * Now open the resources we will need.
  162.     */
  163.    dosBase=(struct DosLibrary *)OpenLibrary(DOSNAME);
  164.    root=(struct RootNode *)dosBase->dl_Root;
  165.    info=(struct DosInfo *)BADDR(root->rn_Info);
  166.    dev_head=(struct DevInfo *)BADDR(info->di_DevInfo);
  167.  
  168.    Forbid();                /* only us & nobody else */
  169.    /*
  170.     * Walk the DevInfo list looking for the locigal names to change.  For
  171.     * each entry in the list we can check each of the entries in the
  172.     * logical name table.  As we find them reset each logical to point
  173.     * to the new system volume.
  174.     */
  175.    for(dev_ele=dev_head; dev_ele->di_Next;
  176.        dev_ele=(struct DevInfo *)BADDR(dev_ele->di_Next))
  177.    {
  178.       /*
  179.        * Check against each of the logical names in the table.
  180.        */
  181.       for(n=0; assigns[n].log_name!=NULL; n++)
  182.       {
  183.          /*
  184.           * See if this one matches.
  185.           */
  186.          if(Bstrcmp(BADDR(dev_ele->di_Name), assigns[n].log_name))
  187.          {
  188.             /*
  189.              * Name matches, is it the correct type?
  190.              */
  191.             if(dev_ele->di_Type!=DLT_DIRECTORY)
  192.             {
  193.                /*
  194.                 * No put logical name back to a C string and error out.
  195.                 */
  196.                BtoCSTR(assigns[n].log_name);
  197.                printf("defdisk-F-cannot reassign %s\n", assigns[n].log_name);
  198.                goto done;
  199.             }
  200.             /*
  201.              * Okay get a lock on the physical name for this logical.
  202.              */
  203.             lock=Lock(assigns[n].phy_name, SHARED_LOCK);
  204.             if(!lock)
  205.             {
  206.                /*
  207.                 * Whoops, failed somehow, tell the world about it.
  208.                 */
  209.                printf("defdisk-F-cannot get a lock on %s\n", assigns[n].phy_name);
  210.                goto done;
  211.             }
  212.             /*
  213.              * Time to clean up the old logical and point it to our
  214.              * physical name.
  215.              */
  216.             UnLock(dev_ele->di_Lock);
  217.             dev_ele->di_Lock=(BPTR)lock;
  218.             dev_ele->di_Task=(APTR)(DeviceProc(assigns[n].phy_name));
  219.             assigns[n].success=1;    /* flag that we did this one */
  220.          }
  221.       }
  222.    }
  223. done:
  224.    Permit();                /* release the system */
  225.    CloseLibrary(dosBase);        /* clean up */
  226.    
  227.    /*
  228.     * Now check to see that all of the assignments were made.
  229.     */
  230.    for(n=0; assigns[n].log_name!=NULL; n++)
  231.    {
  232.       if(assigns[n].success!=1) printf("defdisk-F-assignments failed\n");
  233.    }
  234. }
  235.  
  236.  
  237.  
  238. /*
  239.  * Bstrcmp(a, b)
  240.  *
  241.  * Compare two btrings for equality, case insensitive.
  242.  */
  243. Bstrcmp(a, b)
  244. char *a, *b;
  245. {
  246.    int len=(int)*a;            /* first char of B string is length */
  247.  
  248.    for(len++; len!=0; len--)        /* compare at most all */
  249.    {
  250.       if((*a&~0x20)!=(*b&~0x20)) return(0); /* quit if not equal */
  251.       a++;                /* point both ... */
  252.       b++;                /* ... at next character */
  253.    }
  254.    return(1);                /* they matched */
  255. }
  256.  
  257.  
  258.  
  259. /*
  260.  * BtoCSTR(bstr)
  261.  *
  262.  * Convert (in place) a B string to a C string.
  263.  */
  264. void BtoCSTR(bstr)
  265. char *bstr;
  266. {
  267.    register int i;
  268.    register UBYTE *bptr, *cptr;
  269.  
  270.    cptr=(UBYTE *)bstr;            /* point to input B string */
  271.    bptr=cptr;                /* output starts at B string len spot */
  272.    for(i=*bptr++; i!=0; i--) *cptr++=*bptr++; /* left shift the string */
  273.    *cptr='\0';                /* terminate it */
  274. }
  275.  
  276.  
  277.  
  278. /*
  279.  * CtoBSTR(cstr)
  280.  *
  281.  * Convert (in place) a C string to a B string.
  282.  */
  283. void CtoBSTR(cstr)
  284. char *cstr;
  285. {
  286.    register int i, len;
  287.    register UBYTE *cptr, *bptr;
  288.  
  289.    cptr=(UBYTE *)cstr;            /* pointer to input */
  290.    for(i=0; *cptr; i++, cptr++);    /* count & push pointer to input */
  291.    len=i;                /* save count */
  292.    bptr=cptr;                /* point at end of input */
  293.    cptr--;                /* back up over null */
  294.    while(i--) *bptr--=*cptr--;        /* right the string */
  295.    *bptr=(UBYTE)len;            /* stuff in the string length */
  296. }
  297.