misc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* misc.c
  2. *
  3. * Copyright (C) 1996-2025 Timo Kokkonen
  4. * All Rights Reserved.
  5. *
  6. * SPDX-License-Identifier: GPL-3.0-or-later
  7. *
  8. * This file is part of JPEGoptim.
  9. *
  10. * JPEGoptim is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * JPEGoptim is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with JPEGoptim. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include <stdio.h>
  27. #include <fcntl.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #include <string.h>
  34. #include <stdarg.h>
  35. #include <stdlib.h>
  36. #include <time.h>
  37. #include "jpegoptim.h"
  38. FILE* create_file(const char *name)
  39. {
  40. FILE *f;
  41. int fd;
  42. if (!name)
  43. return NULL;
  44. #ifdef WIN32
  45. fd = open(name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, _S_IREAD | _S_IWRITE);
  46. #else
  47. fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
  48. #endif
  49. if (fd < 0)
  50. return NULL;
  51. if (!(f = fdopen(fd, "wb"))) {
  52. close(fd);
  53. return NULL;
  54. }
  55. return f;
  56. }
  57. FILE *create_temp_file(const char *tmpdir, const char *name, char *filename, size_t filename_len)
  58. {
  59. FILE *f;
  60. int newlen;
  61. #ifdef HAVE_MKSTEMPS
  62. /* Rely on mkstemps() to create us temporary file safely... */
  63. newlen = snprintf(filename, filename_len, "%s%s-%u-%u.XXXXXX.tmp",
  64. tmpdir, name, getuid(), getpid());
  65. #else
  66. /* If platform is missing mkstemps(), try to create at least somewhat "safe" temp file... */
  67. newlen = snprintf(filename, filename_len, "%s%s-%u-%u.%lu.tmp",
  68. tmpdir, name, getuid(), getpid(), (unsigned long)time(NULL));
  69. #endif
  70. if (newlen >= filename_len) {
  71. warn("temp filename too long: %s", filename);
  72. return NULL;
  73. }
  74. #ifdef HAVE_MKSTEMPS
  75. int tmpfd = mkstemps(filename, 4);
  76. if (tmpfd < 0) {
  77. warn("error creating temp file: mkstemps('%s', 4) failed", filename);
  78. return NULL;
  79. }
  80. f = fdopen(tmpfd, "wb");
  81. #else
  82. f = create_file(filename);
  83. #endif
  84. return f;
  85. }
  86. int delete_file(const char *name)
  87. {
  88. int retval;
  89. if (!name)
  90. return -1;
  91. if (verbose_mode > 1 && !quiet_mode)
  92. fprintf(stderr,"deleting: %s\n",name);
  93. if ((retval=unlink(name)) && !quiet_mode)
  94. warn("error removing file: %s",name);
  95. return retval;
  96. }
  97. long file_size(FILE *fp)
  98. {
  99. struct stat buf;
  100. if (!fp)
  101. return -1;
  102. if (fstat(fileno(fp),&buf) != 0)
  103. return -2;
  104. return (long)buf.st_size;
  105. }
  106. int is_directory(const char *pathname)
  107. {
  108. struct stat buf;
  109. if (!pathname)
  110. return 0;
  111. if (stat(pathname,&buf) != 0)
  112. return 0;
  113. return (S_ISDIR(buf.st_mode) ? 1 : 0);
  114. }
  115. int is_file(const char *filename, struct stat *st)
  116. {
  117. struct stat buf;
  118. if (!filename)
  119. return 0;
  120. if (lstat(filename,&buf) != 0)
  121. return 0;
  122. if (st)
  123. *st=buf;
  124. return (S_ISREG(buf.st_mode) ? 1 : 0);
  125. }
  126. int file_exists(const char *pathname)
  127. {
  128. struct stat buf;
  129. if (!pathname)
  130. return 0;
  131. return (stat(pathname,&buf) == 0 ? 1 : 0);
  132. }
  133. int rename_file(const char *old_path, const char *new_path)
  134. {
  135. if (!old_path || !new_path)
  136. return -1;
  137. #ifdef WIN32
  138. if (file_exists(new_path))
  139. delete_file(new_path);
  140. #endif
  141. return rename(old_path,new_path);
  142. }
  143. #define COPY_BUF_SIZE (256 * 1024)
  144. int copy_file(const char *srcfile, const char *dstfile)
  145. {
  146. FILE *in,*out;
  147. unsigned char *buf;
  148. int r,w;
  149. int err=0;
  150. if (!srcfile || !dstfile)
  151. return -1;
  152. if (!(in = fopen(srcfile, "rb"))) {
  153. warn("failed to open file for reading: %s", srcfile);
  154. return -2;
  155. }
  156. if (!(out = create_file(dstfile))) {
  157. fclose(in);
  158. warn("failed to open file for writing: %s", dstfile);
  159. return -3;
  160. }
  161. if (!(buf = calloc(COPY_BUF_SIZE, 1)))
  162. fatal("out of memory");
  163. do {
  164. r = fread(buf, 1, COPY_BUF_SIZE, in);
  165. if (r > 0) {
  166. w = fwrite(buf, 1, r, out);
  167. if (w != r) {
  168. err=1;
  169. warn("error writing to file: %s", dstfile);
  170. break;
  171. }
  172. } else {
  173. if (ferror(in)) {
  174. err=2;
  175. warn("error reading from file: %s", srcfile);
  176. break;
  177. }
  178. }
  179. } while (!feof(in));
  180. fclose(out);
  181. fclose(in);
  182. free(buf);
  183. return err;
  184. }
  185. char *fgetstr(char *s, size_t size, FILE *stream)
  186. {
  187. char *p;
  188. if (!s || size < 1 || !stream)
  189. return NULL;
  190. if (!fgets(s, size, stream))
  191. return NULL;
  192. p = s + strnlen(s, size) - 1;
  193. while ((p >= s) && ((*p == 10) || (*p == 13)))
  194. *p--=0;
  195. return s;
  196. }
  197. char *splitdir(const char *pathname, char *buf, size_t size)
  198. {
  199. char *s;
  200. int len = 0;
  201. if (!pathname || !buf || size < 1)
  202. return NULL;
  203. if ((s = strrchr(pathname, DIR_SEPARATOR_C)))
  204. len = (s - pathname) + 1;
  205. if (len >= size)
  206. return NULL;
  207. if (len > 0)
  208. memcpy(buf, pathname, len);
  209. buf[len] = 0;
  210. return buf;
  211. }
  212. char *splitname(const char *pathname, char *buf, size_t size)
  213. {
  214. const char *s = NULL;
  215. int len;
  216. if (!pathname || !buf || size < 1)
  217. return NULL;
  218. if ((s = strrchr(pathname, DIR_SEPARATOR_C)))
  219. s++;
  220. else
  221. s=pathname;
  222. if ((len = strlen(s)) >= size)
  223. return NULL;
  224. if (len > 0)
  225. memcpy(buf, s, len);
  226. buf[len] = 0;
  227. return buf;
  228. }
  229. char *strncopy(char *dst, const char *src, size_t size)
  230. {
  231. if (!dst || !src || size < 1)
  232. return dst;
  233. if (size > 1)
  234. strncpy(dst, src, size - 1);
  235. dst[size - 1] = 0;
  236. return dst;
  237. }
  238. char *strncatenate(char *dst, const char *src, size_t size)
  239. {
  240. int used, free;
  241. if (!dst || !src || size < 1)
  242. return dst;
  243. /* Check if dst string is already "full" ... */
  244. used = strnlen(dst, size);
  245. if ((free = size - used) <= 1)
  246. return dst;
  247. return strncat(dst + used, src, free - 1);
  248. }
  249. char *str_add_list(char *dst, size_t size, const char *src, const char *delim)
  250. {
  251. if (!dst || !src || !delim || size < 1)
  252. return dst;
  253. if (strnlen(dst, size) > 0)
  254. strncatenate(dst, delim, size);
  255. return strncatenate(dst, src, size);
  256. }
  257. void fatal(const char *format, ...)
  258. {
  259. va_list args;
  260. fprintf(stderr, PROGRAMNAME ": ");
  261. va_start(args,format);
  262. vfprintf(stderr, format, args);
  263. va_end(args);
  264. fprintf(stderr,"\n");
  265. fflush(stderr);
  266. exit(3);
  267. }
  268. void warn(const char *format, ...)
  269. {
  270. va_list args;
  271. if (quiet_mode) return;
  272. fprintf(stderr, PROGRAMNAME ": ");
  273. va_start(args,format);
  274. vfprintf(stderr, format, args);
  275. va_end(args);
  276. fprintf(stderr,"\n");
  277. fflush(stderr);
  278. }
  279. /* eof :-) */