root/opal/mca/pmix/pmix4x/pmix/src/util/os_path.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pmix_os_path

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2005 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2015-2018 Intel, Inc.  All rights reserved.
  13  * $COPYRIGHT$
  14  *
  15  * Additional copyrights may follow
  16  *
  17  * $HEADER$
  18  */
  19 
  20 #include <src/include/pmix_config.h>
  21 
  22 
  23 #include <string.h>
  24 #ifdef HAVE_UNISTD_H
  25 #include <unistd.h>
  26 #endif  /* HAVE_UNISTD_H */
  27 #ifdef HAVE_SYS_PARAM_H
  28 #include <sys/param.h>
  29 #endif  /* HAVE_SYS_PARAM_H */
  30 #include <stdlib.h>
  31 #include <stdarg.h>
  32 
  33 #include "src/util/os_path.h"
  34 
  35 static const char *path_sep = PMIX_PATH_SEP;
  36 
  37 char *pmix_os_path(int relative, ...)
  38 {
  39     va_list ap;
  40     char *element, *path;
  41     size_t num_elements, total_length;
  42 
  43     va_start(ap, relative);
  44 
  45     /* no way to protect ourselves from reading too far, so have to
  46        trust caller that they ended the list with the NULL */
  47 
  48     num_elements = 0;
  49     total_length = 0;
  50     while (NULL != (element = va_arg(ap, char*))) {
  51         num_elements++;
  52         total_length = total_length + strlen(element);
  53         if( path_sep[0] != element[0] ) total_length++;
  54     }
  55     va_end(ap);
  56 
  57     if (0 == num_elements) { /* must be looking for a simple answer */
  58         path = (char *)malloc(3);
  59         path[0] = '\0';
  60         if (relative) {
  61             strcpy(path, ".");
  62             strcat(path, path_sep);
  63         } else {
  64             strcpy(path, path_sep);
  65         }
  66         return(path);
  67     }
  68 
  69     /* setup path with enough room for the string terminator, the elements, and
  70        the separator between each of the elements */
  71     total_length = total_length + num_elements * strlen(path_sep) + 1;
  72     if(relative) {
  73         total_length++;
  74     }
  75 
  76     if (total_length > PMIX_PATH_MAX) {  /* path length is too long - reject it */
  77         return(NULL);
  78     }
  79 
  80     path = (char *)malloc(total_length);
  81     if (NULL == path) {
  82         return(NULL);
  83     }
  84     path[0] = 0;
  85 
  86     if (relative) {
  87         strcpy(path, ".");
  88     }
  89 
  90     va_start(ap, relative);
  91     if( NULL != (element = va_arg(ap, char*)) ) {
  92         if (path_sep[0] != element[0]) {
  93             strcat(path, path_sep);
  94         }
  95         strcat(path, element);
  96     }
  97     while (NULL != (element=va_arg(ap, char*))) {
  98         if (path_sep[0] != element[0]) {
  99             strcat(path, path_sep);
 100         }
 101         strcat(path, element);
 102     }
 103 
 104     va_end(ap);
 105     return pmix_make_filename_os_friendly(path);
 106 }

/* [<][>][^][v][top][bottom][index][help] */