?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /* l_tgt.c - target names list structure.
  2.  
  3.    This is free and unencumbered software released into the public domain.
  4.    For more information, please refer to <http://unlicense.org>. */
  5.  
  6. #include "defs.h"
  7.  
  8. #include <stdbool.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "debug.h"
  12. #include "l_list.h"
  13. #include "l_tgt.h"
  14.  
  15. void
  16.     target_name_entry_clear
  17.     (
  18.         struct target_name_entry_t *self
  19.     )
  20. {
  21.     list_entry_clear (&self->list_entry);
  22.     self->name = NULL;
  23. }
  24.  
  25. void
  26.     target_name_entry_free
  27.     (
  28.         struct target_name_entry_t *self
  29.     )
  30. {
  31.     list_entry_free (&self->list_entry);
  32.     if (self->name)
  33.         free (self->name);
  34.     target_name_entry_clear (self);
  35. }
  36.  
  37. void
  38.     target_names_clear
  39.     (
  40.         struct target_names_t *self
  41.     )
  42. {
  43.     list_clear (&self->list);
  44. }
  45.  
  46. bool
  47.     target_names_add
  48.     (
  49.         struct target_names_t *self,
  50.         const char *name,
  51.         struct target_name_entry_t **result
  52.     )
  53. {
  54.     bool ok;
  55.     struct target_name_entry_t *p;
  56.     char *p_name;
  57. #if DEBUG == 1
  58.     unsigned i;
  59. #endif  // DEBUG == 1
  60.  
  61.     ok = false;
  62.     p = (struct target_name_entry_t *) NULL;
  63.     p_name = (char *) NULL;
  64.  
  65.     if (!self || !name)
  66.     {
  67.         _DBG ("Bad arguments.");
  68.         goto _local_exit;
  69.     }
  70.  
  71.     p = malloc (sizeof (struct target_name_entry_t));
  72.     if (!p)
  73.     {
  74.         _perror ("malloc");
  75.         goto _local_exit;
  76.     }
  77.     p_name = strdup (name);
  78.     if (!p_name)
  79.     {
  80.         _perror ("strdup");
  81.         goto _local_exit;
  82.     }
  83.  
  84.     target_name_entry_clear (p);
  85.     p->name = p_name;
  86.  
  87. #if DEBUG == 1
  88.     i = self->list.count;
  89. #endif  // DEBUG == 1
  90.     list_add_entry ((struct list_t *) self, (struct list_entry_t *) p);
  91.  
  92.     _DBG_ ("Added new target name #%u: '%s'", i, p->name);
  93.  
  94.     ok = true;
  95.  
  96. _local_exit:
  97.     if (!ok)
  98.     {
  99.         if (p)
  100.         {
  101.             free (p);
  102.             p = (struct target_name_entry_t *) NULL;
  103.         }
  104.         if (p_name)
  105.             free (p_name);
  106.     }
  107.     if (result)
  108.         *result = p;
  109.     return !ok;
  110. }
  111.  
  112. bool
  113.     target_names_print
  114.     (
  115.         struct target_names_t *self,
  116.         FILE *stream
  117.     )
  118. {
  119.     const struct target_name_entry_t *p;
  120.     bool padding;
  121.  
  122.     if (!self || !stream)
  123.     {
  124.         _DBG ("Bad arguments.");
  125.         return true;
  126.     }
  127.  
  128.     padding = false;
  129.     for (p = (struct target_name_entry_t *) self->list.first; p;
  130.          p = (struct target_name_entry_t *) p->list_entry.next)
  131.     {
  132.         if (fprintf (stream, padding ? " %s" : "%s", p->name) < 0)
  133.         {
  134.             _perror ("fprintf");
  135.             return true;
  136.         }
  137.         padding = true;
  138.     }
  139.  
  140.     return false;
  141. }
  142.  
  143. #if DEBUG == 1
  144. void
  145.     _DBG_target_names_dump
  146.     (
  147.         struct target_names_t *self
  148.     )
  149. {
  150.     struct target_name_entry_t *p;
  151.     unsigned i;
  152.  
  153.     if (!self)
  154.     {
  155.         _DBG ("Bad arguments.");
  156.         return;
  157.     }
  158.  
  159.     p = (struct target_name_entry_t *) self->list.first;
  160.     if (p)
  161.     {
  162.         i = 0;
  163.         do
  164.         {
  165.             _DBG_ ("Target #%u: user file = '%s'", i, ((struct target_name_entry_t *) p)->name);
  166.             p = (struct target_name_entry_t *) ((struct target_name_entry_t *) p)->list_entry.next;
  167.             i++;
  168.         }
  169.         while (p);
  170.     }
  171.     else
  172.         _DBG ("No target names.");
  173. }
  174. #endif  // DEBUG == 1
  175.  
  176. void
  177.     target_names_free
  178.     (
  179.         struct target_names_t *self
  180.     )
  181. {
  182.     struct target_name_entry_t *p, *n;
  183.  
  184.     p = (struct target_name_entry_t *) self->list.first;
  185.     while (p)
  186.     {
  187.         n = (struct target_name_entry_t *) p->list_entry.next;
  188.         target_name_entry_free (p);
  189.         free (p);
  190.         p = n;
  191.     }
  192.     target_names_clear (self);
  193. }
  194.