"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/src/mimetypes.c" (12 Oct 2016, 5283 Bytes) of package /linux/misc/tin-2.4.1.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 "mimetypes.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.0_vs_2.4.1.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : mimetypes.c
    4  *  Author    : J. Faultless
    5  *  Created   : 2000-03-31
    6  *  Updated   : 2007-12-30
    7  *  Notes     : mime.types handling
    8  *
    9  * Copyright (c) 2000-2017 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     return;
  123 }
  124 
  125 
  126 /*
  127  * look for a filename extension in file for the specified
  128  * type ("major/minor"), the result is stored in extension
  129  */
  130 static t_bool
  131 _lookup_extension(
  132     char *extension,
  133     size_t ext_len,
  134     const char *file,
  135     const char *type)
  136 {
  137     FILE *fp;
  138     char *p;
  139     char buf[8192];
  140 
  141     if ((fp = fopen(file, "r")) == NULL)
  142         return FALSE;
  143     while ((fgets(buf, sizeof(buf), fp)) != NULL) {
  144         if (buf[0] == '#' || buf[0] == '\n' || strncmp(buf, type, strlen(type)))
  145             continue;
  146         if ((p = strtok(buf, " \t\n"))) {
  147             if (strlen(p) != strlen(type))
  148                 continue;
  149             if ((p = strtok(NULL, " \t\n")) != NULL) {
  150                 my_strncpy(extension, p, ext_len - 1);
  151                 fclose(fp);
  152                 return TRUE;
  153             }
  154         }
  155     }
  156     fclose(fp);
  157     return FALSE;
  158 }
  159 
  160 
  161 /*
  162  * look for a filename extension for the specified
  163  * major minor, the result is stored in extension
  164  * files checked are:
  165  *  $HOME/.mime.types
  166  *  /etc/mime.types
  167  *  TIN_DEFAULTS_DIR/mime.types
  168  */
  169 t_bool
  170 lookup_extension(
  171     char *extension,
  172     size_t ext_len,
  173     const char *major,
  174     const char *minor)
  175 {
  176     char *type;
  177     char buf[PATH_LEN];
  178 
  179     if (!major || !minor) {
  180         *extension = '\0';
  181         return FALSE;
  182     }
  183 
  184     type = my_malloc(strlen(major) + 1 + strlen(minor) + 1);
  185     strcpy(type, major);
  186     strcat(type, "/");
  187     strcat(type, minor);
  188 
  189     joinpath(buf, sizeof(buf), homedir, ".mime.types");
  190     if (_lookup_extension(extension, ext_len, buf, type)) {
  191         free(type);
  192         return TRUE;
  193     }
  194 
  195 #ifdef M_UNIX
  196     if (_lookup_extension(extension, ext_len, "/etc/mime.types", type)) {
  197         free(type);
  198         return TRUE;
  199     }
  200 #endif /* M_UNIX */
  201 
  202 #ifdef TIN_DEFAULTS_DIR
  203     joinpath(buf, sizeof(buf), TIN_DEFAULTS_DIR, "mime.types");
  204     if (_lookup_extension(extension, ext_len, buf, type)) {
  205         free(type);
  206         return TRUE;
  207     }
  208 #endif /* TIN_DEFAULTS_DIR */
  209     free(type);
  210     my_strncpy(extension, minor, ext_len - 1);
  211     return FALSE;
  212 }