home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / FILEMODE.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  6KB  |  220 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 1, 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. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  19.    This port is also distributed under the terms of the
  20.    GNU General Public License as published by the
  21.    Free Software Foundation.
  22.  
  23.    Please note that this file is not identical to the
  24.    original GNU release, you should have received this
  25.    code as patch to the official release.
  26.  
  27.    $Header: e:/gnu/fileutil/RCS/filemode.c 1.4.0.1 90/09/17 08:18:12 tho Exp $
  28.  */
  29.  
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #ifdef _POSIX_SOURCE
  33. #define S_IREAD S_IRUSR
  34. #define S_IWRITE S_IWUSR
  35. #define S_IEXEC S_IXUSR
  36. #endif
  37.  
  38. #ifdef MSDOS
  39. extern void filemodestring (struct stat *, char *);
  40. extern void mode_string (unsigned short mode, char *str);
  41. static char ftypelet (unsigned short bits);
  42. static void rwx (unsigned short, char *);
  43. static void setst (unsigned short, char *);
  44. #else /* not MSDOS */
  45. void mode_string ();
  46. static char ftypelet ();
  47. static void rwx ();
  48. static void setst ();
  49. #endif /* not MSDOS */
  50.  
  51. /* filemodestring - fill in string STR with an ls-style ASCII
  52.    representation of the st_mode field of file stats block STATP.
  53.    10 characters are stored in STR; no terminating null is added.
  54.    The characters stored in STR are:
  55.  
  56.    0    File type.  'd' for directory, 'c' for character
  57.     special, 'b' for block special, 'm' for multiplex,
  58.     'l' for symbolic link, 's' for socket, 'p' for fifo,
  59.     '-' for regular, '?' for any other file type
  60.  
  61.    1    'r' if the owner may read, '-' otherwise.
  62.  
  63.    2    'w' if the owner may write, '-' otherwise.
  64.  
  65.    3    'x' if the owner may execute, 's' if the file is
  66.     set-user-id, '-' otherwise.
  67.     'S' if the file is set-user-id, but the execute
  68.     bit isn't set.
  69.  
  70.    4    'r' if group members may read, '-' otherwise.
  71.  
  72.    5    'w' if group members may write, '-' otherwise.
  73.  
  74.    6    'x' if group members may execute, 's' if the file is
  75.     set-group-id, '-' otherwise.
  76.     'S' if it is set-group-id but not executable.
  77.  
  78.    7    'r' if any user may read, '-' otherwise.
  79.  
  80.    8    'w' if any user may write, '-' otherwise.
  81.  
  82.    9    'x' if any user may execute, 't' if the file is "sticky"
  83.     (will be retained in swap space after execution), '-'
  84.     otherwise.
  85.     'T' if the file is sticky but not executable. */
  86.  
  87. void
  88. filemodestring (statp, str)
  89.      struct stat *statp;
  90.      char *str;
  91. {
  92.   mode_string (statp->st_mode, str);
  93. }
  94.  
  95. /* Like filemodestring, but only the relevant part of the `struct stat'
  96.    is given as an argument. */
  97.  
  98. void
  99. mode_string (mode, str)
  100.      unsigned short mode;
  101.      char *str;
  102. {
  103.   str[0] = ftypelet (mode);
  104.   rwx ((mode & 0700) << 0, &str[1]);
  105.   rwx ((mode & 0070) << 3, &str[4]);
  106.   rwx ((mode & 0007) << 6, &str[7]);
  107.   setst (mode, str);
  108. }
  109.  
  110. /* Return a character indicating the type of file described by
  111.    file mode BITS:
  112.    'd' for directories
  113.    'b' for block special files
  114.    'c' for character special files
  115.    'm' for multiplexor files
  116.    'l' for symbolic links
  117.    's' for sockets
  118.    'p' for fifos
  119.    '-' for regular files
  120.    '?' for any other file type. */
  121.  
  122. static char
  123. ftypelet (bits)
  124.      unsigned short bits;
  125. {
  126.   switch (bits & S_IFMT)
  127.     {
  128.     default:
  129.       return '?';
  130.     case S_IFREG:
  131.       return '-';
  132.     case S_IFDIR:
  133.       return 'd';
  134. #ifdef S_IFLNK
  135.     case S_IFLNK:
  136.       return 'l';
  137. #endif
  138. #ifdef S_IFCHR
  139.     case S_IFCHR:
  140.       return 'c';
  141. #endif
  142. #ifdef S_IFBLK
  143.     case S_IFBLK:
  144.       return 'b';
  145. #endif
  146. #ifdef S_IFMPC
  147.     case S_IFMPC:
  148.     case S_IFMPB:
  149.       return 'm';
  150. #endif
  151. #ifdef S_IFSOCK
  152.     case S_IFSOCK:
  153.       return 's';
  154. #endif
  155. #ifdef S_IFIFO
  156. #if S_IFIFO != S_IFSOCK
  157.     case S_IFIFO:
  158.       return 'p';
  159. #endif
  160. #endif
  161. #ifdef S_IFNWK            /* HP-UX */
  162.     case S_IFNWK:
  163.       return 'n';
  164. #endif
  165.     }
  166. }
  167.  
  168. /* Look at read, write, and execute bits in BITS and set
  169.    flags in CHARS accordingly. */
  170.  
  171. static void
  172. rwx (bits, chars)
  173.      unsigned short bits;
  174.      char *chars;
  175. {
  176.   chars[0] = (bits & S_IREAD) ? 'r' : '-';
  177.   chars[1] = (bits & S_IWRITE) ? 'w' : '-';
  178.   chars[2] = (bits & S_IEXEC) ? 'x' : '-';
  179. }
  180.  
  181. /* Set the 's' and 't' flags in file attributes string CHARS,
  182.    according to the file mode BITS. */
  183.  
  184. static void
  185. setst (bits, chars)
  186.      unsigned short bits;
  187.      char *chars;
  188. {
  189. #ifdef S_ISUID
  190.   if (bits & S_ISUID)
  191.     {
  192.       if (chars[3] != 'x')
  193.     /* Set-uid, but not executable by owner. */
  194.     chars[3] = 'S';
  195.       else
  196.     chars[3] = 's';
  197.     }
  198. #endif
  199. #ifdef S_ISGID
  200.   if (bits & S_ISGID)
  201.     {
  202.       if (chars[6] != 'x')
  203.     /* Set-gid, but not executable by group. */
  204.     chars[6] = 'S';
  205.       else
  206.     chars[6] = 's';
  207.     }
  208. #endif
  209. #ifdef S_ISVTX
  210.   if (bits & S_ISVTX)
  211.     {
  212.       if (chars[9] != 'x')
  213.     /* Sticky, but not executable by others. */
  214.     chars[9] = 'T';
  215.       else
  216.     chars[9] = 't';
  217.     }
  218. #endif
  219. }
  220.