aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaroslav Škarvada <jskarvad@redhat.com>2015-05-12 18:06:56 +0200
committerAlexandra Yates <alexandra.yates@linux.intel.com>2015-09-17 17:10:10 -0700
commit19955580df1bc5857c515661214f0bd7bb24fbb7 (patch)
tree44e539816b8bd52d381a788d2db4812ed5bbb2d6
parent981f1c6a5388e604aaf6f9cf514990282f8da379 (diff)
downloadpowertop-2.0-19955580df1bc5857c515661214f0bd7bb24fbb7.tar.gz
Improve handling of reporting filenames
powertop --html generates 'powertop.html' file powertop --html=myfile.suffix generates 'myfile.suffix' file powertop -i 2 --html generates 'powertop-TIMESTAMPS.html' files powertop -i 2 --html=myfile.suffix generates 'myfile-TIMESTAMPS.suffix' files powertop -i 2 --html=myfile generates 'myfile-TIMESTAMPS' files Similarly for CSV. Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com>
-rw-r--r--src/main.cpp10
-rw-r--r--src/report/report.cpp35
2 files changed, 27 insertions, 18 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 16acc73..2709e71 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -420,6 +420,11 @@ int main(int argc, char **argv)
case 'C': /* csv report */
reporttype = REPORT_CSV;
sprintf(filename, "%s", optarg ? optarg : "powertop.csv");
+ if (!strlen(filename))
+ {
+ fprintf(stderr, _("Invalid CSV filename\n"));
+ exit(1);
+ }
break;
case OPT_DEBUG:
/* implemented using getopt_long(3) flag */
@@ -431,6 +436,11 @@ int main(int argc, char **argv)
case 'r': /* html report */
reporttype = REPORT_HTML;
sprintf(filename, "%s", optarg ? optarg : "powertop.html");
+ if (!strlen(filename))
+ {
+ fprintf(stderr, _("Invalid HTML filename\n"));
+ exit(1);
+ }
break;
case 'i':
iterations = (optarg ? atoi(optarg) : 1);
diff --git a/src/report/report.cpp b/src/report/report.cpp
index 3572200..a09a3a8 100644
--- a/src/report/report.cpp
+++ b/src/report/report.cpp
@@ -169,28 +169,27 @@ static void system_info(void)
void init_report_output(char *filename_str, int iterations)
{
size_t period;
- char file_prefix[PATH_MAX];
- char file_postfix[8];
+ string filename;
time_t stamp;
char datestr[200];
- string mystring = string(filename_str);
- sprintf(file_postfix, "%s",
- (reporttype == REPORT_HTML ? "html" : "csv"));
- period=mystring.find_last_of(".");
- snprintf(file_prefix, PATH_MAX, "%s",mystring.substr(0,period).c_str());
- memset(&datestr, 0, 200);
- memset(&stamp, 0, sizeof(time_t));
- stamp=time(NULL);
- strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M%S", localtime(&stamp));
-
- if (iterations != 1)
- snprintf(reportout.filename, PATH_MAX, "%s-%s.%s",
- file_prefix, datestr,file_postfix);
+ if (iterations == 1)
+ snprintf(reportout.filename, PATH_MAX, "%s", filename_str);
else
- snprintf(reportout.filename, PATH_MAX, "%s.%s",
- file_prefix, file_postfix);
-
+ {
+ filename = string(filename_str);
+ period = filename.find_last_of(".");
+ if (period > filename.length())
+ period = filename.length();
+ memset(&datestr, 0, 200);
+ memset(&stamp, 0, sizeof(time_t));
+ stamp = time(NULL);
+ strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M%S", localtime(&stamp));
+ snprintf(reportout.filename, PATH_MAX, "%s-%s%s",
+ filename.substr(0, period).c_str(), datestr,
+ filename.substr(period).c_str());
+ }
+
reportout.report_file = fopen(reportout.filename, "wm");
if (!reportout.report_file) {
fprintf(stderr, _("Cannot open output file %s (%s)\n"),