Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 194 lines (159 sloc) 3.945 kB
8f4af65 @muennich Refactored, new files util.[ch], C89
authored
1 /* sxiv: util.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include <stdlib.h>
26cc5af @muennich Read filenames from stdin
authored
20 #include <string.h>
c21a3e3 @muennich Write thumbnail cache files on exit
authored
21 #include <unistd.h>
22 #include <errno.h>
8f4af65 @muennich Refactored, new files util.[ch], C89
authored
23
24 #include "options.h"
25 #include "util.h"
26
c21a3e3 @muennich Write thumbnail cache files on exit
authored
27 #define FNAME_LEN 1024
26cc5af @muennich Read filenames from stdin
authored
28
8f4af65 @muennich Refactored, new files util.[ch], C89
authored
29 void cleanup();
30
31 void* s_malloc(size_t size) {
32 void *ptr;
33
34 if (!(ptr = malloc(size)))
35 die("could not allocate memory");
36 return ptr;
37 }
38
39 void* s_realloc(void *ptr, size_t size) {
40 if (!(ptr = realloc(ptr, size)))
41 die("could not allocate memory");
42 return ptr;
43 }
44
45 void warn(const char* fmt, ...) {
46 va_list args;
47
48 if (!fmt || options->quiet)
49 return;
50
51 va_start(args, fmt);
52 fprintf(stderr, "sxiv: warning: ");
e8cf8da @muennich Fixed die/warn in util.c
authored
53 vfprintf(stderr, fmt, args);
8f4af65 @muennich Refactored, new files util.[ch], C89
authored
54 fprintf(stderr, "\n");
55 va_end(args);
56 }
57
58 void die(const char* fmt, ...) {
59 va_list args;
60
61 if (!fmt)
62 return;
63
64 va_start(args, fmt);
65 fprintf(stderr, "sxiv: error: ");
e8cf8da @muennich Fixed die/warn in util.c
authored
66 vfprintf(stderr, fmt, args);
8f4af65 @muennich Refactored, new files util.[ch], C89
authored
67 fprintf(stderr, "\n");
68 va_end(args);
69
70 cleanup();
71 exit(1);
72 }
bad9a70 @muennich Display filesize in window title
authored
73
74 void size_readable(float *size, const char **unit) {
75 const char *units[] = { "", "K", "M", "G" };
76 int i;
77
78 for (i = 0; i < LEN(units) && *size > 1024; ++i)
79 *size /= 1024;
80 *unit = units[MIN(i, LEN(units) - 1)];
81 }
26cc5af @muennich Read filenames from stdin
authored
82
c21a3e3 @muennich Write thumbnail cache files on exit
authored
83 char* absolute_path(const char *filename) {
84 size_t len;
85 char *path = NULL;
86 const char *basename;
87 char *dirname = NULL;
88 char *cwd = NULL;
89 char *twd = NULL;
90 char *dir;
91 char *s;
92
93 if (!filename || *filename == '\0' || *filename == '/')
94 return NULL;
95
96 len = FNAME_LEN;
97 cwd = (char*) s_malloc(len);
98 while (!(s = getcwd(cwd, len)) && errno == ERANGE) {
99 len *= 2;
100 cwd = (char*) s_realloc(cwd, len);
101 }
102 if (!s)
103 goto error;
104
105 s = strrchr(filename, '/');
106 if (s) {
107 len = s - filename;
108 dirname = (char*) s_malloc(len + 1);
109 strncpy(dirname, filename, len);
110 dirname[len] = '\0';
111 basename = s + 1;
112
113 if (chdir(cwd))
114 /* we're not able to come back afterwards */
115 goto error;
116 if (chdir(dirname))
117 goto error;
118
119 len = FNAME_LEN;
120 twd = (char*) s_malloc(len);
121 while (!(s = getcwd(twd, len)) && errno == ERANGE) {
122 len *= 2;
123 twd = (char*) s_realloc(twd, len);
124 }
125 if (chdir(cwd))
126 die("could not revert to working directory");
127 if (!s)
128 goto error;
129 dir = twd;
130 } else {
131 /* only a single filename given */
132 basename = filename;
133 dir = cwd;
134 }
135
136 len = strlen(dir) + strlen(basename) + 2;
137 path = (char*) s_malloc(len);
138 snprintf(path, len, "%s/%s", dir, basename);
139
140 goto end;
141
142 error:
143 if (path) {
144 free(path);
145 path = NULL;
146 }
147
148 end:
149 if (dirname)
150 free(dirname);
151 if (cwd)
152 free(cwd);
153 if (twd)
154 free(twd);
155
156 return path;
157 }
158
26cc5af @muennich Read filenames from stdin
authored
159 char* readline(FILE *stream) {
160 size_t len;
161 char *buf, *s, *end;
162
163 if (!stream || feof(stream) || ferror(stream))
164 return NULL;
165
166 len = FNAME_LEN;
167 s = buf = (char*) s_malloc(len * sizeof(char));
168
169 do {
170 *s = '\0';
171 fgets(s, len - (s - buf), stream);
172 if ((end = strchr(s, '\n'))) {
173 *end = '\0';
174 } else if (strlen(s) + 1 == len - (s - buf)) {
175 buf = (char*) s_realloc(buf, 2 * len * sizeof(char));
176 s = buf + len - 1;
177 len *= 2;
178 } else {
179 s += strlen(s);
180 }
181 } while (!end && !feof(stream) && !ferror(stream));
182
e8ed491 @muennich Fixed empty lines in readline()
authored
183 if (ferror(stream)) {
184 s = NULL;
185 } else {
26cc5af @muennich Read filenames from stdin
authored
186 s = (char*) s_malloc((strlen(buf) + 1) * sizeof(char));
187 strcpy(s, buf);
188 }
189
190 free(buf);
191
192 return s;
193 }
Something went wrong with that request. Please try again.