errno.h and Ubuntu 20.04
[geeqie.git] / src / misc.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Authors: Vladimir Nadvornik, Laurent Monin
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "main.h"
22 #include "misc.h"
23 #include "ui_fileops.h"
24
25 #include <errno.h>
26 #include <langinfo.h>
27 #include <locale.h>
28
29 gdouble get_zoom_increment(void)
30 {
31         return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 100.0 : 1.0);
32 }
33
34 gchar *utf8_validate_or_convert(const gchar *text)
35 {
36         gint len;
37
38         if (!text) return NULL;
39
40         len = strlen(text);
41         if (!g_utf8_validate(text, len, NULL))
42                 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
43
44         return g_strdup(text);
45 }
46
47 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
48 {
49         gchar *s1_key, *s2_key;
50         gchar *s1_t, *s2_t;
51         gint ret;
52
53         g_assert(g_utf8_validate(s1, -1, NULL));
54         g_assert(g_utf8_validate(s2, -1, NULL));
55
56         if (!case_sensitive)
57                 {
58                 s1_t = g_utf8_casefold(s1, -1);
59                 s2_t = g_utf8_casefold(s2, -1);
60                 }
61         else
62                 {
63                 s1_t = (gchar *) s1;
64                 s2_t = (gchar *) s2;
65                 }
66
67         s1_key = g_utf8_collate_key(s1_t, -1);
68         s2_key = g_utf8_collate_key(s2_t, -1);
69
70         ret = strcmp(s1_key, s2_key);
71
72         g_free(s1_key);
73         g_free(s2_key);
74
75         if (!case_sensitive)
76                 {
77                 g_free(s1_t);
78                 g_free(s2_t);
79                 }
80
81         return ret;
82 }
83
84 /* Borrowed from gtkfilesystemunix.c */
85 gchar *expand_tilde(const gchar *filename)
86 {
87 #ifndef G_OS_UNIX
88         return g_strdup(filename);
89 #else
90         const gchar *notilde;
91         const gchar *slash;
92         const gchar *home;
93
94         if (filename[0] != '~')
95                 return g_strdup(filename);
96
97         notilde = filename + 1;
98         slash = strchr(notilde, G_DIR_SEPARATOR);
99         if (slash == notilde || !*notilde)
100                 {
101                 home = g_get_home_dir();
102                 if (!home)
103                         return g_strdup(filename);
104                 }
105         else
106                 {
107                 gchar *username;
108                 struct passwd *passwd;
109
110                 if (slash)
111                         username = g_strndup(notilde, slash - notilde);
112                 else
113                         username = g_strdup(notilde);
114
115                 passwd = getpwnam(username);
116                 g_free(username);
117
118                 if (!passwd)
119                         return g_strdup(filename);
120
121                 home = passwd->pw_dir;
122                 }
123
124         if (slash)
125                 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
126         else
127                 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
128 #endif
129 }
130
131 /* Search for latitude/longitude parameters in a string
132  */
133
134 #define GEOCODE_NAME "geocode-parameters.awk"
135 #define BUFSIZE 128
136
137 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
138 {
139         gchar *message;
140         gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
141         gchar *cmd = g_strconcat("echo \'", input_text, "\'  | awk -f ", path, NULL);
142
143         if (g_file_test(path, G_FILE_TEST_EXISTS))
144                 {
145                 gchar buf[BUFSIZE];
146                 FILE *fp;
147
148                 if ((fp = popen(cmd, "r")) == NULL)
149                         {
150                         message = g_strconcat("Error: opening pipe\n", input_text, NULL);
151                         }
152                 else
153                         {
154                         while (fgets(buf, BUFSIZE, fp))
155                                 {
156                                 DEBUG_1("Output: %s", buf);
157                                 }
158
159                         message = g_strconcat(buf, NULL);
160
161                         if(pclose(fp))
162                                 {
163                                 message = g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL);
164                                 }
165                         }
166                 }
167         else
168                 {
169                 message = g_strconcat(input_text, NULL);
170                 }
171
172         g_free(path);
173         g_free(cmd);
174         return message;
175 }
176
177 gchar *decode_geo_parameters(const gchar *input_text)
178 {
179         gchar *message;
180         gchar *dir;
181
182         message = decode_geo_script(gq_bin_dir, input_text);
183         if (strstr(message, "Error"))
184                 {
185                 g_free(message);
186                 dir = g_build_filename(get_rc_dir(), "applications", NULL);
187                 message = decode_geo_script(dir, input_text);
188                 g_free(dir);
189                 }
190
191         return message;
192 }
193
194 /* Run a command like system() but may output debug messages. */
195 int runcmd(gchar *cmd)
196 {
197 #if 1
198         return system(cmd);
199         return 0;
200 #else
201         /* For debugging purposes */
202         int retval = -1;
203         FILE *in;
204
205         DEBUG_1("Running command: %s", cmd);
206
207         in = popen(cmd, "r");
208         if (in)
209                 {
210                 int status;
211                 const gchar *msg;
212                 gchar buf[2048];
213
214                 while (fgets(buf, sizeof(buf), in) != NULL )
215                         {
216                         DEBUG_1("Output: %s", buf);
217                         }
218
219                 status = pclose(in);
220
221                 if (WIFEXITED(status))
222                         {
223                         msg = "Command terminated with exit code";
224                         retval = WEXITSTATUS(status);
225                         }
226                 else if (WIFSIGNALED(status))
227                         {
228                         msg = "Command was killed by signal";
229                         retval = WTERMSIG(status);
230                         }
231                 else
232                         {
233                         msg = "pclose() returned";
234                         retval = status;
235                         }
236
237                 DEBUG_1("%s : %d\n", msg, retval);
238         }
239
240         return retval;
241 #endif
242 }
243
244 /**
245  * @brief Returns integer representing first_day_of_week
246  * @returns Integer in range 1 to 7
247  * 
248  * Uses current locale to get first day of week.
249  * If _NL_TIME_FIRST_WEEKDAY is not available, ISO 8601
250  * states first day of week is Monday.
251  * USA, Mexico and Canada (and others) use Sunday as first day of week.
252  * 
253  * Sunday == 1
254  */
255 gint date_get_first_day_of_week()
256 {
257 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
258         return nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
259 #else
260         gchar *dot;
261         gchar *current_locale;
262
263         current_locale = setlocale(LC_ALL, NULL);
264         dot = strstr(current_locale, ".");
265         if ((strncmp(dot - 2, "US", 2) == 0) || (strncmp(dot - 2, "MX", 2) == 0) || (strncmp(dot - 2, "CA", 2) == 0))
266                 {
267                 return 1;
268                 }
269         else
270                 {
271                 return 2;
272                 }
273 #endif
274 }
275
276 /**
277  * @brief Get an abbreviated day name from locale
278  * @param day Integer in range 1 to 7, representing day of week
279  * @returns String containing abbreviated day name
280  * 
281  *  Uses current locale to get day name
282  * 
283  * Sunday == 1
284  * Result must be freed
285  */
286 gchar *date_get_abbreviated_day_name(gint day)
287 {
288         gchar *abday = NULL;
289
290         switch (day)
291                 {
292                 case 1:
293                 abday = g_strdup(nl_langinfo(ABDAY_1));
294                 break;
295                 case 2:
296                 abday = g_strdup(nl_langinfo(ABDAY_2));
297                 break;
298                 case 3:
299                 abday = g_strdup(nl_langinfo(ABDAY_3));
300                 break;
301                 case 4:
302                 abday = g_strdup(nl_langinfo(ABDAY_4));
303                 break;
304                 case 5:
305                 abday = g_strdup(nl_langinfo(ABDAY_5));
306                 break;
307                 case 6:
308                 abday = g_strdup(nl_langinfo(ABDAY_6));
309                 break;
310                 case 7:
311                 abday = g_strdup(nl_langinfo(ABDAY_7));
312                 break;
313                 }
314
315         return abday;
316 }
317
318 gchar *convert_rating_to_stars(gint rating)
319 {
320         gchar *ret;
321         GString *str = g_string_new(NULL);
322
323         if (rating == -1)
324                 {
325                 str = g_string_append_unichar(str, options->star_rating.rejected);
326                 ret = g_strdup(str->str);
327                 g_string_free(str, TRUE);
328                 }
329         else if (rating > 0 && rating < 6)
330                 {
331                 while (rating > 0)
332                         {
333                         str = g_string_append_unichar(str, options->star_rating.star);
334                         rating = rating - 1;
335                         }
336                 ret = g_strdup(str->str);
337                 g_string_free(str, TRUE);
338                 }
339         else
340                 {
341                 ret = g_strdup("");
342                 }
343
344         return ret;
345 }
346
347 gchar *get_symbolic_link(const gchar *path_utf8)
348 {
349         gchar *sl;
350         struct stat st;
351         gchar *ret = g_strdup("");
352
353         sl = path_from_utf8(path_utf8);
354
355         if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
356                 {
357                 gchar *buf;
358                 gint l;
359
360                 buf = g_malloc(st.st_size + 1);
361                 l = readlink(sl, buf, st.st_size);
362
363                 if (l == st.st_size)
364                         {
365                         buf[l] = '\0';
366
367                         ret = buf;
368                         }
369                 else
370                         {
371                         g_free(buf);
372                         }
373                 }
374
375         g_free(sl);
376
377         return ret;
378 }
379
380 gint get_cpu_cores(void)
381 {
382         FILE *cpuinfo = fopen("/proc/cpuinfo", "rb");
383         char *arg = 0;
384         size_t size = 0;
385         int cores = 1;
386         gchar *siblings_line;
387         gchar *siblings_str;
388
389         while(getline(&arg, &size, cpuinfo) != -1)
390                 {
391                 siblings_line = g_strrstr(arg, "siblings");
392                 if (siblings_line)
393                         {
394                         siblings_str = g_strrstr(siblings_line, ":");
395                         if (siblings_str)
396                                 {
397                                 cores = g_ascii_strtoll(siblings_str + 1, NULL, 0);
398                                 }
399                         }
400                 }
401         free(arg);
402         fclose(cpuinfo);
403
404         return cores;
405 }
406
407 void tree_path_free_wrapper(void *data, void *useradata)
408 {
409         gtk_tree_path_free(data);
410 }
411
412 /* Copied from the libarchive .repo. examples */
413
414 #ifndef HAVE_ARCHIVE
415 gchar *open_archive(FileData *fd)
416 {
417         return NULL;
418 }
419
420 #else
421
422 #include <archive.h>
423 #include <archive_entry.h>
424
425 static void errmsg(const char *);
426 static gboolean extract(const char *filename, int do_extract, int flags);
427 static int copy_data(struct archive *, struct archive *);
428 static void msg(const char *);
429 static int verbose = 0;
430
431 gchar *open_archive(FileData *fd)
432 {
433         int flags;
434         gchar *current_dir;
435         gchar *destination_dir;
436         gboolean success;
437         gint error;
438
439         destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
440
441         if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
442                 {
443                 log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
444                 g_free(destination_dir);
445                 return NULL;
446                 }
447
448         current_dir = g_get_current_dir();
449         error = chdir(destination_dir);
450         if (error)
451                 {
452                 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n  Error code: "), strerror(errno), "\n");
453                 g_free(destination_dir);
454                 g_free(current_dir);
455                 return NULL;
456                 }
457
458         flags = ARCHIVE_EXTRACT_TIME;
459         success = extract(fd->path, 1, flags);
460
461         error = chdir(current_dir);
462         if (error)
463                 {
464                 log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n  Error code: "), strerror(errno), "\n");
465                 g_free(destination_dir);
466                 g_free(current_dir);
467                 return NULL;
468                 }
469         g_free(current_dir);
470
471         if (!success)
472                 {
473                 g_free(destination_dir);
474                 destination_dir = NULL;
475                 }
476
477         return destination_dir;
478 }
479
480 static gboolean extract(const char *filename, int do_extract, int flags)
481 {
482         struct archive *a;
483         struct archive *ext;
484         struct archive_entry *entry;
485         int r;
486
487         a = archive_read_new();
488         ext = archive_write_disk_new();
489         archive_write_disk_set_options(ext, flags);
490         archive_write_disk_set_standard_lookup(ext);
491         archive_read_support_filter_all(a);
492         archive_read_support_format_all(a);
493
494         if (filename != NULL && strcmp(filename, "-") == 0)
495                 {
496                 filename = NULL;
497                 }
498         if ((r = archive_read_open_filename(a, filename, 10240)))
499                 {
500                 errmsg(archive_error_string(a));
501                 errmsg("\n");
502                 return(FALSE);
503                 }
504         for (;;)
505                 {
506                 int needcr = 0;
507
508                 r = archive_read_next_header(a, &entry);
509                 if (r == ARCHIVE_EOF)
510                         {
511                         break;
512                         }
513                 if (r != ARCHIVE_OK)
514                         {
515                         errmsg(archive_error_string(a));
516                         errmsg("\n");
517                         return(FALSE);
518                         }
519                 if (verbose && do_extract)
520                         {
521                         msg("x ");
522                         }
523                 if (verbose || !do_extract)
524                         {
525                         msg(archive_entry_pathname(entry));
526                         msg(" ");
527                         needcr = 1;
528                         }
529                 if (do_extract)
530                         {
531                         r = archive_write_header(ext, entry);
532                         if (r != ARCHIVE_OK)
533                                 {
534                                 errmsg(archive_error_string(a));
535                                 needcr = 1;
536                                 }
537                         else
538                                 {
539                                 r = copy_data(a, ext);
540                                 if (r != ARCHIVE_OK)
541                                         {
542                                         needcr = 1;
543                                         }
544                                 }
545                         }
546                 if (needcr)
547                         {
548                         msg("\n");
549                         }
550                 }
551         archive_read_close(a);
552         archive_read_free(a);
553
554         archive_write_close(ext);
555         archive_write_free(ext);
556         return(TRUE);
557 }
558
559 static int copy_data(struct archive *ar, struct archive *aw)
560 {
561         int r;
562         const void *buff;
563         size_t size;
564         int64_t offset;
565
566         for (;;)
567                 {
568                 r = archive_read_data_block(ar, &buff, &size, &offset);
569                 if (r == ARCHIVE_EOF)
570                         return (ARCHIVE_OK);
571                 if (r != ARCHIVE_OK)
572                         {
573                         errmsg(archive_error_string(ar));
574                         return (r);
575                         }
576                 r = archive_write_data_block(aw, buff, size, offset);
577                 if (r != ARCHIVE_OK)
578                         {
579                         errmsg(archive_error_string(ar));
580                         return (r);
581                         }
582                 }
583 }
584
585 static void msg(const char *m)
586 {
587         log_printf("Open Archive - libarchive error: %s \n", m);
588 }
589
590 static void errmsg(const char *m)
591 {
592         if (m == NULL)
593                 {
594                 m = "Error: No error description provided.\n";
595                 }
596         log_printf("Open Archive - libarchive error: %s \n", m);
597 }
598 #endif
599 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */