misc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* misc.c
  2. *
  3. * Copyright (C) 1996-2022 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 <sys/types.h>
  28. #include <sys/stat.h>
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #include <string.h>
  33. #include <stdarg.h>
  34. #include <stdlib.h>
  35. #include "jpegoptim.h"
  36. int delete_file(const char *name)
  37. {
  38. int retval;
  39. if (!name)
  40. return -1;
  41. if (verbose_mode > 1 && !quiet_mode)
  42. fprintf(stderr,"deleting: %s\n",name);
  43. if ((retval=unlink(name)) && !quiet_mode)
  44. warn("error removing file: %s",name);
  45. return retval;
  46. }
  47. long file_size(FILE *fp)
  48. {
  49. struct stat buf;
  50. if (!fp)
  51. return -1;
  52. if (fstat(fileno(fp),&buf) != 0)
  53. return -2;
  54. return (long)buf.st_size;
  55. }
  56. int is_directory(const char *pathname)
  57. {
  58. struct stat buf;
  59. if (!pathname)
  60. return 0;
  61. if (stat(pathname,&buf) != 0)
  62. return 0;
  63. return (S_ISDIR(buf.st_mode) ? 1 : 0);
  64. }
  65. int is_file(const char *filename, struct stat *st)
  66. {
  67. struct stat buf;
  68. if (!filename)
  69. return 0;
  70. if (lstat(filename,&buf) != 0)
  71. return 0;
  72. if (st)
  73. *st=buf;
  74. return (S_ISREG(buf.st_mode) ? 1 : 0);
  75. }
  76. int file_exists(const char *pathname)
  77. {
  78. struct stat buf;
  79. if (!pathname)
  80. return 0;
  81. return (stat(pathname,&buf) == 0 ? 1 : 0);
  82. }
  83. int rename_file(const char *old_path, const char *new_path)
  84. {
  85. if (!old_path || !new_path)
  86. return -1;
  87. #ifdef WIN32
  88. if (file_exists(new_path))
  89. delete_file(new_path);
  90. #endif
  91. return rename(old_path,new_path);
  92. }
  93. #define COPY_BUF_SIZE (256*1024)
  94. int copy_file(const char *srcfile, const char *dstfile)
  95. {
  96. FILE *in,*out;
  97. unsigned char buf[COPY_BUF_SIZE];
  98. int r,w;
  99. int err=0;
  100. if (!srcfile || !dstfile)
  101. return -1;
  102. in=fopen(srcfile,"rb");
  103. if (!in) {
  104. warn("failed to open file for reading: %s", srcfile);
  105. return -2;
  106. }
  107. out=fopen(dstfile,"wb");
  108. if (!out) {
  109. fclose(in);
  110. warn("failed to open file for writing: %s", dstfile);
  111. return -3;
  112. }
  113. do {
  114. r=fread(buf,1,sizeof(buf),in);
  115. if (r > 0) {
  116. w=fwrite(buf,1,r,out);
  117. if (w != r) {
  118. err=1;
  119. warn("error writing to file: %s", dstfile);
  120. break;
  121. }
  122. } else {
  123. if (ferror(in)) {
  124. err=2;
  125. warn("error reading file: %s", srcfile);
  126. break;
  127. }
  128. }
  129. } while (!feof(in));
  130. fclose(out);
  131. fclose(in);
  132. return err;
  133. }
  134. char *fgetstr(char *s, size_t size, FILE *stream)
  135. {
  136. char *p;
  137. if (!s || size < 1 || !stream)
  138. return NULL;
  139. if (!fgets(s, size, stream))
  140. return NULL;
  141. p = s + strnlen(s, size) - 1;
  142. while ((p >= s) && ((*p == 10) || (*p == 13)))
  143. *p--=0;
  144. return s;
  145. }
  146. char *splitdir(const char *pathname, char *buf, size_t size)
  147. {
  148. char *s;
  149. int len = 0;
  150. if (!pathname || !buf || size < 1)
  151. return NULL;
  152. if ((s = strrchr(pathname, DIR_SEPARATOR_C)))
  153. len = (s - pathname) + 1;
  154. if (len >= size)
  155. return NULL;
  156. if (len > 0)
  157. memcpy(buf, pathname, len);
  158. buf[len] = 0;
  159. return buf;
  160. }
  161. char *splitname(const char *pathname, char *buf, size_t size)
  162. {
  163. const char *s = NULL;
  164. int len;
  165. if (!pathname || !buf || size < 1)
  166. return NULL;
  167. if ((s = strrchr(pathname, DIR_SEPARATOR_C)))
  168. s++;
  169. else
  170. s=pathname;
  171. if ((len = strlen(s)) >= size)
  172. return NULL;
  173. if (len > 0)
  174. memcpy(buf, s, len);
  175. buf[len] = 0;
  176. return buf;
  177. }
  178. char *strncopy(char *dst, const char *src, size_t size)
  179. {
  180. if (!dst || !src || size < 1)
  181. return dst;
  182. if (size > 1)
  183. strncpy(dst, src, size - 1);
  184. dst[size - 1] = 0;
  185. return dst;
  186. }
  187. char *strncatenate(char *dst, const char *src, size_t size)
  188. {
  189. int used, free;
  190. if (!dst || !src || size < 1)
  191. return dst;
  192. /* Check if dst string is already "full" ... */
  193. used = strnlen(dst, size);
  194. if ((free = size - used) <= 1)
  195. return dst;
  196. return strncat(dst + used, src, free - 1);
  197. }
  198. char *str_add_list(char *dst, size_t size, const char *src, const char *delim)
  199. {
  200. if (!dst || !src || !delim || size < 1)
  201. return dst;
  202. if (strnlen(dst, size) > 0)
  203. strncatenate(dst, delim, size);
  204. return strncatenate(dst, src, size);
  205. }
  206. void fatal(const char *format, ...)
  207. {
  208. va_list args;
  209. fprintf(stderr, PROGRAMNAME ": ");
  210. va_start(args,format);
  211. vfprintf(stderr, format, args);
  212. va_end(args);
  213. fprintf(stderr,"\n");
  214. fflush(stderr);
  215. exit(3);
  216. }
  217. void warn(const char *format, ...)
  218. {
  219. va_list args;
  220. if (quiet_mode) return;
  221. fprintf(stderr, PROGRAMNAME ": ");
  222. va_start(args,format);
  223. vfprintf(stderr, format, args);
  224. va_end(args);
  225. fprintf(stderr,"\n");
  226. fflush(stderr);
  227. }
  228. /* eof :-) */