xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/mks/m_pathma.c (revision 59d65d3175825093531e82f44269d948ed510a00)
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_pathmax: mks specific library routine.
29  *
30  * Copyright 1992 by Mortice Kern Systems Inc.  All rights reserved.
31  *
32  */
33 #ifdef M_RCSID
34 #ifndef lint
35 static char rcsID[] = "$Header: /rd/src/libc/mks/rcs/m_pathma.c 1.4 1992/06/19 17:28:24 gord Exp $";
36 #endif
37 #endif /* M_RCSID */
38 
39 #include <mks.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <limits.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #include <assert.h>
46 
47 #ifdef m_pathmax
48 #undef m_pathmax	 /* in case its #define'd in mks.h */
49 #endif
50 
51 /*f
52  * m_pathmax()
53  *  - Determine current configuration value for PC_PATH_MAX
54  *    relative to 'path'.
55  *  - If 'path' is NULL, then relative to "/"
56  *  - Return:
57  *      configuration value
58  *      or M_PATH_MAX if configuration value is indeterminate
59  *      or -1 and errno is set if there's a problem with 'path'
60  */
61 int
62 m_pathmax(path)
63 char* path;
64 {
65         int x;
66 
67         if (path == NULL)
68                 path = "/";
69 
70         errno = 0;
71         x = pathconf(path, _PC_PATH_MAX);
72         if (x == -1) {
73                 if (errno == 0) {
74 			/*
75                          * unlimited size - so we use M_PATH_MAX
76 			 *
77                          * M_PATH_MAX defined in mkslocal.h
78                          * - use a sufficiently LARGE number (e.g 1024 or 2048)
79                          */
80 			if (M_PATH_MAX < _POSIX_PATH_MAX) {
81 				printf("m_pathmax(): Assert failure: ");
82 				printf("M_PATH_MAX < _POSIX_PATH_MAX\n");
83 				(void) exit(126);
84 			}
85                         return M_PATH_MAX;
86                 } else {
87                         /* ASSUME: cannot get errno = EINVAL because PC_PATH_MAX                         *         must be supported, and must
88                          *         be associated with all *valid* 'path's
89                          *         (if 'path' is not valid, then we
90 			 *          should get ENOENT or ENOTDIR,
91 			 *          not EINVAL)
92                          */
93                         if (errno == EINVAL) {
94 				printf("m_pathmax(): Assert failure: ");
95 				printf("pathconf() = -1 and errno = EINVAL\n");
96 				(void) exit(126);
97 			}
98                         return -1;
99                 }
100         }
101 
102         return x;
103 }
104 
105 #ifdef TEST
106 /*
107  * compile with
108  *      "make m_pathma COPTS=TEST"  - using MKS make and MKS environment
109  * or
110  *       "cc -o m_pathma -DTEST m_pathma.c"   - to get sunos std libraries
111  *
112  */
113 main(argc, argv)
114 int argc;
115 char ** argv;
116 {
117     if (argc > 1) {
118 	    int x;
119 	    x = m_pathmax(argv[1]);
120 	    printf("m_pathmax('%s') returns %d\n", argv[1], x );
121 	    if (x == -1)
122 #ifdef __STDC__
123 	       printf("errno = %d (%s)\n", errno, strerror(errno));
124 #else
125 	       printf("errno = %d \n", errno);
126 #endif
127     } else
128          printf("usage: %s filename\n", argv[0]);
129 
130 }
131 #endif /* TEST */
132