common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. 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 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. On Debian systems, the complete text of the GNU General Public License
  15. can be found in /usr/share/common-licenses/GPL-3 file.
  16. */
  17. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  18. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdarg.h>
  23. #include <errno.h>
  24. #include "common.h"
  25. typedef struct _link {
  26. void *data;
  27. struct _link *next;
  28. } LINK;
  29. void die(char *msg,...)
  30. {
  31. va_list args;
  32. va_start(args,msg);
  33. vfprintf(stderr,msg,args);
  34. va_end(args);
  35. fprintf(stderr,"\n");
  36. exit(1);
  37. }
  38. void pdie(char *msg,...)
  39. {
  40. va_list args;
  41. va_start(args,msg);
  42. vfprintf(stderr,msg,args);
  43. va_end(args);
  44. fprintf(stderr,":%s\n",strerror(errno));
  45. exit(1);
  46. }
  47. void *alloc(int size)
  48. {
  49. void *this;
  50. if ((this = malloc(size))) return this;
  51. pdie("malloc");
  52. return NULL; /* for GCC */
  53. }
  54. void *qalloc(void **root,int size)
  55. {
  56. LINK *link;
  57. link = alloc(sizeof(LINK));
  58. link->next = *root;
  59. *root = link;
  60. return link->data = alloc(size);
  61. }
  62. void qfree(void **root)
  63. {
  64. LINK *this;
  65. while (*root) {
  66. this = (LINK *) *root;
  67. *root = this->next;
  68. free(this->data);
  69. free(this);
  70. }
  71. }
  72. int min(int a,int b)
  73. {
  74. return a < b ? a : b;
  75. }
  76. char get_key(char *valid,char *prompt)
  77. {
  78. int ch,okay;
  79. while (1) {
  80. if (prompt) printf("%s ",prompt);
  81. fflush(stdout);
  82. while (ch = getchar(), ch == ' ' || ch == '\t');
  83. if (ch == EOF) exit(1);
  84. if (!strchr(valid,okay = ch)) okay = 0;
  85. while (ch = getchar(), ch != '\n' && ch != EOF);
  86. if (ch == EOF) exit(1);
  87. if (okay) return okay;
  88. printf("Invalid input.\n");
  89. }
  90. }
  91. /* Local Variables: */
  92. /* tab-width: 8 */
  93. /* End: */