]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
fgetlistaddr: add a filedescriptor only version of getlistaddr
authorBaptiste Daroussin <bapt@FreeBSD.org>
Mon, 26 Dec 2022 15:35:43 +0000 (16:35 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Mon, 26 Dec 2022 15:45:02 +0000 (16:45 +0100)
which reduces memory manipulation

include/getlistaddr.h
src/getlistaddr.c

index cee8177dd6b5ca35428946ba8064a821be24bcb2..3d11ae792a330475b20e5f77989e3ecc3e6ae55d 100644 (file)
@@ -25,5 +25,6 @@
 #define GETLISTADDR_H
 
 char *getlistaddr(const char *listdir);
+char *fgetlistaddr(int fd);
 
 #endif /* GETLISTADDR_H */
index 3daa3f230a751a50f775c09b483ab508531e3a1c..1d5bb9e3d5d37d20e80c1f56ddce25845cb72c68 100644 (file)
@@ -1,6 +1,6 @@
-/* Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk>
- *
- * $Id$
+/*
+ * Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk>
+ * Copyright (C) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
 #include "mygetline.h"
 #include "strgen.h"
 
+char *
+fgetlistaddr(int fd)
+{
+       char *tmpstr;
+       int listnamefd;
+
+       if ((listnamefd = openat(fd, "listaddress", O_RDONLY|O_CLOEXEC)) < 0) {
+               log_error(LOG_ARGS, "Could not open 'control/listaddress'");
+               exit(EXIT_FAILURE);
+       }
+       tmpstr = mygetline(listnamefd);
+       if (tmpstr == NULL) {
+               log_error(LOG_ARGS, "FATAL. Could not get listaddress "
+                   "in control/listaddress");
+               exit(EXIT_FAILURE);
+       }
+       if (strchr(tmpstr, '@') == NULL) {
+               log_error(LOG_ARGS, "FATAL. No @ sign in listaddress");
+               exit(EXIT_FAILURE);
+       }
+
+       chomp(tmpstr);
+       close(listnamefd);
+
+       return (tmpstr);
+}
+
 char *getlistaddr(const char *listdir)
 {
        char *tmpstr;