1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 1996, by Sun Microsystems, Inc. 24 * All rights reserved. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * m_pathcat: mks specific library routine. 31 * 32 * Copyright 1992 by Mortice Kern Systems Inc. All rights reserved. 33 * 34 */ 35 36 #ifdef M_RCSID 37 #ifndef lint 38 static char rcsID[] = "$Header: /rd/src/libc/mks/rcs/m_pathca.c 1.11 1995/04/12 14:14:19 miked Exp $"; 39 #endif 40 #endif 41 42 #include <mks.h> 43 #include <errno.h> 44 #include <limits.h> 45 #include <stdlib.h> 46 #include <string.h> 47 48 /*f 49 * Concatenate a directory and filename, inserting / if necessary. 50 * Return dynamically allocated pointer to result string. 51 * On error: return NULL and set errno. 52 * 53 * Errors can occur if: 54 * dir == NULL 55 * file == NULL 56 * dir is an invalid pathname 57 * strlen(dir) + strlen(file) +2 > maximum allowable pathlength 58 * 59 * note: if PATH_MAX has to be retrieved by m_pathmax(dir), 60 * we may return error if the dir name has problems, 61 * which the caller had best expect. 62 */ 63 char * 64 m_pathcat(dir, file) 65 const char *dir; 66 const char *file; 67 { 68 char *dest; 69 int dir_len; 70 int file_len; 71 int m; 72 int l; 73 74 if ((dir == NULL) || (file == NULL)) 75 goto err; 76 77 #ifdef PATH_MAX 78 l = PATH_MAX; 79 #else 80 l = m_pathmax(dir); 81 if (l < 0) 82 return (NULL); /* Errno is set by m_pathmax() */ 83 #endif 84 85 dir_len = strlen(dir); 86 /* 87 * add one for seperator eg. '/' 88 */ 89 m = dir_len+1; 90 91 file_len = strlen(file); 92 if (file_len > 0) { 93 /* 94 * add one null termination char 95 */ 96 m += file_len+1; 97 } 98 99 if (m > l) { 100 err: 101 #ifdef ENAMETOOLONG 102 errno = ENAMETOOLONG; 103 #else 104 /* 105 * we need to return an errno. So pick EINVAL. 106 * This should be common on all systems. 107 */ 108 errno = EINVAL; 109 #endif 110 return (NULL); 111 } 112 113 /* 114 * use m_malloc() - this guarantees a valid errno return on error 115 */ 116 if ((dest = m_malloc(m)) == NULL) { 117 return (NULL); 118 } 119 120 strcpy(dest, dir); 121 if (file_len > 0) { 122 if (dir_len > 0 123 && !M_FSDELIM(dest[dir_len-1]) 124 && !M_DRDELIM(dest[dir_len-1])) { 125 dest[dir_len++] = '/'; 126 } 127 strcpy(dest+dir_len, file); 128 } 129 return (dest); 130 } 131