diff options
author | Tejun Heo <htejun@gmail.com> | 2009-08-16 08:02:36 -0400 |
---|---|---|
committer | Jeff Garzik <jgarzik@redhat.com> | 2009-09-08 21:17:48 -0400 |
commit | 3e5cd1f2576c720f1d0705fdd7ba64f27e8836b7 (patch) | |
tree | 631d41c950d2dc93c110ca86e779cb749c9a8987 /drivers/firmware | |
parent | 02c24fa87724bb3af969463cd74dc3b3feb24740 (diff) |
dmi: extend dmi_get_year() to dmi_get_date()
There are cases where full date information is required instead of
just the year. Add month and day parsing to dmi_get_year() and rename
it to dmi_get_date().
As the original function only required '/' followed by any number of
parseable characters at the end of the string, keep that behavior to
avoid upsetting existing users.
The new function takes dates of format [mm[/dd]]/yy[yy]. Year, month
and date are checked to be in the ranges of [1-9999], [1-12] and
[1-31] respectively and any invalid or out-of-range component is
returned as zero.
The dummy implementation is updated accordingly but the return value
is updated to indicate field not found which is consistent with how
other dummy functions behave.
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Jeff Garzik <jgarzik@redhat.com>
Diffstat (limited to 'drivers/firmware')
-rw-r--r-- | drivers/firmware/dmi_scan.c | 76 |
1 files changed, 58 insertions, 18 deletions
diff --git a/drivers/firmware/dmi_scan.c b/drivers/firmware/dmi_scan.c index 531e621677ce..938100f14b16 100644 --- a/drivers/firmware/dmi_scan.c +++ b/drivers/firmware/dmi_scan.c | |||
@@ -568,36 +568,76 @@ const struct dmi_device * dmi_find_device(int type, const char *name, | |||
568 | EXPORT_SYMBOL(dmi_find_device); | 568 | EXPORT_SYMBOL(dmi_find_device); |
569 | 569 | ||
570 | /** | 570 | /** |
571 | * dmi_get_year - Return year of a DMI date | 571 | * dmi_get_date - parse a DMI date |
572 | * @field: data index (like dmi_get_system_info) | 572 | * @field: data index (see enum dmi_field) |
573 | * @yearp: optional out parameter for the year | ||
574 | * @monthp: optional out parameter for the month | ||
575 | * @dayp: optional out parameter for the day | ||
573 | * | 576 | * |
574 | * Returns -1 when the field doesn't exist. 0 when it is broken. | 577 | * The date field is assumed to be in the form resembling |
578 | * [mm[/dd]]/yy[yy] and the result is stored in the out | ||
579 | * parameters any or all of which can be omitted. | ||
580 | * | ||
581 | * If the field doesn't exist, all out parameters are set to zero | ||
582 | * and false is returned. Otherwise, true is returned with any | ||
583 | * invalid part of date set to zero. | ||
584 | * | ||
585 | * On return, year, month and day are guaranteed to be in the | ||
586 | * range of [0,9999], [0,12] and [0,31] respectively. | ||
575 | */ | 587 | */ |
576 | int dmi_get_year(int field) | 588 | bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp) |
577 | { | 589 | { |
578 | int year; | 590 | int year = 0, month = 0, day = 0; |
579 | const char *s = dmi_get_system_info(field); | 591 | bool exists; |
592 | const char *s, *y; | ||
580 | char *e; | 593 | char *e; |
581 | 594 | ||
582 | if (!s) | 595 | s = dmi_get_system_info(field); |
583 | return -1; | 596 | exists = s; |
584 | if (*s == '\0') | 597 | if (!exists) |
585 | return 0; | 598 | goto out; |
586 | s = strrchr(s, '/'); | ||
587 | if (!s) | ||
588 | return 0; | ||
589 | 599 | ||
590 | s += 1; | 600 | /* |
591 | year = simple_strtoul(s, &e, 10); | 601 | * Determine year first. We assume the date string resembles |
592 | if (s != e && year < 100) { /* 2-digit year */ | 602 | * mm/dd/yy[yy] but the original code extracted only the year |
603 | * from the end. Keep the behavior in the spirit of no | ||
604 | * surprises. | ||
605 | */ | ||
606 | y = strrchr(s, '/'); | ||
607 | if (!y) | ||
608 | goto out; | ||
609 | |||
610 | y++; | ||
611 | year = simple_strtoul(y, &e, 10); | ||
612 | if (y != e && year < 100) { /* 2-digit year */ | ||
593 | year += 1900; | 613 | year += 1900; |
594 | if (year < 1996) /* no dates < spec 1.0 */ | 614 | if (year < 1996) /* no dates < spec 1.0 */ |
595 | year += 100; | 615 | year += 100; |
596 | } | 616 | } |
617 | if (year > 9999) /* year should fit in %04d */ | ||
618 | year = 0; | ||
619 | |||
620 | /* parse the mm and dd */ | ||
621 | month = simple_strtoul(s, &e, 10); | ||
622 | if (s == e || *e != '/' || !month || month > 12) { | ||
623 | month = 0; | ||
624 | goto out; | ||
625 | } | ||
597 | 626 | ||
598 | return year; | 627 | s = e + 1; |
628 | day = simple_strtoul(s, &e, 10); | ||
629 | if (s == y || s == e || *e != '/' || day > 31) | ||
630 | day = 0; | ||
631 | out: | ||
632 | if (yearp) | ||
633 | *yearp = year; | ||
634 | if (monthp) | ||
635 | *monthp = month; | ||
636 | if (dayp) | ||
637 | *dayp = day; | ||
638 | return exists; | ||
599 | } | 639 | } |
600 | EXPORT_SYMBOL(dmi_get_year); | 640 | EXPORT_SYMBOL(dmi_get_date); |
601 | 641 | ||
602 | /** | 642 | /** |
603 | * dmi_walk - Walk the DMI table and get called back for every record | 643 | * dmi_walk - Walk the DMI table and get called back for every record |