root/opal/util/os_path.c

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

DEFINITIONS

This source file includes following definitions.
  1. opal_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) 2018      Cisco Systems, Inc.  All rights reserved.
  13  * $COPYRIGHT$
  14  *
  15  * Additional copyrights may follow
  16  *
  17  * $HEADER$
  18  */
  19 
  20 #include "opal_config.h"
  21 
  22 #include <string.h>
  23 #ifdef HAVE_UNISTD_H
  24 #include <unistd.h>
  25 #endif  /* HAVE_UNISTD_H */
  26 #ifdef HAVE_SYS_PARAM_H
  27 #include <sys/param.h>
  28 #endif  /* HAVE_SYS_PARAM_H */
  29 #include <stdlib.h>
  30 #include <stdarg.h>
  31 
  32 #include "opal/util/os_path.h"
  33 
  34 static const char *path_sep = OPAL_PATH_SEP;
  35 
  36 char *opal_os_path(int relative, ...)
  37 {
  38     va_list ap;
  39     char *element, *path;
  40     size_t num_elements, total_length;
  41 
  42     va_start(ap, relative);
  43 
  44     /* no way to protect ourselves from reading too far, so have to
  45        trust caller that they ended the list with the NULL */
  46 
  47     num_elements = 0;
  48     total_length = 0;
  49     while (NULL != (element = va_arg(ap, char*))) {
  50         num_elements++;
  51         total_length = total_length + strlen(element);
  52         if( path_sep[0] != element[0] ) total_length++;
  53     }
  54     va_end(ap);
  55 
  56     if (0 == num_elements) { /* must be looking for a simple answer */
  57         size_t len = 3;
  58         path = (char *)calloc(len, sizeof(char));
  59         if (relative) {
  60             path[0] = '.';
  61         }
  62         strncat(path, path_sep, len - 1);
  63         return(path);
  64     }
  65 
  66     /* setup path with enough room for the string terminator, the elements, and
  67        the separator between each of the elements */
  68     total_length = total_length + num_elements * strlen(path_sep) + 1;
  69     if(relative) {
  70         total_length++;
  71     }
  72 
  73     if (total_length > OPAL_PATH_MAX) {  /* path length is too long - reject it */
  74         return(NULL);
  75     }
  76 
  77     path = (char *)calloc(total_length, sizeof(char));
  78     if (NULL == path) {
  79         return(NULL);
  80     }
  81 
  82     if (relative) {
  83         path[0] = '.';
  84     }
  85 
  86     va_start(ap, relative);
  87     if( NULL != (element = va_arg(ap, char*)) ) {
  88         if (path_sep[0] != element[0]) {
  89             strncat(path, path_sep, total_length);
  90         }
  91         strcat(path, element);
  92     }
  93     while (NULL != (element=va_arg(ap, char*))) {
  94         if (path_sep[0] != element[0]) {
  95             strncat(path, path_sep, total_length);
  96         }
  97         strncat(path, element, total_length);
  98     }
  99 
 100     va_end(ap);
 101     return opal_make_filename_os_friendly(path);
 102 }

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