#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <string.h>#include <limits.h>#include <unistd.h>#include <sys/wait.h>#include "common.h" enum { Maxrange = 0x1fff,}; Range *getcharset(char *path, int *i, int pjwface){ pid_t pid; Range *ranges; char t[32], *e, *p; int s, fds[2], n, r; unsigned long min, max, k; *i = 0; ranges = NULL; if(pipe(fds) < 0){ perror("pipe"); return NULL; } if((pid = fork()) == 0){ if(dup2(fds[1], 1) < 0 || execlp("fc-query", "fc-query", "--format", "%{charset} ", path, NULL) < 0){ perror("fc-query"); exit(1); } exit(1); } close(fds[1]); if(pid > 0){ for(n = 0;; n = strlen(t)){ if((r = read(fds[0], t+n, sizeof(t)-n-1)) < 0){ perror("charset read"); goto err; }else if(r == 0 && n == 0) break; n += r; t[n] = 0; min = max = strtoul(t, &e, 16); if(e == t || min > UINT32_MAX){formatting: fprintf(stderr, "fc-query: unexpected formatting\n"); goto err; } if(*e == '-'){ max = strtoul(p = e+1, &e, 16); if(e == p || max > UINT32_MAX) goto formatting; }else if(*e != ' ') goto formatting; e++; n = strlen(e); memmove(t, e, n); t[n] = 0; if(min == 0 && pjwface) min++; for(k = min; k <= max; k += Maxrange+1){ ranges = realloc(ranges, (*i+1)*sizeof(*ranges)); ranges[*i].min = k; ranges[*i].max = k+Maxrange<max ? k+Maxrange : max; (*i)++; } } waitpid(pid, &s, 0); }else{ perror("fork"); goto err; } close(fds[0]); return ranges;err: close(fds[0]); free(ranges); return NULL;}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <sys/wait.h>
#include "common.h"
enum {
Maxrange = 0x1fff,
};
Range *
getcharset(char *path, int *i, int pjwface)
{
pid_t pid;
Range *ranges;
char t[32], *e, *p;
int s, fds[2], n, r;
unsigned long min, max, k;
*i = 0;
ranges = NULL;
if(pipe(fds) < 0){
perror("pipe");
return NULL;
}
if((pid = fork()) == 0){
if(dup2(fds[1], 1) < 0 || execlp("fc-query", "fc-query", "--format", "%{charset} ", path, NULL) < 0){
perror("fc-query");
exit(1);
}
exit(1);
}
close(fds[1]);
if(pid > 0){
for(n = 0;; n = strlen(t)){
if((r = read(fds[0], t+n, sizeof(t)-n-1)) < 0){
perror("charset read");
goto err;
}else if(r == 0 && n == 0)
break;
n += r;
t[n] = 0;
min = max = strtoul(t, &e, 16);
if(e == t || min > UINT32_MAX){
formatting:
fprintf(stderr, "fc-query: unexpected formatting\n");
goto err;
}
if(*e == '-'){
max = strtoul(p = e+1, &e, 16);
if(e == p || max > UINT32_MAX)
goto formatting;
}else if(*e != ' ')
goto formatting;
e++;
n = strlen(e);
memmove(t, e, n);
t[n] = 0;
if(min == 0 && pjwface)
min++;
for(k = min; k <= max; k += Maxrange+1){
ranges = realloc(ranges, (*i+1)*sizeof(*ranges));
ranges[*i].min = k;
ranges[*i].max = k+Maxrange<max ? k+Maxrange : max;
(*i)++;
}
}
waitpid(pid, &s, 0);
}else{
perror("fork");
goto err;
}
close(fds[0]);
return ranges;
err:
close(fds[0]);
free(ranges);
return NULL;
}