home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / tbsource.lha / TBSource / Logger / Uptime.S < prev    next >
Text File  |  1993-12-21  |  7KB  |  402 lines

  1. *************************************************
  2. *                        *
  3. *        (C)opyright 1992-93        *
  4. *                        *
  5. *         by Tomi Blinnikka        *
  6. *                        *
  7. *    Don´t try to understand the code    *
  8. *                        *
  9. * Version 0.01    05/07/1992            *
  10. *                        *
  11. * BUGS:    UpDays should've been long not word    *
  12. *                        *
  13. * Version 1.00    10/08/1992            *
  14. *                        *
  15. * BUGS:    Gave things like '0 day'        *
  16. *                        *
  17. * Version 1.01    13/08/1992            *
  18. *                        *
  19. * Version 1.02    22/08/1992            *
  20. *                        *
  21. * Version 1.03    02/02/1993            *
  22. *                        *
  23. * BUGS:    Enforcer hit on logout            *
  24. *                        *
  25. * Version 1.04    14/02/1993            *
  26. *                        *
  27. *************************************************
  28.  
  29.     INCLUDE    "JMPLibs.i"
  30.     INCLUDE    "exec/types.i"
  31.     INCLUDE    "libraries/dos.i"
  32.     INCLUDE    "libraries/dosextens.i"
  33.  
  34.     INCLUDE    "XREF:2.0.xref"
  35.     INCLUDE    "XREF:exec.xref"
  36.     INCLUDE    "XREF:dos.xref"
  37.  
  38.         section    UpTime,CODE
  39.  
  40.         openlib    Dos,ShutDown_Out
  41.  
  42.         bsr    GetSysTime
  43.         bsr    CheckUpTime
  44.  
  45.         lea.l    CLTemplate1,a0
  46.         move.l    a0,d1
  47.         lea.l    CLArray1,a0
  48.         move.l    a0,d2
  49.         clr.l    d3
  50.         lib    Dos,ReadArgs
  51.         move.l    d0,RDArgs1
  52.         beq    NoRDArgs
  53.  
  54.         tst.l    ActionLogin
  55.         bne    DoParse
  56.         tst.l    ActionLogout
  57.         beq    Information
  58.  
  59. DoParse:    move.l    TTYPointer,a2
  60.         cmp.l    #$00,a2
  61.         beq    ArgMissing        ;We still need the TTY!
  62.  
  63.         cmp.b    #'c',(a2)        ;Check for console
  64.         bne    DoParse2
  65.         move.b    #'c',CurrentTTY
  66.         bra    DoParse3
  67.  
  68. DoParse2:    cmp.b    #'0',(a2)        ;Check for 'real' TTY
  69.         bcs    BadNumber
  70.         cmp.b    #'9',(a2)
  71.         bhi    BadNumber
  72.         move.b    (a2),CurrentTTY
  73.  
  74. DoParse3:    move.l    UserPointer,a2
  75.         tst.l    ActionLogout
  76.         bne    DoParse6    ;has a bug was 3.1
  77.         cmp.l    #$00,a2
  78.         beq    ArgMissing
  79.  
  80. DoParse3.1:    lea.l    TTYUser,a0
  81.         move.l    a0,a1
  82.         add.l    #10,a1        ;changed! was 20
  83.  
  84. DoParse4:    cmp.b    #'A',(a2)
  85.         bcs    DoParse5
  86.         cmp.b    #'z',(a2)
  87.         bhi    DoParse5
  88.         move.b    (a2)+,(a0)+
  89.         cmp.l    a0,a1        ;copy only 10 characters!
  90.         bne    DoParse4
  91.  
  92. DoParse5:    clr.b    (a0)+
  93.         tst.l    ActionLogout
  94.         bne    DoParse6
  95.  
  96.         tst.b    TTYUser
  97.         beq    ArgMissing
  98.  
  99. ;Complete requested action
  100.  
  101. DoParse6:    tst.l    ActionLogin
  102.         bne    Login
  103.         tst.l    ActionLogout
  104.         bne    Logout
  105.  
  106.  
  107. ;Get system time
  108. ;Uptime equals (System time - NUT_timeval)
  109. ;Try to read ENV:NUT_login + ENV:NUT_TTYX
  110. ;Loop 0 - 9
  111. ;try reading ENV:UT_loadavg
  112.  
  113. Information:    bsr    GetUpTime
  114.         bsr    DoTTYTest
  115.         bsr    GetLoadAvg
  116.  
  117.         cmp.w    #1,Users
  118.         bne    CheckUsrNum1
  119.         lea.l    SingleString1,a0
  120.         move.l    a0,UsersString
  121. CheckUsrNum1:    bsr    Format
  122.         lea.l    UpTimeText1,a0
  123.         bsr    Printer
  124.  
  125.         lea.l    LoadAvgBuf,a0
  126.         bsr    Printer
  127.  
  128.         lea.l    EndText1,a0
  129.         bsr    Printer
  130.         bra    ShutDown
  131.  
  132. Login:        tst.l    ActionLogout
  133.         bne    TooManyArgs
  134.         sub.l    a1,a1
  135.         lib    Exec,FindTask
  136.         move.l    d0,a0
  137.         move.l    $8c(a0),TTYTaskNum
  138.         cmp.b    #'c',CurrentTTY
  139.         bne    Login1
  140.         lea.l    UpTimeConsole,a0
  141.         bra    Login2
  142. Login1:        move.b    CurrentTTY,TTY
  143.         lea.l    UpTimeTTY,a0
  144. Login2:        move.l    a0,d1
  145.         move.l    #MODE_NEWFILE,d2
  146.         lib    Dos,Open
  147.         tst.l    d0
  148.         beq    ShutDown
  149.         move.l    d0,d4
  150.         move.l    d0,d1
  151.         lea.l    DateTime1,a0
  152.         move.l    a0,d2
  153.         move.l    #4*4+20,d3    ;info + username
  154.         lib    Dos,Write
  155.         move.l    d4,d1
  156.         lib    Dos,Close
  157.         bra    ShutDown
  158.  
  159. Logout:        tst.l    ActionLogin
  160.         bne    TooManyArgs
  161.         cmp.b    #'c',CurrentTTY
  162.         bne    Logout1
  163.         lea.l    UpTimeConsole,a0
  164.         bra    Logout2
  165. Logout1:    move.b    CurrentTTY,TTY
  166.         lea.l    UpTimeTTY,a0
  167. Logout2:    move.l    a0,d1
  168.         lib    Dos,DeleteFile
  169.         bra    ShutDown
  170.  
  171. CheckUpTime:    lea.l    UpTimeName,a3
  172.         move.l    a3,d1
  173.         move.l    #MODE_OLDFILE,d2
  174.         lib    Dos,Open
  175.         tst.l    d0
  176.         beq    CheckUpTime2
  177.         move.l    d0,d4
  178.         move.l    d0,d1
  179.         lea.l    UpTimeBuf,a0
  180.         move.l    a0,d2
  181.         move.l    #3*4,d3
  182.         lib    Dos,Read
  183.         move.l    d4,d1
  184.         lib    Dos,Close
  185.         rts
  186. CheckUpTime2:    move.l    a3,d1
  187.         move.l    #MODE_NEWFILE,d2
  188.         lib    Dos,Open
  189.         tst.l    d0
  190.         beq    CheckUpTime3
  191.         move.l    d0,d4
  192.         move.l    d0,d1
  193.         lea.l    DateTime1,a0
  194.         move.l    a0,d2
  195.         move.l    #3*4,d3
  196.         lib    Dos,Write
  197.         move.l    d4,d1
  198.         lib    Dos,Close
  199.         move.l    DateTime1,UpTimeBuf
  200.         move.l    DateTime1+4,UpTimeBuf+4
  201.         move.l    DateTime1+4,UpTimeBuf+4
  202. CheckUpTime3:    rts
  203.  
  204. GetSysTime:    lea.l    DateTime1,a0
  205.         move.l    a0,d1
  206.         lib    Dos,DateStamp
  207.  
  208.         move.l    DateTime1+4,d0
  209.         divu.w    #60,d0
  210.         move.w    d0,Hours
  211.         swap    d0
  212.         move.w    d0,Minutes
  213.         rts
  214.  
  215. ;GetSystime has already got the current time for us
  216.  
  217. GetUpTime:    move.l    UpTimeBuf,d0
  218.         move.l    DateTime1,d1
  219.         sub.l    d0,d1
  220.         move.l    d1,UpDays
  221.         move.l    UpTimeBuf+4,d0
  222.         move.l    DateTime1+4,d1
  223.         sub.l    d0,d1
  224.         bmi    GetUpTime3
  225. GetUpTime1:    divu.w    #60,d1
  226.         move.w    d1,UpHours
  227.         swap    d1
  228.         move.w    d1,UpMinutes
  229. GetUpTime2:    rts
  230. GetUpTime3:    move.l    UpTimeBuf+4,d0
  231.         move.l    DateTime1+4,d1
  232.         add.l    #1440,d1
  233.         sub.l    d0,d1
  234.         sub.l    #1,UpDays
  235.         cmp.l    #1,UpDays
  236.         bne    GetUpTime1
  237.         lea.l    SingleString1,a0
  238.         move.l    a0,DaysString
  239.         bra    GetUpTime1
  240.  
  241. DoTTYTest:    lea.l    UpTimeConsole,a0
  242.         move.l    a0,d1
  243.         move.l    #MODE_OLDFILE,d2
  244.         lib    Dos,Open
  245.         tst.l    d0
  246.         beq    DoTTYTest1
  247.         move.l    d0,d1
  248.         add.w    #1,Users
  249.         lib    Dos,Close
  250. DoTTYTest1:    move.b    #'0',TTY
  251.  
  252. DoTTYTest2:    lea.l    UpTimeTTY,a0
  253.         move.l    a0,d1
  254.         move.l    #MODE_OLDFILE,d2
  255.         lib    Dos,Open
  256.         tst.l    d0
  257.         beq    DoTTYTest3
  258.         move.l    d0,d1
  259.         add.w    #1,Users
  260.         lib    Dos,Close
  261. DoTTYTest3:    cmp.b    #'9',TTY
  262.         beq    DoTTYTest4
  263.         add.b    #1,TTY
  264.         bra    DoTTYTest2
  265. DoTTYTest4:    rts
  266.  
  267. GetLoadAvg:    lea.l    LoadAvgName,a0
  268.         move.l    a0,d1
  269.         move.l    #MODE_OLDFILE,d2
  270.         lib    Dos,Open
  271.         tst.l    d0
  272.         bne    GetLoadAvg2
  273. GetLoadAvg1:    move.l    #'N/A,',LoadAvgBuf
  274.         move.l    #'  N/',LoadAvgBuf+4
  275.         move.l    #'A,  ',LoadAvgBuf+8
  276.         move.l    #'N/A ',LoadAvgBuf+12
  277.         move.b    #$00,LoadAvgBuf+16
  278.         rts
  279. GetLoadAvg2:    move.l    d0,d4
  280.         move.l    d0,d1
  281.         lea.l    LoadAvgBuf,a0
  282.         move.l    a0,d2
  283.         move.l    #19,d3
  284.         lib    Dos,Read
  285.  
  286.         cmp.l    #19,d0
  287.         beq    GetLoadAvg3
  288.         bsr    GetLoadAvg1
  289. GetLoadAvg3:    move.l    d4,d1
  290.         lib    Dos,Close
  291.         rts
  292.  
  293. Format:        lea.l    fstrl1,a0        ;HEX->ASCII
  294.         lea.l    Hours,a1        ;Start of information
  295.         lea.l    PutChProc,a2
  296.         lea.l    UpTimeText1,a3        ;Destination
  297.         lib    Exec,RawDoFmt
  298.         rts
  299.  
  300. ShutDown:    move.l    RDArgs1,d1
  301.         beq    ShutDown9000
  302.         lib    Dos,FreeArgs
  303. ShutDown9000:
  304. ShutDown1000:    closlib    Dos
  305. ShutDown_Out:    move.l    #RETURN_OK,d0
  306.         rts
  307.  
  308. ArgMissing:    move.l    #ERROR_REQUIRED_ARG_MISSING,d1
  309.         lib    Dos,SetIoErr
  310.         bra    NoRDArgs
  311.  
  312. BadNumber:    move.l    #ERROR_BAD_NUMBER,d1
  313.         lib    Dos,SetIoErr
  314.         bra    NoRDArgs
  315.  
  316. TooManyArgs:    move.l    #ERROR_TOO_MANY_ARGS,d1
  317.         lib    Dos,SetIoErr
  318. ;        bra    NoRDArgs    ;CAREFUL HERE!!
  319.  
  320. NoRDArgs:    lib    Dos,IoErr
  321.         move.l    d0,d1
  322.         clr.l    d2
  323.         lib    Dos,PrintFault
  324.         bra    ShutDown
  325.  
  326. Printer:    printa    a0
  327.         rts
  328.  
  329. PutChProc:    tst.b    d0
  330.         beq    PutChProc_OUT
  331.         move.b    d0,(a3)+
  332. PutChProc_OUT:    rts
  333.  
  334. ;Structures & reservations
  335.  
  336.         libnames
  337.  
  338. RDArgs1:    dc.l    0
  339.  
  340. ;Options
  341.  
  342. CLArray1:
  343. ActionLogin:    dc.l    0
  344. ActionLogout:    dc.l    0
  345. TTYPointer:    dc.l    0
  346. UserPointer:    dc.l    0
  347.  
  348. ;Options, Settings and other stuff
  349.  
  350. CurrentTTY:    dc.b    0
  351. Kludge_1000:    dc.b    0            ;FILL FILL FILL
  352.  
  353. Hours:        dc.w    0
  354. Minutes:    dc.w    0
  355. UpDays:        dc.l    0
  356. DaysString:    dc.l    PluralString1
  357. UpHours:    dc.w    0
  358. UpMinutes:    dc.w    0
  359. Users:        dc.w    0
  360. UsersString:    dc.l    PluralString1
  361. PluralString1:    dc.b    's,',0
  362. SingleString1:    dc.b    ', ',0
  363.  
  364. ;Filenames
  365.  
  366. LoadAvgName:    dc.b    "ENV:UT_loadavg",0
  367. UpTimeName:    dc.b    "ENV:NUT_timeval",0
  368. UpTimeConsole:    dc.b    "ENV:NUT_console",0
  369. UpTimeTTY:    dc.b    "ENV:NUT_ttyp"
  370. TTY:        dc.b    0,0
  371.  
  372. ;Error strings etc...
  373.  
  374.  
  375. ;Information & other strings
  376.  
  377.         ds.w    0
  378. CLTemplate1:    dc.b    "LOGIN/S,LOGOUT/S,TTY/K,USER/K",0
  379.         dc.b    "$VER: Uptime 1.04 (14.2.93) (C)opyright Tomi Blinnikka 1993",0
  380.  
  381. fstrl1:        dc.b    "  %02.2d:%02d up %ld day%s %02.2d:%02.2d, %2.2d user%s load average: ",0
  382. UpTimeText1:    dcb.b    100,0
  383. EndText1:    dc.b    13,10,0
  384.         ds.w    0
  385.  
  386. ;Structures
  387.  
  388. DateTime1:    dc.l    0        ;days past 1978
  389.         dc.l    0        ;mins past 00:00
  390.         dc.l    0        ;ticks past min
  391. TTYTaskNum:    dc.l    0        ;pr_TaskNum
  392. TTYUser:    dcb.b    20,0        ;user on this tty
  393. TTYReal:    dcb.b    20,0        ;users' real name
  394.  
  395.  
  396. ;Buffers
  397.  
  398. UpTimeBuf:    dcb.b    3*4,0        ;read NUT_timeval here
  399. LoadAvgBuf:    dcb.b    20,0        ;read UT_loadavg here
  400.         END
  401.  
  402.