メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

filemode.c

解説を見る。
00001 /* filemode.c -- make a string describing file modes
00002    Copyright (C) 1985, 1990, 1993 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software
00016    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.  */
00017 
00018 #ifdef HAVE_CONFIG_H
00019 #include <config.h>
00020 #endif
00021 
00022 #ifdef HAVE_SYS_TYPES_H
00023 #include <sys/types.h>
00024 #endif
00025 #include <sys/stat.h>
00026 
00027 #if !S_IRUSR
00028 # if S_IREAD
00029 #  define S_IRUSR S_IREAD
00030 # else
00031 #  define S_IRUSR 00400
00032 # endif
00033 #endif
00034 
00035 #if !S_IWUSR
00036 # if S_IWRITE
00037 #  define S_IWUSR S_IWRITE
00038 # else
00039 #  define S_IWUSR 00200
00040 # endif
00041 #endif
00042 
00043 #if !S_IXUSR
00044 # if S_IEXEC
00045 #  define S_IXUSR S_IEXEC
00046 # else
00047 #  define S_IXUSR 00100
00048 # endif
00049 #endif
00050 
00051 #ifdef STAT_MACROS_BROKEN
00052 #undef S_ISBLK
00053 #undef S_ISCHR
00054 #undef S_ISDIR
00055 #undef S_ISFIFO
00056 #undef S_ISLNK
00057 #undef S_ISMPB
00058 #undef S_ISMPC
00059 #undef S_ISNWK
00060 #undef S_ISREG
00061 #undef S_ISSOCK
00062 #endif /* STAT_MACROS_BROKEN.  */
00063 
00064 #if !defined(S_ISBLK) && defined(S_IFBLK)
00065 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
00066 #endif
00067 #if !defined(S_ISCHR) && defined(S_IFCHR)
00068 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
00069 #endif
00070 #if !defined(S_ISDIR) && defined(S_IFDIR)
00071 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
00072 #endif
00073 #if !defined(S_ISREG) && defined(S_IFREG)
00074 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
00075 #endif
00076 #if !defined(S_ISFIFO) && defined(S_IFIFO)
00077 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
00078 #endif
00079 #if !defined(S_ISLNK) && defined(S_IFLNK)
00080 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
00081 #endif
00082 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
00083 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
00084 #endif
00085 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
00086 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
00087 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
00088 #endif
00089 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
00090 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
00091 #endif
00092 
00093 void mode_string ();
00094 static char ftypelet ();
00095 static void rwx ();
00096 static void setst ();
00097 
00098 /* filemodestring - fill in string STR with an ls-style ASCII
00099    representation of the st_mode field of file stats block STATP.
00100    10 characters are stored in STR; no terminating null is added.
00101    The characters stored in STR are:
00102 
00103    0    File type.  'd' for directory, 'c' for character
00104         special, 'b' for block special, 'm' for multiplex,
00105         'l' for symbolic link, 's' for socket, 'p' for fifo,
00106         '-' for regular, '?' for any other file type
00107 
00108    1    'r' if the owner may read, '-' otherwise.
00109 
00110    2    'w' if the owner may write, '-' otherwise.
00111 
00112    3    'x' if the owner may execute, 's' if the file is
00113         set-user-id, '-' otherwise.
00114         'S' if the file is set-user-id, but the execute
00115         bit isn't set.
00116 
00117    4    'r' if group members may read, '-' otherwise.
00118 
00119    5    'w' if group members may write, '-' otherwise.
00120 
00121    6    'x' if group members may execute, 's' if the file is
00122         set-group-id, '-' otherwise.
00123         'S' if it is set-group-id but not executable.
00124 
00125    7    'r' if any user may read, '-' otherwise.
00126 
00127    8    'w' if any user may write, '-' otherwise.
00128 
00129    9    'x' if any user may execute, 't' if the file is "sticky"
00130         (will be retained in swap space after execution), '-'
00131         otherwise.
00132         'T' if the file is sticky but not executable.  */
00133 
00134 void
00135 filemodestring (statp, str)
00136      struct stat *statp;
00137      char *str;
00138 {
00139   mode_string (statp->st_mode, str);
00140 }
00141 
00142 /* Like filemodestring, but only the relevant part of the `struct stat'
00143    is given as an argument.  */
00144 
00145 void
00146 mode_string (mode, str)
00147      unsigned short mode;
00148      char *str;
00149 {
00150   str[0] = ftypelet ((long) mode);
00151   rwx ((mode & 0700) << 0, &str[1]);
00152   rwx ((mode & 0070) << 3, &str[4]);
00153   rwx ((mode & 0007) << 6, &str[7]);
00154   setst (mode, str);
00155 }
00156 
00157 /* Return a character indicating the type of file described by
00158    file mode BITS:
00159    'd' for directories
00160    'b' for block special files
00161    'c' for character special files
00162    'm' for multiplexor files
00163    'l' for symbolic links
00164    's' for sockets
00165    'p' for fifos
00166    '-' for regular files
00167    '?' for any other file type.  */
00168 
00169 static char
00170 ftypelet (bits)
00171      long bits;
00172 {
00173 #ifdef S_ISBLK
00174   if (S_ISBLK (bits))
00175     return 'b';
00176 #endif
00177   if (S_ISCHR (bits))
00178     return 'c';
00179   if (S_ISDIR (bits))
00180     return 'd';
00181   if (S_ISREG (bits))
00182     return '-';
00183 #ifdef S_ISFIFO
00184   if (S_ISFIFO (bits))
00185     return 'p';
00186 #endif
00187 #ifdef S_ISLNK
00188   if (S_ISLNK (bits))
00189     return 'l';
00190 #endif
00191 #ifdef S_ISSOCK
00192   if (S_ISSOCK (bits))
00193     return 's';
00194 #endif
00195 #ifdef S_ISMPC
00196   if (S_ISMPC (bits))
00197     return 'm';
00198 #endif
00199 #ifdef S_ISNWK
00200   if (S_ISNWK (bits))
00201     return 'n';
00202 #endif
00203   return '?';
00204 }
00205 
00206 /* Look at read, write, and execute bits in BITS and set
00207    flags in CHARS accordingly.  */
00208 
00209 static void
00210 rwx (bits, chars)
00211      unsigned short bits;
00212      char *chars;
00213 {
00214   chars[0] = (bits & S_IRUSR) ? 'r' : '-';
00215   chars[1] = (bits & S_IWUSR) ? 'w' : '-';
00216   chars[2] = (bits & S_IXUSR) ? 'x' : '-';
00217 }
00218 
00219 /* Set the 's' and 't' flags in file attributes string CHARS,
00220    according to the file mode BITS.  */
00221 
00222 static void
00223 setst (bits, chars)
00224      unsigned short bits;
00225      char *chars;
00226 {
00227 #ifdef S_ISUID
00228   if (bits & S_ISUID)
00229     {
00230       if (chars[3] != 'x')
00231         /* Set-uid, but not executable by owner.  */
00232         chars[3] = 'S';
00233       else
00234         chars[3] = 's';
00235     }
00236 #endif
00237 #ifdef S_ISGID
00238   if (bits & S_ISGID)
00239     {
00240       if (chars[6] != 'x')
00241         /* Set-gid, but not executable by group.  */
00242         chars[6] = 'S';
00243       else
00244         chars[6] = 's';
00245     }
00246 #endif
00247 #ifdef S_ISVTX
00248   if (bits & S_ISVTX)
00249     {
00250       if (chars[9] != 'x')
00251         /* Sticky, but not executable by others.  */
00252         chars[9] = 'T';
00253       else
00254         chars[9] = 't';
00255     }
00256 #endif
00257 }

Wed Dec 29 11:51:35 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送