From a7cc6fa00fec83351a383992a4901947761db990 Mon Sep 17 00:00:00 2001 From: oetiker Date: Fri, 19 Dec 2008 14:26:47 +0000 Subject: [PATCH] win32 fixes by Stefan . Ludewig . exitgames . com * added portable plbasename since basename is not available on stock win32 * rrd_update was using %d to show a timestamp ... %lli is better since it also will show 64bit time_t correctly. git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.3/program@1716 a5681a0c-68f1-0310-ab6d-d61299d08faa --- src/Makefile.am | 2 +- src/plbasename.c | 211 ++++++++++++++++++++++++++++++++++++++++++++ src/plbasename.h | 45 ++++++++++ src/rrd_graph.c | 2 + src/rrd_update.c | 2 +- win32/rrdlib.vcproj | 8 ++ 6 files changed, 268 insertions(+), 2 deletions(-) create mode 100644 src/plbasename.c create mode 100644 src/plbasename.h diff --git a/src/Makefile.am b/src/Makefile.am index f91ae6c..74d382f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -106,7 +106,7 @@ pkgconfig_DATA = librrd.pc # strftime is here because we do not usually need it. unices have propper # iso date support EXTRA_DIST= librrd.pc.in strftime.c strftime.h rrd_getopt.c rrd_getopt1.c rrd_getopt.h \ - win32comp.c rrd_thread_safe_nt.c get_ver.awk librrd.sym.in.in + win32comp.c rrd_thread_safe_nt.c get_ver.awk librrd.sym.in.in plbasename.c plbasename.h librrd.sym: librrd.sym.in grep -v LIBC_HAS_GETOPT_LONG librrd.sym.in >$@ diff --git a/src/plbasename.c b/src/plbasename.c new file mode 100644 index 0000000..dd695bb --- /dev/null +++ b/src/plbasename.c @@ -0,0 +1,211 @@ +#ifdef WIN32 +/* + * + * Cross-platform basename/dirname + * + * Copyright 2005 Syd Logan, All Rights Reserved + * + * This code is distributed without warranty. You are free to use this + * code for any purpose, however, if this code is republished or + * redistributed in its original form, as hardcopy or electronically, + * then you must include this copyright notice along with the code. + * + */ + +// minor changes 2008 by Stefan Ludewig stefan.ludewig@exitgames.com for WIN32 version RRDtool + +#include +#include +#include "plbasename.h" +#include +#if defined(TEST) +#include +#endif + +#if defined(__cplusplus) + +extern "C" { + +#endif + +const char * +PL_basename(const char *name) +{ + const char *base; + char *p; + static char *tmp = NULL; + int len; + + if (tmp) { + free(tmp); + tmp = NULL; + } + + if (!name || !strcmp(name, "")) + return ""; + + if (!strcmp(name, "/")) + return "/"; + + len = strlen(name); + if (name[len - 1] == '/') { + // ditch the trailing '/' + p = tmp = (char*)malloc(len); + strncpy(p, name, len - 1); + } else { + p = (char *) name; + } + + for (base = p; *p; p++) + if (*p == '/') + base = p + 1; + + return base; +} + +const char * +PL_dirname(const char *name) +{ + static char *ret = NULL; + int len; + int size = 0; + const char *p; + + if (ret) { + free(ret); + ret = NULL; + } + + if (!name || !strcmp(name, "") || !strstr(name, "/")) + return("."); + + if (!strcmp(name, "/")) + return(name); + + // find the last slash in the string + + len = strlen(name); + p = &name[len - 1]; + + if (*p == '/') p--; // skip the trailing / + + while (p != name && *p != '/') p--; + + size = p - name; + if (size) { + ret = (char*)malloc(size + 1); + memcpy(ret, name, size); + ret[size] = '\0'; + } else if (*p == '/') + return "/"; + else + return ""; + + return (const char *) ret; +} + +#if defined(__cplusplus) + +} + +#endif + +#if defined(TEST) + +int +main(int argc, char *argv[]) +{ +/* run the following tests: + + path dirname basename + "/usr/lib" "/usr" "lib" + "/usr/" "/" "usr" + "usr" "." "usr" + "/" "/" "/" + "." "." "." + ".." "." ".." + NULL "." "" + "" "." "" + "./.." "." ".." + + These results can be verified by running the unix commands + basename(1) and dirname(1). One tweek to the test strategy + used here would be, on darwin and linux, to shell out to + get the expected results vs hardcoding. +*/ + if (!strcmp(PL_basename("/usr/lib"), "lib")) + printf("PL_basename /usr/lib passed\n"); + else + printf("PL_basename /usr/lib failed expected lib\n"); + if (!strcmp(PL_dirname("/usr/lib"), "/usr")) + printf("PL_dirname /usr/lib passed\n"); + else + printf("PL_dirname /usr/lib failed expected /usr\n"); + if (!strcmp(PL_basename("/usr/"), "usr")) + printf("PL_basename /usr/ passed\n"); + else + printf("PL_basename /usr/ failed expected usr\n"); + if (!strcmp(PL_dirname("/usr/"), "/")) + printf("PL_dirname /usr/ passed\n"); + else + printf("PL_dirname /usr/ failed expected /\n"); + if (!strcmp(PL_basename("usr"), "usr")) + printf("PL_basename usr passed\n"); + else + printf("PL_basename usr failed expected usr\n"); + if (!strcmp(PL_dirname("usr"), ".")) + printf("PL_dirname usr passed\n"); + else + printf("PL_dirname usr failed expected .\n"); + if (!strcmp(PL_basename("/"), "/")) + printf("PL_basename / passed\n"); + else + printf("PL_basename / failed expected /\n"); + if (!strcmp(PL_dirname("/"), "/")) + printf("PL_dirname / passed\n"); + else + printf("PL_dirname / failed expected /\n"); + if (!strcmp(PL_basename("."), ".")) + printf("PL_basename . passed\n"); + else + printf("PL_basename . failed\n"); + if (!strcmp(PL_dirname("."), ".")) + printf("PL_dirname . passed\n"); + else + printf("PL_dirname . failed expected .\n"); + if (!strcmp(PL_basename(".."), "..")) + printf("PL_basename .. passed\n"); + else + printf("PL_basename .. failed expected ..\n"); + if (!strcmp(PL_dirname(".."), ".")) + printf("PL_dirname .. passed\n"); + else + printf("PL_dirname .. failed expected .\n"); + if (!strcmp(PL_basename(NULL), "")) + printf("PL_basename NULL passed\n"); + else + printf("PL_basename NULL failed expected \"\"\n"); + if (!strcmp(PL_dirname(NULL), ".")) + printf("PL_dirname NULL passed\n"); + else + printf("PL_dirname NULL failed expected .\n"); + if (!strcmp(PL_basename(""), "")) + printf("PL_basename \"\" passed\n"); + else + printf("PL_basename \"\" failed expected \"\"\n"); + if (!strcmp(PL_dirname(""), ".")) + printf("PL_dirname \"\" passed\n"); + else + printf("PL_dirname \"\" failed expected .\n"); + + if (!strcmp(PL_basename("./.."), "..")) + printf("PL_basename ./.. passed\n"); + else + printf("PL_basename ./.. failed expected ..\n"); + if (!strcmp(PL_dirname("./.."), ".")) + printf("PL_dirname ./.. passed\n"); + else + printf("PL_dirname ./.. failed expected .\n"); +} +#endif +#endif // WIN32 diff --git a/src/plbasename.h b/src/plbasename.h new file mode 100644 index 0000000..28786a1 --- /dev/null +++ b/src/plbasename.h @@ -0,0 +1,45 @@ +#ifdef WIN32 +/* + * + * Cross-platform basename/dirname + * + * Copyright 2005 Syd Logan, All Rights Reserved + * + * This code is distributed without warranty. You are free to use this + * code for any purpose, however, if this code is republished or + * redistributed in its original form, as hardcopy or electronically, + * then you must include this copyright notice along with the code. + * + */ + +// minor changes 2008 by Stefan Ludewig stefan.ludewig@exitgames.com for WIN32 version RRDtool + +#if !defined(__PL_BASENAME_H__) +#define __PL_BASENAME_H__ + +/* + path dirname basename + "/usr/lib" "/usr" "lib" + "/usr/" "/" "usr" + "usr" "." "usr" + "/" "/" "/" + "." "." "." + ".." "." ".." +*/ + +#if defined(__cplusplus) +extern "C" { +#endif + +const char *PL_basename(const char *name); +const char *PL_dirname(const char *name); + +#define basename(name) ((char*)PL_basename(name)) +#define dirname(name) ((char*)PL_dirname(name)) + +#if defined(__cplusplus) +} +#endif + +#endif +#endif // WIN32 diff --git a/src/rrd_graph.c b/src/rrd_graph.c index 0323077..c4e8bf0 100644 --- a/src/rrd_graph.c +++ b/src/rrd_graph.c @@ -9,7 +9,9 @@ #ifdef WIN32 #include "strftime.h" +#include "plbasename.h" #endif + #include "rrd_tool.h" #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) diff --git a/src/rrd_update.c b/src/rrd_update.c index aec0012..fbd2536 100644 --- a/src/rrd_update.c +++ b/src/rrd_update.c @@ -1943,7 +1943,7 @@ static int write_RRA_row( /* append info to the return hash */ *pcdp_summary = rrd_info_push(*pcdp_summary, sprintf_alloc - ("[%d]RRA[%s][%lu]DS[%s]", rra_time, + ("[%lli]RRA[%s][%lu]DS[%s]", rra_time, rrd->rra_def[rra_idx].cf_nam, rrd->rra_def[rra_idx].pdp_cnt, rrd->ds_def[ds_idx].ds_nam), diff --git a/win32/rrdlib.vcproj b/win32/rrdlib.vcproj index 00a8b01..dd5947e 100644 --- a/win32/rrdlib.vcproj +++ b/win32/rrdlib.vcproj @@ -164,6 +164,10 @@ RelativePath="..\src\hash_32.c" > + + @@ -298,6 +302,10 @@ RelativePath="..\src\fnv.h" > + + -- 2.30.2