home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / PR8ADPL7.TAR / productivity_tools / PR8ADPL7 / ChangeUser.java < prev    next >
Encoding:
Java Source  |  1996-05-23  |  1.6 KB  |  69 lines

  1. // ChangeUser
  2. // A command line program for adding or editing a JFS user
  3. import java.io.*;
  4. import java.util.Vector;
  5.  
  6. public class ChangeUser
  7. {
  8.     public static void main(String argv[])
  9.     {
  10.     if (argv.length != 5) {
  11.         System.err.println("usage: ChangeUser <JFS root> <username> "+
  12.                    "<real name> <password> <home dir>");
  13.         System.exit(1);
  14.         }
  15.     FileSystem.init(argv[0]);
  16.  
  17.     // Read in users
  18.     BufferInputStream ibuf = null;
  19.     try ibuf = new BufferInputStream(
  20.            FileSystem.getfile("/etc/users", 0, -1, -1));
  21.     catch(BadPathException e) {
  22.         System.err.println("Could not open /etc/users");
  23.         System.exit(1);
  24.         }
  25.     Vector users = new Vector();
  26.     try while(true) users.addElement(new JFSuser(ibuf.gets()));
  27.     catch(IOException e);
  28.  
  29.     // Check if this user already exists
  30.     JFSuser old = null;
  31.     for(int i=0; i<users.size(); i++) {
  32.         JFSuser u = (JFSuser)users.elementAt(i);
  33.         if (u.name.equals(argv[1]))
  34.             old = u;
  35.         }
  36.  
  37.     if (old == null) {
  38.         // Add the user
  39.         JFSuser nu = new JFSuser();
  40.         nu.name = argv[1];
  41.         nu.realname = argv[2];
  42.         nu.password = argv[3];
  43.         nu.home = argv[4];
  44.         users.addElement(nu);
  45.         }
  46.     else {
  47.         // Update the user's real name and password
  48.         old.realname = argv[2];
  49.         old.password = argv[3];
  50.         }
  51.  
  52.     // Write out user's file
  53.     BufferOutputStream obuf = new BufferOutputStream();
  54.     for(int i=0; i<users.size(); i++)
  55.         obuf.puts(((JFSuser)users.elementAt(i)).output());
  56.     try FileSystem.putfile("/etc/users", 0, obuf.getarray(),
  57.                    "root", "text/plain");
  58.     catch(BadPathException e) {
  59.         System.err.println("Failed to write /etc/users");
  60.         System.exit(1);
  61.         }
  62.  
  63.     // Create home dir
  64.     try FileSystem.mkdir(argv[4], argv[1]);
  65.     catch(BadPathException e);
  66.     }
  67. }
  68.  
  69.