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