Handle hostnames with upper-case letters
[webmin.git] / time / linux-lib.pl
1 # Functions for getting and setting the timezone on Linux
2
3 $timezones_file = "/usr/share/zoneinfo/zone.tab";
4 $currentzone_link = "/etc/localtime";
5 $currentzone_file = "/etc/timezone";
6 $timezones_dir = "/usr/share/zoneinfo";
7 $sysclock_file = "/etc/sysconfig/clock";
8
9 # list_timezones()
10 sub list_timezones
11 {
12 local @rv;
13 local %done;
14 &open_readfile(ZONE, $timezones_file) || return ( );
15 while(<ZONE>) {
16         s/\r|\n//g;
17         s/^\s*#.*$//;
18         if (/^(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)/) {
19                 push(@rv, [ $3, $4 ]);
20                 $done{$3}++;
21                 }
22         elsif (/^(\S+)\s+(\S+)\s+(\S+)/) {
23                 push(@rv, [ $3, undef ]);
24                 $done{$3}++;
25                 }
26         }
27 close(ZONE);
28 push(@rv, [ "GMT", "GMT" ]) if (!$done{'GMT'});
29 push(@rv, [ "UTC", "UTC" ]) if (!$done{'UTC'});
30 return sort { $a->[0] cmp $b->[0] } @rv;
31 }
32
33 # get_current_timezone()
34 sub get_current_timezone
35 {
36 local $lnk = readlink(&translate_filename($currentzone_link));
37 if ($lnk) {
38         # Easy - it a link
39         $lnk =~ s/$timezones_dir\///;
40         return $lnk;
41         }
42 else {
43         # Need to compare with all timezone files!
44         return &find_same_zone($currentzone_link);
45         }
46 }
47
48 # set_current_timezone(zone)
49 sub set_current_timezone
50 {
51 &lock_file($currentzone_link);
52 unlink(&translate_filename($currentzone_link));
53 symlink(&translate_filename("$timezones_dir/$_[0]"),
54         &translate_filename($currentzone_link));
55 &unlock_file($currentzone_link);
56
57 if (-r $currentzone_file) {
58         # This file is used on Debian systems
59         &open_lock_tempfile(FILE, ">$currentzone_file");
60         &print_tempfile(FILE, $_[0],"\n");
61         &close_tempfile(FILE);
62         }
63
64 local %clock;
65 if (&read_env_file($sysclock_file, \%clock)) {
66         $clock{'ZONE'} = $_[0];
67         &lock_file($sysclock_file);
68         &write_env_file($sysclock_file, \%clock);
69         &unlock_file($sysclock_file);
70         }
71 }
72
73 sub os_has_timezones
74 {
75 return -r $timezones_file;
76 }
77
78 sub timezone_files
79 {
80 local @rv = ( $currentzone_link );
81 push(@rv, $currentzone_file) if (-r $currentzone_file);
82 push(@rv, $sysclock_file) if (-r $sysclock_file);
83 return @rv;
84 }
85
86 1;
87