io.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. In and output, error messages, etc.
  3. */
  4. /* $Header$ */
  5. #include <stdio.h>
  6. #include <varargs.h>
  7. #include "logging.h"
  8. #include "global.h"
  9. #include "mem.h"
  10. #include "linfil.h"
  11. extern int running; /* from main.c */
  12. extern char *prog_name; /* from main.c */
  13. extern char *load_name; /* from init.c */
  14. /******** The message file ********/
  15. extern char mess_file[64]; /* from main.c */
  16. long mess_id; /* Id, to determine unique mess file */
  17. FILE *mess_fp; /* Filepointer of message file */
  18. PRIVATE do_fatal();
  19. incr_mess_id()
  20. { /* for a new child */
  21. mess_id++;
  22. }
  23. #ifdef LOGGING
  24. extern long inr; /* from log.c */
  25. #endif /* LOGGING */
  26. /******** General file handling ********/
  27. PRIVATE int highestfd();
  28. int fd_limit = 100; /* first non-available file descriptor */
  29. FILE *fcreat_high(fn)
  30. char *fn;
  31. {
  32. /* Creates an unbuffered FILE with name fn on the highest
  33. possible file descriptor.
  34. */
  35. register int fd;
  36. register FILE *fp;
  37. if ((fd = creat(fn, 0644)) == -1)
  38. return NULL;
  39. fd = highestfd(fd);
  40. if ((fp = fdopen(fd, "w")) == NULL)
  41. return NULL;
  42. setbuf(fp, (char *) 0); /* unbuffered! */
  43. fd_limit = fd;
  44. return fp;
  45. }
  46. PRIVATE int highestfd(fd)
  47. int fd;
  48. {
  49. /* Moves the (open) file descriptor fd to the highest available
  50. position and returns the new fd. Does this without knowing
  51. how many fd-s are available.
  52. */
  53. register int newfd, higherfd;
  54. /* try to get a better fd */
  55. newfd = dup(fd);
  56. if (newfd < 0) {
  57. return fd;
  58. }
  59. if (newfd > 99) {
  60. /* for systems with an unlimited supply of file descriptors */
  61. close(newfd);
  62. return fd;
  63. }
  64. /* occupying the new fd, try to do even better */
  65. higherfd = highestfd(newfd);
  66. close(fd);
  67. return higherfd; /* this is a deep one */
  68. }
  69. init_ofiles(firsttime)
  70. int firsttime;
  71. {
  72. if (!firsttime) {
  73. fclose(mess_fp); /* old message file */
  74. mess_fp = 0;
  75. sprintf(mess_file, "%s_%ld", mess_file, mess_id);
  76. }
  77. /* Create messagefile */
  78. if ((mess_fp = fcreat_high(mess_file)) == NULL)
  79. fatal("Cannot create messagefile '%s'", mess_file);
  80. init_wmsg();
  81. mess_id = 1; /* ID of next child */
  82. #ifdef LOGGING
  83. open_log(firsttime);
  84. #endif /* LOGGING */
  85. }
  86. /*VARARGS0*/
  87. fatal(va_alist)
  88. va_dcl
  89. {
  90. va_list ap;
  91. fprintf(stderr, "%s: ", prog_name);
  92. va_start(ap);
  93. {
  94. register char *fmt = va_arg(ap, char *);
  95. do_fatal(stderr, fmt, ap);
  96. }
  97. va_end(ap);
  98. if (mess_fp) {
  99. va_start(ap);
  100. {
  101. register char *fmt = va_arg(ap, char *);
  102. do_fatal(mess_fp, fmt, ap);
  103. }
  104. va_end(ap);
  105. }
  106. if (running)
  107. core_dump();
  108. close_down(1);
  109. }
  110. close_down(rc)
  111. int rc;
  112. {
  113. /* all exits should go through here */
  114. if (mess_fp) {
  115. fclose(mess_fp);
  116. mess_fp = 0;
  117. }
  118. #ifdef LOGGING
  119. close_log();
  120. #endif /* LOGGING */
  121. exit(rc);
  122. }
  123. PRIVATE do_fatal(fp, fmt, ap)
  124. FILE *fp;
  125. char *fmt;
  126. va_list ap;
  127. {
  128. fprintf(fp, "(Fatal error) ");
  129. if (load_name)
  130. fprintf(fp, "%s: ", load_name);
  131. vfprintf(fp, fmt, ap);
  132. fputc('\n', fp);
  133. }
  134. /*VARARGS0*/
  135. message(va_alist)
  136. va_dcl
  137. {
  138. va_list ap;
  139. fprintf(mess_fp, "(Message): ");
  140. va_start(ap);
  141. {
  142. register char *fmt = va_arg(ap, char *);
  143. vfprintf(mess_fp, fmt, ap);
  144. }
  145. va_end(ap);
  146. fprintf(mess_fp, " at %s\n", position());
  147. }
  148. char *position() /* transient */
  149. {
  150. static char buff[300];
  151. register char *fn = dt_fname(getFIL());
  152. #ifdef LOGGING
  153. sprintf(buff, "\"%s\", line %ld, INR = %ld", fn, getLIN(), inr);
  154. #else /* LOGGING */
  155. sprintf(buff, "\"%s\", line %ld", fn, getLIN());
  156. #endif /* LOGGING */
  157. return buff;
  158. }
  159. char *dt_fname(p)
  160. ptr p;
  161. {
  162. return (p ? &data_loc(p) : "<unknown>");
  163. }