chiark / gitweb /
core/killall: use procfs_file_alloca
[elogind.git] / src / shared / util.h
index 3aac165e67d0792f11857bb1f830783bf1ab2f36..6575f5681199108799b54dfa5fb5059538dce9b8 100644 (file)
@@ -21,6 +21,7 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <alloca.h>
 #include <inttypes.h>
 #include <time.h>
 #include <sys/time.h>
@@ -684,3 +685,24 @@ int unlink_noerrno(const char *path);
                 _new_ = alloca(_len_);                  \
                 (void *) memset(_new_, 0, _len_);       \
         })
+
+#define strappenda(a, b)                                \
+        ({                                              \
+                const char *_a_ = (a), *_b_ = (b);      \
+                char *_c_;                              \
+                size_t _x_, _y_;                        \
+                _x_ = strlen(_a_);                      \
+                _y_ = strlen(_b_);                      \
+                _c_ = alloca(_x_ + _y_ + 1);            \
+                strcpy(stpcpy(_c_, _a_), _b_);          \
+                _c_;                                    \
+        })
+
+#define procfs_file_alloca(pid, field)                                  \
+        ({                                                              \
+                pid_t _pid_ = (pid);                                    \
+                char *_r_;                                              \
+                _r_ = alloca(sizeof("/proc/") -1 + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
+                sprintf(_r_, "/proc/%lu/" field, (unsigned long) _pid_); \
+                _r_;                                                    \
+        })