2 In and output, error messages, etc.
5 /* $Id: io.c,v 2.5 1995/08/17 14:30:55 ceriel Exp $ */
10 extern fatal(char *, ...);
20 extern int running; /* from main.c */
21 extern char *prog_name; /* from main.c */
22 extern char *load_name; /* from init.c */
24 /******** The message file ********/
26 extern char mess_file[64]; /* from main.c */
27 long mess_id; /* Id, to determine unique mess file */
28 FILE *mess_fp; /* Filepointer of message file */
33 { /* for a new child */
38 extern long inr; /* from log.c */
41 /******** General file handling ********/
43 PRIVATE int highestfd();
45 int fd_limit = 100; /* first non-available file descriptor */
50 /* Creates an unbuffered FILE with name fn on the highest
51 possible file descriptor.
56 if ((fd = creat(fn, 0644)) == -1)
59 if ((fp = fdopen(fd, "w")) == NULL)
61 setbuf(fp, (char *) 0); /* unbuffered! */
66 PRIVATE int highestfd(fd)
69 /* Moves the (open) file descriptor fd to the highest available
70 position and returns the new fd. Does this without knowing
71 how many fd-s are available.
73 register int newfd, higherfd;
75 /* try to get a better fd */
81 /* for systems with an unlimited supply of file descriptors */
86 /* occupying the new fd, try to do even better */
87 higherfd = highestfd(newfd);
89 return higherfd; /* this is a deep one */
92 init_ofiles(firsttime)
96 fclose(mess_fp); /* old message file */
98 sprintf(mess_file, "%s_%ld", mess_file, mess_id);
101 /* Create messagefile */
102 if ((mess_fp = fcreat_high(mess_file)) == NULL)
103 fatal("Cannot create messagefile '%s'", mess_file);
106 mess_id = 1; /* ID of next child */
115 fatal(char *fmt, ...)
119 fprintf(stderr, "%s: ", prog_name);
123 do_fatal(stderr, fmt, ap);
130 do_fatal(mess_fp, fmt, ap);
147 fprintf(stderr, "%s: ", prog_name);
151 register char *fmt = va_arg(ap, char *);
152 do_fatal(stderr, fmt, ap);
159 register char *fmt = va_arg(ap, char *);
160 do_fatal(mess_fp, fmt, ap);
175 /* all exits should go through here */
188 PRIVATE do_fatal(fp, fmt, ap)
193 fprintf(fp, "(Fatal error) ");
195 fprintf(fp, "%s: ", load_name);
196 vfprintf(fp, fmt, ap);
202 message(char *fmt, ...)
206 fprintf(mess_fp, "(Message): ");
210 vfprintf(mess_fp, fmt, ap);
214 fprintf(mess_fp, " at %s\n", position());
223 fprintf(mess_fp, "(Message): ");
227 register char *fmt = va_arg(ap, char *);
228 vfprintf(mess_fp, fmt, ap);
232 fprintf(mess_fp, " at %s\n", position());
236 char *position() /* transient */
238 static char buff[300];
239 register char *fn = dt_fname(getFIL());
242 sprintf(buff, "\"%s\", line %ld, INR = %ld", fn, getLIN(), inr);
244 sprintf(buff, "\"%s\", line %ld", fn, getLIN());
252 return (p ? &data_loc(p) : "<unknown>");