All of lore.kernel.org
 help / color / mirror / Atom feed
From: QI Fuli <qi.fuli@jp.fujitsu.com>
To: linux-nvdimm@lists.01.org
Subject: [RFC PATCH v2 2/7] ndctl: nvdimmd: add nvdimmd necessary util functions
Date: Tue, 14 Nov 2017 16:46:59 +0900	[thread overview]
Message-ID: <20171114074704.3446-3-qi.fuli@jp.fujitsu.com> (raw)
In-Reply-To: <20171114074704.3446-1-qi.fuli@jp.fujitsu.com>

This patch is used to provide helper functions needed for nvdimm daemon.
These util functions can be used by other features as well in the future.

Signed-off-by: QI Fuli <qi.fuli@jp.fujitsu.com>
---
 nvdimmd/util.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 nvdimmd/util.h | 33 ++++++++++++++++++++++++
 2 files changed, 113 insertions(+)
 create mode 100644 nvdimmd/util.c
 create mode 100644 nvdimmd/util.h

diff --git a/nvdimmd/util.c b/nvdimmd/util.c
new file mode 100644
index 0000000..ef6819e
--- /dev/null
+++ b/nvdimmd/util.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2017, FUJITSU LIMITED. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include "util.h"
+
+void logrotate(char *file)
+{
+	time_t c_time;
+	char buf[4096], tstamp[32];
+
+	c_time = time(NULL);
+	strftime(tstamp, sizeof(tstamp), ".%Y%m%d%H%M%S", localtime(&c_time));
+	strcpy(buf, file);
+	strcat(buf, tstamp);
+	rename(file, buf);
+}
+
+long get_size(char *file)
+{
+	struct stat buf;
+	if (stat(file, &buf) == 0)
+		return buf.st_size;
+	return -1L;
+}
+
+char *set_string(char **str, const char *value)
+{
+	*str = strdup(value);
+	return *str;
+}
+
+char *trim_string(char *str)
+{
+	char *start;
+	char *end;
+	int len = strlen(str);
+
+	if (len == 0)
+		return NULL;
+
+	if (str[len-1] == '\n') {
+		len--;
+		str[len] = 0;
+	}
+
+	start = str;
+	end = str + len - 1;
+	while(*start && isspace(*start))
+		start++;
+	while(*end && isspace(*end))
+		*end-- = 0;
+	strcpy(str, start);
+	return str;
+}
+
+int split_string(char *str, const char *deli, char *outlist[])
+{
+	int cnt = 0;
+	char *temp;
+
+	temp = strtok(str, deli);
+	while (temp != NULL && cnt < NUM_MAX_DIMM) {
+		outlist[cnt++] = temp;
+		temp = strtok(NULL, deli);
+	}
+	return cnt;
+}
diff --git a/nvdimmd/util.h b/nvdimmd/util.h
new file mode 100644
index 0000000..9648256
--- /dev/null
+++ b/nvdimmd/util.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2017, FUJITSU LIMITED. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+
+#ifndef _UTIL_H_
+#define _UTIL_H_
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <ctype.h>
+#define NUM_MAX_DIMM 1024
+
+void logrotate(char *file);
+
+long get_size(char *file);
+
+char *set_string(char **str, const char *value);
+
+char *trim_string(char *str);
+
+int split_string(char *str, const char *deli, char *outlist[]);
+
+#endif
-- 
2.9.5


_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm

  parent reply	other threads:[~2017-11-14  7:43 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-14  7:46 [RFC PATCH v2 0/7] ndctl: nvdimmd: notify/monitor the feathers of over threshold event QI Fuli
2017-11-14  7:46 ` [RFC PATCH v2 1/7] ndctl: nvdimmd: add LOG_NOTICE level into log_priority QI Fuli
2017-11-14  7:46 ` QI Fuli [this message]
2017-11-20 21:19   ` [RFC PATCH v2 2/7] ndctl: nvdimmd: add nvdimmd necessary util functions Dan Williams
2017-12-06  7:14     ` Qi, Fuli
2017-12-06 16:53       ` Dan Williams
2017-12-07  9:55         ` Qi, Fuli
2017-12-09 23:54           ` Dan Williams
2017-11-14  7:47 ` [RFC PATCH v2 3/7] ndctl: nvdimmd: add nvdimmd necessary functions QI Fuli
2017-11-14  7:47 ` [RFC PATCH v2 4/7] ndctl: nvdimmd: add body file of nvdimm daemon QI Fuli
2017-11-20 21:31   ` Dan Williams
2017-11-14  7:47 ` [RFC PATCH v2 5/7] ndctl: nvdimmd: add nvdimmd config file QI Fuli
2017-11-14  7:47 ` [RFC PATCH v2 6/7] ndctl: nvdimmd: add the unit file of systemd for nvdimmd service QI Fuli
2017-11-14  7:47 ` [RFC PATCH v2 7/7] ndctl: nvdimmd: add a temporary test for nvdimm daemon QI Fuli
2017-11-20  6:33 ` [RFC PATCH v2 0/7] ndctl: nvdimmd: notify/monitor the feathers of over threshold event Dan Williams
2017-11-20 10:07   ` Yasunori Goto
2017-11-20 16:13     ` Dan Williams
2017-11-21  1:59       ` Qi, Fuli
2017-12-07  9:58         ` Qi, Fuli
2017-12-07 19:34           ` Dan Williams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171114074704.3446-3-qi.fuli@jp.fujitsu.com \
    --to=qi.fuli@jp.fujitsu.com \
    --cc=linux-nvdimm@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.