util/strdup.hh
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 26 Apr 2024 09:27:21 +0200
changeset 1105 8128cad6a6e3
parent 351 e7d236750709
permissions -rw-r--r--
merge
351
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     1
/*
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     3
 *
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     4
 *  Copyright (C) 2011  Mario de Sousa (msousa@fe.up.pt)
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     5
 *
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     6
 *  This program is free software: you can redistribute it and/or modify
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     7
 *  it under the terms of the GNU General Public License as published by
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     8
 *  the Free Software Foundation, either version 3 of the License, or
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     9
 *  (at your option) any later version.
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    10
 *
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    11
 *  This program is distributed in the hope that it will be useful,
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    14
 *  GNU General Public License for more details.
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    15
 *
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    16
 *  You should have received a copy of the GNU General Public License
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    18
 *
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    19
 *
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    20
 * This code is made available on the understanding that it will not be
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    21
 * used in safety-critical situations without a full and competent review.
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    22
 */
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    23
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    24
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    25
/*******************************************/
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    26
/* String Duplication Utility Functions... */
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    27
/*******************************************/
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    28
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    29
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    30
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    31
#ifndef STRDUP__H
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    32
#define STRDUP__H
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    33
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    34
#include <stdlib.h>   /* required for malloc() */
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    35
#include <string.h>   /* required for strcat() & strlen() */
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    36
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    37
/*
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    38
 * Join two strings together. Allocate space with malloc(3).
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    39
 */
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    40
static char *strdup2(const char *a, const char *b) {
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    41
  char *res = (char *)malloc(strlen(a) + strlen(b) + 1);
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    42
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    43
  if (res == NULL) return NULL;
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    44
  return strcat(strcpy(res, a), b);
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    45
}
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    46
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    47
/*
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    48
 * Join three strings together. Allocate space with malloc(3).
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    49
 */
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    50
static char *strdup3(const char *a, const char *b, const char *c) {
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    51
  char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1);
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    52
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    53
  if (res == NULL) return NULL;
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    54
  return strcat(strcat(strcpy(res, a), b), c);
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    55
}
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    56
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    57
e7d236750709 Support for semantic verification of calls to standard function calls.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    58
#endif /* STRDUP__H */