| 406 | Return true if successful. */ |
| 407 | |
| 408 | static bool |
| 409 | batch_convert (char const *input_filename, |
| 410 | char const *format, bool format_in_c_locale, |
| 411 | timezone_t tz, char const *tzstring) |
| 412 | { |
| 413 | FILE *in_stream; |
| 414 | |
| 415 | if (streq (input_filename, "-")) |
| 416 | { |
| 417 | input_filename = _("standard input"); |
| 418 | in_stream = stdin; |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | in_stream = fopen (input_filename, "r"); |
| 423 | if (in_stream == NULL) |
| 424 | error (EXIT_FAILURE, errno, "%s", quotef (input_filename)); |
| 425 | } |
| 426 | |
| 427 | char *line = NULL; |
| 428 | size_t buflen = 0; |
| 429 | bool ok = true; |
| 430 | while (true) |
| 431 | { |
| 432 | ssize_t line_length = getline (&line, &buflen, in_stream); |
| 433 | if (line_length < 0) |
| 434 | { |
| 435 | if (ferror (in_stream)) |
| 436 | error (EXIT_FAILURE, errno, _("%s: read error"), |
| 437 | quotef (input_filename)); |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | struct timespec when; |
| 442 | if (! parse_datetime2 (&when, line, NULL, |
| 443 | parse_datetime_flags, tz, tzstring)) |
| 444 | { |
| 445 | if (line[line_length - 1] == '\n') |
| 446 | line[line_length - 1] = '\0'; |
| 447 | error (0, 0, _("invalid date %s"), quote (line)); |
| 448 | ok = false; |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | ok &= show_date_helper (format, format_in_c_locale, when, tz); |
| 453 | } |
| 454 | |
| 455 | if (ferror (stdout)) |
| 456 | write_error (); |
| 457 | } |
| 458 | |
| 459 | if (fclose (in_stream) == EOF) |
| 460 | error (EXIT_FAILURE, errno, "%s", quotef (input_filename)); |
| 461 | |
| 462 | free (line); |
| 463 | |
| 464 | return ok; |
| 465 | } |
no test coverage detected