common.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* common.c - Common functions
  2. Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
  3. Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  4. Copyright (C) 2008-2013 Daniel Baumann <mail@daniel-baumann.ch>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. On Debian systems, the complete text of the GNU General Public License
  16. can be found in /usr/share/common-licenses/GPL-3 file.
  17. */
  18. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  19. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdarg.h>
  24. #include <errno.h>
  25. #include "common.h"
  26. typedef struct _link {
  27. void *data;
  28. struct _link *next;
  29. } LINK;
  30. void die(char *msg, ...)
  31. {
  32. va_list args;
  33. va_start(args, msg);
  34. vfprintf(stderr, msg, args);
  35. va_end(args);
  36. fprintf(stderr, "\n");
  37. exit(1);
  38. }
  39. void pdie(char *msg, ...)
  40. {
  41. va_list args;
  42. va_start(args, msg);
  43. vfprintf(stderr, msg, args);
  44. va_end(args);
  45. fprintf(stderr, ":%s\n", strerror(errno));
  46. exit(1);
  47. }
  48. void *alloc(int size)
  49. {
  50. void *this;
  51. if ((this = malloc(size)))
  52. return this;
  53. pdie("malloc");
  54. return NULL; /* for GCC */
  55. }
  56. void *qalloc(void **root, int size)
  57. {
  58. LINK *link;
  59. link = alloc(sizeof(LINK));
  60. link->next = *root;
  61. *root = link;
  62. return link->data = alloc(size);
  63. }
  64. void qfree(void **root)
  65. {
  66. LINK *this;
  67. while (*root) {
  68. this = (LINK *) * root;
  69. *root = this->next;
  70. free(this->data);
  71. free(this);
  72. }
  73. }
  74. int min(int a, int b)
  75. {
  76. return a < b ? a : b;
  77. }
  78. char get_key(char *valid, char *prompt)
  79. {
  80. int ch, okay;
  81. while (1) {
  82. if (prompt)
  83. printf("%s ", prompt);
  84. fflush(stdout);
  85. while (ch = getchar(), ch == ' ' || ch == '\t') ;
  86. if (ch == EOF)
  87. exit(1);
  88. if (!strchr(valid, okay = ch))
  89. okay = 0;
  90. while (ch = getchar(), ch != '\n' && ch != EOF) ;
  91. if (ch == EOF)
  92. exit(1);
  93. if (okay)
  94. return okay;
  95. printf("Invalid input.\n");
  96. }
  97. }