next up previous contents
Next: Util Up: Samples / No Previous: File

Stream

exec.java

  1: /*
  2:  * exec.java  0.1  96/08/01
  3:  *
  4:  * Copyright (c) 1996  MARUYAMA Fujio  All Rights Reserved.
  5:  *
  6:  * Permission to use, copy, modify, and distribute this software
  7:  * and its documentation for NON-COMMERCIAL purposes and without
  8:  * fee is hereby granted provided that this copyright notice
  9:  * appears in all copies. 
 10:  *
 11:  *           Mail : maruyama@wakhok.ac.jp
 12:  *
 13:  */
 14: 
 15: import java.lang.* ;
 16: import java.io.* ;
 17: 
 18: class exec {
 19:    Process proc = null ;
 20:    DataInputStream in = null;
 21:    PrintStream out = null ;
 22:    String cmd[] = new String[3] ;
 23:    StringBuffer args = new StringBuffer();
 24: 
 25:     public void setCommandArray(String arg[]) {
 26:         if (arg.length == 0) return ;
 27:         for (int i = 0; i < arg.length; i++) {
 28:             args.append(' ').append(arg[i]);
 29:         }
 30:         cmd[0] = "/usr/bin/ksh";
 31:         cmd[1] = "-c";
 32:         cmd[2] = args.toString();
 33:     }
 34: 
 35:    exec( String arg[] ){
 36:      try{
 37:         setCommandArray(arg);
 38:         proc = Runtime.getRuntime().exec(cmd);
 39:         in  = new DataInputStream(proc.getInputStream());
 40:         out = new PrintStream(proc.getOutputStream());
 41:      } catch ( Exception ex){
 42:         System.err.println("exec: "+ex);
 43:      }
 44:    } 
 45: 
 46:   void  prout(OutputStream pout){
 47:    try{
 48:       String line = null ;
 49:       while( true  ){
 50:         if ( in.available() > 0 ){
 51:           while( (line = in.readLine()) != null ){
 52:              for( int i=0; i < line.length(); i++ )
 53:                   pout.write( line.charAt(i) );
 54:                   pout.write( 'n' );
 55:           } 
 56:           break ;
 57:         } else {
 58:           Thread.sleep(200);
 59:         } 
 60:       }
 61:    } catch ( Exception ex){
 62:         System.err.println("prout:"+ex);
 63:    }
 64:  }
 65: 
 66:    public static void main(String argv[]){
 67:        new exec(argv).prout(System.out);
 68:    }
 69: }

parser.java

  1: /*
  2:  * parser.java  0.1  96/08/01
  3:  *
  4:  * Copyright (c) 1996  MARUYAMA Fujio  All Rights Reserved.
  5:  *
  6:  * Permission to use, copy, modify, and distribute this software
  7:  * and its documentation for NON-COMMERCIAL purposes and without
  8:  * fee is hereby granted provided that this copyright notice
  9:  * appears in all copies. 
 10:  *
 11:  *           Mail : maruyama@wakhok.ac.jp
 12:  *
 13:  */
 14: 
 15: import java.io.*;
 16: import java.net.*;
 17: 
 18: class Parser extends StreamTokenizer {
 19: 
 20:   Parser(InputStream in ){
 21:     super(in);
 22:     resetSyntax();
 23: 
 24:     whitespaceChars(0, ' ');
 25:     wordChars( 33, 255);
 26:       
 27:     ordinaryChar('<');
 28:     ordinaryChar('>');
 29:   }
 30: 
 31:   public static void main (String argv[]){
 32:      Parser tok = null;
 33:      try {
 34:        String src = argv[0] ;
 35:        if ( src.startsWith("http://") ){
 36:           tok = new Parser(new URL(src).openStream());
 37:        } else {
 38:           tok = new Parser(new FileInputStream(src));
 39:        }
 40: 
 41:        String text = "" ;
 42:        String tag  = "" ;
 43:        String tagName  = "" ;
 44:        String tmp  = "" ;
 45: 
 46:        int ret ;
 47:        boolean inTag   = false ;
 48: 
 49:        while( (ret = tok.nextToken()) != TT_EOF ){
 50:           switch ( ret ) {
 51:               case '<' : // System.out.print("<");
 52:                          if ( text.length() != 0 ){
 53:                               System.out.println("Text: " + text);
 54:                          }
 55:                          tag="";
 56:                          inTag=true;
 57:                          break ;
 58:               case '>' : // System.out.print(">");
 59:                          System.out.println("Tag : " + tag);
 60:                          text="";
 61:                          inTag=false;
 62:                          break ;
 63:               default :  tmp = tok.sval ;
 64:                          if ( inTag ){
 65:                              tag  += tmp + " ";
 66:                          } else {
 67:                              text += tmp + " ";
 68:                          }
 69:           }
 70:        }
 71:      } catch (Exception e ){
 72:           System.err.println("Exception :" + e);   
 73:      }
 74:   }
 75: }

homedir.java

  1: /*
  2:  * homedir.java  0.1  96/08/01
  3:  *
  4:  * Copyright (c) 1996  MARUYAMA Fujio  All Rights Reserved.
  5:  *
  6:  * Permission to use, copy, modify, and distribute this software
  7:  * and its documentation for NON-COMMERCIAL purposes and without
  8:  * fee is hereby granted provided that this copyright notice
  9:  * appears in all copies. 
 10:  *
 11:  *           Mail : maruyama@wakhok.ac.jp
 12:  *
 13:  */
 14: 
 15: import java.lang.* ;
 16: import java.io.* ;
 17: import java.util.* ;
 18: 
 19: class homedir {
 20:    Process proc = null ;
 21:    DataInputStream in = null;
 22:    String cmd[] = new String[2] ;
 23:    Hashtable ht = new Hashtable();
 24:    StringTokenizer st = null ;
 25: 
 26:    String line = null ;
 27:    String username = null ;
 28:    String dummy = null ;
 29:    String homedir = null ;
 30: 
 31:     public void setCommandArray() {
 32:         cmd[0] = "/usr/bin/ypcat";
 33:         cmd[1] = "passwd";
 34:     }
 35: 
 36:    homedir(){
 37:      try{
 38:         setCommandArray();
 39:         proc = Runtime.getRuntime().exec(cmd);
 40:         in  = new DataInputStream(proc.getInputStream());
 41:      } catch ( Exception ex){
 42:         System.err.println("homedir: "+ex);
 43:      }
 44:    } 
 45:  
 46:   String  fillnull(String str){
 47:     StringBuffer sb = new StringBuffer();
 48:     for(int i=0; i < str.length() - 1; i++){
 49:       char first  = str.charAt(i); 
 50:       char second = str.charAt(i+1); 
 51:       if ( first == ':' && first == second ){
 52:          sb.append(first);
 53:          sb.append(' ');
 54:       } else {
 55:          sb.append(first);
 56:       }
 57:     }
 58:     return new String(sb);
 59:   }
 60: 
 61:   void  register(){
 62:    try{
 63: 
 64:       while( true  ){
 65:         if ( in.available() > 0 ){
 66:           while( (line = in.readLine()) != null ){
 67:              if ( line.indexOf("::") != -1 )
 68:                 line=fillnull(line);
 69:              st = new StringTokenizer(line,":n");
 70:              if ( st.hasMoreTokens() ) {
 71:                 username = st.nextToken();
 72:                 dummy = st.nextToken();
 73:                 dummy = st.nextToken();
 74:                 dummy = st.nextToken();
 75:                 dummy = st.nextToken();
 76:                 homedir = st.nextToken();
 77:                 if ( username != null && homedir != null )
 78:                     ht.put(username,homedir);
 79:                 }
 80:                 continue;
 81:           } 
 82:           break ;
 83:         } else {
 84:           Thread.sleep(200);
 85:         } 
 86:       }
 87:    } catch ( Exception ex){
 88:         System.err.println("register:"+ex);
 89:    }
 90:  }
 91: 
 92:    public static void main(String argv[]){
 93:        homedir hd = new homedir();
 94:        hd.register();
 95:        System.out.println( (String)hd.ht.get(argv[0]) );
 96:    }
 97: }


maruyama@wakhok.ac.jp