/***************************************************************/
/*                                                             */
/*                Device Renamer v1.5 21.8.1990                */
/*                                                             */
/*                  written by Stefan Rosewig                  */
/*                                                             */
/*                       PUBLIC DOMAIN                         */
/*                                                             */
/***************************************************************/
/*                                                             */
/*      Aufrufe :      cc devren                               */
/*                     ln devren.o -lc                         */
/*                                                             */
/***************************************************************/

      /*   Was gebraucht wird   */

#include <exec/types.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <libraries/filehandler.h>

#define STR(x) (((char *)BADDR(x))+1)

extern struct DosLibrary *DOSBase;
struct DeviceNode *DeviceNode;
struct RootNode *RootNode;
struct DosInfo *DosInfo;

      /*   Gebrauchsanleitung   */

usage()
{
   printf("DevRen v1.5 by STR\n");
   printf("USAGE: DevRen <old> <new>   to rename devices\n");
   printf("       DevRen -l            to list devices\n");
   exit();
}

      /*   Fehlermeldungen   */

fault(f,fehl)
USHORT f;
char *fehl;
{
   printf("ERROR!!! %d:   ",f);
   switch (f)
   {
      case 1:
         printf("unknown parameter %s\n",fehl);
      break;
      case 2:
         printf("can't find old device %s:\n",fehl);
      break;
      case 3:
         printf("device %s: already exists\n",fehl);
      break;
      case 4:
         printf("<old> and <new> MUST have the same length\n");
      break;
      case 5:
         printf("too many parameters\n");
   }
   exit();
}

      /*   Suchen eines Devices   */

find(DeviceName)
char *DeviceName;
{
   DeviceNode = (struct DeviceNode *)BADDR(DosInfo->di_DevInfo);
   do
   {
      if (DeviceNode->dn_Type == (ULONG)DLT_DEVICE)          /*   Device ?   */
      {
         if (strcmp(STR(DeviceNode->dn_Name),DeviceName) == 0)
            return(TRUE);                              /*  richtiger Name ?  */
      }
      DeviceNode = (struct DeviceNode *)BADDR(DeviceNode->dn_Next);
   }
   while (DeviceNode != 0L);                           /*   nicht gefunden   */
   return (FALSE);
}

      /*   Auflisten bekannter devices   */

liste()
{
   REGISTER USHORT x;
   x = 0;
   DeviceNode = (struct DeviceNode *)BADDR(DosInfo->di_DevInfo);
   do
   {
      if (DeviceNode->dn_Type == (ULONG)DLT_DEVICE)
      {
         printf(" %s:   ",STR(DeviceNode->dn_Name));
         x++;
         if (x == 7)
         {
            printf("\n");
            x = 0;
         }
      }
      DeviceNode = (struct DeviceNode *)BADDR(DeviceNode->dn_Next);
   }
   while(DeviceNode != 0L);
   printf("\n");
   exit();
}

main(argc,argv)
int argc;
char *argv[];
{
   REGISTER int l1,l2,x;
   USHORT fehler;
   char *fehlstr;
   RootNode = (struct RootNode *)DOSBase->dl_Root;
   DosInfo  = (struct DosInfo  *)BADDR(RootNode->rn_Info);
   switch(argc)
   {
      case 1:
         usage();
      case 2:
         if (strcmp(argv[1],"?")== 0) usage();
         if ((strcmp(argv[1],"-l") == 0) || (strcmp(argv[1],"-L") == 0))
            liste();
         fault (1,argv[1]);
      case 3:
         l1 = strlen(argv[1]);
         l2 = strlen(argv[2]);
         if ( l1 != l2 ) fault(4,"");                /*  erlaubter Name ?  */
         for (x = 0 ; x <= l1 ; x++)                 /*   Großbuchstaben   */
         {
            argv[1][x] = toupper(argv[1][x]);
            argv[2][x] = toupper(argv[2][x]);
         }
         if (argv[1][l1-1] == ':') argv[1][l1-1] = '\0';   /*   ':' muß    */
         if (argv[2][l2-1] == ':') argv[2][l2-1] = '\0';   /*  nicht sein  */
         if (find(argv[2])) fault(3,argv[2]);    /* Device existiert schon */
         if (!find(argv[1])) fault(2,argv[1]);   /* Device existiert nicht */
         strcpy(STR(DeviceNode->dn_Name),argv[2]);    /*   Umbenennen   */
         exit();
      default :
         fault(5,"");                                  /*   Eingabefeher   */
   }
}
