Skip to content

Commit

Permalink
resolve /etc/localtime by calling realpath
Browse files Browse the repository at this point in the history
  • Loading branch information
jgzhuang committed Jan 14, 2017
1 parent ea1717e commit 095f66a
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions tz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (c) 2015, 2016 Howard Hinnant
// Copyright (c) 2015 Ville Voutilainen
// Copyright (c) 2016 Alexander Kormanovsky
// Copyright (c) 2016 Jiangang Zhuang
// Copyright (c) 2016, 2017 Jiangang Zhuang
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -110,6 +110,8 @@
#else // !WIN32
# include <unistd.h>
# include <wordexp.h>
# include <limits.h>
# include <string.h>
# if !USE_SHELL_API
# include <sys/stat.h>
# include <sys/fcntl.h>
Expand Down Expand Up @@ -3097,11 +3099,19 @@ current_zone()
CONSTDATA auto timezone = "/etc/localtime";
if (lstat(timezone, &sb) == 0 && S_ISLNK(sb.st_mode) && sb.st_size > 0)
{
std::string result(sb.st_size, '\0');
auto sz = readlink(timezone, &result.front(), result.size());
if (sz == -1)
throw std::runtime_error("readlink failure");
result.resize(sz);
std::string result;
char rp[PATH_MAX];
if (realpath(timezone, rp))
result = std::string(rp);
else
{
std::ostringstream os;
char message[128];
strerror_r(errno, message, 128);
os << "realpath failure: errno = " << errno << "; " << message;
throw std::runtime_error(os.str());
}

const char zonepath[] = "/usr/share/zoneinfo/";
const std::size_t zonepath_len = sizeof(zonepath)/sizeof(zonepath[0])-1;
const std::size_t pos = result.find(zonepath);
Expand Down

0 comments on commit 095f66a

Please sign in to comment.