home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 640b.lha / HardblocksLibrary_v1.1 / src / tool.c < prev   
C/C++ Source or Header  |  1992-05-14  |  8KB  |  282 lines

  1. /* $Revision Header *** Header built automatically - do not edit! ***********
  2.  *
  3.  *    (C) Copyright 1992 by Torsten Jürgeleit
  4.  *
  5.  *    Name .....: tool.c
  6.  *    Created ..: Wednesday 19-Feb-92 10:05:27
  7.  *    Revision .: 2
  8.  *
  9.  *    Date        Author                 Comment
  10.  *    =========   ====================   ====================
  11.  *    27-Apr-92   Torsten Jürgeleit      add no library option
  12.  *    11-Mar-92   Torsten Jürgeleit      argv[ARGUMENT_UNIT] is a (BYTE *)
  13.  *    19-Feb-92   Torsten Jürgeleit      Created this file!
  14.  *
  15.  ****************************************************************************
  16.  *
  17.  *    Tool for manipulating hardblocks
  18.  *
  19.  * $Revision Header ********************************************************/
  20.  
  21.     /* Includes */
  22.  
  23. #include <exec/types.h>
  24. #include <devices/hardblocks.h>
  25. #include <libraries/arpbase.h>
  26. #include <functions.h>
  27. #include <stdlib.h>
  28. #ifndef LINK_TEST
  29. #include "hardblocks.h"
  30. #else LINK_TEST
  31. #include "hardblocks_link.h"
  32. #endif LINK_TEST
  33.  
  34.     /* Defines */
  35.  
  36. #define DEFAULT_DEVICE        "scsi.device"
  37. #define DEFAULT_UNIT        "0"
  38.  
  39. #define MAX_ARGUMENTS        11
  40.  
  41. #define ARGUMENT_DEVICE        0
  42. #define ARGUMENT_UNIT        1
  43. #define ARGUMENT_FILE        2
  44. #define ARGUMENT_LOAD        3
  45. #define ARGUMENT_RESTORE    4
  46. #define ARGUMENT_DEFAULT    5
  47. #define ARGUMENT_SHOW        6
  48. #define ARGUMENT_OUTPUT        7
  49. #define ARGUMENT_SAVE        8
  50. #define ARGUMENT_BACKUP        9
  51. #define ARGUMENT_REMOVE        10
  52.  
  53.     /* Externals */
  54.  
  55. IMPORT struct DOSBase   *DOSBase;
  56.  
  57.     /* Globals */
  58.  
  59. struct ArpBase    *ArpBase;
  60. #ifndef LINK_TEST
  61. struct Library    *HardBlocksBase;
  62. #endif LINK_TEST
  63.  
  64. BYTE template[]  = "Device,Unit,FILE/k,LOAD/s,RESTORE/s,DEFAULT/s,SHOW/s,"
  65.            "OUTPUT/k,SAVE/s,BACKUP/s,REMOVE/s",
  66.      xtra_help[] = "HBtool v1.1 - Copyright © 1992 Torsten Jürgeleit\n\n"
  67.            "Usage: HBtool [Device] [Unit] [FILE name] [LOAD]"
  68.            " [RESTORE] [DEFAULT] [SHOW] [OUTPUT file] [SAVE]"
  69.            " [BACKUP] [REMOVE]\n"
  70.            "\t[Device]      = device name (default: scsi.device)\n"
  71.            "\t[Unit]        = device unit num (default 0)\n"
  72.            "\t[FILE name]   = file to restore/save hardblocks\n"
  73.            "\t[LOAD]        = load hardblocks from device (default)\n"
  74.            "\t[RESTORE]     = restore hardblocks from file\n"
  75.            "\t[DEFAULT]     = create standard rigid disk block\n"
  76.            "\t[SHOW]        = show currently loaded hardblocks (default)\n"
  77.            "\t[OUTPUT file] = output file for show (default: NULL -> stdout)\n"
  78.            "\t[SAVE]        = save currently loaded hardblocks to device\n"
  79.            "\t[BACKUP]      = backup currently loaded hardblocks to file\n"
  80.            "\t[REMOVE]      = delete rigid disk block from device";
  81.  
  82.     /* Prototypes */
  83.  
  84. LONG _main(LONG alen, BYTE *aptr);
  85. BOOL safety_check(BYTE *device, ULONG unit);
  86.  
  87.     /* Main routine - no startup code */
  88.  
  89.    LONG
  90. _main(LONG alen, BYTE *aptr)
  91. {
  92.    LONG return_code = RETURN_FAIL;
  93.  
  94.    /* First open ARP library */
  95.    if (!(ArpBase = OpenLibrary(ArpName, ArpVersion))) {
  96.       Write(Output(), "Need ARP library V39+\n", 22L);
  97.    } else {
  98. #ifndef LINK_TEST
  99.       if (!(HardBlocksBase = OpenLibrary(HardBlocksName,
  100.                               HardBlocksVersion))) {
  101.      Puts("Need hardblocks library");
  102.       } else {
  103. #endif LINK_TEST
  104.      BYTE   *argv[MAX_ARGUMENTS];
  105.      USHORT i;
  106.  
  107.      /* Clear argument array */
  108.      for (i = 0; i < MAX_ARGUMENTS; i++) {
  109.         argv[i] = NULL;
  110.      }
  111.  
  112.      /* Parse command line arguments */
  113.      if (GADS(aptr, alen, &xtra_help[0], &argv[0], &template[0]) < 0) {
  114.         Puts(argv[0]);
  115.      } else {
  116.         struct RigidDiskBlock  rdb;
  117.         BPTR   fh;
  118.         BYTE   *device, *file = argv[ARGUMENT_FILE];
  119.         ULONG  unit;
  120.         USHORT error;
  121.  
  122.         /* Install default arguments if not present */
  123.         if (!argv[ARGUMENT_DEVICE]) {
  124.            argv[ARGUMENT_DEVICE] = DEFAULT_DEVICE;
  125.         }
  126.         device = argv[ARGUMENT_DEVICE];
  127.         if (!argv[ARGUMENT_UNIT]) {
  128.            argv[ARGUMENT_UNIT] = DEFAULT_UNIT;
  129.         }
  130.         unit = Atol(argv[ARGUMENT_UNIT]);
  131.         if (!argv[ARGUMENT_LOAD] && !argv[ARGUMENT_RESTORE] &&
  132.             !argv[ARGUMENT_DEFAULT] && !argv[ARGUMENT_REMOVE]) {
  133.            argv[ARGUMENT_LOAD] = (BYTE *)-1L;
  134.         }
  135.         if (!argv[ARGUMENT_SHOW] && !argv[ARGUMENT_SAVE] &&
  136.              !argv[ARGUMENT_BACKUP] && !argv[ARGUMENT_REMOVE]) {
  137.            argv[ARGUMENT_SHOW] = (BYTE *)-1L;
  138.         }
  139.  
  140.         /* Get output file handle for show */
  141.         if (argv[ARGUMENT_SHOW]) {
  142.            BYTE *output = argv[ARGUMENT_OUTPUT];
  143.  
  144.            if (!output) {
  145.           fh = Output();
  146.            } else {
  147.           if (!(fh = Open(output, (LONG)MODE_NEWFILE))) {
  148.              Printf("Can't open '%s'\n", output);
  149.              error = HBERR_FILE_OPEN_FAILED;
  150.           }
  151.            }
  152.         }
  153.  
  154.         /* Load hardblocks from device */
  155.         if (!error && argv[ARGUMENT_LOAD]) {
  156.            Printf("Loading hardblocks from unit %ld of `%s'\n", unit,
  157.                                     device);
  158.            if (!(error = LoadHardBlocks(&rdb, device, unit))) {
  159.  
  160.           /* Now play with hardblocks data */
  161.           if (argv[ARGUMENT_SHOW]) {
  162.              error = PrintHardBlocks(&rdb, fh);
  163.           }
  164.           if (!error && argv[ARGUMENT_BACKUP]) {
  165.              if (!file) {
  166.             Printf("No backup file name\n");
  167.             error = HBERR_FILE_OPEN_FAILED;
  168.              } else {
  169.             Printf("Backup hardblocks to `%s'\n", file);
  170.             error = BackupHardBlocks(&rdb, file);
  171.              }
  172.           }
  173.           FreeHardBlocks(&rdb);
  174.            }
  175.         }
  176.         
  177.         /* Restore hardblocks from file */
  178.         if (!error && argv[ARGUMENT_RESTORE]) {
  179.            if (!file) {
  180.           Printf("No restore file name\n");
  181.           error = HBERR_FILE_OPEN_FAILED;
  182.            } else {
  183.           Printf("Restoring hardblocks from `%s'\n", file);
  184.           if (!(error = RestoreHardBlocks(&rdb, file))) {
  185.  
  186.              /* Now play with hardblocks data */
  187.              if (argv[ARGUMENT_SHOW]) {
  188.             error = PrintHardBlocks(&rdb, fh);
  189.              }
  190.              if (!error && argv[ARGUMENT_SAVE]) {
  191.             Printf("Saving hardblocks to unit %ld of"
  192.                            " `%s'\n", unit, device);
  193.             if (safety_check(device, unit) == TRUE) {
  194.                error = SaveHardBlocks(&rdb, device, unit);
  195.             }
  196.              }
  197.              FreeHardBlocks(&rdb);
  198.           }
  199.            }
  200.         }
  201.  
  202.         /* Create standard rigid disk block */
  203.         if (!error && argv[ARGUMENT_DEFAULT]) {
  204.            Printf("Creating standard rigid disk block for unit %ld"
  205.                         " of `%s'\n", unit, device);
  206.            if (!(error = InitRigidDiskBlock(&rdb, device, unit))) {
  207.  
  208.           /* Now play with hardblocks data */
  209.           if (argv[ARGUMENT_SHOW]) {
  210.              error = PrintHardBlocks(&rdb, fh);
  211.           }
  212.           if (!error && argv[ARGUMENT_SAVE]) {
  213.              Printf("Saving hardblocks to unit %ld of `%s'\n", unit,
  214.                                     device);
  215.              if (safety_check(device, unit) == TRUE) {
  216.             error = SaveHardBlocks(&rdb, device, unit);
  217.              }
  218.           }
  219.           if (!error && argv[ARGUMENT_BACKUP]) {
  220.              if (!file) {
  221.             Printf("No backup file\n");
  222.             error = HBERR_FILE_OPEN_FAILED;
  223.              } else {
  224.             Printf("Backup hardblocks to `%s'\n", file);
  225.             error = BackupHardBlocks(&rdb, file);
  226.              }
  227.           }
  228.           FreeHardBlocks(&rdb);
  229.            }
  230.         }
  231.  
  232.         /* Delete rigid disk block */
  233.         if (!error && argv[ARGUMENT_REMOVE]) {
  234.            Printf("Removing hardblocks from unit %ld of `%s'\n", unit,
  235.                                     device);
  236.            if (safety_check(device, unit) == TRUE) {
  237.           error = RemoveHardBlocks(device, unit);
  238.            }
  239.         }
  240.  
  241.         /* Close output file handle opened for show */
  242.         if (argv[ARGUMENT_SHOW] && argv[ARGUMENT_OUTPUT]) {
  243.            Close(fh);
  244.         }
  245.  
  246.         /* Print error msg */
  247.         if (error) {
  248.            Printf("Error: primary=%d   secondary=%ld\n", error, IoErr());
  249.         } else {
  250.            return_code = RETURN_OK;
  251.         }
  252.      }
  253. #ifndef LINK_TEST
  254.      CloseLibrary(HardBlocksBase);
  255.       }
  256. #endif LINK_TEST
  257.       CloseLibrary(ArpBase);
  258.    }
  259.  
  260.    /* MANX crt0.asm forget to close DOS library, so we have to do it */
  261.    CloseLibrary(DOSBase);
  262.    return(return_code);
  263. }
  264.     /* Safety check before any writing to device unit */
  265.  
  266.    STATIC BOOL
  267. safety_check(BYTE *device, ULONG unit)
  268. {
  269.    BYTE buffer[MaxInputBuf];
  270.    BOOL answer = FALSE;
  271.  
  272.    Printf("DANGER: Do you really want to change hardblocks on unit %ld of\n"
  273.       "        device `%s' (YES|NO)? ", unit, device);
  274.    ReadLine(&buffer[0]);
  275.    if (strcmp(&buffer[0], "YES")) {
  276.       Puts("Change aborted");
  277.    } else {
  278.       answer = TRUE;
  279.    }
  280.    return(answer);
  281. }
  282.