1 /* misc.c
2 *
3 * Copyright (C) 1996-2014 Timo Kokkonen
4 * All Rights Reserved.
5 *
6 */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <string.h>
18 #include <stdarg.h>
19 #include <stdlib.h>
20
21
22 #include "jpegoptim.h"
23
24
delete_file(const char * name)25 int delete_file(const char *name)
26 {
27 int retval;
28
29 if (!name) return -1;
30 if (verbose_mode > 1 && !quiet_mode) fprintf(stderr,"deleting: %s\n",name);
31 if ((retval=unlink(name)) && !quiet_mode)
32 warn("error removing file: %s",name);
33
34 return retval;
35 }
36
37
file_size(FILE * fp)38 long file_size(FILE *fp)
39 {
40 struct stat buf;
41
42 if (!fp) return -1;
43 if (fstat(fileno(fp),&buf)) return -2;
44 return (long)buf.st_size;
45 }
46
47
is_directory(const char * pathname)48 int is_directory(const char *pathname)
49 {
50 struct stat buf;
51
52 if (!pathname) return 0;
53 if (stat(pathname,&buf) != 0) return 0;
54 if (S_ISDIR(buf.st_mode)) return 1;
55 return 0;
56 }
57
58
is_file(const char * filename,struct stat * st)59 int is_file(const char *filename, struct stat *st)
60 {
61 struct stat buf;
62
63 if (!filename) return 0;
64 if (lstat(filename,&buf) != 0) return 0;
65 if (st) *st=buf;
66 if (S_ISREG(buf.st_mode)) return 1;
67 return 0;
68 }
69
70
file_exists(const char * pathname)71 int file_exists(const char *pathname)
72 {
73 struct stat buf;
74
75 if (!pathname) return 0;
76 if (stat(pathname,&buf) != 0) return 0;
77 return 1;
78 }
79
80
rename_file(const char * old_path,const char * new_path)81 int rename_file(const char *old_path, const char *new_path)
82 {
83 if (!old_path || !new_path) return -1;
84 #ifdef WIN32
85 if (file_exists(new_path)) delete_file(new_path);
86 #endif
87 return rename(old_path,new_path);
88 }
89
90
91 #define COPY_BUF_SIZE (256*1024)
92
copy_file(const char * srcfile,const char * dstfile)93 int copy_file(const char *srcfile, const char *dstfile)
94 {
95 FILE *in,*out;
96 unsigned char buf[COPY_BUF_SIZE];
97 int r,w;
98 int err=0;
99
100 if (!srcfile || !dstfile) return -1;
101
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
114
115 do {
116 r=fread(buf,1,sizeof(buf),in);
117 if (r > 0) {
118 w=fwrite(buf,1,r,out);
119 if (w != r) {
120 err=1;
121 warn("error writing to file: %s", dstfile);
122 break;
123 }
124 } else {
125 if (ferror(in)) {
126 err=2;
127 warn("error reading file: %s", srcfile);
128 break;
129 }
130 }
131 } while (!feof(in));
132
133 fclose(out);
134 fclose(in);
135 return err;
136 }
137
138
splitdir(const char * pathname,char * buf,int buflen)139 char *splitdir(const char *pathname, char *buf, int buflen)
140 {
141 char *s = NULL;
142 int size = 0;
143
144 if (!pathname || !buf || buflen < 2) return NULL;
145
146 if ((s = strrchr(pathname,DIR_SEPARATOR_C))) size = (s-pathname)+1;
147 if (size >= buflen) return NULL;
148 if (size > 0) memcpy(buf,pathname,size);
149 buf[size]=0;
150
151 return buf;
152 }
153
splitname(const char * pathname,char * buf,int buflen)154 char *splitname(const char *pathname, char *buf, int buflen)
155 {
156 const char *s = NULL;
157 int size = 0;
158
159 if (!pathname || !buf || buflen < 2) return NULL;
160
161 if ((s = strrchr(pathname,DIR_SEPARATOR_C))) {
162 s++;
163 } else {
164 s=pathname;
165 }
166
167 size=strlen(s);
168 if (size >= buflen) return NULL;
169 if (size > 0) memcpy(buf,s,size);
170 buf[size]=0;
171
172 return buf;
173 }
174
fatal(const char * format,...)175 void fatal(const char *format, ...)
176 {
177 va_list args;
178
179 fprintf(stderr, PROGRAMNAME ": ");
180 va_start(args,format);
181 vfprintf(stderr, format, args);
182 va_end(args);
183 fprintf(stderr,"\n");
184 fflush(stderr);
185
186 exit(3);
187 }
188
warn(const char * format,...)189 void warn(const char *format, ...)
190 {
191 va_list args;
192
193 if (quiet_mode) return;
194
195 fprintf(stderr, PROGRAMNAME ": ");
196 va_start(args,format);
197 vfprintf(stderr, format, args);
198 va_end(args);
199 fprintf(stderr,"\n");
200 fflush(stderr);
201 }
202
203
204 /* eof */
205