archive_entry_strmode.c revision 228761
1248619Sdes/*- 265668Skris * Copyright (c) 2003-2007 Tim Kientzle 3126274Sdes * All rights reserved. 465668Skris * 5126274Sdes * Redistribution and use in source and binary forms, with or without 6126274Sdes * modification, are permitted provided that the following conditions 7126274Sdes * are met: 865668Skris * 1. Redistributions of source code must retain the above copyright 9126274Sdes * notice, this list of conditions and the following disclaimer. 10126274Sdes * 2. Redistributions in binary form must reproduce the above copyright 11126274Sdes * notice, this list of conditions and the following disclaimer in the 12126274Sdes * documentation and/or other materials provided with the distribution. 13126274Sdes * 14126274Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15126274Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1665668Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17162852Sdes * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 1865668Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1965668Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20162852Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21162852Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22162852Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23162852Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24162852Sdes */ 25162852Sdes 26181111Sdes#include "archive_platform.h" 27181111Sdes__FBSDID("$FreeBSD: src/lib/libarchive/archive_entry_strmode.c,v 1.4 2008/06/15 05:14:01 kientzle Exp $"); 28181111Sdes 29181111Sdes#ifdef HAVE_SYS_STAT_H 30181111Sdes#include <sys/stat.h> 31181111Sdes#endif 32162852Sdes#ifdef HAVE_STRING_H 33162852Sdes#include <string.h> 34162852Sdes#endif 35162852Sdes 36162852Sdes#include "archive_entry.h" 37162852Sdes#include "archive_entry_private.h" 38162852Sdes 39162852Sdesconst char * 40162852Sdesarchive_entry_strmode(struct archive_entry *entry) 41162852Sdes{ 42162852Sdes static const mode_t permbits[] = 43162852Sdes { 0400, 0200, 0100, 0040, 0020, 0010, 0004, 0002, 0001 }; 44162852Sdes char *bp = entry->strmode; 45162852Sdes mode_t mode; 4665668Skris int i; 4776259Sgreen 48157016Sdes /* Fill in a default string, then selectively override. */ 49162852Sdes strcpy(bp, "?rwxrwxrwx "); 5065668Skris 5176259Sgreen mode = archive_entry_mode(entry); 5276259Sgreen switch (archive_entry_filetype(entry)) { 5365668Skris case AE_IFREG: bp[0] = '-'; break; 5465668Skris case AE_IFBLK: bp[0] = 'b'; break; 5576259Sgreen case AE_IFCHR: bp[0] = 'c'; break; 5665668Skris case AE_IFDIR: bp[0] = 'd'; break; 5765668Skris case AE_IFLNK: bp[0] = 'l'; break; 5865668Skris case AE_IFSOCK: bp[0] = 's'; break; 59162852Sdes case AE_IFIFO: bp[0] = 'p'; break; 60162852Sdes default: 6198937Sdes if (archive_entry_hardlink(entry) != NULL) { 62162852Sdes bp[0] = 'h'; 63162852Sdes break; 64162852Sdes } 65162852Sdes } 6665668Skris 6765668Skris for (i = 0; i < 9; i++) 6865668Skris if (!(mode & permbits[i])) 6965668Skris bp[i+1] = '-'; 7076259Sgreen 71226046Sdes if (mode & S_ISUID) { 7276259Sgreen if (mode & 0100) bp[3] = 's'; 73204917Sdes else bp[3] = 'S'; 74204917Sdes } 75204917Sdes if (mode & S_ISGID) { 76124208Sdes if (mode & 0010) bp[6] = 's'; 7765668Skris else bp[6] = 'S'; 7865668Skris } 7965668Skris if (mode & S_ISVTX) { 8076259Sgreen if (mode & 0001) bp[9] = 't'; 8165668Skris else bp[9] = 'T'; 8265668Skris } 8365668Skris if (archive_entry_acl_count(entry, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)) 8465668Skris bp[10] = '+'; 8565668Skris 8692555Sdes return (bp); 8765668Skris} 8865668Skris