-
Notifications
You must be signed in to change notification settings - Fork 3
/
chown.java
56 lines (49 loc) · 1.63 KB
/
chown.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @author Denys Shcherbak
*/
public class chown
{
/**
* The name of this program.
* This is the program name that is used
* when displaying error messages.
*/
public static String PROGRAM_NAME = "chown" ;
/**
* Checks if the path exists, and tries to change it's owner
* @exception java.lang.Exception if an exception is thrown
* by an underlying operation
*/
public static void main( String[] args ) throws Exception
{
// initialize the file system simulator kernel
Kernel.initialize() ;
if(args.length < 2){
Kernel.perror( PROGRAM_NAME ) ;
Kernel.exit( 1 );
}
short owner = Short.parseShort(args[0]);
for(int i = 1; i < args.length; i++) {
String name = args[i];
int status = 0;
// stat the name to get information about the file or directory
Stat stat = new Stat();
status = Kernel.stat(name, stat);
if(status == -1) {
Kernel.perror(PROGRAM_NAME);
Kernel.exit(1);
}
short empty = -1;
status = Kernel.chown(name, owner, empty);
if(status == -1) {
System.err.println(PROGRAM_NAME + ": You don't have rights to perform this operation!");
Kernel.exit(1);
} else if(status == -2) {
System.err.println(PROGRAM_NAME + ": Incorrect uid parameter '" + owner + "'");
Kernel.exit(1);
}
}
// exit with success if we process all the arguments
Kernel.exit( 0 ) ;
}
}