home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / util / cli / nice.lha / Nice / nice.c next >
Encoding:
C/C++ Source or Header  |  1996-12-27  |  2.3 KB  |  103 lines

  1. //****************************************************************
  2. //
  3. // Name : Nice.c
  4. //
  5. // Date : 20/12/96
  6. //
  7. // Author : Tak Tang
  8. //
  9. // Usage : Nice priority command
  10. //
  11. // Synopsis : Nice runs a command at a different priority.  The
  12. //            default is at 3 below the current.  DOS only.
  13. //
  14. //****************************************************************
  15.  
  16. //******** Definitions
  17.  
  18. #define __USE_SYSBASE
  19.  
  20. #define DOSLIB  "dos.library"
  21. #define DOSVER  36L
  22.  
  23. #define THISPROC   ((struct Process *)(SysBase->ThisTask))
  24. #define Result2(x) THISPROC->pr_Result2 = x
  25.  
  26. #define BUFLEN 256
  27.  
  28. #define MSG_FAILED "Nice failed"
  29.  
  30. #define TEMPLATE  "-S=STICKY/S,PRI=PRIORITY/N/K,ABS=ABSOLUTE/S,CMD=COMMAND/F"
  31. #define OPT_STICK 0
  32. #define OPT_PRI   1
  33. #define OPT_ABS   2
  34. #define OPT_CMD   3
  35. #define OPT_COUNT 4
  36.  
  37.  
  38. //******** Header files
  39.  
  40. #include <dos/dos.h>
  41. #include <dos/dostags.h>
  42. #include <dos/rdargs.h>
  43. #include <dos/dosextens.h>
  44.  
  45. #include <exec/types.h>
  46. #include <exec/execbase.h>
  47. #include <exec/memory.h>
  48.  
  49. #include <string.h>
  50.  
  51. #include <proto/dos.h>
  52. #include <proto/exec.h>
  53.  
  54.  
  55. //******** Storage
  56.  
  57. UBYTE VersTag[]="\0$VER: Nice 1.0 (20/12/96)";
  58.  
  59.  
  60. //******** Main entry
  61.  
  62. int cmd_nice(void) {
  63.    struct ExecBase *SysBase = (*((struct ExecBase **) 4));
  64.    struct DosLibrary *DOSBase;
  65.    struct RDArgs *rdargs;
  66.    struct Task *task;
  67.    LONG opts[OPT_COUNT];
  68.    LONG rc, newpri, oldpri;
  69.  
  70.    rc = RETURN_FAIL;
  71.  
  72.    if ((DOSBase = (struct DosLibrary *)OpenLibrary(DOSLIB, DOSVER))) {
  73.      memset((char *)opts, 0, sizeof(opts));
  74.      rdargs = ReadArgs(TEMPLATE, opts, NULL);
  75.      if (rdargs == NULL) {
  76.        PrintFault(IoErr(), NULL);
  77.      } else {
  78.        if (opts[OPT_PRI]) newpri= *(LONG *)opts[OPT_PRI];
  79.        else               newpri= -3;
  80.        task=FindTask(NULL);
  81.        if ( opts[OPT_ABS] == NULL) newpri+=task->tc_Node.ln_Pri;
  82.        if (newpri < -128) newpri= -128;
  83.        if (newpri >  127) newpri=  127;
  84.        oldpri=SetTaskPri(task,newpri);
  85.  
  86.        if ( opts[OPT_CMD] ) {
  87.      rc= SystemTags( (char *)opts[OPT_CMD], SYS_Input, Input(),
  88.         SYS_Output, Output(), TAG_DONE);
  89.        }
  90.  
  91.        if ( opts[OPT_STICK] == NULL ) SetTaskPri(task,oldpri);
  92.        FreeArgs(rdargs);
  93.      } // if rdargs
  94.      CloseLibrary((struct Library *)DOSBase);
  95.    } else {
  96.      Result2(ERROR_INVALID_RESIDENT_LIBRARY);
  97.    } // if open library(dos)
  98.    return(rc);
  99. } // cmd_nice
  100.  
  101. //******** End of file
  102.  
  103.