home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_33.arc / ROS-PCB.ARC / TROSKMS.INC < prev    next >
Text File  |  1986-10-23  |  10KB  |  329 lines

  1. { TROSKMS.INC - Remote Operating System Kernel - Miscellaneous routines }
  2.  
  3. { THIS FILE ALTERED FOR TRANSFER OF ROS DATA TO PCBOARD FORMAT. THE
  4.   FUNCTION FormTAD ONLY WAS CHANGED. }
  5.  
  6. procedure SetSect(Drive, User: integer);
  7. { Set to file section }
  8.   begin
  9.     BDOS(seldrive, Drive);
  10.     BDOS(getseluser, User)
  11.   end;
  12.  
  13. procedure FindSect(req: FileName; var Drive, User: integer; var found: boolean);
  14. { Find file section from requested name }
  15.   var
  16.     this: SectPtr;
  17.   begin
  18.     this := SectBase;
  19.     while (req <> this^.SectName) and (this <> nil) do
  20.       this := this^.next;
  21.     found := ((req = this^.SectName) and (cold or (user_rec.access >= this^.SectAccs)));
  22.     if found
  23.       then
  24.         begin
  25.           Drive := this^.SectDrive;
  26.           User := this^.SectUser
  27.         end
  28.   end;
  29.  
  30. function diskfree: integer;
  31. { Compute amount of disk space free on current drive }
  32.   type
  33.     param =
  34.       record
  35.         spt: integer;
  36.         bsh, blm, exm: byte;
  37.         dsm, drm, al, cks, off: integer
  38.       end;
  39.   var
  40.     allocptr, reserved, blocksize, disksize, i: integer;
  41.     dpbptr: ^param;
  42.   begin
  43.     allocptr := BDOSHL(getallocvec, 0);
  44.     dpbptr   := ptr(BDOSHL(getdiskparm, 0));
  45.     with dpbptr^ do
  46.       begin
  47.         reserved := 0;
  48.         for i := 0 to 15 do
  49.           reserved := reserved + (al shr i) and 1;
  50.         disksize := succ(dsm) - reserved;
  51.         for i := reserved to dsm do
  52.           disksize := disksize - (((mem[allocptr + i shr 3] shl (i mod 8)) and $80) shr 7);
  53.         blocksize := 1 shl (bsh - 3)
  54.       end;
  55.     diskfree := disksize * blocksize
  56.   end;
  57.  
  58. procedure hide_release(name: FileName; status: record_status);
  59. { Hide or release file }
  60.   var
  61.     i: integer;
  62.     temp_file: file;
  63.   begin
  64.     Assign(temp_file, name);
  65.     i := pos('.', name) + 2;
  66.     if status = public
  67.       then name[i] := chr($7F and ord(name[i]))  { Turn $SYS bit off }
  68.       else name[i] := chr($80 or ord(name[i]));  { Turn $SYS bit on }
  69.     {$I-} Rename(temp_file, name) {$I+};
  70.     if IOresult <> 0
  71.       then writeln(USR, name, ' not found.')
  72.   end;
  73.  
  74. function min(x, y: integer): integer;
  75. { Return minimum of two integers }
  76.   begin
  77.     if x < y
  78.       then min := x
  79.       else min := y
  80.   end;
  81.  
  82. function max(x, y: integer): integer;
  83. { Return greater of two integers }
  84.   begin
  85.     if x > y
  86.       then max := x
  87.       else max := y
  88.   end;
  89.  
  90. function trim(st: StrStd): StrStd;
  91. { Remove leading and trailing blanks }
  92.   var
  93.    i, j: integer;
  94.   begin
  95.     i := 1;
  96.     j := length(st);
  97.     while (st[i] = ' ') and (i <= j) do
  98.       i := succ(i);
  99.     while (st[j] = ' ') and (j >= i) do
  100.       j := pred(j);
  101.     trim := copy(st, i, succ(j - i))
  102.   end;
  103.  
  104. function pad(st: StrStd; i: integer): StrStd;
  105. { Pad string with spaces to length of i }
  106.   begin
  107.     while length(st) < i do
  108.       st := st + ' ';
  109.     pad := st
  110.   end;
  111.  
  112. function intstr(n, w: integer): Str10;
  113. { Return a string value (width 'w')for the input integer ('n') }
  114.   var
  115.     st: Str10;
  116.   begin
  117.     str(n:w, st);
  118.     intstr := st
  119.   end;
  120.  
  121. function strint(st: Str10): integer;
  122. { Convert string to integer }
  123.   var
  124.     x, code: integer;
  125.   begin
  126.     if st[1] = '+'
  127.       then delete(st, 1, 1);
  128.     if st = ''
  129.       then code := 1
  130.       else val(st, x, code);
  131.     if code = 0
  132.       then strint := x
  133.       else strint := 0                      { Error, return with 0 }
  134.   end;
  135.  
  136. function zeller(day, month, year: integer): integer;
  137. { Compute the day of the week using Zeller's Congruence }
  138.   var
  139.     century: integer;
  140.   begin
  141.     if month > 2
  142.       then month := month - 2
  143.       else
  144.         begin
  145.           month := month + 10;
  146.           year := pred(year)
  147.         end;
  148.     century := year div 100;
  149.     year := year mod 100;
  150.     zeller := (day - 1 + ((13 * month - 1) div 5) + (5 * year div 4) +
  151.               century div 4 - 2 * century + 1) mod 7
  152.   end;
  153.  
  154. function FormTAD(t: tad_array): StrTAD;
  155. { Build printable string of current time and date }
  156.   const
  157.     day: array [0..6] of string[6] =
  158.       ('   Sun','   Mon','  Tues','Wednes',' Thurs',
  159.        '   Fri',' Satur');
  160.     month: array [1..12] of string[2] =
  161.       ('01','02','03','04','05','06','07','08','09','10','11','12');
  162.   var
  163.     i: integer;
  164.     line: StrTAD;
  165.   begin
  166.     if (t[1] in [0..59]) and (t[2] in [0..23])
  167.       then line := intstr(t[2], 2) + ':' + intstr(t[1], 2)
  168.       else line := '00:00';
  169.     if (t[3] in [1..31]) and (t[4] in [1..12]) and (t[5] in [0..99])
  170.       then FormTAD :=
  171.         line +
  172.         intstr(t[4], 2) + '-' + intstr(t[3], 2) + '-' + intstr(t[5], 2)
  173.       else FormTAD := 'No Date'
  174.   end;
  175.  
  176. procedure send_time(size: integer; var mm, ss: integer);
  177. { Compute the file transfer time }
  178.   var
  179.     tr_time: real;
  180.   begin
  181.     tr_time := size * 23.5 / rate;          { Factor is empirically derived  }
  182.     mm := trunc(tr_time);
  183.     ss := round(60.0 * frac(tr_time))
  184.   end;
  185.  
  186. procedure timer(var time_on, time_left: integer);
  187. { Compute the time on and the time remaining to the current user }
  188.   var
  189.     t: tad_array;
  190.   begin
  191.     GtTAD(t);
  192.     time_on := 60 * (t[2] - login_t[2]) + t[1] - login_t[1];
  193.     if time_on < 0
  194.       then time_on := time_on + 1440;
  195.     time_left := user_rec.limit + extra_time - user_rec.time_today - time_on
  196.   end;
  197.  
  198. procedure log(activity: byte; text: FileName);
  199. { Update log file }
  200.   begin
  201.     seek(logr_file, FileSize(logr_file));
  202.     GtTAD(logr_rec.date);
  203.     logr_rec.action := activity;
  204.     logr_rec.user := user_loc;
  205.     logr_rec.text := text;
  206.     write(logr_file, logr_rec)
  207.   end;
  208.  
  209. procedure mesg_insert(TypMsg: byte);
  210. { Insert message into linked list }
  211.   var
  212.     this: MesgPtr;
  213.   begin
  214.     new(this);
  215.     if MesgBase = nil
  216.       then MesgBase := this
  217.       else MesgLast^.next := this;
  218.     MesgLast := this;
  219.     MesgLast^.MesgNo := summ_rec.num;
  220.     MesgLast^.SummLoc := pred(FilePos(summ_file));
  221.     MesgLast^.TypMsg := TypMsg;
  222.     MesgLast^.next := nil
  223.   end;
  224.  
  225. procedure InsertFile(fname: name_array; index, size: integer;
  226.                      var entries, total: integer; var first: FilePtr);
  227. { Insert a new file name into an alphabetic list }
  228.   var
  229.     space: integer;
  230.     f,                                      { File name entry being created }
  231.     this, last: FilePtr;                    { Followers for insertion }
  232.     fn: FileName;
  233.   begin
  234.     fn := '           ';                    { Initialize string }
  235.     move(fname, fn[1], 11);                 { Move name into place }
  236.     insert('.', fn, 9);
  237.     last := nil;
  238.     this := first;
  239.     while (this <> nil) and (this^.fname < fn) do
  240.       begin
  241.         last := this;
  242.         this := this^.next
  243.       end;
  244.     space := size shr 3;
  245.     if (size mod 8) <> 0
  246.       then space := succ(space);
  247.     if this^.fname <> fn
  248.       then
  249.         begin
  250.           entries := succ(entries);
  251.           total := total + space;
  252.           new(f);
  253.           f^.fname := fn;
  254.           f^.index := index;
  255.           f^.fsize := size;
  256.           f^.next  := this;
  257.           if last = nil
  258.             then first := f
  259.             else last^.next := f
  260.         end
  261.     else if (this^.fname = fn) and (this^.fsize < size)
  262.       then
  263.         begin
  264.           total := total + space;
  265.           space := this^.fsize shr 3;
  266.           if (this^.fsize mod 8) <> 0
  267.             then space := succ(space);
  268.           total := total - space;
  269.           this^.fsize := size
  270.         end
  271.   end;
  272.  
  273. { Notes on updcrc:
  274.  
  275.    Purists that want ROS to be written COMPLETELY in Pascal, should use the
  276.    Pascal version, but it is slower than the inline code version.  The inline
  277.    code version is, of course, Z-80 specific, but it is MUCH faster.
  278.  
  279.    The two procedures are functionally equivalent - simply comment out the
  280.    procedure you don't want to use.
  281. }
  282.  
  283. (*
  284. procedure updcrc(var crc: integer; acc: integer);
  285. { Update CRC with passed value }
  286.     var
  287.       carry: boolean;
  288.       i: integer;
  289.     begin
  290.       for i := 1 to 8 do
  291.         begin
  292.           carry := ((crc and $8000) <> 0);
  293.           crc := crc shl 1;
  294.           if (acc and $0080) <> 0
  295.             then crc := succ(crc);
  296.           acc := acc shl 1;
  297.           if carry
  298.             then crc := crc xor $1021       { Use $8005 for CRC-16 }
  299.         end
  300.     end;
  301. *)
  302.  
  303. procedure updcrc(var crc: integer; acc: integer);
  304. { Update CRC with passed value }
  305.   begin
  306.     inline($2A/crc/       {         LD      HL,(crc)    ; point to crc    }
  307.            $5E/           {         LD      E,(HL)      ; put crc into DE }
  308.            $23/           {         INC     HL          ;                 }
  309.            $56/           {         LD      D,(HL)      ;                 }
  310.            $EB/           {         EX      DE,HL       ; put it into HL  }
  311.            $ED/$4B/acc/   {         LD      BC,(acc)    ; get acc into C  }
  312.            $06/$08/       {         LD      B,8         ; shift 8 times   }
  313.            $CB/$01/       { UPDLP:  RLC     C           ; shift input     }
  314.            $ED/$6A/       {         ADC     HL,HL       ; shift crc       }
  315.            $30/$08/       {         JR      NC,SKIPIT   ; jump if no carry}
  316.            $7C/           {         LD      A,H         ; xor with $1021  }
  317.            $EE/$10/       {         XOR     10H         ; use $8005 for   }
  318.            $67/           {         LD      H,A         ;   CRC-16        }
  319.            $7D/           {         LD      A,L         ;                 }
  320.            $EE/$21/       {         XOR     21H         ;                 }
  321.            $6F/           {         LD      L,A         ;                 }
  322.            $10/$F0/       { SKIPIT: DJNZ    UPDLP       ; done?           }
  323.            $EB/           {         EX      DE,HL       ; result to DE    }
  324.            $72/           {         LD      (HL),E      ; then into       }
  325.            $2B/           {         DEC     HL          ;   into          }
  326.            $73)           {         LD      (HL),D      ;     memory      }
  327. end;
  328.  
  329.