11extern crate pretty_bytes;
2+ extern crate libc;
23
34use ansi_term:: Style ;
45use chrono:: { DateTime , Utc } ;
@@ -9,6 +10,7 @@ use std::{fs, io};
910use structopt:: StructOpt ;
1011use termion:: color;
1112use users:: { get_current_uid, get_group_by_gid, get_user_by_uid, uid_t, get_current_gid} ;
13+ use libc:: { S_IRGRP , S_IROTH , S_IRUSR , S_IWGRP , S_IWOTH , S_IWUSR , S_IXGRP , S_IXOTH , S_IXUSR } ;
1214
1315mod single;
1416
@@ -25,11 +27,15 @@ struct Cli {
2527 help = "File to search for"
2628 ) ]
2729 file : String ,
30+
31+ #[ structopt( short = "l" , long = "headline" , help = "enable the headline" ) ]
32+ headline_on : bool ,
2833}
2934
3035fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
3136 let args = Cli :: from_args ( ) ;
3237 let directory = & args. path ;
38+ let headline_on = & args. headline_on ;
3339
3440 let entries = fs:: read_dir ( directory) ?
3541 . map ( |res| res. map ( |e| e. path ( ) ) )
@@ -51,10 +57,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5157 }
5258
5359 let mut found = false ;
54-
55- draw_headline ( "permissions" , 2 , false ) ;
56- draw_headline ( "size" , size_count - 4 , true ) ;
57- draw_headline ( "last modified" , 6 , true ) ;
60+
61+ if * headline_on {
62+ draw_headline ( "permissions" , 0 , false ) ;
63+ draw_headline ( "size" , 0 , true ) ;
64+ draw_headline ( "last modified" , 0 , true ) ;
5865 let mut groups_size: i32 = 0 ;
5966 if get_group_by_gid ( get_current_gid ( ) ) . unwrap ( ) . name ( ) . to_str ( ) . unwrap ( ) . len ( ) - 5 < 1 {
6067 groups_size = 0 ;
@@ -68,10 +75,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6875 } else {
6976 user_size = get_user_by_uid ( get_current_uid ( ) ) . unwrap ( ) . name ( ) . to_str ( ) . unwrap ( ) . len ( ) as i32 - 4 ;
7077 }
71- draw_headline ( "user" , user_size as usize , true ) ;
78+ draw_headline ( "user" , 0 , true ) ;
7279 draw_headline ( "name" , 0 , true ) ;
7380
74- print ! ( "\n " ) ;
81+ print ! ( "\n " ) ;
82+ }
7583
7684 if & args. file != "" {
7785 for e in & entries {
@@ -101,52 +109,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
101109
102110 for e in & entries {
103111 let meta = fs:: symlink_metadata ( & e) ?;
104- println ! ( "{:?}" , meta. permissions( ) . mode( ) ) ;
105112 let mode = meta. permissions ( ) . mode ( ) ;
106- let user_has_write_access = mode & 0o200 ;
107- let user_has_read_write_access= mode & 0o600 ;
108- let group_has_read_access = mode & 0o040 ;
109- let others_have_exec_access = mode & 0o001 ;
110- println ! ( "{:o} {:o} {:o} {:o}" , user_has_write_access, user_has_read_write_access, group_has_read_access, others_have_exec_access) ;
111- let mut mode_count = 0 ;
112- if user_has_write_access == 128 {
113- print ! ( "{}" , color:: Fg ( color:: Red ) ) ;
114- print ! ( "w" ) ;
115- print ! ( "{}" , color:: Fg ( color:: White ) ) ;
116- print ! ( "-" ) ;
117- mode_count += 2 ;
118- }
119- if user_has_read_write_access == 384 {
120- print ! ( "{}" , color:: Fg ( color:: LightYellow ) ) ;
121- print ! ( "r" ) ;
122- print ! ( "{}" , color:: Fg ( color:: LightRed ) ) ;
123- print ! ( "w" ) ;
124- print ! ( "{}" , color:: Fg ( color:: White ) ) ;
125- print ! ( "-" ) ;
126- mode_count += 3 ;
127- }
128- if group_has_read_access == 32 {
129- print ! ( "{}" , color:: Fg ( color:: Green ) ) ;
130- print ! ( "x" ) ;
131- print ! ( "{}" , color:: Fg ( color:: LightYellow ) ) ;
132- print ! ( "a" ) ;
133- print ! ( "{}" , color:: Fg ( color:: White ) ) ;
134- print ! ( "-" ) ;
135- mode_count += 3 ;
136- }
137- if others_have_exec_access == 1 {
138- print ! ( "{}" , color:: Fg ( color:: Yellow ) ) ;
139- print ! ( "xw" ) ;
140- print ! ( "{}" , color:: Fg ( color:: White ) ) ;
141- print ! ( "-" ) ;
142- mode_count += 3 ;
143- }
113+ let mode_count = perms ( mode as u16 ) . len ( ) ;
114+
144115 print ! ( "{}" , color:: Fg ( color:: White ) ) ;
145- print ! ( "-@" ) ;
146- mode_count += 2 ;
147- for _ in 0 ..( 13 - mode_count) {
148- print ! ( " " )
149- }
116+
117+ print ! ( "{}" , perms( mode as u16 ) ) ;
150118
151119 for _ in 0 ..( size_count - convert ( fs:: symlink_metadata ( & e) ?. size ( ) as f64 ) . len ( ) ) {
152120 print ! ( " " )
@@ -233,3 +201,24 @@ fn get_user_name(uid: uid_t) -> String {
233201 . unwrap ( )
234202 . to_string ( )
235203}
204+
205+
206+ pub fn perms ( mode : u16 ) -> String {
207+ let user = triplet ( mode, S_IRUSR , S_IWUSR , S_IXUSR ) ;
208+ let group = triplet ( mode, S_IRGRP , S_IWGRP , S_IXGRP ) ;
209+ let other = triplet ( mode, S_IROTH , S_IWOTH , S_IXOTH ) ;
210+ [ user, group, other] . join ( "" )
211+ }
212+
213+ pub fn triplet ( mode : u16 , read : u16 , write : u16 , execute : u16 ) -> String {
214+ match ( mode & read, mode & write, mode & execute) {
215+ ( 0 , 0 , 0 ) => "---" ,
216+ ( _, 0 , 0 ) => "r--" ,
217+ ( 0 , _, 0 ) => "-w-" ,
218+ ( 0 , 0 , _) => "--x" ,
219+ ( _, 0 , _) => "r-x" ,
220+ ( _, _, 0 ) => "rw-" ,
221+ ( 0 , _, _) => "-wx" ,
222+ ( _, _, _) => "rwx" ,
223+ } . to_string ( )
224+ }
0 commit comments