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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/*
* Fig2dev: Translate Fig code to various Devices
* Copyright (c) 1991 by Micah Beck
* Parts Copyright (c) 1985-1988 by Supoj Sutanthavibul
* Parts Copyright (c) 1989-2015 by Brian V. Smith
* Parts Copyright (c) 2015-2019 by Thomas Loimer
*
* Any party obtaining a copy of these files is granted, free of charge, a
* full and unrestricted irrevocable, world-wide, paid up, royalty-free,
* nonexclusive right and license to deal in this software and documentation
* files (the "Software"), including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense and/or sell copies
* of the Software, and to permit persons who receive copies from any such
* party to do so, with the only requirement being that the above copyright
* and this permission notice remain intact.
*
*/
/*
* readpics.c: read image files
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "readpics.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "messages.h"
#include "xtmpfile.h"
void
init_stream(struct xfig_stream *restrict xf_stream)
{
xf_stream->fp = NULL;
xf_stream->name = xf_stream->name_buf;
xf_stream->name_on_disk = xf_stream->name_on_disk_buf;
xf_stream->uncompress = NULL;
xf_stream->content = xf_stream->content_buf;
*xf_stream->content = '\0';
}
void
free_stream(struct xfig_stream *restrict xf_stream)
{
if (xf_stream->content != xf_stream->name_on_disk) {
if (*xf_stream->content && unlink(xf_stream->content)) {
err_msg("Cannot remove temporary file %s",
xf_stream->content);
}
if (xf_stream->content != xf_stream->content_buf) {
free(xf_stream->content);
xf_stream->content = xf_stream->content_buf;
}
}
if (xf_stream->name != xf_stream->name_buf) {
free(xf_stream->name);
xf_stream->name = xf_stream->name_buf;
}
if (xf_stream->name_on_disk != xf_stream->name_on_disk_buf) {
free(xf_stream->name_on_disk);
xf_stream->name_on_disk = xf_stream->name_on_disk_buf;
}
}
/*
* Given name, search and return the name of the corresponding file on disk in
* found. This may be "name", or "name" with a compression suffix appended,
* e.g., "name.gz". If the file must be uncompressed, return the compression
* command in a static string pointed to by "uncompress", otherwise let
* "uncompress" point to the empty string. If the size of the character buffer
* provided in found is too small to accomodate the string, return a pointer to
* a malloc()'ed string.
* Return 0 on success, -1 on failure.
* Usage:
* char found_buf[len];
* char *found = found_buf;
* file_on_disk(name, &found, sizeof found_buf, &uncompress);
* ...
* if (found != found_buf)
* free(found);
*/
static int
file_on_disk(char *restrict name, char *restrict *found, size_t len,
const char *restrict *uncompress)
{
int i;
size_t name_len;
char *suffix;
struct stat status;
static const char empty[] = "";
static char *const filetypes[][2] = {
/* sorted by popularity? */
#define FILEONDISK_ADD 5 /* must be max(strlen(filetypes[0][])) + 1 */
{ ".gz", "gunzip -c" },
{ ".Z", "gunzip -c" },
{ ".z", "gunzip -c" },
{ ".zip", "unzip -p" },
{ ".bz2", "bunzip2 -c" },
{ ".bz", "bunzip2 -c" },
{ ".xz", "unxz -c" }
};
const int filetypes_len =
(int)(sizeof filetypes / sizeof(filetypes[1]));
name_len = strlen(name);
if (name_len >= len &&
(*found = malloc(name_len + FILEONDISK_ADD)) == NULL) {
put_msg(Err_mem);
return -1;
}
strcpy(*found, name);
/*
* Possibilities, e.g.,
* name name on disk uncompress
* img.ppm img.ppm ""
* img.ppm img.ppm.gz gunzip -c
* img.ppm.gz img.ppm.gz gunzip -c
* img.ppm.gz img.ppm ""
*/
if (stat(name, &status)) {
/* File not found. Now try, whether a file with one of
the known suffices appended exists. */
if (len > name_len && len < name_len + FILEONDISK_ADD &&
(*found = malloc(name_len + FILEONDISK_ADD))
== NULL) {
put_msg(Err_mem);
return -1;
}
suffix = *found + name_len;
for (i = 0; i < filetypes_len; ++i) {
strcpy(suffix, filetypes[i][0]);
if (!stat(*found, &status)) {
*uncompress = filetypes[i][1];
break;
}
}
/* Not found. Check, whether the file has one of the known
compression suffices, but the uncompressed file
exists on disk. */
*suffix = '\0';
if (i == filetypes_len && (suffix = strrchr(name, '.'))) {
for (i = 0; i < filetypes_len; ++i) {
if (!strcmp(suffix, filetypes[i][0])) {
*(*found + (suffix - name)) = '\0';
if (!stat(*found, &status)) {
*uncompress = empty;
break;
} else {
*found[0] = '\0';
i = filetypes_len;
}
}
}
}
if (i == filetypes_len) {
/* not found */
*found[0] = '\0';
return -1;
}
} else {
/* File exists. Check, whether the name has one of the known
compression suffices. */
if ((suffix = strrchr(name, '.'))) {
for (i = 0; i < filetypes_len; ++i) {
if (!strcmp(suffix, filetypes[i][0])) {
*uncompress = filetypes[i][1];
break;
}
}
} else {
i = filetypes_len;
}
if (i == filetypes_len)
*uncompress = empty;
}
return 0;
}
/*
* Return a file stream, either to a pipe or to a regular file.
* If xf_stream->uncompress[0] == '\0', it is a regular file, otherwise a pipe.
*/
FILE *
open_stream(char *restrict name, struct xfig_stream *restrict xf_stream)
{
size_t len;
if (xf_stream->name != name) {
/* strcpy (xf_stream->name, name) */
len = strlen(name);
if (len >= sizeof xf_stream->name_buf) {
if ((xf_stream->name = malloc(len + 1)) == NULL) {
put_msg(Err_mem);
return NULL;
}
}
memcpy(xf_stream->name, name, len + 1);
}
if (file_on_disk(name, &xf_stream->name_on_disk,
sizeof xf_stream->name_on_disk_buf,
&xf_stream->uncompress)) {
free_stream(xf_stream);
return NULL;
}
if (xf_stream->uncompress && *xf_stream->uncompress) {
/* a compressed file */
char command_buf[256];
char *command = command_buf;
len = strlen(xf_stream->name_on_disk) +
strlen(xf_stream->uncompress) + 2;
if (len > sizeof command_buf) {
if ((command = malloc(len)) == NULL) {
put_msg(Err_mem);
return NULL;
}
}
sprintf(command, "%s %s",
xf_stream->uncompress, xf_stream->name_on_disk);
xf_stream->fp = popen(command, "r");
if (command != command_buf)
free(command);
} else {
/* uncompressed file */
xf_stream->fp = fopen(xf_stream->name_on_disk, "rb");
}
return xf_stream->fp;
}
/*
* Close a file stream opened by open_file(). Do not free xf_stream.
*/
int
close_stream(struct xfig_stream *restrict xf_stream)
{
if (xf_stream->fp == NULL)
return -1;
if (xf_stream->uncompress[0] == '\0') {
/* a regular file */
return fclose(xf_stream->fp);
} else {
/* a pipe */
char trash[BUFSIZ];
/* for a pipe, must read everything or
we'll get a broken pipe message */
while (fread(trash, (size_t)1, (size_t)BUFSIZ, xf_stream->fp) ==
(size_t)BUFSIZ)
;
return pclose(xf_stream->fp);
}
}
FILE *
rewind_stream(struct xfig_stream *restrict xf_stream)
{
if (xf_stream->fp == NULL)
return NULL;
if (xf_stream->uncompress[0] == '\0') {
/* a regular file */
rewind(xf_stream->fp);
return xf_stream->fp;
} else {
/* a pipe */
(void)close_stream(xf_stream);
/* if, in the meantime, e.g., the file on disk
was uncompressed, change the filetype. */
return open_stream(xf_stream->name, xf_stream);
}
}
/*
* Have xf_stream->content either point to a regular file containing the
* uncompressed content of xf_stream->name, or to xf_stream->name_on_disk, if
* name_on_disk is not compressed.
* Return 0 on succes, -1 on failure.
* Use after a call to open_stream():
* struct xfig_stream xf_stream;
* open_stream(name, &xf_stream);
* close_stream(&xf_stream);
* uncompressed_content(&xf_stream)
* do_something(xf_stream->content);
* free_stream(&xf_stream);
*/
int
uncompressed_content(struct xfig_stream *restrict xf_stream)
{
int ret = -1;
int len;
char command_buf[256];
char *command = command_buf;
char *const uncompress_fmt = "%s '%s' >%s";
FILE *f;
if (*xf_stream->uncompress == '\0') {
xf_stream->content = xf_stream->name_on_disk;
return 0;
}
/* return early, if uncompressed_content was already called */
if (*xf_stream->uncompress)
return 0;
/* create a temporary file */
strcpy(xf_stream->content, "f2dXXXXXX");
f = xtmpfile(&xf_stream->content, sizeof xf_stream->content_buf);
if (f)
fclose(f);
else
return ret;
/* uncompress to a temporary file */
len = snprintf(command, sizeof command_buf, uncompress_fmt,
xf_stream->uncompress, xf_stream->name_on_disk,
xf_stream->content);
if (len >= (int)(sizeof command_buf)) {
if ((command = malloc(len + 1)) == NULL) {
put_msg(Err_mem);
return ret;
}
len = sprintf(command, uncompress_fmt, xf_stream->uncompress,
xf_stream->name_on_disk, xf_stream->uncompress);
}
if (len < 0) {
err_msg("Unable to write command to uncompress file");
return ret;
}
if (system(command) == 0)
ret = 0;
else
err_msg("Could not uncompress %s, command, %s",
xf_stream->name_on_disk, command);
if (command != command_buf)
free(command);
return ret;
}
|