# define the bits used for various file permissions
#
BEGIN {
    __S_IFMT	= 0170000;  # These bits determine file type.
    __S_IFDIR	= 0040000;  # Directory.
    __S_IFCHR	= 0020000;  # Character device.
    __S_IFBLK	= 0060000;  # Block device.
    __S_IFREG	= 0100000;  # Regular file.
    __S_IFIFO	= 0010000;  # FIFO.
    __S_IFLNK	= 0120000;  # Symbolic link.
    __S_IFSOCK	= 0140000;  # Socket.

    # Protection bits.
    __S_PROTB   = 0007777;  # These bits determine file protections
    __S_PERMB   = 0000777;  # These bits determine File permissions
    __S_ISUID	= 0004000;  # Set user ID on execution.
    __S_ISGID	= 0002000;  # Set group ID on execution.
    __S_ISVTX	= 0001000;  # Save swapped text after use (sticky).
    __S_IREAD	= 0000400;  # Read by owner.
    __S_IWRITE	= 0000200; 	# Write by owner.
    __S_IEXEC	= 0000100;  # Execute by owner.
    __S_IREADG  = 0000040;  # Read by group
    __S_IWRITEG = 0000020; 	# Write by group.
    __S_IEXECG  = 0000010;  # Execute by group.
    __S_IREADO  = 0000004;  # Read by Others
    __S_IWRITEO = 0000002; 	# Write by Others.
    __S_IEXECO  = 0000001;  # Execute by Others.
}

# function to convert file attribute numeric to file attribute string
# for Linux (and Unix I suppose)
# Note: attribute bits set in 'BEGIN' module at top of file
function attr_string(file_at) {
    local ats, uat, gat, oat, rs;
    local file_type_bits;


    ats[0] = "-";
    ats[1] = "x";
    ats[2] = "w";
    ats[4] = "r";

    file_at += 0;
    file_type_bits = file_at & __S_IFMT;

    if ( file_type_bits == __S_IFREG)         rs = "-"; # set regular file flag
      else if ( file_type_bits == __S_IFDIR ) rs = "d"; # set directory flag
      else if ( file_type_bits == __S_IFCHR ) rs = "c"; # set character device flag
      else if ( file_type_bits == __S_IFBLK ) rs = "b"; # set block device flag
      else if ( file_type_bits == __S_IFLNK ) rs = "s"; # set symbolic link flag
      else rs = "-";

    oat = file_at % 8;
    file_at /= 8;
    gat = file_at % 8;
    file_at /= 8;
    uat = file_at % 8;

    # set user, group and other flags
    rs ><= ats[uat & 4] >< ats[uat & 2] >< ats[uat & 1] ><
           ats[gat & 4] >< ats[gat & 2] >< ats[gat & 1] ><
           ats[oat & 4] >< ats[oat & 2] >< ats[oat & 1];

}