      INTEGER FUNCTION IGETLU()                                                 
C                                                                               
C MODULE        IGETLU                                                          
C VERSION       1.0                                                             
C LAST MODIFIED 25/01/82                                                        
C AUTHOR        N C SEDGWICK                                                    
C                                                                               
C THIS FUNCTION RETURNS THE NUMBER OF AN UNUSED LOGICAL UNIT.                   
C IF NO LOGICAL UNITS ARE AVAILABLE THEN IT RETURNS '-1'                        
C                                                                               
C LOCAL VARIABLES                                                               
C ***************                                                               
C                                                                               
C LUN     I    LOGICAL UNIT NUMBER UNDER CONSIDERATION                          
C PBLK    IA   PARAMETER BLOCK FOR SVC7                                         
C IOST    I    STATUS/ERROR FROM SVC7                                           
C                                                                               
      INTEGER LUN,PBLK(7),IOST                                                  
C                                                                               
C==================================================================             
C                                                                               
C LOOP OVER ALL POSSIBLE LUNS THAT MIGHT BE FREE                                
C **********************************************                                
C                                                                               
C INITIALISE LOOP                                                               
C ---------------                                                               
C                                                                               
      LUN=0                                                                     
C                                                                               
C FETCH LUN ATTRIBUTES USING SVC7                                               
C -------------------------------                                               
C                                                                               
1000  CONTINUE                                                                  
      PBLK(1)=Y'00000000'+LUN                                                   
      DO 1010 I=2,7                                                             
      PBLK(I)=0                                                                 
1010  CONTINUE                                                                  
      CALL SVC7(PBLK)                                                           
C                                                                               
C CHECK STATUS/ERROR FROM SVC7                                                  
C ----------------------------                                                  
C                                                                               
      CALL ILBYTE(IOST,PBLK(1),2)                                               
C                                                                               
C =2 IS ILLEGAL LUN SO HAVE REACHED END OF AVAILABLE LUNS                       
      IF(IOST.EQ.2) GOTO 9001                                                   
C                                                                               
C =9 IS LUN NOT ASSIGNED. THEREFORE RETURN THAT LUN AS USABLE                   
      IF(IOST.EQ.9) GOTO 2000                                                   
C                                                                               
C =0 IS LUN IN USE. OTHER VALUES ARE UNEXPECTED AND IN ERROR                    
      IF(IOST.NE.0) GOTO 9002                                                   
C                                                                               
C INCREMENT COUNTER AND LOOP                                                    
C --------------------------                                                    
C                                                                               
      LUN=LUN+1                                                                 
      GOTO 1000                                                                 
C                                                                               
C USABLE LUN FOUND SO RETURN IT FOR USE                                         
C *************************************                                         
C                                                                               
2000  CONTINUE                                                                  
      IGETLU=LUN                                                                
      RETURN                                                                    
C                                                                               
C==================================================================             
C                                                                               
C ERRORS                                                                        
C ******                                                                        
C                                                                               
C NO FREE LUNS FOUND                                                            
9001  GOTO 9999                                                                 
C                                                                               
C UNEXPECTED ERROR CODE FROM SVC7                                               
9002  CONTINUE                                                                  
      PAUSE 'IGETLU- ??? SVC7 CODE ???'                                         
      GOTO 9999                                                                 
C                                                                               
C GENERAL ERROR EXIT                                                            
9999  CONTINUE                                                                  
      IGETLU=-1                                                                 
      RETURN                                                                    
      END                                                                       
