"Fossies" - the Fresh Open Source Software Archive

Member "minidlna-1.3.0/options.c" (24 Nov 2020, 5438 Bytes) of package /linux/privat/minidlna-1.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "options.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.1_vs_1.3.0.

    1 /* MiniUPnP project
    2  * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
    3  * author: Ryan Wagoner
    4  *
    5  * Copyright (c) 2006, Thomas Bernard
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions are met:
   10  *     * Redistributions of source code must retain the above copyright
   11  *       notice, this list of conditions and the following disclaimer.
   12  *     * Redistributions in binary form must reproduce the above copyright
   13  *       notice, this list of conditions and the following disclaimer in the
   14  *       documentation and/or other materials provided with the distribution.
   15  *     * The name of the author may not be used to endorse or promote products
   16  *       derived from this software without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   28  * POSSIBILITY OF SUCH DAMAGE.
   29  */
   30 #include <stdio.h>
   31 #include <string.h>
   32 #include <stdlib.h>
   33 #include <ctype.h>
   34 #include "options.h"
   35 #include "utils.h"
   36 #include "upnpglobalvars.h"
   37 
   38 struct option * ary_options = NULL;
   39 int num_options = 0;
   40 
   41 static const struct {
   42     enum upnpconfigoptions id;
   43     const char * name;
   44 } optionids[] = {
   45     { UPNPIFNAME, "network_interface" },
   46     { UPNPPORT, "port" },
   47     { UPNPPRESENTATIONURL, "presentation_url" },
   48     { UPNPNOTIFY_INTERVAL, "notify_interval" },
   49     { UPNPUUID, "uuid"},
   50     { UPNPSERIAL, "serial"},
   51     { UPNPMODEL_NAME, "model_name"},
   52     { UPNPMODEL_NUMBER, "model_number"},
   53     { UPNPFRIENDLYNAME, "friendly_name"},
   54     { UPNPMEDIADIR, "media_dir"},
   55     { UPNPALBUMART_NAMES, "album_art_names"},
   56     { UPNPINOTIFY, "inotify" },
   57     { UPNPDBDIR, "db_dir" },
   58     { UPNPLOGDIR, "log_dir" },
   59     { UPNPLOGLEVEL, "log_level" },
   60     { UPNPMINISSDPDSOCKET, "minissdpdsocket"},
   61     { ENABLE_TIVO, "enable_tivo" },
   62     { ENABLE_DLNA_STRICT, "strict_dlna" },
   63     { ROOT_CONTAINER, "root_container" },
   64     { USER_ACCOUNT, "user" },
   65     { FORCE_SORT_CRITERIA, "force_sort_criteria" },
   66     { MAX_CONNECTIONS, "max_connections" },
   67     { MERGE_MEDIA_DIRS, "merge_media_dirs" },
   68     { WIDE_LINKS, "wide_links" },
   69     { TIVO_DISCOVERY, "tivo_discovery" },
   70     { ENABLE_SUBTITLES, "enable_subtitles" },
   71 };
   72 
   73 int
   74 readoptionsfile(const char * fname)
   75 {
   76     FILE *hfile = NULL;
   77     char buffer[1024];
   78     char *equals;
   79     char *name;
   80     char *value;
   81     char *t;
   82     int linenum = 0;
   83     int i;
   84     enum upnpconfigoptions id;
   85 
   86     if(!fname || *fname == '\0')
   87         return -1;
   88 
   89     memset(buffer, 0, sizeof(buffer));
   90 
   91 #ifdef DEBUG
   92     printf("Reading configuration from file %s\n", fname);
   93 #endif
   94 
   95     if(!(hfile = fopen(fname, "r")))
   96         return -1;
   97 
   98     while(fgets(buffer, sizeof(buffer), hfile))
   99     {
  100         linenum++;
  101         t = strchr(buffer, '\n'); 
  102         if(t)
  103         {
  104             *t = '\0';
  105             t--;
  106             while((t >= buffer) && isspace(*t))
  107             {
  108                 *t = '\0';
  109                 t--;
  110             }
  111         }
  112 
  113         /* skip leading whitespaces */
  114         name = buffer;
  115         while(isspace(*name))
  116             name++;
  117 
  118         /* check for comments or empty lines */
  119         if(name[0] == '#' || name[0] == '\0') continue;
  120 
  121         if(!(equals = strchr(name, '=')))
  122         {
  123             fprintf(stderr, "parsing error file %s line %d : %s\n",
  124                     fname, linenum, name);
  125             continue;
  126         }
  127 
  128         /* remove ending whitespaces */
  129         for(t=equals-1; t>name && isspace(*t); t--)
  130             *t = '\0';
  131 
  132         *equals = '\0';
  133         value = equals+1;
  134 
  135         /* skip leading whitespaces */
  136         while(isspace(*value))
  137             value++;
  138 
  139         id = UPNP_INVALID;
  140         for(i=0; i<sizeof(optionids)/sizeof(optionids[0]); i++)
  141         {
  142             /*printf("%2d %2d %s %s\n", i, optionids[i].id, name,
  143                    optionids[i].name); */
  144 
  145             if(0 == strcmp(name, optionids[i].name))
  146             {
  147                 id = optionids[i].id;
  148                 break;
  149             }
  150         }
  151 
  152         if(id == UPNP_INVALID)
  153         {
  154             if (strcmp(name, "include") == 0)
  155                 readoptionsfile(value);
  156             else
  157                 fprintf(stderr, "parsing error file %s line %d : %s=%s\n",
  158                         fname, linenum, name, value);
  159         }
  160         else
  161         {
  162             num_options++;
  163             t = realloc(ary_options, num_options * sizeof(struct option));
  164             if(!t)
  165             {
  166                 fprintf(stderr, "memory allocation error: %s=%s\n",
  167                     name, value);
  168                 num_options--;
  169                 continue;
  170             }
  171             else
  172                 ary_options = (struct option *)t;
  173 
  174             ary_options[num_options-1].id = id;
  175             strncpyt(ary_options[num_options-1].value, value, MAX_OPTION_VALUE_LEN);
  176         }
  177 
  178     }
  179     
  180     fclose(hfile);
  181     
  182     return 0;
  183 }
  184 
  185 void
  186 freeoptions(void)
  187 {
  188     struct media_dir_s *media_path, *last_path;
  189     struct album_art_name_s *art_names, *last_name;
  190     
  191     media_path = media_dirs;
  192     while (media_path)
  193     {
  194         free(media_path->path);
  195         last_path = media_path;
  196         media_path = media_path->next;
  197         free(last_path);
  198     }
  199 
  200     art_names = album_art_names;
  201     while (art_names)
  202     {
  203         free(art_names->name);
  204         last_name = art_names;
  205         art_names = art_names->next;
  206         free(last_name);
  207     }
  208 
  209     if(ary_options)
  210     {
  211         free(ary_options);
  212         ary_options = NULL;
  213         num_options = 0;
  214     }
  215 }
  216