root/libutil/makepath.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. makepath

   1 /*
   2  * Copyright (c) 1997, 1998, 1999, 2000
   3  *      Tama Communications Corporation
   4  *
   5  * This file is part of GNU GLOBAL.
   6  *
   7  * This program is free software: you can redistribute it and/or modify
   8  * it under the terms of the GNU General Public License as published by
   9  * the Free Software Foundation, either version 3 of the License, or
  10  * (at your option) any later version.
  11  * 
  12  * This program is distributed in the hope that it will be useful,
  13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  * GNU General Public License for more details.
  16  * 
  17  * You should have received a copy of the GNU General Public License
  18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19  */
  20 
  21 #ifdef HAVE_CONFIG_H
  22 #include <config.h>
  23 #endif
  24 #ifdef HAVE_STRING_H
  25 #include <string.h>
  26 #else
  27 #include <strings.h>
  28 #endif
  29 
  30 #include "gparam.h"
  31 #include "die.h"
  32 #include "makepath.h"
  33 #include "strbuf.h"
  34 
  35 /**
  36  * @details
  37  * makepath: make path from directory and file.
  38  *
  39  *      @param[in]      dir     directory(optional), @CODE{NULL} if not used.
  40  *      @param[in]      file    file
  41  *      @param[in]      suffix  suffix(optional), @CODE{NULL} if not used.
  42  *      @return         path
  43  *
  44  * @note Only makes a name, no real directories or files are created.
  45  *
  46  * The path in argument @a dir can be an absoulute or relative one (@EMPH{Not sure if
  47  * can be relative one on WIN32}). <br>
  48  * A @CODE{'.'} (dot) will be added to the @a suffix (file extension) argument, 
  49  * if not already present. Any existing suffix within @a file will not be removed. <br>
  50  * A separator (@CODE{'/'} or @CODE{'\\'}) will be added to @a dir, if not present,
  51  * then @a file is appended to the end of @a dir. @a file and @a dir are used as
  52  * given. None of the paths given or generated are checked for validity, but they
  53  * are checked for size (#MAXPATHLEN), (calls die() if too big).
  54  *
  55  * @attention
  56  * It is necessary to note the usage of @NAME{makepath()}, because it returns
  57  * module local area. If @NAME{makepath()} is called again in the function which
  58  * is passed the return value of @NAME{makepath()}, then the value is overwritten. <br>
  59  * This may cause the bug which is not understood easily.
  60  * You must not pass the return value except for the safe functions
  61  * described below.
  62  * 
  63  * @attention
  64  * Here are safe functions.
  65  * - functions in standard C library.
  66  * - following libutil functions: <br>
  67  *   test(), dbop_open(), strlimcpy(), strbuf_puts(), die()
  68  */
  69 const char *
  70 makepath(const char *dir, const char *file, const char *suffix)
  71 {
  72         STATIC_STRBUF(sb);
  73         int length;
  74         char sep = '/';
  75 
  76         strbuf_clear(sb);
  77         if (dir != NULL) {
  78                 if ((length = strlen(dir)) > MAXPATHLEN)
  79                         die("path name too long. '%s'\n", dir);
  80 
  81 #if defined(_WIN32) || defined(__DJGPP__)
  82                 /* follows native way. */
  83                 if (dir[0] == '\\' || dir[2] == '\\')
  84                         sep = '\\';
  85 #endif
  86                 strbuf_puts(sb, dir);
  87                 strbuf_unputc(sb, sep);
  88                 strbuf_putc(sb, sep);
  89         }
  90         strbuf_puts(sb, file);
  91         if (suffix) {
  92                 if (*suffix != '.')
  93                         strbuf_putc(sb, '.');
  94                 strbuf_puts(sb, suffix);
  95         }
  96         if ((length = strlen(strbuf_value(sb))) > MAXPATHLEN)
  97                 die("path name too long. '%s'\n", strbuf_value(sb));
  98         return strbuf_value(sb);
  99 }

/* [previous][next][first][last][top][bottom][index][help] */