home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / filemode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-11  |  4.6 KB  |  203 lines

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