Subversion Repositories Kolibri OS

Rev

Rev 5217 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* filemode.c -- make a string describing file modes
  2.    Copyright (C) 1985-2015 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  17.    02110-1301, USA.  */
  18. #include "sysdep.h"
  19. #include "bfd.h"
  20. #include "bucomm.h"
  21.  
  22. static char ftypelet (unsigned long);
  23. static void setst (unsigned long, char *);
  24.  
  25. /* filemodestring - fill in string STR with an ls-style ASCII
  26.    representation of the st_mode field of file stats block STATP.
  27.    10 characters are stored in STR; no terminating null is added.
  28.    The characters stored in STR are:
  29.  
  30.    0    File type.  'd' for directory, 'c' for character
  31.         special, 'b' for block special, 'm' for multiplex,
  32.         'l' for symbolic link, 's' for socket, 'p' for fifo,
  33.         '-' for any other file type
  34.  
  35.    1    'r' if the owner may read, '-' otherwise.
  36.  
  37.    2    'w' if the owner may write, '-' otherwise.
  38.  
  39.    3    'x' if the owner may execute, 's' if the file is
  40.         set-user-id, '-' otherwise.
  41.         'S' if the file is set-user-id, but the execute
  42.         bit isn't set.
  43.  
  44.    4    'r' if group members may read, '-' otherwise.
  45.  
  46.    5    'w' if group members may write, '-' otherwise.
  47.  
  48.    6    'x' if group members may execute, 's' if the file is
  49.         set-group-id, '-' otherwise.
  50.         'S' if it is set-group-id but not executable.
  51.  
  52.    7    'r' if any user may read, '-' otherwise.
  53.  
  54.    8    'w' if any user may write, '-' otherwise.
  55.  
  56.    9    'x' if any user may execute, 't' if the file is "sticky"
  57.         (will be retained in swap space after execution), '-'
  58.         otherwise.
  59.         'T' if the file is sticky but not executable.  */
  60.  
  61. /* Get definitions for the file permission bits.  */
  62.  
  63. #ifndef S_IRWXU
  64. #define S_IRWXU 0700
  65. #endif
  66. #ifndef S_IRUSR
  67. #define S_IRUSR 0400
  68. #endif
  69. #ifndef S_IWUSR
  70. #define S_IWUSR 0200
  71. #endif
  72. #ifndef S_IXUSR
  73. #define S_IXUSR 0100
  74. #endif
  75.  
  76. #ifndef S_IRWXG
  77. #define S_IRWXG 0070
  78. #endif
  79. #ifndef S_IRGRP
  80. #define S_IRGRP 0040
  81. #endif
  82. #ifndef S_IWGRP
  83. #define S_IWGRP 0020
  84. #endif
  85. #ifndef S_IXGRP
  86. #define S_IXGRP 0010
  87. #endif
  88.  
  89. #ifndef S_IRWXO
  90. #define S_IRWXO 0007
  91. #endif
  92. #ifndef S_IROTH
  93. #define S_IROTH 0004
  94. #endif
  95. #ifndef S_IWOTH
  96. #define S_IWOTH 0002
  97. #endif
  98. #ifndef S_IXOTH
  99. #define S_IXOTH 0001
  100. #endif
  101.  
  102. /* Like filemodestring, but only the relevant part of the `struct stat'
  103.    is given as an argument.  */
  104.  
  105. void
  106. mode_string (unsigned long mode, char *str)
  107. {
  108.   str[0] = ftypelet ((unsigned long) mode);
  109.   str[1] = (mode & S_IRUSR) != 0 ? 'r' : '-';
  110.   str[2] = (mode & S_IWUSR) != 0 ? 'w' : '-';
  111.   str[3] = (mode & S_IXUSR) != 0 ? 'x' : '-';
  112.   str[4] = (mode & S_IRGRP) != 0 ? 'r' : '-';
  113.   str[5] = (mode & S_IWGRP) != 0 ? 'w' : '-';
  114.   str[6] = (mode & S_IXGRP) != 0 ? 'x' : '-';
  115.   str[7] = (mode & S_IROTH) != 0 ? 'r' : '-';
  116.   str[8] = (mode & S_IWOTH) != 0 ? 'w' : '-';
  117.   str[9] = (mode & S_IXOTH) != 0 ? 'x' : '-';
  118.   setst ((unsigned long) mode, str);
  119. }
  120.  
  121. /* Return a character indicating the type of file described by
  122.    file mode BITS:
  123.    'd' for directories
  124.    'b' for block special files
  125.    'c' for character special files
  126.    'm' for multiplexer files
  127.    'l' for symbolic links
  128.    's' for sockets
  129.    'p' for fifos
  130.    '-' for any other file type.  */
  131.  
  132. #ifndef S_ISDIR
  133. #ifdef S_IFDIR
  134. #define S_ISDIR(i) (((i) & S_IFMT) == S_IFDIR)
  135. #else /* ! defined (S_IFDIR) */
  136. #define S_ISDIR(i) (((i) & 0170000) == 040000)
  137. #endif /* ! defined (S_IFDIR) */
  138. #endif /* ! defined (S_ISDIR) */
  139.  
  140. #ifndef S_ISBLK
  141. #ifdef S_IFBLK
  142. #define S_ISBLK(i) (((i) & S_IFMT) == S_IFBLK)
  143. #else /* ! defined (S_IFBLK) */
  144. #define S_ISBLK(i) 0
  145. #endif /* ! defined (S_IFBLK) */
  146. #endif /* ! defined (S_ISBLK) */
  147.  
  148. #ifndef S_ISCHR
  149. #ifdef S_IFCHR
  150. #define S_ISCHR(i) (((i) & S_IFMT) == S_IFCHR)
  151. #else /* ! defined (S_IFCHR) */
  152. #define S_ISCHR(i) 0
  153. #endif /* ! defined (S_IFCHR) */
  154. #endif /* ! defined (S_ISCHR) */
  155.  
  156. #ifndef S_ISFIFO
  157. #ifdef S_IFIFO
  158. #define S_ISFIFO(i) (((i) & S_IFMT) == S_IFIFO)
  159. #else /* ! defined (S_IFIFO) */
  160. #define S_ISFIFO(i) 0
  161. #endif /* ! defined (S_IFIFO) */
  162. #endif /* ! defined (S_ISFIFO) */
  163.  
  164. #ifndef S_ISSOCK
  165. #ifdef S_IFSOCK
  166. #define S_ISSOCK(i) (((i) & S_IFMT) == S_IFSOCK)
  167. #else /* ! defined (S_IFSOCK) */
  168. #define S_ISSOCK(i) 0
  169. #endif /* ! defined (S_IFSOCK) */
  170. #endif /* ! defined (S_ISSOCK) */
  171.  
  172. #ifndef S_ISLNK
  173. #ifdef S_IFLNK
  174. #define S_ISLNK(i) (((i) & S_IFMT) == S_IFLNK)
  175. #else /* ! defined (S_IFLNK) */
  176. #define S_ISLNK(i) 0
  177. #endif /* ! defined (S_IFLNK) */
  178. #endif /* ! defined (S_ISLNK) */
  179.  
  180. static char
  181. ftypelet (unsigned long bits)
  182. {
  183.   if (S_ISDIR (bits))
  184.     return 'd';
  185.   if (S_ISLNK (bits))
  186.     return 'l';
  187.   if (S_ISBLK (bits))
  188.     return 'b';
  189.   if (S_ISCHR (bits))
  190.     return 'c';
  191.   if (S_ISSOCK (bits))
  192.     return 's';
  193.   if (S_ISFIFO (bits))
  194.     return 'p';
  195.  
  196. #ifdef S_IFMT
  197. #ifdef S_IFMPC
  198.   if ((bits & S_IFMT) == S_IFMPC
  199.       || (bits & S_IFMT) == S_IFMPB)
  200.     return 'm';
  201. #endif
  202. #ifdef S_IFNWK
  203.   if ((bits & S_IFMT) == S_IFNWK)
  204.     return 'n';
  205. #endif
  206. #endif
  207.  
  208.   return '-';
  209. }
  210.  
  211. /* Set the 's' and 't' flags in file attributes string CHARS,
  212.    according to the file mode BITS.  */
  213.  
  214. static void
  215. setst (unsigned long bits ATTRIBUTE_UNUSED, char *chars ATTRIBUTE_UNUSED)
  216. {
  217. #ifdef S_ISUID
  218.   if (bits & S_ISUID)
  219.     {
  220.       if (chars[3] != 'x')
  221.         /* Set-uid, but not executable by owner.  */
  222.         chars[3] = 'S';
  223.       else
  224.         chars[3] = 's';
  225.     }
  226. #endif
  227. #ifdef S_ISGID
  228.   if (bits & S_ISGID)
  229.     {
  230.       if (chars[6] != 'x')
  231.         /* Set-gid, but not executable by group.  */
  232.         chars[6] = 'S';
  233.       else
  234.         chars[6] = 's';
  235.     }
  236. #endif
  237. #ifdef S_ISVTX
  238.   if (bits & S_ISVTX)
  239.     {
  240.       if (chars[9] != 'x')
  241.         /* Sticky, but not executable by others.  */
  242.         chars[9] = 'T';
  243.       else
  244.         chars[9] = 't';
  245.     }
  246. #endif
  247. }
  248.