Factor out common logic of unchanged_attrs and itemize into report_ATTR
[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
8e41b68e
WD
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
0f78b815
WD
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 *
e7c67065 19 * You should have received a copy of the GNU General Public License along
4fd842f9 20 * with this program; if not, visit the http://fsf.org website.
0f78b815 21 */
740819ef 22
0e916c60 23#include "rsync.h"
740819ef 24
0f78b815
WD
25/* Produce a string representation of Unix mode bits like that used by ls(1).
26 * The "buf" buffer must be at least 11 characters. */
65854cf9 27void permstring(char *perms, mode_t mode)
740819ef
MP
28{
29 static const char *perm_map = "rwxrwxrwx";
30 int i;
31
c9bce0b8 32 strlcpy(perms, "----------", 11);
0f78b815
WD
33
34 for (i = 0; i < 9; i++) {
35 if (mode & (1 << i))
36 perms[9-i] = perm_map[8-i];
740819ef 37 }
087173c8
MP
38
39 /* Handle setuid/sticky bits. You might think the indices are
40 * off by one, but remember there's a type char at the
41 * start. */
42 if (mode & S_ISUID)
43 perms[3] = (mode & S_IXUSR) ? 's' : 'S';
44
45 if (mode & S_ISGID)
46 perms[6] = (mode & S_IXGRP) ? 's' : 'S';
0f78b815 47
7ea84b68 48#ifdef S_ISVTX
087173c8
MP
49 if (mode & S_ISVTX)
50 perms[9] = (mode & S_IXOTH) ? 't' : 'T';
7ea84b68 51#endif
740819ef 52
0f78b815
WD
53 if (S_ISDIR(mode))
54 perms[0] = 'd';
55 else if (S_ISLNK(mode))
56 perms[0] = 'l';
57 else if (S_ISBLK(mode))
58 perms[0] = 'b';
59 else if (S_ISCHR(mode))
60 perms[0] = 'c';
61 else if (S_ISSOCK(mode))
62 perms[0] = 's';
63 else if (S_ISFIFO(mode))
64 perms[0] = 'p';
65}