chiark / gitweb /
Use format patterns for usec_t, pid_t, nsec_t, usec_t
[elogind.git] / src / shared / util.h
index b37072f24a2032402c90e30a4899c70891194cf1..d9720d0a368a7a18aa3c3c6a160e9efc59bf67af 100644 (file)
 #include <mntent.h>
 #include <sys/socket.h>
 
+#if SIZEOF_PID_T == 4
+#  define PID_FMT "%" PRIu32
+#elif SIZEOF_PID_T == 2
+#  define PID_FMT "%" PRIu16
+#else
+#  error Unknown pid_t size
+#endif
+
+#if SIZEOF_UID_T == 4
+#  define UID_FMT "%" PRIu32
+#elif SIZEOF_UID_T == 2
+#  define UID_FMT "%" PRIu16
+#else
+#  error Unknown uid_t size
+#endif
+
 #include "macro.h"
 #include "time-util.h"
 
 /* What is interpreted as whitespace? */
 #define WHITESPACE " \t\n\r"
-#define NEWLINE "\n\r"
-#define QUOTES "\"\'"
-#define COMMENTS "#;"
+#define NEWLINE    "\n\r"
+#define QUOTES     "\"\'"
+#define COMMENTS   "#;"
+#define GLOB_CHARS "*?["
 
 #define FORMAT_BYTES_MAX 8
 
@@ -627,6 +644,13 @@ bool path_is_safe(const char *p) _pure_;
 bool string_is_safe(const char *p) _pure_;
 bool string_has_cc(const char *p) _pure_;
 
+/**
+ * Check if a string contains any glob patterns.
+ */
+_pure_ static inline bool string_is_glob(const char *p) {
+        return !!strpbrk(p, GLOB_CHARS);
+}
+
 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
                  int (*compar) (const void *, const void *, void *),
                  void *arg);
@@ -755,7 +779,7 @@ int unlink_noerrno(const char *path);
                 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_); \
+                sprintf(_r_, "/proc/"PID_FMT"/" field, _pid_); \
                 _r_;                                                    \
         })