?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /* l_src.c - sources 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 <stdarg.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "debug.h"
  14. #include "l_list.h"
  15. #include "l_src.h"
  16.  
  17. void
  18.     source_entry_clear
  19.     (
  20.         struct source_entry_t *self
  21.     )
  22. {
  23.     list_entry_clear (&self->list_entry);
  24.     self->real = NULL;
  25.     self->base = NULL;
  26.     self->user = NULL;
  27.     self->flags = 0;
  28.     included_files_clear (&self->included);
  29. }
  30.  
  31. void
  32.     source_entry_free
  33.     (
  34.         struct source_entry_t *self
  35.     )
  36. {
  37.     list_entry_free (&self->list_entry);
  38.     if (self->real)
  39.         free (self->real);
  40.     if (self->base)
  41.         free (self->base);
  42.     if (self->user)
  43.         free (self->user);
  44.     included_files_free (&self->included);
  45.     source_entry_clear (self);
  46. }
  47.  
  48. void
  49.     sources_clear
  50.     (
  51.         struct sources_t *self
  52.     )
  53. {
  54.     list_clear (&self->list);
  55. }
  56.  
  57. bool
  58.     sources_add
  59.     (
  60.         struct sources_t *self,
  61.         const char *real,
  62.         const char *base,
  63.         const char *user,
  64.         unsigned flags,
  65.         struct source_entry_t **result
  66.     )
  67. {
  68.     bool ok;
  69.     struct source_entry_t *p;
  70.     char *p_real, *p_base, *p_user;
  71. #if DEBUG == 1
  72.     unsigned i;
  73. #endif  // DEBUG == 1
  74.  
  75.     ok = false;
  76.     p = (struct source_entry_t *) NULL;
  77.     p_real = (char *) NULL;
  78.     p_base = (char *) NULL;
  79.     p_user = (char *) NULL;
  80.  
  81.     if (!self || !real || !base || !user)
  82.     {
  83.         _DBG ("Bad arguments.");
  84.         goto _local_exit;
  85.     }
  86.  
  87.     p = malloc (sizeof (struct source_entry_t));
  88.     if (!p)
  89.     {
  90.         _perror ("malloc");
  91.         goto _local_exit;
  92.     }
  93.     p_real = strdup (real);
  94.     if (!p_real)
  95.     {
  96.         _perror ("strdup");
  97.         goto _local_exit;
  98.     }
  99.     p_base = strdup (base);
  100.     if (!p_base)
  101.     {
  102.         _perror ("strdup");
  103.         goto _local_exit;
  104.     }
  105.     p_user = strdup (user);
  106.     if (!p_user)
  107.     {
  108.         _perror ("strdup");
  109.         goto _local_exit;
  110.     }
  111.  
  112.     source_entry_clear (p);
  113.     p->real = p_real;
  114.     p->base = p_base;
  115.     p->user = p_user;
  116.     p->flags = flags;
  117.  
  118. #if DEBUG == 1
  119.     i = self->list.count;
  120. #endif  // DEBUG == 1
  121.     list_add_entry ((struct list_t *) self, (struct list_entry_t *) p);
  122.  
  123.     _DBG_ ("Added new source #%u:", i);
  124.     _DBG_ ("Source #%u: flags = 0x%x", i, p->flags);
  125.     _DBG_ ("Source #%u: user file = '%s'", i, p->user);
  126.     _DBG_ ("Source #%u: base path = '%s'", i, p->base);
  127.     _DBG_ ("Source #%u: real file = '%s'", i, p->real);
  128.  
  129.     ok = true;
  130.  
  131. _local_exit:
  132.     if (!ok)
  133.     {
  134.         if (p)
  135.         {
  136.             free (p);
  137.             p = (struct source_entry_t *) NULL;
  138.         }
  139.         if (p_real)
  140.             free (p_real);
  141.         if (p_base)
  142.             free (p_base);
  143.         if (p_user)
  144.             free (p_user);
  145.     }
  146.     if (result)
  147.         *result = p;
  148.     return !ok;
  149. }
  150.  
  151. bool
  152.     sources_find_real
  153.     (
  154.         struct sources_t *self,
  155.         const char *real,
  156.         struct source_entry_t **result
  157.     )
  158. {
  159.     bool ok;
  160.     struct source_entry_t *p;
  161.     unsigned i;
  162.  
  163.     ok = false;
  164.     p = (struct source_entry_t *) NULL;
  165.  
  166.     if (!self || !real)
  167.     {
  168.         _DBG ("Bad arguments.");
  169.         goto _local_exit;
  170.     }
  171.  
  172.     p = (struct source_entry_t *) self->list.first;
  173.     i = 0;
  174.     while (p)
  175.     {
  176.         if (!strcmp (p->real, real))
  177.         {
  178.             // Success
  179.             _DBG_ ("Found user file '%s' (real file '%s') at #%u.", p->user, p->real, i);
  180.             ok = true;
  181.             goto _local_exit;
  182.         }
  183.         p = (struct source_entry_t *) p->list_entry.next;
  184.         i++;
  185.     }
  186.  
  187.     // Fail
  188.     //p = (struct source_entry_t *) NULL;
  189.     _DBG_ ("Failed to find real file '%s'.", real);
  190.  
  191. _local_exit:
  192.     if (result)
  193.         *result = p;
  194.     return !ok;
  195. }
  196.  
  197. bool
  198.     sources_find_user
  199.     (
  200.         struct sources_t *self,
  201.         const char *user,
  202.         struct source_entry_t **result
  203.     )
  204. {
  205.     bool ok;
  206.     struct source_entry_t *p;
  207.     unsigned i;
  208.  
  209.     ok = false;
  210.     p = (struct source_entry_t *) NULL;
  211.  
  212.     if (!self || !user)
  213.     {
  214.         _DBG ("Bad arguments.");
  215.         goto _local_exit;
  216.     }
  217.  
  218.     p = (struct source_entry_t *) self->list.first;
  219.     i = 0;
  220.     while (p)
  221.     {
  222.         if (!strcmp (p->user, user))
  223.         {
  224.             // Success
  225.             _DBG_ ("Found user file '%s' (real file '%s') at #%u.", p->user, p->real, i);
  226.             ok = true;
  227.             goto _local_exit;
  228.         }
  229.         p = (struct source_entry_t *) p->list_entry.next;
  230.         i++;
  231.     }
  232.  
  233.     // Fail
  234.     //p = (struct source_entry_t *) NULL;
  235.     _DBG_ ("Failed to find user file '%s'.", user);
  236.  
  237. _local_exit:
  238.     if (result)
  239.         *result = p;
  240.     return !ok;
  241. }
  242.  
  243. void
  244.     sources_free
  245.     (
  246.         struct sources_t *self
  247.     )
  248. {
  249.     struct source_entry_t *p, *n;
  250.  
  251.     p = (struct source_entry_t *) self->list.first;
  252.     while (p)
  253.     {
  254.         n = (struct source_entry_t *) p->list_entry.next;
  255.         source_entry_free (p);
  256.         free (p);
  257.         p = n;
  258.     }
  259.     sources_clear (self);
  260. }
  261.