1178476Sjb/*-
2178476Sjb * Copyright (c) 2003-2007 Tim Kientzle
3178476Sjb * All rights reserved.
4178476Sjb *
5178476Sjb * Redistribution and use in source and binary forms, with or without
6178476Sjb * modification, are permitted provided that the following conditions
7178476Sjb * are met:
8178476Sjb * 1. Redistributions of source code must retain the above copyright
9178476Sjb *    notice, this list of conditions and the following disclaimer.
10178476Sjb * 2. Redistributions in binary form must reproduce the above copyright
11178476Sjb *    notice, this list of conditions and the following disclaimer in the
12178476Sjb *    documentation and/or other materials provided with the distribution.
13178476Sjb *
14178476Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15178476Sjb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16178476Sjb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17178476Sjb * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18178476Sjb * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19178476Sjb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20178476Sjb * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21178476Sjb * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22178476Sjb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23178476Sjb * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24178476Sjb */
25178476Sjb
26178476Sjb#include "archive_platform.h"
27178476Sjb__FBSDID("$FreeBSD$");
28178476Sjb
29178476Sjb#ifdef HAVE_SYS_STAT_H
30178476Sjb#include <sys/stat.h>
31178476Sjb#endif
32178476Sjb#ifdef HAVE_STRING_H
33178476Sjb#include <string.h>
34178476Sjb#endif
35178476Sjb
36178476Sjb#include "archive_entry.h"
37178476Sjb#include "archive_entry_private.h"
38178476Sjb
39178476Sjbconst char *
40178476Sjbarchive_entry_strmode(struct archive_entry *entry)
41178476Sjb{
42178476Sjb	static const mode_t permbits[] =
43178476Sjb	    { 0400, 0200, 0100, 0040, 0020, 0010, 0004, 0002, 0001 };
44178476Sjb	char *bp = entry->strmode;
45178476Sjb	mode_t mode;
46178476Sjb	int i;
47178476Sjb
48178476Sjb	/* Fill in a default string, then selectively override. */
49178476Sjb	strcpy(bp, "?rwxrwxrwx ");
50178476Sjb
51178476Sjb	mode = archive_entry_mode(entry);
52178476Sjb	switch (archive_entry_filetype(entry)) {
53178476Sjb	case AE_IFREG:  bp[0] = '-'; break;
54178476Sjb	case AE_IFBLK:  bp[0] = 'b'; break;
55178476Sjb	case AE_IFCHR:  bp[0] = 'c'; break;
56178476Sjb	case AE_IFDIR:  bp[0] = 'd'; break;
57178476Sjb	case AE_IFLNK:  bp[0] = 'l'; break;
58178476Sjb	case AE_IFSOCK: bp[0] = 's'; break;
59178476Sjb	case AE_IFIFO:  bp[0] = 'p'; break;
60178476Sjb	default:
61178476Sjb		if (archive_entry_hardlink(entry) != NULL) {
62178476Sjb			bp[0] = 'h';
63178476Sjb			break;
64178476Sjb		}
65178476Sjb	}
66178476Sjb
67178476Sjb	for (i = 0; i < 9; i++)
68178476Sjb		if (!(mode & permbits[i]))
69178476Sjb			bp[i+1] = '-';
70178476Sjb
71178476Sjb	if (mode & S_ISUID) {
72178476Sjb		if (mode & 0100) bp[3] = 's';
73178476Sjb		else bp[3] = 'S';
74178476Sjb	}
75178476Sjb	if (mode & S_ISGID) {
76178476Sjb		if (mode & 0010) bp[6] = 's';
77178476Sjb		else bp[6] = 'S';
78178476Sjb	}
79178476Sjb	if (mode & S_ISVTX) {
80178476Sjb		if (mode & 0001) bp[9] = 't';
81178476Sjb		else bp[9] = 'T';
82178476Sjb	}
83	if (archive_entry_acl_count(entry, ARCHIVE_ENTRY_ACL_TYPE_ACCESS))
84		bp[10] = '+';
85
86	return (bp);
87}
88