Path: blob/main/core/coreutils/src/compat/strmode.c
1067 views
/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <sys/types.h>32#include <sys/stat.h>33#include <string.h>3435void strmode(/* mode_t */ int mode, char *p) {36/* print type */37switch (mode & S_IFMT) {38case S_IFDIR: /* directory */39*p++ = 'd';40break;41case S_IFCHR: /* character special */42*p++ = 'c';43break;44case S_IFBLK: /* block special */45*p++ = 'b';46break;47case S_IFREG: /* regular */48*p++ = '-';49break;50case S_IFLNK: /* symbolic link */51*p++ = 'l';52break;53case S_IFSOCK: /* socket */54*p++ = 's';55break;56#ifdef S_IFWHT57case S_IFWHT: /* whiteout */58*p++ = 'w';59break;60#endif61default: /* unknown */62*p++ = '?';63break;64}65/* usr */66if (mode & S_IRUSR)67*p++ = 'r';68else69*p++ = '-';70if (mode & S_IWUSR)71*p++ = 'w';72else73*p++ = '-';74switch (mode & (S_IXUSR | S_ISUID)) {75case 0:76*p++ = '-';77break;78case S_IXUSR:79*p++ = 'x';80break;81case S_ISUID:82*p++ = 'S';83break;84case S_IXUSR | S_ISUID:85*p++ = 's';86break;87}88/* group */89if (mode & S_IRGRP)90*p++ = 'r';91else92*p++ = '-';93if (mode & S_IWGRP)94*p++ = 'w';95else96*p++ = '-';97switch (mode & (S_IXGRP | S_ISGID)) {98case 0:99*p++ = '-';100break;101case S_IXGRP:102*p++ = 'x';103break;104case S_ISGID:105*p++ = 'S';106break;107case S_IXGRP | S_ISGID:108*p++ = 's';109break;110}111/* other */112if (mode & S_IROTH)113*p++ = 'r';114else115*p++ = '-';116if (mode & S_IWOTH)117*p++ = 'w';118else119*p++ = '-';120switch (mode & (S_IXOTH | S_ISVTX)) {121case 0:122*p++ = '-';123break;124case S_IXOTH:125*p++ = 'x';126break;127case S_ISVTX:128*p++ = 'T';129break;130case S_IXOTH | S_ISVTX:131*p++ = 't';132break;133}134*p++ = ' ';135*p = '\0';136}137138139