File: misc.c

package info (click to toggle)
jpegoptim 1.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 504 kB
  • sloc: sh: 3,222; ansic: 1,861; makefile: 67
file content (207 lines) | stat: -rw-r--r-- 3,397 bytes parent folder | download
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
/* misc.c
 *
 * Copyright (C) 1996-2014 Timo Kokkonen
 * All Rights Reserved.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>


#include "jpegoptim.h"


int delete_file(const char *name)
{
	int retval;

	if (!name) return -1;
	if (verbose_mode > 1 && !quiet_mode) fprintf(stderr,"deleting: %s\n",name);
	if ((retval=unlink(name)) && !quiet_mode)
		warn("error removing file: %s",name);

	return retval;
}


long file_size(FILE *fp)
{
	struct stat buf;

	if (!fp) return -1;
	if (fstat(fileno(fp),&buf)) return -2;
	return (long)buf.st_size;
}


int is_directory(const char *pathname)
{
	struct stat buf;

	if (!pathname) return 0;
	if (stat(pathname,&buf) != 0) return 0;
	if (S_ISDIR(buf.st_mode)) return 1;
	return 0;
}


int is_file(const char *filename, struct stat *st)
{
	struct stat buf;

	if (!filename) return 0;
	if (lstat(filename,&buf) != 0) return 0;
	if (st) *st=buf;
	if (S_ISREG(buf.st_mode)) return 1;
	return 0;
}


int file_exists(const char *pathname)
{
	struct stat buf;

	if (!pathname) return 0;
	if (stat(pathname,&buf) != 0) return 0;
	return 1;
}


int rename_file(const char *old_path, const char *new_path)
{
	if (!old_path || !new_path) return -1;
#ifdef WIN32
	if (file_exists(new_path)) delete_file(new_path);
#endif
	return rename(old_path,new_path);
}


#define COPY_BUF_SIZE  (256*1024)

int copy_file(const char *srcfile, const char *dstfile)
{
	FILE *in,*out;
	unsigned char buf[COPY_BUF_SIZE];
	int r,w;
	int err=0;

	if (!srcfile || !dstfile) return -1;

	in=fopen(srcfile,"rb");
	if (!in) {
		warn("failed to open file for reading: %s", srcfile);
		return -2;
	}
	out=fopen(dstfile,"wb");
	if (!out) {
		fclose(in);
		warn("failed to open file for writing: %s", dstfile);
		return -3;
	}


	do {
		r=fread(buf,1,sizeof(buf),in);
		if (r > 0) {
			w=fwrite(buf,1,r,out);
			if (w != r) {
				err=1;
				warn("error writing to file: %s", dstfile);
				break;
			}
		} else {
			if (ferror(in)) {
				err=2;
				warn("error reading file: %s", srcfile);
				break;
			}
		}
	} while (!feof(in));

	fclose(out);
	fclose(in);
	return err;
}


char *splitdir(const char *pathname, char *buf, int buflen)
{
	char *s = NULL;
	int size = 0;

	if (!pathname || !buf || buflen < 2) return NULL;

	if ((s = strrchr(pathname,DIR_SEPARATOR_C))) size = (s-pathname)+1;
	if (size >= buflen) return NULL;
	if (size > 0) memcpy(buf,pathname,size);
	buf[size]=0;

	return buf;
}


char *splitname(const char *pathname, char *buf, int buflen)
{
	const char *s = NULL;
	int size = 0;

	if (!pathname || !buf || buflen < 2) return NULL;

	if ((s = strrchr(pathname,DIR_SEPARATOR_C))) {
		s++;
	} else {
		s=pathname;
	}

	size=strlen(s);
	if (size >= buflen) return NULL;
	if (size > 0) memcpy(buf,s,size);
	buf[size]=0;

	return buf;
}


void fatal(const char *format, ...)
{
	va_list args;

	fprintf(stderr, PROGRAMNAME ": ");
	va_start(args,format);
	vfprintf(stderr, format, args);
	va_end(args);
	fprintf(stderr,"\n");
	fflush(stderr);

	exit(3);
}


void warn(const char *format, ...)
{
	va_list args;

	if (quiet_mode) return;

	fprintf(stderr, PROGRAMNAME ": ");
	va_start(args,format);
	vfprintf(stderr, format, args);
	va_end(args);
	fprintf(stderr,"\n");
	fflush(stderr);
}


/* eof :-) */