"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.2/src/mimetypes.c" (8 Dec 2017, 5273 Bytes) of package /linux/misc/tin-2.4.2.tar.xz:


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 "mimetypes.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.1_vs_2.4.2.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : mimetypes.c
    4  *  Author    : J. Faultless
    5  *  Created   : 2000-03-31
    6  *  Updated   : 2017-03-28
    7  *  Notes     : mime.types handling
    8  *
    9  * Copyright (c) 2000-2018 Jason Faultless <jason@altarstone.com>
   10  * All rights reserved.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. The name of the author may not be used to endorse or promote
   21  *    products derived from this software without specific prior written
   22  *    permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
   25  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   28  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   30  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   35  */
   36 
   37 #ifndef TIN_H
   38 #   include "tin.h"
   39 #endif /* !TIN_H */
   40 
   41 
   42 /*
   43  * local prototypes
   44  */
   45 static t_bool _lookup_mimetype(const char *file, const char *ext, t_part *part);
   46 static t_bool _lookup_extension(char *extension, size_t ext_len, const char *file, const char *type);
   47 
   48 
   49 /*
   50  * Match a filename extension to a content-type / subtype pair in mime.types
   51  * Update the passed-in attachment structure with type/subtype if found
   52  * Return TRUE if found
   53  */
   54 static t_bool
   55 _lookup_mimetype(
   56     const char *file,
   57     const char *ext,
   58     t_part *part)
   59 {
   60     FILE *fp;
   61     char *exts;
   62     char *ptr;
   63     char buf[PATH_LEN];
   64     int i;
   65 
   66     if ((fp = fopen(file, "r")) == NULL)
   67         return FALSE;
   68 
   69     while ((fgets(buf, sizeof(buf), fp)) != NULL) {
   70         if (buf[0] == '#' || buf[0] == '\n')        /* Skip comments & blank lines */
   71             continue;
   72 
   73         if (strtok(buf, " \t\n")) {
   74             while ((exts = strtok(NULL, " \t\n")) != NULL) {    /* Work through list of extensions */
   75                 if (strcasecmp(ext, exts) == 0) {
   76                     if ((i = content_type(strtok(buf, "/"))) != -1) {
   77                         if ((ptr = strtok(NULL, "\n")) != NULL) {
   78                             part->type = i;
   79                             FreeIfNeeded(part->subtype);
   80                             part->subtype = my_strdup(ptr);
   81                             fclose(fp);
   82                             return TRUE;
   83                         }
   84                     }
   85                 }
   86             }
   87         }
   88     }
   89 
   90     fclose(fp);
   91     return FALSE;
   92 }
   93 
   94 
   95 /*
   96  * Check:
   97  *  $HOME/.mime.types
   98  *  /etc/mime.types
   99  *  TIN_DEFAULTS_DIR/mime.types
  100  */
  101 void
  102 lookup_mimetype(
  103     const char *ext,
  104     t_part *part)
  105 {
  106     char buf[PATH_LEN];
  107 
  108     joinpath(buf, sizeof(buf), homedir, ".mime.types");
  109     if (_lookup_mimetype(buf, ext, part))
  110         return;
  111 
  112 #ifdef M_UNIX
  113     if (_lookup_mimetype("/etc/mime.types", ext, part))
  114         return;
  115 #endif /* M_UNIX */
  116 
  117 #ifdef TIN_DEFAULTS_DIR
  118     joinpath(buf, sizeof(buf), TIN_DEFAULTS_DIR, "mime.types");
  119     _lookup_mimetype(buf, ext, part);
  120 #endif /* TIN_DEFAULTS_DIR */
  121 }
  122 
  123 
  124 /*
  125  * look for a filename extension in file for the specified
  126  * type ("major/minor"), the result is stored in extension
  127  */
  128 static t_bool
  129 _lookup_extension(
  130     char *extension,
  131     size_t ext_len,
  132     const char *file,
  133     const char *type)
  134 {
  135     FILE *fp;
  136     char *p;
  137     char buf[8192];
  138 
  139     if ((fp = fopen(file, "r")) == NULL)
  140         return FALSE;
  141     while ((fgets(buf, sizeof(buf), fp)) != NULL) {
  142         if (buf[0] == '#' || buf[0] == '\n' || strncmp(buf, type, strlen(type)))
  143             continue;
  144         if ((p = strtok(buf, " \t\n"))) {
  145             if (strlen(p) != strlen(type))
  146                 continue;
  147             if ((p = strtok(NULL, " \t\n")) != NULL) {
  148                 my_strncpy(extension, p, ext_len - 1);
  149                 fclose(fp);
  150                 return TRUE;
  151             }
  152         }
  153     }
  154     fclose(fp);
  155     return FALSE;
  156 }
  157 
  158 
  159 /*
  160  * look for a filename extension for the specified
  161  * major minor, the result is stored in extension
  162  * files checked are:
  163  *  $HOME/.mime.types
  164  *  /etc/mime.types
  165  *  TIN_DEFAULTS_DIR/mime.types
  166  */
  167 t_bool
  168 lookup_extension(
  169     char *extension,
  170     size_t ext_len,
  171     const char *major,
  172     const char *minor)
  173 {
  174     char *type;
  175     char buf[PATH_LEN];
  176 
  177     if (!major || !minor) {
  178         *extension = '\0';
  179         return FALSE;
  180     }
  181 
  182     type = my_malloc(strlen(major) + 1 + strlen(minor) + 1);
  183     strcpy(type, major);
  184     strcat(type, "/");
  185     strcat(type, minor);
  186 
  187     joinpath(buf, sizeof(buf), homedir, ".mime.types");
  188     if (_lookup_extension(extension, ext_len, buf, type)) {
  189         free(type);
  190         return TRUE;
  191     }
  192 
  193 #ifdef M_UNIX
  194     if (_lookup_extension(extension, ext_len, "/etc/mime.types", type)) {
  195         free(type);
  196         return TRUE;
  197     }
  198 #endif /* M_UNIX */
  199 
  200 #ifdef TIN_DEFAULTS_DIR
  201     joinpath(buf, sizeof(buf), TIN_DEFAULTS_DIR, "mime.types");
  202     if (_lookup_extension(extension, ext_len, buf, type)) {
  203         free(type);
  204         return TRUE;
  205     }
  206 #endif /* TIN_DEFAULTS_DIR */
  207     free(type);
  208     my_strncpy(extension, minor, ext_len - 1);
  209     return FALSE;
  210 }