archive_entry_strmode.c revision 228753
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 2003-2007 Tim Kientzle
31553Srgrimes * All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes *
141553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
151553Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161553Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171553Srgrimes * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
181553Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191553Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201553Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211553Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221553Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231553Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241553Srgrimes */
251553Srgrimes
261553Srgrimes#include "archive_platform.h"
271553Srgrimes__FBSDID("$FreeBSD: src/lib/libarchive/archive_entry_strmode.c,v 1.4 2008/06/15 05:14:01 kientzle Exp $");
281553Srgrimes
2950479Speter#ifdef HAVE_SYS_STAT_H
301553Srgrimes#include <sys/stat.h>
31283875Ssmh#endif
321553Srgrimes#ifdef HAVE_STRING_H
3379537Sru#include <string.h>
341553Srgrimes#endif
351553Srgrimes
361553Srgrimes#include "archive_entry.h"
371553Srgrimes#include "archive_entry_private.h"
3868965Sru
39204165Sgavinconst char *
401553Srgrimesarchive_entry_strmode(struct archive_entry *entry)
411553Srgrimes{
421553Srgrimes	static const mode_t permbits[] =
431553Srgrimes	    { 0400, 0200, 0100, 0040, 0020, 0010, 0004, 0002, 0001 };
4471898Sru	char *bp = entry->strmode;
4572432Sru	mode_t mode;
4668965Sru	int i;
47204165Sgavin
481553Srgrimes	/* Fill in a default string, then selectively override. */
491553Srgrimes	strcpy(bp, "?rwxrwxrwx ");
501553Srgrimes
511553Srgrimes	mode = archive_entry_mode(entry);
5271898Sru	switch (archive_entry_filetype(entry)) {
5329105Scharnier	case AE_IFREG:  bp[0] = '-'; break;
541553Srgrimes	case AE_IFBLK:  bp[0] = 'b'; break;
5599968Scharnier	case AE_IFCHR:  bp[0] = 'c'; break;
5699968Scharnier	case AE_IFDIR:  bp[0] = 'd'; break;
5799968Scharnier	case AE_IFLNK:  bp[0] = 'l'; break;
5894309Strhodes	case AE_IFSOCK: bp[0] = 's'; break;
5997558Sru	case AE_IFIFO:  bp[0] = 'p'; break;
6094309Strhodes	default:
611553Srgrimes		if (archive_entry_hardlink(entry) != NULL) {
621553Srgrimes			bp[0] = 'h';
631553Srgrimes			break;
641553Srgrimes		}
651553Srgrimes	}
661553Srgrimes
67283875Ssmh	for (i = 0; i < 9; i++)
68283875Ssmh		if (!(mode & permbits[i]))
69283875Ssmh			bp[i+1] = '-';
701553Srgrimes
711553Srgrimes	if (mode & S_ISUID) {
721553Srgrimes		if (mode & 0100) bp[3] = 's';
731553Srgrimes		else bp[3] = 'S';
741553Srgrimes	}
751553Srgrimes	if (mode & S_ISGID) {
761553Srgrimes		if (mode & 0010) bp[6] = 's';
771553Srgrimes		else bp[6] = 'S';
7877160Sru	}
791553Srgrimes	if (mode & S_ISVTX) {
80283875Ssmh		if (mode & 0001) bp[9] = 't';
81283875Ssmh		else bp[9] = 'T';
82109845Skeramida	}
83109845Skeramida	if (archive_entry_acl_count(entry, ARCHIVE_ENTRY_ACL_TYPE_ACCESS))
84109845Skeramida		bp[10] = '+';
85109845Skeramida
861553Srgrimes	return (bp);
87141846Sru}
881553Srgrimes