home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / text / emacssrc.lha / emacs-18.58 / src / filemode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-26  |  5.1 KB  |  211 lines

  1. /* Examine the result of  stat  and make a string describing file modes.
  2.    Copyright (C) 1985 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. In other words, you are welcome to use, share and improve this program.
  19. You are forbidden to forbid anyone else to use, share and improve
  20. what you give them.   Help stamp out software-hoarding!  */
  21.  
  22.  
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25.  
  26. /* filemodestring - set file attribute data 
  27.  
  28. *** WARNING!  FILE STRUCTURE DEPENDENT ***
  29.  
  30.    Filemodestring converts the data in the st_mode field of file status
  31. block `s' to a 10 character attribute string, which it stores in
  32. the block that `a' points to.
  33. This attribute string is modelled after the string produced by the Berkeley ls.
  34.  
  35. As usual under Unix, the elements of the string are numbered
  36. from 0.  Their meanings are:
  37.  
  38.    0    File type.  'd' for directory, 'c' for character
  39.     special, 'b' for block special, 'm' for multiplex,
  40.     'l' for symbolic link, 's' for socket, 'p' for fifo,
  41.     '-' for any other file type
  42.  
  43.    1    'r' if the owner may read, '-' otherwise.
  44.  
  45.    2    'w' if the owner may write, '-' otherwise.
  46.  
  47.    3    'x' if the owner may execute, 's' if the file is
  48.     set-user-id, '-' otherwise.
  49.     'S' if the file is set-user-id, but the execute
  50.     bit isn't set.  (sys v `feature' which helps to
  51.     catch screw case.)
  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.  
  69.  */
  70.  
  71. #define VOID void
  72.  
  73. #ifdef AMIGA
  74. VOID
  75. filemodestring (s,a)
  76.    struct stat    *s;
  77.    char *a;
  78. {
  79.    a[0] = s->st_mode & S_IFDIR ? 'd' : '-';
  80.    a[7] = a[4] = a[1] = s->st_mode & S_IREAD ? 'r' : '-';
  81.    a[8] = a[5] = a[2] = s->st_mode & S_IREAD ? 'w' : '-';
  82.    a[9] = a[6] = a[3] = s->st_mode & S_IREAD ? 'x' : '-';
  83. }
  84.  
  85. #else /* not AMIGA */
  86.  
  87. static char ftypelet ();
  88. static VOID rwx (), setst ();
  89.  
  90. VOID
  91. filemodestring (s,a)
  92.    struct stat    *s;
  93.    char *a;
  94. {
  95.    a[0] = ftypelet (s);
  96.    /* Aren't there symbolic names for these byte-fields? */
  97.    rwx ((s->st_mode & 0700) << 0, &(a[1]));
  98.    rwx ((s->st_mode & 0070) << 3, &(a[4]));
  99.    rwx ((s->st_mode & 0007) << 6, &(a[7]));
  100.    setst (s->st_mode, a);
  101. }
  102.  
  103. /* ftypelet - file type letter
  104.  
  105. *** WARNING!  FILE STRUCTURE DEPENDENT ***
  106.  
  107.    Ftypelet accepts a file status block and returns a character
  108. code describing the type of the file.  'd' is returned for
  109. directories, 'b' for block special files, 'c' for character
  110. special files, 'm' for multiplexor files, 'l' for symbolic link,
  111. 's' for socket, 'p' for fifo, '-' for any other file type
  112.  */
  113.  
  114. static char
  115. ftypelet(s)
  116.    struct stat *s;
  117. {
  118.   switch (s->st_mode & S_IFMT)
  119.     {
  120.     default:
  121.       return '-';
  122.     case S_IFDIR:
  123.       return 'd';
  124. #ifdef S_IFLNK
  125.     case S_IFLNK:
  126.       return 'l';
  127. #endif
  128. #ifdef S_IFCHR
  129.     case S_IFCHR:
  130.       return 'c';
  131. #endif
  132. #ifdef S_IFBLK
  133.     case S_IFBLK:
  134.       return 'b';
  135. #endif
  136. #ifdef S_IFMPC
  137. /* These do not seem to exist */
  138.     case S_IFMPC:
  139.     case S_IFMPB:
  140.       return 'm';
  141. #endif
  142. #ifdef S_IFSOCK
  143.     case S_IFSOCK:
  144.       return 's';
  145. #endif
  146. #ifdef S_IFIFO
  147. #if S_IFIFO != S_IFSOCK
  148.     case S_IFIFO:
  149.       return 'p';
  150. #endif
  151. #endif
  152. #ifdef S_IFNWK /* hp-ux hack */
  153.     case S_IFNWK:
  154.       return 'n';
  155. #endif
  156.     }
  157. }
  158.  
  159.  
  160. /* rwx - look at read, write, and execute bits and set character
  161. flags accordingly
  162.  
  163. *** WARNING!  FILE STRUCTURE DEPENDENT ***
  164.  
  165.  */
  166.  
  167. static VOID
  168. rwx (bits, chars)
  169.    unsigned short bits;
  170.    char chars[];
  171. {
  172.   chars[0] = (bits & S_IREAD)  ? 'r' : '-';
  173.   chars[1] = (bits & S_IWRITE) ? 'w' : '-';
  174.   chars[2] = (bits & S_IEXEC)  ? 'x' : '-';
  175. }
  176.  
  177.  
  178. /* setst - set s & t flags in a file attributes string */
  179. /* *** WARNING!  FILE STRUCTURE DEPENDENT *** */
  180. static VOID
  181. setst (bits, chars)
  182.    unsigned short bits;
  183.    char chars[];
  184. {
  185. #ifdef S_ISUID
  186.    if (bits & S_ISUID)
  187.      {
  188.        if (chars[3] != 'x')
  189.      /* Screw case: set-uid, but not executable. */
  190.      chars[3] = 'S';
  191.        else
  192.      chars[3] = 's';
  193.      }
  194. #endif
  195. #ifdef S_ISGID
  196.    if (bits & S_ISGID)
  197.      {
  198.        if (chars[6] != 'x')
  199.      /* Screw case: set-gid, but not executable. */
  200.      chars[6] = 'S';
  201.        else
  202.      chars[6] = 's';
  203.      }
  204. #endif
  205. #ifdef S_ISVTX
  206.    if (bits & S_ISVTX)
  207.       chars[9] = 't';
  208. #endif
  209. }
  210. #endif /* not AMIGA */
  211.