8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / dumpadm / minfree.c
blobb93c9360d166d2cebe120af9a275f7c1acebc0dd
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 2000 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
30 #include <sys/statvfs.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <fcntl.h>
39 #include "utils.h"
41 static FILE *
42 minfree_open(const char *dir, int oflags, const char *fmode)
44 char path[MAXPATHLEN];
45 int fd;
47 (void) snprintf(path, sizeof (path), "%s/minfree", dir);
49 if ((fd = open(path, oflags, S_IRUSR | S_IWUSR)) >= 0)
50 return (fdopen(fd, fmode));
52 return (NULL);
55 int
56 minfree_read(const char *dir, unsigned long long *ullp)
58 FILE *fp = minfree_open(dir, O_RDONLY, "r");
60 if (fp != NULL) {
61 char buf[BUFSIZ];
62 int status = -1;
64 if (fgets(buf, BUFSIZ, fp) != NULL) {
65 if (valid_str2ull(buf, ullp))
66 status = 0;
67 else
68 warn(gettext("\"%s/minfree\": invalid minfree "
69 "value -- %s\n"), dir, buf);
72 (void) fclose(fp);
73 return (status);
76 return (-1);
79 int
80 minfree_write(const char *dir, unsigned long long ull)
82 FILE *fp = minfree_open(dir, O_WRONLY | O_CREAT | O_TRUNC, "w");
84 if (fp != NULL) {
85 int status = fprintf(fp, "%llu\n", ull);
86 (void) fclose(fp);
87 return (status);
90 return (-1);
93 int
94 minfree_compute(const char *dir, char *s, unsigned long long *ullp)
96 size_t len = strlen(s);
97 unsigned long long m = 1;
99 struct statvfs64 fsb;
100 int pct;
102 switch (s[len - 1]) {
103 case '%':
104 s[len - 1] = '\0';
106 if (!valid_str2int(s, &pct) || pct > 100) {
107 warn(gettext("invalid minfree %% -- %s\n"), s);
108 return (-1);
111 if (statvfs64(dir, &fsb) == -1) {
112 warn(gettext("failed to statvfs %s"), dir);
113 return (-1);
116 *ullp = fsb.f_blocks * fsb.f_frsize *
117 (u_longlong_t)pct / 100ULL / 1024ULL;
119 return (0);
121 case 'm':
122 case 'M':
123 m = 1024ULL;
124 /*FALLTHRU*/
126 case 'k':
127 case 'K':
128 s[len - 1] = '\0';
130 if (valid_str2ull(s, ullp)) {
131 *ullp *= m;
132 return (0);
135 warn(gettext("invalid minfree value -- %s\n"), s);
136 return (-1);
138 default:
139 warn(gettext("expected m, k, or %% unit after "
140 "minfree -- %s\n"), s);
141 return (-1);