home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / binutils-2.2.1 / binutils / filemode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-16  |  4.4 KB  |  195 lines

  1. /* filemode.c -- make a string describing file modes
  2.    Copyright (C) 1985, 1990, 1991 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 2, 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "bfd.h"
  19. #include "sysdep.h"
  20.  
  21.  
  22. void mode_string ();
  23. static char ftypelet ();
  24. static void rwx ();
  25. static void setst ();
  26.  
  27. /* filemodestring - fill in string STR with an ls-style ASCII
  28.    representation of the st_mode field of file stats block STATP.
  29.    10 characters are stored in STR; no terminating null is added.
  30.    The characters stored in STR are:
  31.  
  32.    0    File type.  'd' for directory, 'c' for character
  33.     special, 'b' for block special, 'm' for multiplex,
  34.     'l' for symbolic link, 's' for socket, 'p' for fifo,
  35.     '-' for any other file type
  36.  
  37.    1    'r' if the owner may read, '-' otherwise.
  38.  
  39.    2    'w' if the owner may write, '-' otherwise.
  40.  
  41.    3    'x' if the owner may execute, 's' if the file is
  42.     set-user-id, '-' otherwise.
  43.     'S' if the file is set-user-id, but the execute
  44.     bit isn't set.
  45.  
  46.    4    'r' if group members may read, '-' otherwise.
  47.  
  48.    5    'w' if group members may write, '-' otherwise.
  49.  
  50.    6    'x' if group members may execute, 's' if the file is
  51.     set-group-id, '-' otherwise.
  52.     'S' if it is set-group-id but not executable.
  53.  
  54.    7    'r' if any user may read, '-' otherwise.
  55.  
  56.    8    'w' if any user may write, '-' otherwise.
  57.  
  58.    9    'x' if any user may execute, 't' if the file is "sticky"
  59.     (will be retained in swap space after execution), '-'
  60.     otherwise.
  61.     'T' if the file is sticky but not executable. */
  62.  
  63. void
  64. filemodestring (statp, str)
  65.      struct stat *statp;
  66.      char *str;
  67. {
  68.   mode_string (statp->st_mode, str);
  69. }
  70.  
  71. /* Like filemodestring, but only the relevant part of the `struct stat'
  72.    is given as an argument. */
  73.  
  74. void
  75. mode_string (mode, str)
  76.      unsigned short mode;
  77.      char *str;
  78. {
  79.   str[0] = ftypelet (mode);
  80.   rwx ((mode & 0700) << 0, &str[1]);
  81.   rwx ((mode & 0070) << 3, &str[4]);
  82.   rwx ((mode & 0007) << 6, &str[7]);
  83.   setst (mode, str);
  84. }
  85.  
  86. /* Return a character indicating the type of file described by
  87.    file mode BITS:
  88.    'd' for directories
  89.    'b' for block special files
  90.    'c' for character special files
  91.    'm' for multiplexor files
  92.    'l' for symbolic links
  93.    's' for sockets
  94.    'p' for fifos
  95.    '-' for any other file type. */
  96.  
  97. static char
  98. ftypelet (bits)
  99.      unsigned short bits;
  100. {
  101.   switch (bits & S_IFMT)
  102.     {
  103.     default:
  104.       return '-';
  105.     case S_IFDIR:
  106.       return 'd';
  107. #ifdef S_IFLNK
  108.     case S_IFLNK:
  109.       return 'l';
  110. #endif
  111. #ifdef S_IFCHR
  112.     case S_IFCHR:
  113.       return 'c';
  114. #endif
  115. #ifdef S_IFBLK
  116.     case S_IFBLK:
  117.       return 'b';
  118. #endif
  119. #ifdef S_IFMPC
  120.     case S_IFMPC:
  121.     case S_IFMPB:
  122.       return 'm';
  123. #endif
  124. #ifdef S_IFSOCK
  125.     case S_IFSOCK:
  126.       return 's';
  127. #endif
  128. #ifdef S_IFIFO
  129. #if S_IFIFO != S_IFSOCK
  130.     case S_IFIFO:
  131.       return 'p';
  132. #endif
  133. #endif
  134. #ifdef S_IFNWK            /* HP-UX */
  135.     case S_IFNWK:
  136.       return 'n';
  137. #endif
  138.     }
  139. }
  140.  
  141. /* Look at read, write, and execute bits in BITS and set
  142.    flags in CHARS accordingly. */
  143.  
  144. static void
  145. rwx (bits, chars)
  146.      unsigned short bits;
  147.      char *chars;
  148. {
  149.   chars[0] = (bits & S_IREAD) ? 'r' : '-';
  150.   chars[1] = (bits & S_IWRITE) ? 'w' : '-';
  151.   chars[2] = (bits & S_IEXEC) ? 'x' : '-';
  152. }
  153.  
  154. /* Set the 's' and 't' flags in file attributes string CHARS,
  155.    according to the file mode BITS. */
  156.  
  157. static void
  158. setst (bits, chars)
  159.      unsigned short bits;
  160.      char *chars;
  161. {
  162. #ifdef S_ISUID
  163.   if (bits & S_ISUID)
  164.     {
  165.       if (chars[3] != 'x')
  166.     /* Set-uid, but not executable by owner. */
  167.     chars[3] = 'S';
  168.       else
  169.     chars[3] = 's';
  170.     }
  171. #endif
  172. #ifdef S_ISGID
  173.   if (bits & S_ISGID)
  174.     {
  175.       if (chars[6] != 'x')
  176.     /* Set-gid, but not executable by group. */
  177.     chars[6] = 'S';
  178.       else
  179.     chars[6] = 's';
  180.     }
  181. #endif
  182. #ifdef S_ISVTX
  183.   if (bits & S_ISVTX)
  184.     {
  185.       if (chars[9] != 'x')
  186.     /* Sticky, but not executable by others. */
  187.     chars[9] = 'T';
  188.       else
  189.     chars[9] = 't';
  190.     }
  191. #endif
  192. }
  193.  
  194.  
  195.