Sfoglia il codice sorgente

vcc: use container_of instead of members in klist_node

intirain 4 anni fa
parent
commit
4f816eb84f
9 ha cambiato i file con 46 aggiunte e 15 eliminazioni
  1. 1 1
      Makefile
  2. 18 8
      cmd.c
  3. 2 2
      include/klist.h
  4. 8 0
      include/vcc/interfaces.h
  5. 2 2
      include/vcc/version.h
  6. 0 1
      klist.c
  7. 8 0
      plugins/m4/m4.c
  8. 3 0
      po/zh_CN.po
  9. 4 1
      vcc.c

+ 1 - 1
Makefile

@@ -1,6 +1,6 @@
 VCC_MAJOR 		= 1
 VCC_MINOR 		= 26
-VCC_PATCH 		= 0
+VCC_PATCH 		= 1
 VCC_STR 		= -rc 
 
 # change this to '/usr/share/' or '/opt/vcc/' etc

+ 18 - 8
cmd.c

@@ -550,7 +550,8 @@ int do_cmd_cqd(int argc, char **argv) {
 
 int do_cmd_ban(int argc, char **argv) {
 	char 			*people_name, people_arr[USRNAME_SIZE];
-	struct klist_node	*i;
+	struct banlist 		*i;
+	struct klist_node 	*n;
 
 	
 	switch (argc) {
@@ -572,7 +573,9 @@ int do_cmd_ban(int argc, char **argv) {
 		return 1;
 	}
 
-	for (i = banned_people.next; i != &banned_people; i = i->next) {
+	for (n = banned_people.next; n != &banned_people; n = n->next) {
+		i = banlist_of(n);
+
 		if (!strcmp(people_name, i->usrname)) {
 			fprintf(stderr, _("User %s is already banned\n"), people_name);
 			free(people_name);
@@ -581,15 +584,16 @@ int do_cmd_ban(int argc, char **argv) {
 		}
 	}
 
-	if (unlikely(!(i = malloc(sizeof(struct klist_node))))) {
+	if (unlikely(!(i = malloc(sizeof(struct banlist))))) {
 		fprintf(stderr, "*** malloc() failed\n");
 
 		return 1;
 	}
 
-	klist_init(i);
 	i->usrname = people_name;
-	klist_add(&banned_people, i);
+	klist_init(&i->node);
+
+	klist_add(&banned_people, &i->node);
 
 	return 0;
 }
@@ -597,6 +601,7 @@ int do_cmd_ban(int argc, char **argv) {
 int do_cmd_unban(int argc, char **argv) {
 	char 			*people_name;
 	struct klist_node 	*i;
+	struct banlist 		*ban;
 
 	switch (argc) {
 	case 1:
@@ -619,15 +624,20 @@ int do_cmd_unban(int argc, char **argv) {
 	}
 	
 	for (i = banned_people.next; i != &banned_people; i = i->next) {
-		if (!strcmp(people_name, i->usrname)) {
+		ban = banlist_of(i);
+
+		if (!strcmp(people_name, ban->usrname)) {
 			klist_del(&banned_people, i);
-			free(i->usrname);
-			free(i);
+
+			free(ban->usrname);
 
 			break;
 		}
 	}
 
+	if (i == &banned_people) 
+		fprintf(stderr, _("User not banned. \n"));
+
 	if (argc == 1) 
 		free(people_name);
 

+ 2 - 2
include/klist.h

@@ -23,6 +23,7 @@
 #define __KLIST_H
 
 #include <vcc/vcc.h>
+#include <stddef.h>
 
 
 /* before, i think using a 'container_of' in things we did in kernel 
@@ -30,7 +31,6 @@
  * too many things in the structure. */
 
 struct klist_node {
-	char				*usrname;
 	struct klist_node 		*next, *prev;
 };
 
@@ -44,11 +44,11 @@ void klist_del(struct klist_node *head, struct klist_node *will_del);
 #define klist_empty(list) ((list)->next == (list))
 
 #define KLIST_NODE_INIT(__x) { \
-	.usrname = NULL, \
 	.next = __x, \
 	.prev = __x, \
 }
 
+#define container_of(ptr, type, member) ((type *) ((void *) (ptr) - offsetof(type, member)))
 
 #endif
 

+ 8 - 0
include/vcc/interfaces.h

@@ -24,6 +24,14 @@
 
 #include <sys/types.h>
 #include <vcc/vcc.h>
+#include <klist.h>
+
+struct banlist {
+	char 			*usrname;
+	struct klist_node 	node;
+};
+
+#define banlist_of(n) 		container_of(n, struct banlist, node)
 
 int vcc_login(char *, char *);
 int do_intiauth(void);

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.26.0 Wed Jun  8 12:56:51 PM CST 2022 root"
-#define VCC_SEMVER "vcc 1.26.0-rc "
+#define VCC_VERSION "VCC 1.26.1 Wed Jun  8 01:11:02 PM CST 2022 root"
+#define VCC_SEMVER "vcc 1.26.1-rc "

+ 0 - 1
klist.c

@@ -28,7 +28,6 @@
 void klist_init(struct klist_node *head) {
 	head->next = head;
 	head->prev = head;
-	head->usrname = NULL;
 
 	return;
 }

+ 8 - 0
plugins/m4/m4.c

@@ -95,6 +95,13 @@ int do_cmd_m4def(int argc, char **argv) {
 	return 0;
 }
 
+int m4_write_predef(int fd) {
+	(void) fd;
+
+	return 0;
+}
+
+
 char *m4_msghook(char *s) {
 	int 	fdt, size;
 	FILE 	*fp;
@@ -107,6 +114,7 @@ char *m4_msghook(char *s) {
 	}
 	
 	write(fdt, m4cachep, m4cache_size);
+	m4_write_predef(fdt);
 
 	size = strlen(s) + 1;
 	write(fdt, s, size);

+ 3 - 0
po/zh_CN.po

@@ -467,4 +467,7 @@ msgstr "%s: 无此命令\n"
 msgid "Do you mean -%s? \n"
 msgstr "你是想使用 -%s 吗?\n"
 
+#, c-format
+msgid "User not banned. \n"
+msgstr "该用户未被办\n"
 

+ 4 - 1
vcc.c

@@ -373,9 +373,12 @@ int incr_score(char *name, int incr) {
 
 int is_user_banned(char *usrname) {
 	struct klist_node *item;
+	struct banlist *ban;
 
 	for (item = banned_people.next; item != &banned_people; item = item->next) {
-		if (item->usrname && !strcmp(usrname, item->usrname)) {
+		ban = banlist_of(item);
+
+		if (ban->usrname && !strcmp(usrname, ban->usrname)) {
 			return 1;
 		}
 	}