"Fossies" - the Fresh Open Source Software Archive

Member "libtimidity-0.2.7/src/common.c" (17 Mar 2021, 3665 Bytes) of package /linux/privat/libtimidity-0.2.7.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 "common.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.2.6_vs_0.2.7.

    1 /* libTiMidity is licensed under the terms of the GNU Lesser General
    2  * Public License: see COPYING for details.
    3  *
    4  * Note that the included TiMidity source, based on timidity-0.2i, was
    5  * originally licensed under the GPL, but the author extended it so it
    6  * can also be used separately under the GNU LGPL or the Perl Artistic
    7  * License: see the notice by Tuukka Toivonen as it appears on the web
    8  * at http://ieee.uwaterloo.ca/sca/www.cgs.fi/tt/timidity/ .
    9  */
   10 
   11 /*
   12  * TiMidity -- Experimental MIDI to WAVE converter
   13  * Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
   14  *
   15  * This program is free software; you can redistribute it and/or modify
   16  * it under the terms of the GNU General Public License as published by
   17  * the Free Software Foundation; either version 2 of the License, or (at
   18  * your option) any later version.
   19  *
   20  * This program is distributed in the hope that it will be useful, but
   21  * WITHOUT ANY WARRANTY; without even the implied warranty of
   22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   23  *
   24  * See the GNU General Public License for more details.
   25  *
   26  * You should have received a copy of the GNU General Public License along
   27  * with this program; if not, write to the Free Software Foundation, Inc.,
   28  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   29  */
   30 
   31 #ifdef HAVE_CONFIG_H
   32 #include "config.h"
   33 #endif
   34 
   35 #ifdef HAVE_SYS_PARAM_H
   36 #include <sys/param.h>
   37 #endif
   38 
   39 #include <stdio.h>
   40 #include <stdlib.h>
   41 #include <string.h>
   42 
   43 /* I guess "rb" should be right for any libc */
   44 #define OPEN_MODE "rb"
   45 
   46 #include "timidity_internal.h"
   47 #include "common.h"
   48 #include "ospaths.h"
   49 
   50 /* The paths in this list will be tried by timi_openfile() */
   51 struct _PathList {
   52   char *path;
   53   struct _PathList *next;
   54 };
   55 
   56 static PathList *pathlist = NULL;
   57 
   58 /* This is meant to find and open files for reading */
   59 FILE *timi_openfile(const char *name)
   60 {
   61   FILE *fp;
   62 
   63   if (!name || !(*name))
   64     {
   65       DEBUG_MSG("Attempted to open nameless file.\n");
   66       return NULL;
   67     }
   68 
   69   /* First try the given name */
   70   DEBUG_MSG("Trying to open %s\n", name);
   71   if ((fp = fopen(name, OPEN_MODE)) != NULL)
   72     return fp;
   73 
   74   if (!is_abspath(name))
   75   {
   76     char current_filename[TIM_MAXPATH];
   77     PathList *plp = pathlist;
   78     int l;
   79 
   80     while (plp) { /* Try along the path then */
   81     *current_filename = 0;
   82     l = strlen(plp->path);
   83     if(l)
   84       {
   85         strcpy(current_filename, plp->path);
   86         if (!is_dirsep(current_filename[l - 1]))
   87         {
   88           current_filename[l] = CHAR_DIRSEP;
   89           current_filename[l + 1] = '\0';
   90         }
   91       }
   92     strcat(current_filename, name);
   93     DEBUG_MSG("Trying to open %s\n", current_filename);
   94     if ((fp = fopen(current_filename, OPEN_MODE)) != NULL)
   95       return fp;
   96     plp = plp->next;
   97     }
   98   }
   99 
  100   /* Nothing could be opened. */
  101   DEBUG_MSG("Could not open %s\n", name);
  102   return NULL;
  103 }
  104 
  105 /* This allocates memory and clears it. */
  106 void *timi_calloc(size_t count)
  107 {
  108   void *p = timi_malloc(count);
  109   if (p == NULL) {
  110     DEBUG_MSG("malloc() failed for %lu bytes.\n", (unsigned long)count);
  111     return NULL;
  112   }
  113   memset(p, 0, count);
  114   return p;
  115 }
  116 
  117 /* This adds a directory to the path list */
  118 int timi_add_pathlist(const char *s, size_t l)
  119 {
  120   PathList *plp = (PathList *) timi_malloc(sizeof(PathList));
  121   if (!plp) return -2;
  122   plp->path = (char *) timi_malloc(l + 1);
  123   if (!plp->path) {
  124     timi_free (plp);
  125     return -2;
  126   }
  127   plp->next = pathlist;
  128   pathlist = plp;
  129   memcpy(plp->path, s, l);
  130   plp->path[l] = 0;
  131   return 0;
  132 }
  133 
  134 void timi_free_pathlist(void)
  135 {
  136     PathList *plp = pathlist;
  137     PathList *next;
  138 
  139     while (plp) {
  140     next = plp->next;
  141     timi_free(plp->path);
  142     timi_free(plp);
  143     plp = next;
  144     }
  145     pathlist = NULL;
  146 }