]>
diplodocus.org Git - nmh/blob - docs/historical/2.9BSD/cmds/rescue.c
2 * Read up the messages in the named files and rehabilitate them into
3 * UNIX+ style messages on standard output.
5 * +Unix is a trademark of Bell Laboratories.
6 * The optional -d flag tests the arpa net to unix date format
7 * transformer by reading lines from standard input, modifying them
8 * to unix format, and outputing them on standard output.
15 char *index(), *rindex(), *reform();
25 register char *cp
, *fname
;
28 fprintf(stderr
, "Usage: rescue name ...\n");
34 if (strcmp(fname
, "-d") == 0) {
39 cp
= rindex(fname
, '/');
44 if (strcmp(cp
, "cur") == 0)
46 if (strcmp(cp
, "select") == 0)
51 if (!isdigit(*cp
++)) {
52 fprintf(stderr
, "%s: not mh message file\n",
60 fprintf(stderr
, "%d bad date(s) replaced by current date\n",
66 * Fix the named file by putting together a reasonable header
67 * and outputing the stuff onto standard output.
73 char linebuf
[BUFSIZ
], from
[BUFSIZ
], date
[BUFSIZ
];
77 if ((in
= fopen(name
, "r")) == NULL
) {
82 from
[0] = date
[0] = 0;
83 for (; from
[0] == 0 || date
[0] == 0;) {
84 if (fgets(linebuf
, BUFSIZ
, in
) == NULL
)
86 if (strlen(linebuf
) == 1)
88 if (linebuf
[0] == '-')
90 if (isfield(linebuf
, "from")) {
91 cp
= index(linebuf
, ':');
95 while (*cp
&& isspace(*cp
))
100 if (isfield(linebuf
, "date")) {
101 cp
= index(linebuf
, ':');
105 while (*cp
&& isspace(*cp
))
112 * Ready to print the from line
118 printf("From %s %s\n", from
, reform(date
));
122 while (fgets(linebuf
, BUFSIZ
, in
) != NULL
) {
124 (isfield(linebuf
, "from") || isfield(linebuf
, "date")))
126 if (strlen(linebuf
) == 1)
128 fputs(linebuf
, stdout
);
130 if (linebuf
[0] == '\n' && linebuf
[1] == 0)
136 fprintf(stderr
, "%s: Missing, bad, or incomplete header\n", name
);
142 * Determine if the passed line is a header of the given field name
148 register char *cp
, *cp2
;
152 while (lower(*cp
++) == lower(*cp2
++))
154 if (*--cp
== ':' && *--cp2
== 0)
160 * Lower case the given character
172 * Remove trailing newline from str, if present.
179 cp
= index(str
, '\n');
185 * Reformat the given Arpa net style date
186 * back into a unix ctime(3) date.
187 * Unfortunately, there appears to be NO standard arpa net
191 char *month
= "janfebmaraprmayjunjulaugsepoctnovdec";
197 static char retdate
[35];
199 register char *cp
, *cp2
, *mptr
;
207 while (*cp
!= ')' && *cp
)
224 *cp2
++ = tolower(*cp
++);
232 * Okie dokie. Now pick off the date part and store
233 * it away. Only possible formats here are:
238 if (index(dbuf
, '/')) {
239 d
.tm_mon
= atoi(dbuf
) - 1;
240 cp
= index(dbuf
, '/') + 1;
241 d
.tm_mday
= atoi(cp
);
242 if ((cp
= index(cp
, '/')) == 0)
245 d
.tm_year
= atoi(cp
);
246 if (d
.tm_year
> 1900)
253 d
.tm_mday
= atoi(dbuf
);
254 cp
= index(dbuf
, ' ');
257 while (*cp
&& isspace(*cp
))
261 for (mptr
= month
; *mptr
; mptr
+= 3)
262 if (strncmp(mptr
, cp
, 3) == 0)
266 d
.tm_mon
= (mptr
- month
)/3;
270 while (*cp
&& isspace(*cp
))
274 d
.tm_year
= atoi(cp
);
275 if (d
.tm_year
> 1900)
282 * Got the month part, now fix up the time.
288 * Basically, we lose by ignoring time zone.
291 while (*cp
&& isspace(*cp
))
295 if (strncmp(cp
, "at ", 3) != 0)
300 if (index(cp
, ':')) {
301 d
.tm_hour
= atoi(cp
);
302 cp
= index(cp
, ':') + 1;
304 if (index(cp
, ':')) {
305 cp
= index(cp
, ':') + 1;
308 while (*cp
&& !isspace(*cp
))
310 while (*cp
&& isspace(*cp
))
312 if (strncmp(cp
, "pm", 2) == 0 && d
.tm_hour
< 12)
321 strcpy(retdate
, ctime(&then
));
328 strcpy(retdate
, "************************");
331 strcpy(retdate
, ctime(&then
));
338 * Test the arpa net to UNIX date modifier.
339 * Reads lines from standard input, converting them
340 * to unix format, and displaying both on stdout.
349 printf("\"%s\" \"%s\"\n", buf
, cp
);
354 * Routine to convert a localtime(3) format date back into
355 * a system format date.
357 * Hats off to Bob Kridle for the insight that the way to do
358 * this is by binary search of the system date space.
361 struct tm
*localtime();
374 for (i
= 31; i
>= 0; i
--) {
377 if (dcmp(localtime(&conv
), dp
) > 0)
384 * Compare two localtime dates, return result.
388 if (dp->a > dp2->a) \
390 if (dp->a < dp2->a) \
394 register struct tm
*dp
, *dp2
;