Blob Blame History Raw
From 58161da08c108243d59d58c6c1f9d3c97bb9a3ad Mon Sep 17 00:00:00 2001
From: Hans de Goede <hdegoede@redhat.com>
Date: Mon, 7 Mar 2022 12:25:56 +0100
Subject: [PATCH 1/6] ply-utils: Reintroduce ply_string_has_prefix helper

ply_string_has_prefix was dropped in commit c7965ea19abf ("ply-utils:
Drop unused ply_string_has_prefix helper"). We have a need for this
helper again, so reintroduce it.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 src/libply/ply-utils.c | 9 +++++++++
 src/libply/ply-utils.h | 1 +
 2 files changed, 10 insertions(+)

diff --git a/src/libply/ply-utils.c b/src/libply/ply-utils.c
index f90ac40..f457579 100644
--- a/src/libply/ply-utils.c
+++ b/src/libply/ply-utils.c
@@ -432,60 +432,69 @@ ply_copy_string_array (const char *const *array)
         int i;
 
         for (i = 0; array[i] != NULL; i++) {
         }
 
         copy = calloc (i + 1, sizeof(char *));
 
         for (i = 0; array[i] != NULL; i++) {
                 copy[i] = strdup (array[i]);
         }
 
         return copy;
 }
 
 void
 ply_free_string_array (char **array)
 {
         int i;
 
         if (array == NULL)
                 return;
 
         for (i = 0; array[i] != NULL; i++) {
                 free (array[i]);
                 array[i] = NULL;
         }
 
         free (array);
 }
 
+bool
+ply_string_has_prefix (const char *str, const char *prefix)
+{
+        if (str == NULL || prefix == NULL)
+                return false;
+
+        return strncmp (str, prefix, strlen (prefix)) == 0;
+}
+
 double
 ply_get_timestamp (void)
 {
         const double nanoseconds_per_second = 1000000000.0;
         double timestamp;
         struct timespec now = { 0L, /* zero-filled */ };
 
         clock_gettime (CLOCK_MONOTONIC, &now);
         timestamp = ((nanoseconds_per_second * now.tv_sec) + now.tv_nsec) /
                     nanoseconds_per_second;
 
         return timestamp;
 }
 
 void
 ply_save_errno (void)
 {
         assert (errno_stack_position < PLY_ERRNO_STACK_SIZE);
         errno_stack[errno_stack_position] = errno;
         errno_stack_position++;
 }
 
 void
 ply_restore_errno (void)
 {
         assert (errno_stack_position > 0);
         errno_stack_position--;
         errno = errno_stack[errno_stack_position];
 }
 
diff --git a/src/libply/ply-utils.h b/src/libply/ply-utils.h
index d7b7622..b4565c3 100644
--- a/src/libply/ply-utils.h
+++ b/src/libply/ply-utils.h
@@ -56,60 +56,61 @@ typedef enum
 #ifndef PLY_HIDE_FUNCTION_DECLARATIONS
 
 #define ply_round_to_multiple(n, m) (((n) + (((m) - 1))) & ~((m) - 1))
 
 bool ply_open_unidirectional_pipe (int *sender_fd,
                                    int *receiver_fd);
 int ply_connect_to_unix_socket (const char            *path,
                                 ply_unix_socket_type_t type);
 int ply_listen_to_unix_socket (const char            *path,
                                ply_unix_socket_type_t type);
 bool ply_get_credentials_from_fd (int    fd,
                                   pid_t *pid,
                                   uid_t *uid,
                                   gid_t *gid);
 
 bool ply_write (int         fd,
                 const void *buffer,
                 size_t      number_of_bytes);
 bool ply_write_uint32 (int      fd,
                        uint32_t value);
 bool ply_read (int    fd,
                void  *buffer,
                size_t number_of_bytes);
 bool ply_read_uint32 (int       fd,
                       uint32_t *value);
 
 bool ply_fd_has_data (int fd);
 bool ply_set_fd_as_blocking (int fd);
 char **ply_copy_string_array (const char *const *array);
 void ply_free_string_array (char **array);
+bool ply_string_has_prefix (const char *str, const char *prefix);
 double ply_get_timestamp (void);
 
 void ply_save_errno (void);
 void ply_restore_errno (void);
 
 bool ply_directory_exists (const char *dir);
 bool ply_file_exists (const char *file);
 bool ply_character_device_exists (const char *device);
 
 ply_module_handle_t *ply_open_module (const char *module_path);
 ply_module_handle_t *ply_open_built_in_module (void);
 
 ply_module_function_t ply_module_look_up_function (ply_module_handle_t *handle,
                                                    const char          *function_name);
 void ply_close_module (ply_module_handle_t *handle);
 
 bool ply_create_directory (const char *directory);
 bool ply_create_file_link (const char *source,
                            const char *destination);
 void ply_show_new_kernel_messages (bool should_show);
 
 ply_daemon_handle_t *ply_create_daemon (void);
 bool ply_detach_daemon (ply_daemon_handle_t *handle,
                         int                  exit_code);
 
 int ply_utf8_character_get_size (const char *string,
                                  size_t      n);
 int ply_utf8_string_get_length (const char *string,
                                 size_t      n);
 
-- 
2.37.0.rc1