home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff314.lha / zc / zc.lzh / Examples / Amiga / Pri / Pri.c < prev   
C/C++ Source or Header  |  1988-05-04  |  3KB  |  72 lines

  1. /* PRI, by Ali T. Ozer (ARPA: ali@score.stanford.edu)
  2. ** Written April 1988. Freely distributable.
  3. ** 
  4. ** Usage: pri <pri> <process>
  5. ** 
  6. ** Sets the priority of the specified process to pri. These have to be CLI
  7. ** processes; pri won't change the prioirity of programs started from the WB.
  8. */
  9.  
  10. #include <libraries/dosextens.h>
  11.  
  12. struct DosLibrary *DosBase, *OpenLibrary();
  13. struct FileHandle *Output();
  14.  
  15. #define ILLEGALNUM (-9999)  
  16.  
  17. void Bye (msg) char *msg; {
  18.   struct FileHandle *out;
  19.   if (msg && (out = Output())) Write (out, msg, (long)strlen(msg));
  20.   if (DosBase) CloseLibrary (DosBase);
  21.   exit (msg ? 5 : 0);
  22. }
  23.  
  24. /* Parse a signed integer. Return ILLEGALNUM on parse error.
  25. */
  26. int myatoi (str) char *str; {
  27.   int sign = 1, num = 0;
  28.   if (*str == '-') {str++; sign = -1;} else if (*str == '+') str++;
  29.   while (*str) 
  30.     if (*str < '0' || *str > '9') return (ILLEGALNUM);
  31.     else num = num * 10 - '0' + (*str++);
  32.   return (sign * num);
  33. }
  34.  
  35. main (argc, argv) int argc; char **argv;
  36. {
  37.   int prn, pri;  /* Process number and new desired priority */
  38.   struct RootNode *rn;
  39.   long *lptr;
  40.  
  41.   if (argc == 0) Bye (NULL);  /* Don't run from Workbench */
  42.  
  43.   if (((DosBase = OpenLibrary ("dos.library", 0L)) == NULL) ||
  44.       ((rn = (struct RootNode *)DosBase->dl_Root) == NULL) ||
  45.       ((lptr = (long *)((rn->rn_TaskArray) << 2L)) == NULL)) Bye ("DOS?\n");
  46.  
  47.   /* Now parse the arguments. We need two arguments, first the priority 
  48.   ** (-128..127), and second, the process number (1..Max# of CLIs allowed).
  49.   */
  50.  
  51.   if (argc != 3)
  52.   Bye("Usage: pri <pri> <process>\npri 2 1 sets process 1 to priority 2.\n");
  53.   if ((pri = myatoi(argv[1])) > 127 || pri < -128) Bye ("Bad priority\n");
  54.   prn = myatoi(argv[2]);
  55.  
  56.   /* At this stage, lptr points to AmigaDOS's array of CLIs. The first element
  57.   ** in this array is the maximum number of CLIs allowed. Then follows this
  58.   ** many pointers, which, if not NULL, point to CLI processes. (Actually,
  59.   ** they do not point to the process. A process is simply an Amiga 
  60.   ** Task structure, followed by an Amiga MsgPort structure, and then
  61.   ** followed other stuff. These pointers point to the MsgPort structure.
  62.   ** Thus, to get to the task structure (so we can SetTaskPri()), we'll need
  63.   ** to subtract the size of the task structure from the MsgPort pointer.)
  64.   */
  65.  
  66.   Forbid();
  67.   if ((prn > 0) && ((long)prn <= *lptr) && *(lptr+prn))
  68.     SetTaskPri ((struct Task *)(*(lptr+prn) - sizeof(struct Task)), (long)pri);
  69.     else Bye ("No such process\n");
  70.   Permit();
  71. }
  72.