home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / fileutil.lha / fileutils-3.3 / lib / filemode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  5.9 KB  |  241 lines

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