Skip to content

Commit

Permalink
Merge pull request #11 from wictory/use_safe_functions
Browse files Browse the repository at this point in the history
Use safe functions
  • Loading branch information
pbeckingham committed Apr 27, 2019
2 parents eb90db9 + ce18895 commit 4178b49
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ set (HAVE_CMAKE true)
project (shared)
include (CXXSniffer)

include (CheckFunctionExists)

set (PROJECT_VERSION "1.0.0")

set (PACKAGE "${PROJECT_NAME}")
Expand All @@ -16,6 +18,8 @@ set (PACKAGE_TARNAME "${PACKAGE}")
set (PACKAGE_VERSION "${VERSION}")
set (PACKAGE_STRING "${PACKAGE} ${VERSION}")

check_function_exists(strlcpy HAVE_STRLCPY)

message ("-- Configuring cmake.h")
configure_file (
${CMAKE_SOURCE_DIR}/cmake.h.in
Expand Down
3 changes: 3 additions & 0 deletions cmake.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
/* Found st.st_birthtime struct member */
#cmakedefine HAVE_ST_BIRTHTIME

/* Found strlcpy() */
#cmakedefine HAVE_STRLCPY

/* Functions */
#cmakedefine HAVE_GET_CURRENT_DIR_NAME
#cmakedefine HAVE_TIMEGM
Expand Down
26 changes: 15 additions & 11 deletions src/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ std::string formatBytes (size_t bytes)
{
char formatted[24];

if (bytes >= 995000000) sprintf (formatted, "%.1f GiB", bytes / 1000000000.0);
else if (bytes >= 995000) sprintf (formatted, "%.1f MiB", bytes / 1000000.0);
else if (bytes >= 995) sprintf (formatted, "%.1f KiB", bytes / 1000.0);
else sprintf (formatted, "%d B", (int)bytes);
if (bytes >= 995000000) snprintf (formatted, sizeof(formatted), "%.1f GiB", bytes / 1000000000.0);
else if (bytes >= 995000) snprintf (formatted, sizeof(formatted), "%.1f MiB", bytes / 1000000.0);
else if (bytes >= 995) snprintf (formatted, sizeof(formatted), "%.1f KiB", bytes / 1000.0);
else snprintf (formatted, sizeof(formatted), "%d B", (int)bytes);

return commify (formatted);
}
Expand All @@ -259,14 +259,18 @@ std::string formatTime (time_t seconds)
char formatted[24];
float days = (float) seconds / 86400.0;

if (seconds >= 86400 * 365) sprintf (formatted, "%.1f y", (days / 365.0));
else if (seconds >= 86400 * 84) sprintf (formatted, "%1d mo", (int) (days / 30));
else if (seconds >= 86400 * 13) sprintf (formatted, "%d wk", (int) (float) (days / 7.0));
else if (seconds >= 86400) sprintf (formatted, "%d d", (int) days);
else if (seconds >= 3600) sprintf (formatted, "%d h", (int) (seconds / 3600));
else if (seconds >= 60) sprintf (formatted, "%d m", (int) (seconds / 60));
else if (seconds >= 1) sprintf (formatted, "%d s", (int) seconds);
if (seconds >= 86400 * 365) snprintf (formatted, sizeof(formatted), "%.1f y", (days / 365.0));
else if (seconds >= 86400 * 84) snprintf (formatted, sizeof(formatted), "%1d mo", (int) (days / 30));
else if (seconds >= 86400 * 13) snprintf (formatted, sizeof(formatted), "%d wk", (int) (float) (days / 7.0));
else if (seconds >= 86400) snprintf (formatted, sizeof(formatted), "%d d", (int) days);
else if (seconds >= 3600) snprintf (formatted, sizeof(formatted), "%d h", (int) (seconds / 3600));
else if (seconds >= 60) snprintf (formatted, sizeof(formatted), "%d m", (int) (seconds / 60));
else if (seconds >= 1) snprintf (formatted, sizeof(formatted), "%d s", (int) seconds);
#ifdef HAVE_STRLCPY
else strlcpy (formatted, "-", sizeof(formatted));
#else
else strcpy (formatted, "-");
#endif /* HAVE_STRLCPY */

return std::string (formatted);
}
Expand Down

0 comments on commit 4178b49

Please sign in to comment.