Blame lib/filemode.c

Packit Service a2489d
/* filemode.c -- make a string describing file modes
Packit Service a2489d
Packit Service a2489d
   Copyright (C) 1985, 1990, 1993, 1998-2000, 2004, 2006, 2009-2018 Free
Packit Service a2489d
   Software Foundation, Inc.
Packit Service a2489d
Packit Service a2489d
   This program is free software: you can redistribute it and/or modify
Packit Service a2489d
   it under the terms of the GNU General Public License as published by
Packit Service a2489d
   the Free Software Foundation; either version 3 of the License, or
Packit Service a2489d
   (at your option) any later version.
Packit Service a2489d
Packit Service a2489d
   This program is distributed in the hope that it will be useful,
Packit Service a2489d
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2489d
   GNU General Public License for more details.
Packit Service a2489d
Packit Service a2489d
   You should have received a copy of the GNU General Public License
Packit Service a2489d
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service a2489d
Packit Service a2489d
#include <config.h>
Packit Service a2489d
Packit Service a2489d
#include "filemode.h"
Packit Service a2489d
Packit Service a2489d
/* The following is for Cray DMF (Data Migration Facility), which is a
Packit Service a2489d
   HSM file system.  A migrated file has a 'st_dm_mode' that is
Packit Service a2489d
   different from the normal 'st_mode', so any tests for migrated
Packit Service a2489d
   files should use the former.  */
Packit Service a2489d
#if HAVE_ST_DM_MODE
Packit Service a2489d
# define IS_MIGRATED_FILE(statp) \
Packit Service a2489d
    (S_ISOFD (statp->st_dm_mode) || S_ISOFL (statp->st_dm_mode))
Packit Service a2489d
#else
Packit Service a2489d
# define IS_MIGRATED_FILE(statp) 0
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
#if ! HAVE_DECL_STRMODE
Packit Service a2489d
Packit Service a2489d
/* Return a character indicating the type of file described by
Packit Service a2489d
   file mode BITS:
Packit Service a2489d
   '-' regular file
Packit Service a2489d
   'b' block special file
Packit Service a2489d
   'c' character special file
Packit Service a2489d
   'C' high performance ("contiguous data") file
Packit Service a2489d
   'd' directory
Packit Service a2489d
   'D' door
Packit Service a2489d
   'l' symbolic link
Packit Service a2489d
   'm' multiplexed file (7th edition Unix; obsolete)
Packit Service a2489d
   'n' network special file (HP-UX)
Packit Service a2489d
   'p' fifo (named pipe)
Packit Service a2489d
   'P' port
Packit Service a2489d
   's' socket
Packit Service a2489d
   'w' whiteout (4.4BSD)
Packit Service a2489d
   '?' some other file type  */
Packit Service a2489d
Packit Service a2489d
static char
Packit Service a2489d
ftypelet (mode_t bits)
Packit Service a2489d
{
Packit Service a2489d
  /* These are the most common, so test for them first.  */
Packit Service a2489d
  if (S_ISREG (bits))
Packit Service a2489d
    return '-';
Packit Service a2489d
  if (S_ISDIR (bits))
Packit Service a2489d
    return 'd';
Packit Service a2489d
Packit Service a2489d
  /* Other letters standardized by POSIX 1003.1-2004.  */
Packit Service a2489d
  if (S_ISBLK (bits))
Packit Service a2489d
    return 'b';
Packit Service a2489d
  if (S_ISCHR (bits))
Packit Service a2489d
    return 'c';
Packit Service a2489d
  if (S_ISLNK (bits))
Packit Service a2489d
    return 'l';
Packit Service a2489d
  if (S_ISFIFO (bits))
Packit Service a2489d
    return 'p';
Packit Service a2489d
Packit Service a2489d
  /* Other file types (though not letters) standardized by POSIX.  */
Packit Service a2489d
  if (S_ISSOCK (bits))
Packit Service a2489d
    return 's';
Packit Service a2489d
Packit Service a2489d
  /* Nonstandard file types.  */
Packit Service a2489d
  if (S_ISCTG (bits))
Packit Service a2489d
    return 'C';
Packit Service a2489d
  if (S_ISDOOR (bits))
Packit Service a2489d
    return 'D';
Packit Service a2489d
  if (S_ISMPB (bits) || S_ISMPC (bits) || S_ISMPX (bits))
Packit Service a2489d
    return 'm';
Packit Service a2489d
  if (S_ISNWK (bits))
Packit Service a2489d
    return 'n';
Packit Service a2489d
  if (S_ISPORT (bits))
Packit Service a2489d
    return 'P';
Packit Service a2489d
  if (S_ISWHT (bits))
Packit Service a2489d
    return 'w';
Packit Service a2489d
Packit Service a2489d
  return '?';
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
/* Like filemodestring, but rely only on MODE.  */
Packit Service a2489d
Packit Service a2489d
void
Packit Service a2489d
strmode (mode_t mode, char *str)
Packit Service a2489d
{
Packit Service a2489d
  str[0] = ftypelet (mode);
Packit Service a2489d
  str[1] = mode & S_IRUSR ? 'r' : '-';
Packit Service a2489d
  str[2] = mode & S_IWUSR ? 'w' : '-';
Packit Service a2489d
  str[3] = (mode & S_ISUID
Packit Service a2489d
            ? (mode & S_IXUSR ? 's' : 'S')
Packit Service a2489d
            : (mode & S_IXUSR ? 'x' : '-'));
Packit Service a2489d
  str[4] = mode & S_IRGRP ? 'r' : '-';
Packit Service a2489d
  str[5] = mode & S_IWGRP ? 'w' : '-';
Packit Service a2489d
  str[6] = (mode & S_ISGID
Packit Service a2489d
            ? (mode & S_IXGRP ? 's' : 'S')
Packit Service a2489d
            : (mode & S_IXGRP ? 'x' : '-'));
Packit Service a2489d
  str[7] = mode & S_IROTH ? 'r' : '-';
Packit Service a2489d
  str[8] = mode & S_IWOTH ? 'w' : '-';
Packit Service a2489d
  str[9] = (mode & S_ISVTX
Packit Service a2489d
            ? (mode & S_IXOTH ? 't' : 'T')
Packit Service a2489d
            : (mode & S_IXOTH ? 'x' : '-'));
Packit Service a2489d
  str[10] = ' ';
Packit Service a2489d
  str[11] = '\0';
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
#endif /* ! HAVE_DECL_STRMODE */
Packit Service a2489d
Packit Service a2489d
/* filemodestring - fill in string STR with an ls-style ASCII
Packit Service a2489d
   representation of the st_mode field of file stats block STATP.
Packit Service a2489d
   12 characters are stored in STR.
Packit Service a2489d
   The characters stored in STR are:
Packit Service a2489d
Packit Service a2489d
   0    File type, as in ftypelet above, except that other letters are used
Packit Service a2489d
        for files whose type cannot be determined solely from st_mode:
Packit Service a2489d
Packit Service a2489d
            'F' semaphore
Packit Service a2489d
            'M' migrated file (Cray DMF)
Packit Service a2489d
            'Q' message queue
Packit Service a2489d
            'S' shared memory object
Packit Service a2489d
            'T' typed memory object
Packit Service a2489d
Packit Service a2489d
   1    'r' if the owner may read, '-' otherwise.
Packit Service a2489d
Packit Service a2489d
   2    'w' if the owner may write, '-' otherwise.
Packit Service a2489d
Packit Service a2489d
   3    'x' if the owner may execute, 's' if the file is
Packit Service a2489d
        set-user-id, '-' otherwise.
Packit Service a2489d
        'S' if the file is set-user-id, but the execute
Packit Service a2489d
        bit isn't set.
Packit Service a2489d
Packit Service a2489d
   4    'r' if group members may read, '-' otherwise.
Packit Service a2489d
Packit Service a2489d
   5    'w' if group members may write, '-' otherwise.
Packit Service a2489d
Packit Service a2489d
   6    'x' if group members may execute, 's' if the file is
Packit Service a2489d
        set-group-id, '-' otherwise.
Packit Service a2489d
        'S' if it is set-group-id but not executable.
Packit Service a2489d
Packit Service a2489d
   7    'r' if any user may read, '-' otherwise.
Packit Service a2489d
Packit Service a2489d
   8    'w' if any user may write, '-' otherwise.
Packit Service a2489d
Packit Service a2489d
   9    'x' if any user may execute, 't' if the file is "sticky"
Packit Service a2489d
        (will be retained in swap space after execution), '-'
Packit Service a2489d
        otherwise.
Packit Service a2489d
        'T' if the file is sticky but not executable.
Packit Service a2489d
Packit Service a2489d
   10   ' ' for compatibility with 4.4BSD strmode,
Packit Service a2489d
        since this interface does not support ACLs.
Packit Service a2489d
Packit Service a2489d
   11   '\0'.  */
Packit Service a2489d
Packit Service a2489d
void
Packit Service a2489d
filemodestring (struct stat const *statp, char *str)
Packit Service a2489d
{
Packit Service a2489d
  strmode (statp->st_mode, str);
Packit Service a2489d
Packit Service a2489d
  if (S_TYPEISSEM (statp))
Packit Service a2489d
    str[0] = 'F';
Packit Service a2489d
  else if (IS_MIGRATED_FILE (statp))
Packit Service a2489d
    str[0] = 'M';
Packit Service a2489d
  else if (S_TYPEISMQ (statp))
Packit Service a2489d
    str[0] = 'Q';
Packit Service a2489d
  else if (S_TYPEISSHM (statp))
Packit Service a2489d
    str[0] = 'S';
Packit Service a2489d
  else if (S_TYPEISTMO (statp))
Packit Service a2489d
    str[0] = 'T';
Packit Service a2489d
}