msousa@351: /*
msousa@351:  *  matiec - a compiler for the programming languages defined in IEC 61131-3
msousa@351:  *
msousa@351:  *  Copyright (C) 2011  Mario de Sousa (msousa@fe.up.pt)
msousa@351:  *
msousa@351:  *  This program is free software: you can redistribute it and/or modify
msousa@351:  *  it under the terms of the GNU General Public License as published by
msousa@351:  *  the Free Software Foundation, either version 3 of the License, or
msousa@351:  *  (at your option) any later version.
msousa@351:  *
msousa@351:  *  This program is distributed in the hope that it will be useful,
msousa@351:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
msousa@351:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
msousa@351:  *  GNU General Public License for more details.
msousa@351:  *
msousa@351:  *  You should have received a copy of the GNU General Public License
msousa@351:  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
msousa@351:  *
msousa@351:  *
msousa@351:  * This code is made available on the understanding that it will not be
msousa@351:  * used in safety-critical situations without a full and competent review.
msousa@351:  */
msousa@351: 
msousa@351: 
msousa@351: /*******************************************/
msousa@351: /* String Duplication Utility Functions... */
msousa@351: /*******************************************/
msousa@351: 
msousa@351: 
msousa@351: 
msousa@351: #ifndef STRDUP__H
msousa@351: #define STRDUP__H
msousa@351: 
msousa@351: #include <stdlib.h>   /* required for malloc() */
msousa@351: #include <string.h>   /* required for strcat() & strlen() */
msousa@351: 
msousa@351: /*
msousa@351:  * Join two strings together. Allocate space with malloc(3).
msousa@351:  */
msousa@351: static char *strdup2(const char *a, const char *b) {
msousa@351:   char *res = (char *)malloc(strlen(a) + strlen(b) + 1);
msousa@351: 
msousa@351:   if (res == NULL) return NULL;
msousa@351:   return strcat(strcpy(res, a), b);
msousa@351: }
msousa@351: 
msousa@351: /*
msousa@351:  * Join three strings together. Allocate space with malloc(3).
msousa@351:  */
msousa@351: static char *strdup3(const char *a, const char *b, const char *c) {
msousa@351:   char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1);
msousa@351: 
msousa@351:   if (res == NULL) return NULL;
msousa@351:   return strcat(strcat(strcpy(res, a), b), c);
msousa@351: }
msousa@351: 
msousa@351: 
msousa@351: #endif /* STRDUP__H */