1157016Sdes/* $OpenBSD: strmode.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ 298937Sdes/*- 398937Sdes * Copyright (c) 1990 The Regents of the University of California. 498937Sdes * All rights reserved. 598937Sdes * 698937Sdes * Redistribution and use in source and binary forms, with or without 798937Sdes * modification, are permitted provided that the following conditions 898937Sdes * are met: 998937Sdes * 1. Redistributions of source code must retain the above copyright 1098937Sdes * notice, this list of conditions and the following disclaimer. 1198937Sdes * 2. Redistributions in binary form must reproduce the above copyright 1298937Sdes * notice, this list of conditions and the following disclaimer in the 1398937Sdes * documentation and/or other materials provided with the distribution. 14124208Sdes * 3. Neither the name of the University nor the names of its contributors 1598937Sdes * may be used to endorse or promote products derived from this software 1698937Sdes * without specific prior written permission. 1798937Sdes * 1898937Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1998937Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2098937Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2198937Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2298937Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2398937Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2498937Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2598937Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2698937Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2798937Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2898937Sdes * SUCH DAMAGE. 2998937Sdes */ 3098937Sdes 31157016Sdes/* OPENBSD ORIGINAL: lib/libc/string/strmode.c */ 32157016Sdes 3398937Sdes#include "includes.h" 3498937Sdes#ifndef HAVE_STRMODE 3598937Sdes 3698937Sdes#include <sys/types.h> 3798937Sdes#include <sys/stat.h> 3898937Sdes#include <string.h> 3998937Sdes 40124208Sdes/* XXX mode should be mode_t */ 41124208Sdes 4298937Sdesvoid 43124208Sdesstrmode(int mode, char *p) 4498937Sdes{ 4598937Sdes /* print type */ 4698937Sdes switch (mode & S_IFMT) { 4798937Sdes case S_IFDIR: /* directory */ 4898937Sdes *p++ = 'd'; 4998937Sdes break; 5098937Sdes case S_IFCHR: /* character special */ 5198937Sdes *p++ = 'c'; 5298937Sdes break; 5398937Sdes case S_IFBLK: /* block special */ 5498937Sdes *p++ = 'b'; 5598937Sdes break; 5698937Sdes case S_IFREG: /* regular */ 5798937Sdes *p++ = '-'; 5898937Sdes break; 5998937Sdes case S_IFLNK: /* symbolic link */ 6098937Sdes *p++ = 'l'; 6198937Sdes break; 6298937Sdes#ifdef S_IFSOCK 6398937Sdes case S_IFSOCK: /* socket */ 6498937Sdes *p++ = 's'; 6598937Sdes break; 6698937Sdes#endif 6798937Sdes#ifdef S_IFIFO 6898937Sdes case S_IFIFO: /* fifo */ 6998937Sdes *p++ = 'p'; 7098937Sdes break; 7198937Sdes#endif 7298937Sdes default: /* unknown */ 7398937Sdes *p++ = '?'; 7498937Sdes break; 7598937Sdes } 7698937Sdes /* usr */ 7798937Sdes if (mode & S_IRUSR) 7898937Sdes *p++ = 'r'; 7998937Sdes else 8098937Sdes *p++ = '-'; 8198937Sdes if (mode & S_IWUSR) 8298937Sdes *p++ = 'w'; 8398937Sdes else 8498937Sdes *p++ = '-'; 8598937Sdes switch (mode & (S_IXUSR | S_ISUID)) { 8698937Sdes case 0: 8798937Sdes *p++ = '-'; 8898937Sdes break; 8998937Sdes case S_IXUSR: 9098937Sdes *p++ = 'x'; 9198937Sdes break; 9298937Sdes case S_ISUID: 9398937Sdes *p++ = 'S'; 9498937Sdes break; 9598937Sdes case S_IXUSR | S_ISUID: 9698937Sdes *p++ = 's'; 9798937Sdes break; 9898937Sdes } 9998937Sdes /* group */ 10098937Sdes if (mode & S_IRGRP) 10198937Sdes *p++ = 'r'; 10298937Sdes else 10398937Sdes *p++ = '-'; 10498937Sdes if (mode & S_IWGRP) 10598937Sdes *p++ = 'w'; 10698937Sdes else 10798937Sdes *p++ = '-'; 10898937Sdes switch (mode & (S_IXGRP | S_ISGID)) { 10998937Sdes case 0: 11098937Sdes *p++ = '-'; 11198937Sdes break; 11298937Sdes case S_IXGRP: 11398937Sdes *p++ = 'x'; 11498937Sdes break; 11598937Sdes case S_ISGID: 11698937Sdes *p++ = 'S'; 11798937Sdes break; 11898937Sdes case S_IXGRP | S_ISGID: 11998937Sdes *p++ = 's'; 12098937Sdes break; 12198937Sdes } 12298937Sdes /* other */ 12398937Sdes if (mode & S_IROTH) 12498937Sdes *p++ = 'r'; 12598937Sdes else 12698937Sdes *p++ = '-'; 12798937Sdes if (mode & S_IWOTH) 12898937Sdes *p++ = 'w'; 12998937Sdes else 13098937Sdes *p++ = '-'; 13198937Sdes switch (mode & (S_IXOTH | S_ISVTX)) { 13298937Sdes case 0: 13398937Sdes *p++ = '-'; 13498937Sdes break; 13598937Sdes case S_IXOTH: 13698937Sdes *p++ = 'x'; 13798937Sdes break; 13898937Sdes case S_ISVTX: 13998937Sdes *p++ = 'T'; 14098937Sdes break; 14198937Sdes case S_IXOTH | S_ISVTX: 14298937Sdes *p++ = 't'; 14398937Sdes break; 14498937Sdes } 14598937Sdes *p++ = ' '; /* will be a '+' if ACL's implemented */ 14698937Sdes *p = '\0'; 14798937Sdes} 14898937Sdes#endif 149