A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

78 satır
1.8 KiB

  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. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include "options.h"
  21. #include "util.h"
  22. void cleanup();
  23. void* s_malloc(size_t size) {
  24. void *ptr;
  25. if (!(ptr = malloc(size)))
  26. die("could not allocate memory");
  27. return ptr;
  28. }
  29. void* s_realloc(void *ptr, size_t size) {
  30. if (!(ptr = realloc(ptr, size)))
  31. die("could not allocate memory");
  32. return ptr;
  33. }
  34. void warn(const char* fmt, ...) {
  35. va_list args;
  36. if (!fmt || options->quiet)
  37. return;
  38. va_start(args, fmt);
  39. fprintf(stderr, "sxiv: warning: ");
  40. vfprintf(stderr, fmt, args);
  41. fprintf(stderr, "\n");
  42. va_end(args);
  43. }
  44. void die(const char* fmt, ...) {
  45. va_list args;
  46. if (!fmt)
  47. return;
  48. va_start(args, fmt);
  49. fprintf(stderr, "sxiv: error: ");
  50. vfprintf(stderr, fmt, args);
  51. fprintf(stderr, "\n");
  52. va_end(args);
  53. cleanup();
  54. exit(1);
  55. }
  56. void size_readable(float *size, const char **unit) {
  57. const char *units[] = { "", "K", "M", "G" };
  58. int i;
  59. for (i = 0; i < LEN(units) && *size > 1024; ++i)
  60. *size /= 1024;
  61. *unit = units[MIN(i, LEN(units) - 1)];
  62. }