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 /*
28  * m_pathcat: mks specific library routine.
29  *
30  * Copyright 1992 by Mortice Kern Systems Inc.  All rights reserved.
31  *
32  */
33 
34 #ifdef M_RCSID
35 #ifndef lint
36 static char rcsID[] = "$Header: /rd/src/libc/mks/rcs/m_pathca.c 1.11 1995/04/12 14:14:19 miked Exp $";
37 #endif
38 #endif
39 
40 #include <mks.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 /*f
47  * Concatenate a directory and filename, inserting / if necessary.
48  * Return dynamically allocated pointer to result string.
49  * On error: return NULL and set errno.
50  *
51  * Errors can occur if:
52  *    dir == NULL
53  *    file == NULL
54  *    dir is an invalid pathname
55  *    strlen(dir) + strlen(file) +2 > maximum allowable pathlength
56  *
57  * note: if PATH_MAX has to be retrieved by m_pathmax(dir),
58  * we may return error if the dir name has problems,
59  * which the caller had best expect.
60  */
61 char *
m_pathcat(dir,file)62 m_pathcat(dir, file)
63 const char *dir;
64 const char *file;
65 {
66 	char *dest;
67 	int dir_len;
68 	int file_len;
69 	int m;
70 	int l;
71 
72 	if ((dir == NULL) || (file == NULL))
73 		goto err;
74 
75 #ifdef	PATH_MAX
76 	l = PATH_MAX;
77 #else
78 	l = m_pathmax(dir);
79 	if (l < 0)
80 		return (NULL);	/* Errno is set by m_pathmax() */
81 #endif
82 
83 	dir_len = strlen(dir);
84 	/*
85 	 * add one for seperator eg. '/'
86 	 */
87 	m = dir_len+1;
88 
89 	file_len = strlen(file);
90 	if (file_len > 0) {
91 		/*
92 		 * add one null termination char
93 		 */
94 		m += file_len+1;
95 	}
96 
97 	if (m > l) {
98 err:
99 #ifdef	ENAMETOOLONG
100 		errno = ENAMETOOLONG;
101 #else
102 		/*
103 		 * we need to return an errno. So pick EINVAL.
104 		 * This should be common on all systems.
105 		 */
106 		errno = EINVAL;
107 #endif
108 		return (NULL);
109 	}
110 
111 	/*
112 	 * use m_malloc() - this guarantees a valid errno return on error
113 	 */
114 	if ((dest = m_malloc(m)) == NULL) {
115 		return (NULL);
116 	}
117 
118 	strcpy(dest, dir);
119 	if (file_len > 0) {
120 		if (dir_len > 0
121 		    && !M_FSDELIM(dest[dir_len-1])
122 		    && !M_DRDELIM(dest[dir_len-1])) {
123 			dest[dir_len++] = '/';
124 		}
125 		strcpy(dest+dir_len, file);
126 	}
127 	return (dest);
128 }
129