xref: /titanic_50/usr/src/cmd/bnu/strsave.c (revision 7c1d35456b77b833f089b886b99aa431a24e1377)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* from SVR4 bnu:strsave.c 1.5 */
27 
28 #include "uucp.h"
29 
30 /* #include <errno.h> */
31 /* #include <malloc.h> */
32 /* #include <string.h> */
33 /* #include <sys/types.h> */
34 /* #include <sys/stat.h> */
35 
36 /* copy str into data space -- caller should report errors. */
37 
38 GLOBAL char *
39 strsave(str)
40 register char *str;
41 {
42 	register char *rval;
43 
44 	rval = (char *)malloc(strlen(str) + 1);
45 	if (rval != 0)
46 		strcpy(rval, str);
47 	return(rval);
48 }
49 
50 /*	Determine if the effective user id has the appropriate permission
51 	on a file.  Modeled after access(2).
52 	amode:
53 		00	just checks for file existence.
54 		04	checks read permission.
55 		02	checks write permission.
56 		01	checks execute/search permission.
57 		other bits are ignored quietly.
58 */
59 
60 GLOBAL int
61 eaccess( path, amode )
62 char		*path;
63 register mode_t	amode;
64 {
65 	struct stat	s;
66 	uid_t euid;
67 
68 	if( stat( path, &s ) == -1 )
69 		return(-1);		/* can't stat file */
70 	amode &= 07;
71 
72 	if( (euid = geteuid()) == 0 )
73 	    return(0);			/* root can do all */
74 	if( euid == s.st_uid )
75 	    s.st_mode >>= 6;		/* use owner bits */
76 	else if( getegid() == s.st_gid )
77 	    s.st_mode >>= 3;		/* use group bits */
78 
79 	if( (amode & s.st_mode) == amode )
80 		return(0);		/* access permitted */
81 	errno = EACCES;
82 	return(-1);
83 }
84