home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / nonvolatile / SetNVProtection.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-31  |  3.1 KB  |  138 lines

  1. /* SetNVProtection.c
  2.  * 
  3.  * 
  4.  * 
  5.  * 
  6.  */
  7.  
  8. /* Includes --------------------------------------------- */
  9. #include <exec/types.h>
  10. #include <exec/libraries.h>
  11. #include <exec/memory.h>
  12. #include <dos/dos.h>
  13. #include <dos/rdargs.h>
  14. #include <libraries/nonvolatile.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/dos_protos.h>
  17. #include <clib/nonvolatile_protos.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. #include <pragmas/nonvolatile_pragmas.h>
  22. #include "nv.h"
  23.  
  24. /* Defines ------------------------------------------ */
  25. #define PROGNAME "SetNVProtection"
  26. #define ERRMSG_LIBNOOPEN "Couldn't open %s V%ld (or better)!\n"
  27. #define TEMPLATE "NAME=APPNAME/K,ITEM=ITEMNAME/K,FLAGS=HEXFLAGS/K,DELETE/S,NN=NULLNAME/S,NI=NULLITEM/S,KILLREQ/S"
  28.  
  29.  
  30. /* Structs ------------------------------ */
  31. struct Opts {
  32.     STRPTR name;
  33.     STRPTR item;
  34.     STRPTR flags;
  35.     LONG   delete;
  36.     LONG   nullName;
  37.     LONG   nullItem;
  38.     LONG   killReq;
  39. };
  40.  
  41. /* Protos ------------------------------------------ */
  42. static VOID GoodBye(int);
  43. static LONG doInit(VOID);
  44. extern BOOL xStrToULong(STRPTR, ULONG *);
  45.  
  46. /* Globals --------------------------------------- */
  47. struct Opts     opts;
  48. struct RDArgs  *rdargs;
  49. struct Library *NVBase;
  50.  
  51.  
  52. VOID main(int argc, UBYTE *argv[]) {
  53.     STRPTR name, item;
  54.     LONG flags=0UL;
  55.     BOOL snvErr, killReq;
  56.     ULONG hexNo=0;
  57.     int rc = RETURN_OK;
  58.  
  59.     if (!doInit()) {
  60.         GoodBye(RETURN_FAIL);
  61.     }
  62.  
  63.     name = ((opts.name) ? opts.name : (STRPTR)DEFAULT_APP_NAME);
  64.     item = ((opts.item) ? opts.item : (STRPTR)DEFAULT_ITEM_NAME);
  65.     killReq = ((opts.killReq) ? TRUE : FALSE);
  66.  
  67.     if (opts.nullName) {
  68.         name = NULL;
  69.     }
  70.     if (opts.nullItem) {
  71.         item = NULL;
  72.     }
  73.     if (opts.delete) {
  74.         flags = NVEF_DELETE;
  75.     }
  76.  
  77.     if (opts.flags) {
  78.         if (!xStrToULong(opts.flags, &hexNo)) {
  79.             Printf("Bad hex number: '%s'!\n", opts.flags);
  80.             GoodBye(RETURN_FAIL);
  81.         }
  82.         flags |= hexNo;
  83.     }
  84.  
  85.     Printf("Calling SetNVProtection('%s', '%s', $%08lx)...\n",
  86.             name, item, flags);
  87.  
  88.     snvErr = SetNVProtection(name, item, flags, killReq);
  89.     if (snvErr == FALSE) {
  90.         PrintFault(IoErr(), PROGNAME);
  91.         Printf("SetNVProtection() returned failure, %ld!\n", (LONG)snvErr);
  92.         rc = RETURN_ERROR;
  93.     }
  94.     else {
  95.         PutStr("SetNVProtection() returned success.\n");
  96.     }
  97.  
  98.     GoodBye(rc);
  99. }
  100.  
  101. /* GoodBye ===============================================
  102.    Clean-exit routine.
  103.  */
  104. static VOID GoodBye(int rc) {
  105.  
  106.     if (NVBase) {
  107.         CloseLibrary(NVBase);
  108.     }
  109.  
  110.     if (rdargs) {
  111.         FreeArgs(rdargs);
  112.     }
  113.  
  114.     exit(rc);
  115. }
  116.  
  117. /* doInit =============================================
  118.  * Open libraries, call ReadArgs() if necessary.
  119.  * Returns TRUE for success, FALSE otherwise.
  120.  */
  121. static LONG doInit(VOID) {
  122.  
  123.     rdargs = ReadArgs(TEMPLATE, (LONG *)&opts, NULL);
  124.     if (!rdargs) {
  125.         PrintFault(IoErr(), PROGNAME);
  126.         return(FALSE);
  127.     }
  128.  
  129.     NVBase = OpenLibrary("nonvolatile.library", 40L);
  130.     if (!NVBase) {
  131.         Printf(ERRMSG_LIBNOOPEN, "nonvolatile.library", 40L);
  132.         return(FALSE);
  133.     }
  134.  
  135.     return(TRUE);
  136. }
  137.  
  138.