xref: /titanic_44/usr/src/cmd/fs.d/cachefs/cachefspack/subr.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
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 #include <stdio.h>
30 #include <string.h>
31 #include <sys/param.h>
32 #include <stdlib.h>
33 
34 #include "rules.h"
35 
36 char *gettext(const char *);
37 
38 char *
39 get_fname(char *fullpath)
40 {
41 	static char buf[MAXPATHLEN];
42 	char *s;
43 	int len;
44 
45 	strcpy(buf, fullpath);
46 	len = strlen(buf);
47 	if (len == 1) {
48 		return ((char *) NULL);
49 	}
50 	if (buf[len-1] == '/')
51 		buf[len-1] = (char)0;
52 	s = strrchr(buf, '/');
53 	if (s != (char *)0) {
54 		s++;
55 		return (s);
56 	}
57 	return ((char *) NULL);
58 }
59 
60 char *
61 get_dirname(char *fullpath)
62 {
63 	static char buf[MAXPATHLEN];
64 	char *s;
65 	int len;
66 
67 	strcpy(buf, fullpath);
68 	len = strlen(buf);
69 	if (len == 1)
70 		return (buf);
71 	if (buf[len-1] == '/')
72 		buf[len-1] = '\0';
73 	s = strrchr(buf, '/');
74 	if (s != (char *)0) {
75 		if (s != buf) {
76 			*s = '\0';
77 		} else {
78 			s++;
79 			*s = '\0';
80 		}
81 		return (buf);
82 	}
83 	return ((char *) NULL);
84 }
85 
86 FILE *
87 open_rulesfile()
88 {
89 	int pid;
90 	char rulesnam[MAXPATHLEN];
91 	FILE *rfd;
92 	int err;
93 
94 	pid = getpid();
95 
96 #ifdef CFS_PK_CURD
97 	/*
98 	 * Try to creat file in current directory
99 	 */
100 	sprintf(rulesnam, "./%s.%d", TMPRULES, pid);
101 	rfd = fopen(rulesnam, "w");
102 	if (rfd != NULL) fclose(rfd);
103 	rfd = fopen(rulesnam, "r+");
104 	if (rfd != NULL) {
105 #ifdef DEBUG
106 		printf("open_rulesfile: tmp rules file = %s\n", rulesnam);
107 #endif /* DEBUG */
108 		goto unlink;
109 	}
110 #endif /* CFS_PK_CURD */
111 
112 	/*
113 	 * try to create file in /tmp directory
114 	 */
115 	sprintf(rulesnam, "/tmp/%s.%d", TMPRULES, pid);
116 	rfd = fopen(rulesnam, "w");
117 	if (rfd != NULL) fclose(rfd);
118 	rfd = fopen(rulesnam, "r+");
119 	if (rfd != NULL) {
120 #ifdef DEBUG
121 		printf("open_rulesfile: tmp rules file = %s\n", rulesnam);
122 #endif /* DEBUG */
123 		goto unlink;
124 	}
125 	perror("cachefspack: Can't open packing rules file\n");
126 	exit(1);
127 
128 unlink:
129 #ifndef DEBUG
130 	err = unlink(rulesnam);
131 	if (err < 0) {
132 		perror("error unlinking temporary packing rules file");
133 		exit(1);
134 	}
135 #endif /* ! DEBUG */
136 
137 	return (rfd);
138 }
139 
140 /*
141  * mstrdup - my strdup
142  *
143  * This is done so there is common error processing for all strdup(s).
144  */
145 char *
146 mstrdup(const char *str)
147 {
148 	char *s;
149 
150 	s = strdup(str);
151 	if (s == (char *)0) {
152 		fprintf(stderr, gettext("strdup failed - no space"));
153 		exit(1);
154 	}
155 	return (s);
156 }
157 
158 /*
159  * mmalloc - my malloc
160  *
161  * This is done so there is common error processing for all malloc(s).
162  */
163 void *
164 mmalloc(size_t size)
165 {
166 	void *p;
167 
168 	p = malloc(size);
169 	if (p == NULL) {
170 		fprintf(stderr, gettext("malloc  failed - no space"));
171 		exit(1);
172 	}
173 	return (p);
174 }
175