home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / filemode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-25  |  4.4 KB  |  183 lines

  1. /* filemode.c -- make a string describing file modes
  2.    Copyright (C) 1985, 1990, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: FSF 19.28. */
  21.  
  22. #include <config.h>
  23. #include "lisp.h"
  24.  
  25. #include "sysfile.h"
  26.  
  27. static void mode_string (unsigned short mode, char *str);
  28. static char ftypelet (mode_t bits);
  29. static void rwx (unsigned short bits, char *chars);
  30. static void setst (unsigned short bits, char *chars);
  31.  
  32. /* filemodestring - fill in string STR with an ls-style ASCII
  33.    representation of the st_mode field of file stats block STATP.
  34.    10 characters are stored in STR; no terminating null is added.
  35.    The characters stored in STR are:
  36.  
  37.    0    File type.  'd' for directory, 'c' for character
  38.     special, 'b' for block special, 'm' for multiplex,
  39.     'l' for symbolic link, 's' for socket, 'p' for fifo,
  40.     '-' for regular, '?' for any other file type
  41.  
  42.    1    'r' if the owner may read, '-' otherwise.
  43.  
  44.    2    'w' if the owner may write, '-' otherwise.
  45.  
  46.    3    'x' if the owner may execute, 's' if the file is
  47.     set-user-id, '-' otherwise.
  48.     'S' if the file is set-user-id, but the execute
  49.     bit isn't set.
  50.  
  51.    4    'r' if group members may read, '-' otherwise.
  52.  
  53.    5    'w' if group members may write, '-' otherwise.
  54.  
  55.    6    'x' if group members may execute, 's' if the file is
  56.     set-group-id, '-' otherwise.
  57.     'S' if it is set-group-id but not executable.
  58.  
  59.    7    'r' if any user may read, '-' otherwise.
  60.  
  61.    8    'w' if any user may write, '-' otherwise.
  62.  
  63.    9    'x' if any user may execute, 't' if the file is "sticky"
  64.     (will be retained in swap space after execution), '-'
  65.     otherwise.
  66.     'T' if the file is sticky but not executable. */
  67.  
  68. void
  69. filemodestring (struct stat *statp, char *str)
  70. {
  71.   mode_string (statp->st_mode, str);
  72. }
  73.  
  74. /* Like filemodestring, but only the relevant part of the `struct stat'
  75.    is given as an argument. */
  76.  
  77. static void
  78. mode_string (unsigned short mode, char *str)
  79. {
  80.   str[0] = ftypelet (mode);
  81.   rwx ((mode & 0700) << 0, &str[1]);
  82.   rwx ((mode & 0070) << 3, &str[4]);
  83.   rwx ((mode & 0007) << 6, &str[7]);
  84.   setst (mode, str);
  85. }
  86.  
  87. /* Return a character indicating the type of file described by
  88.    file mode BITS:
  89.    'd' for directories
  90.    'b' for block special files
  91.    'c' for character special files
  92.    'm' for multiplexor files
  93.    'l' for symbolic links
  94.    's' for sockets
  95.    'p' for fifos
  96.    '-' for regular files
  97.    '?' for any other file type. */
  98.  
  99. static char
  100. ftypelet (mode_t bits)
  101. {
  102. #ifdef S_ISBLK
  103.   if (S_ISBLK (bits))
  104.     return 'b';
  105. #endif
  106.   if (S_ISCHR (bits))
  107.     return 'c';
  108.   if (S_ISDIR (bits))
  109.     return 'd';
  110.   if (S_ISREG (bits))
  111.     return '-';
  112. #ifdef S_ISFIFO
  113.   if (S_ISFIFO (bits))
  114.     return 'p';
  115. #endif
  116. #ifdef S_ISLNK
  117.   if (S_ISLNK (bits))
  118.     return 'l';
  119. #endif
  120. #ifdef S_ISSOCK
  121.   if (S_ISSOCK (bits))
  122.     return 's';
  123. #endif
  124. #ifdef S_ISMPC
  125.   if (S_ISMPC (bits))
  126.     return 'm';
  127. #endif
  128. #ifdef S_ISNWK
  129.   if (S_ISNWK (bits))
  130.     return 'n';
  131. #endif
  132.   return '?';
  133. }
  134.  
  135. /* Look at read, write, and execute bits in BITS and set
  136.    flags in CHARS accordingly. */
  137.  
  138. static void
  139. rwx (unsigned short bits, char *chars)
  140. {
  141.   chars[0] = (bits & S_IREAD) ? 'r' : '-';
  142.   chars[1] = (bits & S_IWRITE) ? 'w' : '-';
  143.   chars[2] = (bits & S_IEXEC) ? 'x' : '-';
  144. }
  145.  
  146. /* Set the 's' and 't' flags in file attributes string CHARS,
  147.    according to the file mode BITS. */
  148.  
  149. static void
  150. setst (unsigned short bits, char *chars)
  151. {
  152. #ifdef S_ISUID
  153.   if (bits & S_ISUID)
  154.     {
  155.       if (chars[3] != 'x')
  156.     /* Set-uid, but not executable by owner. */
  157.     chars[3] = 'S';
  158.       else
  159.     chars[3] = 's';
  160.     }
  161. #endif
  162. #ifdef S_ISGID
  163.   if (bits & S_ISGID)
  164.     {
  165.       if (chars[6] != 'x')
  166.     /* Set-gid, but not executable by group. */
  167.     chars[6] = 'S';
  168.       else
  169.     chars[6] = 's';
  170.     }
  171. #endif
  172. #ifdef S_ISVTX
  173.   if (bits & S_ISVTX)
  174.     {
  175.       if (chars[9] != 'x')
  176.     /* Sticky, but not executable by others. */
  177.     chars[9] = 'T';
  178.       else
  179.     chars[9] = 't';
  180.     }
  181. #endif
  182. }
  183.