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
|
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static char *argv0;
static int
single(int argc, char *argv[])
{
char size[10], target[4096], source[4096];
char *p;
ssize_t r;
int fd;
p = strchr(argv[0], 'x');
if (!p)
return 1;
*p = '\0';
stpcpy(size, argv[0]);
*p = 'x';
p = strchr(p, '/');
if (!p)
return 1;
p = stpcpy(stpcpy(source, "scalable"), p);
if (strcmp(&p[-4], ".png"))
return 1;
stpcpy(&p[-4], ".svg");
for (p = argv[0]; (p = strchr(p, '/')); p++) {
*p = '\0';
if (mkdir(argv[0], 0777) && errno != EEXIST) {
fprintf(stderr, "%s: mkdir %s 0777: %s\n", argv0, argv[0], strerror(errno));
return 1;
}
*p = '/';
}
r = readlink(source, target, sizeof(target) - 1);
if (r >= 0)
target[r] = 0;
if (r >= 0 && (r >= sizeof(target) - 1 || r < 4 || strcmp(&target[r - 4], ".svg"))) {
return 1;
} else if (r < 0 && errno == EINVAL) {
fd = open(argv[0], O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
fprintf(stderr, "%s: open %s O_WRONLY|O_CREAT|O_TRUNC 0666: %s\n", argv0, argv[0], strerror(errno));
return 1;
}
if (fd != STDOUT_FILENO) {
if (dup2(fd, STDOUT_FILENO) != STDOUT_FILENO)
fprintf(stderr, "%s: dup2 %i 1: %s\n", argv0, fd, strerror(errno));
close(fd);
}
execlp("rsvg-convert", "rsvg-convert", "-w", size, "-h", size, "-f", "png", source, NULL);
fprintf(stderr, "%s: execvp rsvg-convert: %s\n", argv0, strerror(errno));
return 1;
} else if (r < 0) {
fprintf(stderr, "%s: readlink %s: %s\n", argv0, source, strerror(errno));
return 1;
} else {
stpcpy(&target[r - 4], ".png");
if (symlink(target, argv[0])) {
if (errno == EEXIST) {
unlink(argv[0]);
if (!symlink(target, argv[0]))
return 0;
}
fprintf(stderr, "%s: symlink %s %s: %s\n", argv0, target, argv[0], strerror(errno));
return 1;
}
return 0;
}
}
static int
multi(int argc, char *argv[])
{
char *source, output[4096], target[4096];
char *p, *q;
pid_t pid;
int status, fd, i;
ssize_t r;
source = *argv++, argc--;
q = strchr(source, '/');
if (!q)
return 1;
for (i = 0; i < argc; i++) {
stpcpy(stpcpy(stpcpy(stpcpy(output, argv[i]), "x"), argv[i]), q);
for (p = output; (p = strchr(p, '/')); p++) {
*p = '\0';
if (mkdir(output, 0777) && errno != EEXIST) {
fprintf(stderr, "%s: mkdir %s 0777: %s\n", argv0, output, strerror(errno));
return 1;
}
*p = '/';
}
}
r = readlink(source, target, sizeof(target) - 1);
if (r >= 0)
target[r] = 0;
if (r >= 0 && (r >= sizeof(target) - 1 || r < 4 || strcmp(&target[r - 4], ".svg"))) {
return 1;
} else if (r < 0 && errno == EINVAL) {
p = stpcpy(stpcpy(stpcpy(stpcpy(output, argv[0]), "x"), argv[0]), q);
stpcpy(&p[-4], ".png");
for (; *argv; argv++, argc--) {
pid = argc == 1 ? 0 : fork();
if (pid == -1) {
fprintf(stderr, "%s: fork: %s\n", argv0, strerror(errno));
return 1;
}
if (pid) {
p = stpcpy(stpcpy(stpcpy(stpcpy(output, argv[1]), "x"), argv[1]), q);
stpcpy(&p[-4], ".png");
if (waitpid(pid, &status, 0) != pid) {
fprintf(stderr, "%s: waitpid rsvg-convert: %s\n", argv0, strerror(errno));
return 1;
}
if (status)
return 1;
continue;
}
fd = open(output, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
fprintf(stderr, "%s: open %s O_WRONLY|O_CREAT|O_TRUNC 0666: %s\n", argv0, output, strerror(errno));
return 1;
}
if (fd != STDOUT_FILENO) {
if (dup2(fd, STDOUT_FILENO) != STDOUT_FILENO)
fprintf(stderr, "%s: dup2 %i 1: %s\n", argv0, fd, strerror(errno));
close(fd);
}
execlp("rsvg-convert", "rsvg-convert", "-w", *argv, "-h", *argv, "-f", "png", source, NULL);
fprintf(stderr, "%s: execvp rsvg-convert: %s\n", argv0, strerror(errno));
return 1;
}
} else if (r < 0) {
fprintf(stderr, "%s: readlink %s: %s\n", argv0, source, strerror(errno));
return 1;
} else {
stpcpy(&target[r - 4], ".png");
for (; *argv; argv++) {
p = stpcpy(stpcpy(stpcpy(stpcpy(output, *argv), "x"), *argv), q);
stpcpy(&p[-4], ".png");
if (symlink(target, output)) {
if (errno == EEXIST) {
unlink(output);
if (!symlink(target, output))
continue;
}
fprintf(stderr, "%s: symlink %s %s: %s\n", argv0, target, output, strerror(errno));
return 1;
}
}
return 0;
}
}
int
main(int argc, char *argv[])
{
argv0 = *argv++, argc--;
if (argc == 1)
return single(argc, argv);
if (argc > 1)
return multi(argc, argv);
return 1;
}
|