-
Notifications
You must be signed in to change notification settings - Fork 3
/
chmod.java
58 lines (53 loc) · 1.78 KB
/
chmod.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
57
58
/**
* @author Denys Shcherbak
*/
public class chmod
{
/**
* The name of this program.
* This is the program name that is used
* when displaying error messages.
*/
public static String PROGRAM_NAME = "chmod" ;
/**
* 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 mode = 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 );
}
status = Kernel.chmod(name, mode);
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 file" ) ;
Kernel.exit( 1 );
} else if (status == -3){
System.err.println( PROGRAM_NAME + ": Incorrect mode parameter '" + mode + "'") ;
Kernel.exit( 1 );
}
}
// exit with success if we process all the arguments
Kernel.exit( 0 ) ;
}
}