forked from mahkoh/scrot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.c
More file actions
115 lines (104 loc) · 2.07 KB
/
Copy pathutils.c
File metadata and controls
115 lines (104 loc) · 2.07 KB
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
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <malloc.h>
#include "utils.h"
#include "image.h"
static char *util_fmt_int(char *fmt, int n)
{
static char tmp[32];
sprintf(tmp, fmt, n);
return tmp;
}
static char *util_fmt_char(char c, char *path_im, char *path_thumb, Image im)
{
switch (c) {
case 'f':
return path_im;
case 'm':
return path_thumb;
case 'n':
if (path_im) {
char *tmp = strrchr(path_im, '/');
if (tmp)
return tmp;
}
return path_im;
case 'w':
return util_fmt_int("%d", image_width(im));
case 'h':
return util_fmt_int("%d", image_height(im));
case 's':
if (path_im) {
struct stat st;
if (!stat(path_im, &st))
return util_fmt_int("%d", st.st_size);
else
return "[err]";
}
return NULL;
case 'p':
return util_fmt_int("%d", image_width(im) * image_height(im));
case 't':
return image_format(im);
case '$':
return "$";
default:
return util_fmt_int("%c", c);
}
}
char *util_fmt_str(const char *str, struct tm *tm, char *path_im, char *path_thumb, Image im)
{
size_t buff_len = 4092;
char buf[buff_len];
strftime(buf, buff_len-1, str, tm);
int left = buff_len;
char *ret = malloc(buff_len);
ret[0] = '\0';
for (char *c = buf; *c != '\0'; c++) {
if (*c != '$' && *c != '\\') {
if (left == 1) {
left += buff_len;
buff_len *= 2;
ret = realloc(ret, buff_len);
}
ret[buff_len-left] = *c;
left--;
ret[buff_len-left] = '\0';
continue;
}
char *tmp = NULL;
if (*c == '$') {
c++;
tmp = util_fmt_char(*c, path_im, path_thumb, im);
} else if (*c == '\\') {
c++;
if (*c == 'n') {
if (path_im != NULL)
tmp = "\n";
} else {
tmp = util_fmt_int("%c", *c);
}
}
if (tmp != NULL) {
int len = strlen(tmp);
while (left < len + 1) {
left += buff_len;
buff_len *= 2;
ret = realloc(ret, buff_len);
}
left -= len;
strcat(ret, tmp);
}
}
return ret;
}
void util_error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
exit(EXIT_FAILURE);
}