xref: /titanic_44/usr/src/cmd/fs.d/cachefs/cachefspack/subr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include "rules.h"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate char *gettext(const char *);
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate char *
get_fname(char * fullpath)39*7c478bd9Sstevel@tonic-gate get_fname(char *fullpath)
40*7c478bd9Sstevel@tonic-gate {
41*7c478bd9Sstevel@tonic-gate 	static char buf[MAXPATHLEN];
42*7c478bd9Sstevel@tonic-gate 	char *s;
43*7c478bd9Sstevel@tonic-gate 	int len;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 	strcpy(buf, fullpath);
46*7c478bd9Sstevel@tonic-gate 	len = strlen(buf);
47*7c478bd9Sstevel@tonic-gate 	if (len == 1) {
48*7c478bd9Sstevel@tonic-gate 		return ((char *) NULL);
49*7c478bd9Sstevel@tonic-gate 	}
50*7c478bd9Sstevel@tonic-gate 	if (buf[len-1] == '/')
51*7c478bd9Sstevel@tonic-gate 		buf[len-1] = (char)0;
52*7c478bd9Sstevel@tonic-gate 	s = strrchr(buf, '/');
53*7c478bd9Sstevel@tonic-gate 	if (s != (char *)0) {
54*7c478bd9Sstevel@tonic-gate 		s++;
55*7c478bd9Sstevel@tonic-gate 		return (s);
56*7c478bd9Sstevel@tonic-gate 	}
57*7c478bd9Sstevel@tonic-gate 	return ((char *) NULL);
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate char *
get_dirname(char * fullpath)61*7c478bd9Sstevel@tonic-gate get_dirname(char *fullpath)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	static char buf[MAXPATHLEN];
64*7c478bd9Sstevel@tonic-gate 	char *s;
65*7c478bd9Sstevel@tonic-gate 	int len;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	strcpy(buf, fullpath);
68*7c478bd9Sstevel@tonic-gate 	len = strlen(buf);
69*7c478bd9Sstevel@tonic-gate 	if (len == 1)
70*7c478bd9Sstevel@tonic-gate 		return (buf);
71*7c478bd9Sstevel@tonic-gate 	if (buf[len-1] == '/')
72*7c478bd9Sstevel@tonic-gate 		buf[len-1] = '\0';
73*7c478bd9Sstevel@tonic-gate 	s = strrchr(buf, '/');
74*7c478bd9Sstevel@tonic-gate 	if (s != (char *)0) {
75*7c478bd9Sstevel@tonic-gate 		if (s != buf) {
76*7c478bd9Sstevel@tonic-gate 			*s = '\0';
77*7c478bd9Sstevel@tonic-gate 		} else {
78*7c478bd9Sstevel@tonic-gate 			s++;
79*7c478bd9Sstevel@tonic-gate 			*s = '\0';
80*7c478bd9Sstevel@tonic-gate 		}
81*7c478bd9Sstevel@tonic-gate 		return (buf);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	return ((char *) NULL);
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate FILE *
open_rulesfile()87*7c478bd9Sstevel@tonic-gate open_rulesfile()
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	int pid;
90*7c478bd9Sstevel@tonic-gate 	char rulesnam[MAXPATHLEN];
91*7c478bd9Sstevel@tonic-gate 	FILE *rfd;
92*7c478bd9Sstevel@tonic-gate 	int err;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	pid = getpid();
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate #ifdef CFS_PK_CURD
97*7c478bd9Sstevel@tonic-gate 	/*
98*7c478bd9Sstevel@tonic-gate 	 * Try to creat file in current directory
99*7c478bd9Sstevel@tonic-gate 	 */
100*7c478bd9Sstevel@tonic-gate 	sprintf(rulesnam, "./%s.%d", TMPRULES, pid);
101*7c478bd9Sstevel@tonic-gate 	rfd = fopen(rulesnam, "w");
102*7c478bd9Sstevel@tonic-gate 	if (rfd != NULL) fclose(rfd);
103*7c478bd9Sstevel@tonic-gate 	rfd = fopen(rulesnam, "r+");
104*7c478bd9Sstevel@tonic-gate 	if (rfd != NULL) {
105*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
106*7c478bd9Sstevel@tonic-gate 		printf("open_rulesfile: tmp rules file = %s\n", rulesnam);
107*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
108*7c478bd9Sstevel@tonic-gate 		goto unlink;
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate #endif /* CFS_PK_CURD */
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	/*
113*7c478bd9Sstevel@tonic-gate 	 * try to create file in /tmp directory
114*7c478bd9Sstevel@tonic-gate 	 */
115*7c478bd9Sstevel@tonic-gate 	sprintf(rulesnam, "/tmp/%s.%d", TMPRULES, pid);
116*7c478bd9Sstevel@tonic-gate 	rfd = fopen(rulesnam, "w");
117*7c478bd9Sstevel@tonic-gate 	if (rfd != NULL) fclose(rfd);
118*7c478bd9Sstevel@tonic-gate 	rfd = fopen(rulesnam, "r+");
119*7c478bd9Sstevel@tonic-gate 	if (rfd != NULL) {
120*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
121*7c478bd9Sstevel@tonic-gate 		printf("open_rulesfile: tmp rules file = %s\n", rulesnam);
122*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
123*7c478bd9Sstevel@tonic-gate 		goto unlink;
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 	perror("cachefspack: Can't open packing rules file\n");
126*7c478bd9Sstevel@tonic-gate 	exit(1);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate unlink:
129*7c478bd9Sstevel@tonic-gate #ifndef DEBUG
130*7c478bd9Sstevel@tonic-gate 	err = unlink(rulesnam);
131*7c478bd9Sstevel@tonic-gate 	if (err < 0) {
132*7c478bd9Sstevel@tonic-gate 		perror("error unlinking temporary packing rules file");
133*7c478bd9Sstevel@tonic-gate 		exit(1);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate #endif /* ! DEBUG */
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	return (rfd);
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate /*
141*7c478bd9Sstevel@tonic-gate  * mstrdup - my strdup
142*7c478bd9Sstevel@tonic-gate  *
143*7c478bd9Sstevel@tonic-gate  * This is done so there is common error processing for all strdup(s).
144*7c478bd9Sstevel@tonic-gate  */
145*7c478bd9Sstevel@tonic-gate char *
mstrdup(const char * str)146*7c478bd9Sstevel@tonic-gate mstrdup(const char *str)
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate 	char *s;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	s = strdup(str);
151*7c478bd9Sstevel@tonic-gate 	if (s == (char *)0) {
152*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("strdup failed - no space"));
153*7c478bd9Sstevel@tonic-gate 		exit(1);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 	return (s);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate /*
159*7c478bd9Sstevel@tonic-gate  * mmalloc - my malloc
160*7c478bd9Sstevel@tonic-gate  *
161*7c478bd9Sstevel@tonic-gate  * This is done so there is common error processing for all malloc(s).
162*7c478bd9Sstevel@tonic-gate  */
163*7c478bd9Sstevel@tonic-gate void *
mmalloc(size_t size)164*7c478bd9Sstevel@tonic-gate mmalloc(size_t size)
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	void *p;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	p = malloc(size);
169*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
170*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("malloc  failed - no space"));
171*7c478bd9Sstevel@tonic-gate 		exit(1);
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 	return (p);
174*7c478bd9Sstevel@tonic-gate }
175