io.c 3.4 KB

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