- Updated the address for the FSF in the opening comment.
[rsync/rsync.git] / lib / permstring.c
CommitLineData
0f78b815
WD
1/*
2 * A single utility routine.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2003, 2006 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
740819ef 23
0e916c60 24#include "rsync.h"
740819ef 25
0f78b815
WD
26/* Produce a string representation of Unix mode bits like that used by ls(1).
27 * The "buf" buffer must be at least 11 characters. */
65854cf9 28void permstring(char *perms, mode_t mode)
740819ef
MP
29{
30 static const char *perm_map = "rwxrwxrwx";
31 int i;
32
33 strcpy(perms, "----------");
0f78b815
WD
34
35 for (i = 0; i < 9; i++) {
36 if (mode & (1 << i))
37 perms[9-i] = perm_map[8-i];
740819ef 38 }
087173c8
MP
39
40 /* Handle setuid/sticky bits. You might think the indices are
41 * off by one, but remember there's a type char at the
42 * start. */
43 if (mode & S_ISUID)
44 perms[3] = (mode & S_IXUSR) ? 's' : 'S';
45
46 if (mode & S_ISGID)
47 perms[6] = (mode & S_IXGRP) ? 's' : 'S';
0f78b815 48
7ea84b68 49#ifdef S_ISVTX
087173c8
MP
50 if (mode & S_ISVTX)
51 perms[9] = (mode & S_IXOTH) ? 't' : 'T';
7ea84b68 52#endif
740819ef 53
0f78b815
WD
54 if (S_ISDIR(mode))
55 perms[0] = 'd';
56 else if (S_ISLNK(mode))
57 perms[0] = 'l';
58 else if (S_ISBLK(mode))
59 perms[0] = 'b';
60 else if (S_ISCHR(mode))
61 perms[0] = 'c';
62 else if (S_ISSOCK(mode))
63 perms[0] = 's';
64 else if (S_ISFIFO(mode))
65 perms[0] = 'p';
66}