-
Notifications
You must be signed in to change notification settings - Fork 21
/
json.c
188 lines (160 loc) · 5.7 KB
/
json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* $Copyright: $
* Copyright (c) 1996 - 2022 by Steve Baker ([email protected])
* All Rights reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tree.h"
extern bool dflag, lflag, pflag, sflag, Fflag, aflag, fflag, uflag, gflag;
extern bool Dflag, inodeflag, devflag, Rflag, cflag, hflag, siflag, duflag;
extern bool noindent, force_color, xdev, nolinks, flimit, noreport;
extern const int ifmt[];
extern const char fmt[], *ftype[];
extern FILE *outfile;
extern int Level, *dirs, maxdirs, errors;
extern char *endcode;
/* JSON code courtesy of Florian Sesser <[email protected]>
[
{"type": "directory", "name": "name", "mode": "0777", "user": "user", "group": "group", "inode": ###, "dev": ####, "time": "00:00 00-00-0000", "info": "<file comment>", "contents": [
{"type": "link", "name": "name", "target": "name", "info": "...", "contents": [... if link is followed, otherwise this is empty.]}
{"type": "file", "name": "name", "mode": "0777", "size": ###, "group": "group", "inode": ###, "dev": ###, "time": "00:00 00-00-0000", "info": "..."}
{"type": "socket", "name": "", "info": "...", "error": "some error" ...}
{"type": "block", "name": "" ...},
{"type": "char", "name": "" ...},
{"type": "fifo", "name": "" ...},
{"type": "door", "name": "" ...},
{"type": "port", "name": "" ...}
]},
{"type": "report", "size": ###, "files": ###, "directories": ###}
]
*/
/**
* JSON encoded strings are not HTML/XML strings:
* https://tools.ietf.org/html/rfc8259#section-7
* FIXME: Still not UTF-8
*/
void json_encode(FILE *fd, char *s)
{
char *ctrl = "0-------btn-fr------------------";
for(;*s;s++) {
if ((unsigned char)*s < 32) {
if (ctrl[(unsigned char)*s] != '-') fprintf(fd, "\\%c", ctrl[(unsigned char)*s]);
else fprintf(fd, "\\u%04x", (unsigned char)*s);
} else if (*s == '"' || *s == '\\') fprintf(fd, "\\%c", *s);
else fprintf(fd, "%c", *s);
}
}
void json_indent(int maxlevel)
{
int i;
fprintf(outfile, " ");
for(i=0; i<maxlevel; i++)
fprintf(outfile, " ");
}
void json_fillinfo(struct _info *ent)
{
#ifdef __USE_FILE_OFFSET64
if (inodeflag) fprintf(outfile,",\"inode\":%lld",(long long)ent->inode);
#else
if (inodeflag) fprintf(outfile,",\"inode\":%ld",(long int)ent->inode);
#endif
if (devflag) fprintf(outfile, ",\"dev\":%d", (int)ent->dev);
#ifdef __EMX__
if (pflag) fprintf(outfile, ",\"mode\":\"%04o\",\"prot\":\"%s\"",ent->attr, prot(ent->attr));
#else
if (pflag) fprintf(outfile, ",\"mode\":\"%04o\",\"prot\":\"%s\"", ent->mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX), prot(ent->mode));
#endif
if (uflag) fprintf(outfile, ",\"user\":\"%s\"", uidtoname(ent->uid));
if (gflag) fprintf(outfile, ",\"group\":\"%s\"", gidtoname(ent->gid));
if (sflag) {
if (hflag || siflag) {
char nbuf[64];
int i;
psize(nbuf,ent->size);
for(i=0; isspace(nbuf[i]); i++); // trim() hack
fprintf(outfile, ",\"size\":\"%s\"", nbuf+i);
} else
fprintf(outfile, ",\"size\":%lld", (long long int)ent->size);
}
if (Dflag) fprintf(outfile, ",\"time\":\"%s\"", do_date(cflag? ent->ctime : ent->mtime));
}
void json_intro(void)
{
extern char *_nl;
fprintf(outfile, "[%s", noindent? "" : _nl);
}
void json_outtro(void)
{
extern char *_nl;
fprintf(outfile, "%s]\n", noindent? "" : _nl);
}
int json_printinfo(char *dirname, struct _info *file, int level)
{
mode_t mt;
int t;
if (!noindent) json_indent(level);
if (file->lnk) mt = file->mode & S_IFMT;
else mt = file->mode & S_IFMT;
for(t=0;ifmt[t];t++)
if (ifmt[t] == mt) break;
fprintf(outfile,"{\"type\":\"%s\"", ftype[t]);
return 0;
}
int json_printfile(char *dirname, char *filename, struct _info *file, int descend)
{
fprintf(outfile, ",\"name\":\"");
json_encode(outfile, filename);
fputc('"',outfile);
if (file && file->comment) {
fprintf(outfile, ",\"info\":\"");
for(int i=0; file->comment[i]; i++) {
json_encode(outfile, file->comment[i]);
if (file->comment[i+1]) fprintf(outfile, "\\n");
}
fprintf(outfile, "\"");
}
if (file && file->lnk) {
fprintf(outfile, ",\"target\":\"");
json_encode(outfile, file->lnk);
fputc('"',outfile);
}
if (file) json_fillinfo(file);
if (!descend) fputc('}',outfile);
else fprintf(outfile, ",\"contents\":[");
return descend;
}
int json_error(char *error)
{
fprintf(outfile,"{\"error\": \"%s\"}%s",error, noindent?"":"\n");
return 0;
}
void json_newline(struct _info *file, int level, int postdir, int needcomma)
{
extern char *_nl;
fprintf(outfile, "%s%s", needcomma? "," : "", _nl);
}
void json_close(struct _info *file, int level, int needcomma)
{
if (!noindent) json_indent(level-1);
fprintf(outfile,"]}%s%s", needcomma? ",":"", noindent? "":"\n");
}
void json_report(struct totals tot)
{
fprintf(outfile, ",%s{\"type\":\"report\"",noindent?"":"\n ");
if (duflag) fprintf(outfile,",\"size\":%lld", (long long int)tot.size);
fprintf(outfile,",\"directories\":%ld", tot.dirs);
if (!dflag) fprintf(outfile,",\"files\":%ld", tot.files);
fprintf(outfile, "}");
}