home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / ulock < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  3.2 KB  |  106 lines

  1. #!/bin/ksh
  2. # @(#) ulock.ksh 2.0 96/06/18
  3. # 91/01/15 john h. dubois iii (john@armory.com)
  4. # 91/02/25: only lock ttys that being with the same letter as current tty,
  5. #          or that begin with a digit if current tty does.
  6. # 91/02/27: make keys dead instead of mapping to ^A; use LOCKMAP var
  7. # 92/08/04: use ttys= instead of unset ttys
  8. # 96/06/18 Auto-generate mapchan file.
  9.  
  10. # ulock: lock all multiscreens or mscreens that the user is logged into.
  11. # Locks ttys by using mapchan to make all keys dead keys, 
  12. # so all characters are discarded.
  13. # When lock exits all of the other ttys are unlocked with mapchan -n.
  14. # Current tty is locked with "lock".
  15.  
  16. name=${0##*/}
  17. mfile=.lockmap
  18. if [ $# -gt 0 ]; then
  19.     print \
  20. "$name: lock all multiscreens or mscreens that the user is logged into.
  21.      If the name of the current TTY begins with a letter, all TTYs that the
  22. user is logged into that begin with that letter (except the current TTY) are
  23. locked with mapchan.
  24.      If the name of the current TTY begins with a digit, all TTYs that the user
  25. is logged into that begin with a digit (except the current TTY) are locked with
  26. mapchan.
  27.      lock is then run to lock the current TTY.  When lock exits all of the
  28. other TTYs are also unlocked.
  29.      The first time $name is run, it creates a mapchan file name $mfile in the
  30. invoking user's home directory to use to lock TTYs.  If the environment
  31. variable LOCKMAP is set, it is used instead; in that case the file must already
  32. exist."
  33.     exit
  34. fi
  35.  
  36. # Set USER, TTY (current tty), and LOCKMAP if they are not already set
  37. [ -z "$USER" ] && USER=`logname`
  38. [ -z "$TTY" ] && TTY=`tty`
  39. if [ -z "$LOCKMAP" ]; then
  40.     LOCKMAP=$HOME/$mfile
  41.     if [ ! -f "$LOCKMAP" ]; then
  42.     > $lockmap || exit 1
  43.     {
  44.     print \
  45.     "# mapchan file to discard all terminal input, generated by $name"
  46.     print "input"
  47.     typeset -i i=1
  48.     while [ i -lt 256 ]; do
  49.         print "dead\t$i"
  50.         let i+=1
  51.     done
  52.     print "\noutput"
  53.     } > $LOCKMAP 
  54.     fi
  55. fi
  56. tty=${TTY#/dev/}
  57.  
  58. # Get tty type identifier (first char of tty name after /dev/tty)
  59. ttyn=${tty#tty}
  60. ttylet=${ttyn#?}
  61. ttylet=${ttyn%$ttylet}
  62.  
  63. # Set tty search pattern depending on tty type
  64. [[ $ttylet = [0-9] ]] && ttypat='tty[0-9]*' || ttypat=tty$ttylet\*
  65.  
  66. #set users to USER and ALTUSERS separated by | so they can be used as a pattern
  67. users=`( IFS=\|; set $USER $ALTUSERS; echo "$*"; )`
  68.  
  69. echo "Looking for ttys logged into by: $USER $ALTUSERS"
  70.  
  71. # set ttys to all ttys of current type that user is logged into,
  72. # other than current tty
  73.  
  74. # Don't unset ttys because 3.2v4 ksh gives nonzero status if it wasn't set,
  75. # so that set -e shell will exit
  76. ttys=
  77. who | while read user line time; do
  78. [[ ( ( $user = @($users) ) && ( $line != $tty ) ) && ( $line = $ttypat ) ]] \
  79.     && ttys="$ttys $line"
  80. done
  81.  
  82. # sort tty list
  83. set $tty $ttys
  84. set -s
  85.  
  86. # If no ttys of current type other than current tty, 
  87. # don't bother with mapchan etc.
  88. if [ -n "$ttys" ]; then
  89.     if [ ! -r $LOCKMAP ]; then
  90.     echo "Cannot read $LOCKMAP."
  91.     exit 1
  92.     fi
  93.     echo Locking: $*.
  94.     # Unmap channels on normal exit or exit due to interrupt
  95.     trap "echo Unlocking: $*.; mapchan -n $ttys 2> /dev/null" exit
  96.     mapchan -f $LOCKMAP $ttys
  97. else
  98.     echo "Locking $tty (no other ttys of current type)."
  99. fi
  100.  
  101. # For some reason one version of lock sends sigint to shell when it exits!
  102. # Ignore interrupt 2.
  103. trap "" 2    
  104.  
  105. lock
  106.