stagit

humanreadable filemodes (code from sbase ls)

Hiltjo Posthuma contact@arjunchoudhary.com

commit: 55f98df parent: 35c2cbd
1 files changed, 43 insertions(+), 1 deletions(-)
Murmoms.c+43-1
M · urmoms.c +43, -1
 1@@ -633,6 +633,48 @@ writeblob(git_object *obj, const char *filename, git_off_t filesize)
 2 	return 0;
 3 }
 4 
 5+const char *
 6+filemode(git_filemode_t m)
 7+{
 8+	static char mode[11];
 9+
10+	memset(mode, '-', sizeof(mode) - 1);
11+	mode[10] = '\0';
12+
13+	if (S_ISREG(m))
14+		mode[0] = '-';
15+	else if (S_ISBLK(m))
16+		mode[0] = 'b';
17+	else if (S_ISCHR(m))
18+		mode[0] = 'c';
19+	else if (S_ISDIR(m))
20+		mode[0] = 'd';
21+	else if (S_ISFIFO(m))
22+		mode[0] = 'p';
23+	else if (S_ISLNK(m))
24+		mode[0] = 'l';
25+	else if (S_ISSOCK(m))
26+		mode[0] = 's';
27+	else
28+		mode[0] = '?';
29+
30+	if (m & S_IRUSR) mode[1] = 'r';
31+	if (m & S_IWUSR) mode[2] = 'w';
32+	if (m & S_IXUSR) mode[3] = 'x';
33+	if (m & S_IRGRP) mode[4] = 'r';
34+	if (m & S_IWGRP) mode[5] = 'w';
35+	if (m & S_IXGRP) mode[6] = 'x';
36+	if (m & S_IROTH) mode[7] = 'r';
37+	if (m & S_IWOTH) mode[8] = 'w';
38+	if (m & S_IXOTH) mode[9] = 'x';
39+
40+	if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
41+	if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
42+	if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
43+
44+	return mode;
45+}
46+
47 int
48 writefilestree(FILE *fp, git_tree *tree, const char *path)
49 {
50@@ -673,7 +715,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
51 		filesize = git_blob_rawsize((git_blob *)obj);
52 
53 		fputs("<tr><td>", fp);
54-		fprintf(fp, "%u", git_tree_entry_filemode_raw(entry));
55+		fprintf(fp, "%s", filemode(git_tree_entry_filemode(entry)));
56 		fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
57 		xmlencode(fp, filename, strlen(filename));
58 		fputs(".html\">", fp);