Back to index

cell-binutils  2.17cvs20070401
filemode.c
Go to the documentation of this file.
00001 /* filemode.c -- make a string describing file modes
00002    Copyright 1985, 1990, 1991, 1994, 1995, 1997, 1999, 2002, 2003, 2005
00003    Free Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
00018    02110-1301, USA.  */
00019 
00020 #include "bfd.h"
00021 #include "bucomm.h"
00022 
00023 static char ftypelet (unsigned long);
00024 static void setst (unsigned long, char *);
00025 
00026 /* filemodestring - fill in string STR with an ls-style ASCII
00027    representation of the st_mode field of file stats block STATP.
00028    10 characters are stored in STR; no terminating null is added.
00029    The characters stored in STR are:
00030 
00031    0   File type.  'd' for directory, 'c' for character
00032        special, 'b' for block special, 'm' for multiplex,
00033        'l' for symbolic link, 's' for socket, 'p' for fifo,
00034        '-' for any other file type
00035 
00036    1   'r' if the owner may read, '-' otherwise.
00037 
00038    2   'w' if the owner may write, '-' otherwise.
00039 
00040    3   'x' if the owner may execute, 's' if the file is
00041        set-user-id, '-' otherwise.
00042        'S' if the file is set-user-id, but the execute
00043        bit isn't set.
00044 
00045    4   'r' if group members may read, '-' otherwise.
00046 
00047    5   'w' if group members may write, '-' otherwise.
00048 
00049    6   'x' if group members may execute, 's' if the file is
00050        set-group-id, '-' otherwise.
00051        'S' if it is set-group-id but not executable.
00052 
00053    7   'r' if any user may read, '-' otherwise.
00054 
00055    8   'w' if any user may write, '-' otherwise.
00056 
00057    9   'x' if any user may execute, 't' if the file is "sticky"
00058        (will be retained in swap space after execution), '-'
00059        otherwise.
00060        'T' if the file is sticky but not executable.  */
00061 
00062 /* Get definitions for the file permission bits.  */
00063 
00064 #ifndef S_IRWXU
00065 #define S_IRWXU 0700
00066 #endif
00067 #ifndef S_IRUSR
00068 #define S_IRUSR 0400
00069 #endif
00070 #ifndef S_IWUSR
00071 #define S_IWUSR 0200
00072 #endif
00073 #ifndef S_IXUSR
00074 #define S_IXUSR 0100
00075 #endif
00076 
00077 #ifndef S_IRWXG
00078 #define S_IRWXG 0070
00079 #endif
00080 #ifndef S_IRGRP
00081 #define S_IRGRP 0040
00082 #endif
00083 #ifndef S_IWGRP
00084 #define S_IWGRP 0020
00085 #endif
00086 #ifndef S_IXGRP
00087 #define S_IXGRP 0010
00088 #endif
00089 
00090 #ifndef S_IRWXO
00091 #define S_IRWXO 0007
00092 #endif
00093 #ifndef S_IROTH
00094 #define S_IROTH 0004
00095 #endif
00096 #ifndef S_IWOTH
00097 #define S_IWOTH 0002
00098 #endif
00099 #ifndef S_IXOTH
00100 #define S_IXOTH 0001
00101 #endif
00102 
00103 /* Like filemodestring, but only the relevant part of the `struct stat'
00104    is given as an argument.  */
00105 
00106 void
00107 mode_string (unsigned long mode, char *str)
00108 {
00109   str[0] = ftypelet ((unsigned long) mode);
00110   str[1] = (mode & S_IRUSR) != 0 ? 'r' : '-';
00111   str[2] = (mode & S_IWUSR) != 0 ? 'w' : '-';
00112   str[3] = (mode & S_IXUSR) != 0 ? 'x' : '-';
00113   str[4] = (mode & S_IRGRP) != 0 ? 'r' : '-';
00114   str[5] = (mode & S_IWGRP) != 0 ? 'w' : '-';
00115   str[6] = (mode & S_IXGRP) != 0 ? 'x' : '-';
00116   str[7] = (mode & S_IROTH) != 0 ? 'r' : '-';
00117   str[8] = (mode & S_IWOTH) != 0 ? 'w' : '-';
00118   str[9] = (mode & S_IXOTH) != 0 ? 'x' : '-';
00119   setst ((unsigned long) mode, str);
00120 }
00121 
00122 /* Return a character indicating the type of file described by
00123    file mode BITS:
00124    'd' for directories
00125    'b' for block special files
00126    'c' for character special files
00127    'm' for multiplexer files
00128    'l' for symbolic links
00129    's' for sockets
00130    'p' for fifos
00131    '-' for any other file type.  */
00132 
00133 #ifndef S_ISDIR
00134 #ifdef S_IFDIR
00135 #define S_ISDIR(i) (((i) & S_IFMT) == S_IFDIR)
00136 #else /* ! defined (S_IFDIR) */
00137 #define S_ISDIR(i) (((i) & 0170000) == 040000)
00138 #endif /* ! defined (S_IFDIR) */
00139 #endif /* ! defined (S_ISDIR) */
00140 
00141 #ifndef S_ISBLK
00142 #ifdef S_IFBLK
00143 #define S_ISBLK(i) (((i) & S_IFMT) == S_IFBLK)
00144 #else /* ! defined (S_IFBLK) */
00145 #define S_ISBLK(i) 0
00146 #endif /* ! defined (S_IFBLK) */
00147 #endif /* ! defined (S_ISBLK) */
00148 
00149 #ifndef S_ISCHR
00150 #ifdef S_IFCHR
00151 #define S_ISCHR(i) (((i) & S_IFMT) == S_IFCHR)
00152 #else /* ! defined (S_IFCHR) */
00153 #define S_ISCHR(i) 0
00154 #endif /* ! defined (S_IFCHR) */
00155 #endif /* ! defined (S_ISCHR) */
00156 
00157 #ifndef S_ISFIFO
00158 #ifdef S_IFIFO
00159 #define S_ISFIFO(i) (((i) & S_IFMT) == S_IFIFO)
00160 #else /* ! defined (S_IFIFO) */
00161 #define S_ISFIFO(i) 0
00162 #endif /* ! defined (S_IFIFO) */
00163 #endif /* ! defined (S_ISFIFO) */
00164 
00165 #ifndef S_ISSOCK
00166 #ifdef S_IFSOCK
00167 #define S_ISSOCK(i) (((i) & S_IFMT) == S_IFSOCK)
00168 #else /* ! defined (S_IFSOCK) */
00169 #define S_ISSOCK(i) 0
00170 #endif /* ! defined (S_IFSOCK) */
00171 #endif /* ! defined (S_ISSOCK) */
00172 
00173 #ifndef S_ISLNK
00174 #ifdef S_IFLNK
00175 #define S_ISLNK(i) (((i) & S_IFMT) == S_IFLNK)
00176 #else /* ! defined (S_IFLNK) */
00177 #define S_ISLNK(i) 0
00178 #endif /* ! defined (S_IFLNK) */
00179 #endif /* ! defined (S_ISLNK) */
00180 
00181 static char
00182 ftypelet (unsigned long bits)
00183 {
00184   if (S_ISDIR (bits))
00185     return 'd';
00186   if (S_ISLNK (bits))
00187     return 'l';
00188   if (S_ISBLK (bits))
00189     return 'b';
00190   if (S_ISCHR (bits))
00191     return 'c';
00192   if (S_ISSOCK (bits))
00193     return 's';
00194   if (S_ISFIFO (bits))
00195     return 'p';
00196 
00197 #ifdef S_IFMT
00198 #ifdef S_IFMPC
00199   if ((bits & S_IFMT) == S_IFMPC
00200       || (bits & S_IFMT) == S_IFMPB)
00201     return 'm';
00202 #endif
00203 #ifdef S_IFNWK
00204   if ((bits & S_IFMT) == S_IFNWK)
00205     return 'n';
00206 #endif
00207 #endif
00208 
00209   return '-';
00210 }
00211 
00212 /* Set the 's' and 't' flags in file attributes string CHARS,
00213    according to the file mode BITS.  */
00214 
00215 static void
00216 setst (unsigned long bits ATTRIBUTE_UNUSED, char *chars ATTRIBUTE_UNUSED)
00217 {
00218 #ifdef S_ISUID
00219   if (bits & S_ISUID)
00220     {
00221       if (chars[3] != 'x')
00222        /* Set-uid, but not executable by owner.  */
00223        chars[3] = 'S';
00224       else
00225        chars[3] = 's';
00226     }
00227 #endif
00228 #ifdef S_ISGID
00229   if (bits & S_ISGID)
00230     {
00231       if (chars[6] != 'x')
00232        /* Set-gid, but not executable by group.  */
00233        chars[6] = 'S';
00234       else
00235        chars[6] = 's';
00236     }
00237 #endif
00238 #ifdef S_ISVTX
00239   if (bits & S_ISVTX)
00240     {
00241       if (chars[9] != 'x')
00242        /* Sticky, but not executable by others.  */
00243        chars[9] = 'T';
00244       else
00245        chars[9] = 't';
00246     }
00247 #endif
00248 }