1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #if 0 38 #ifndef lint 39 static const char copyright[] = 40 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42 #endif /* not lint */ 43 44 #endif 45 #include <sys/cdefs.h> 46 #include <sys/param.h> 47 #include <sys/stat.h> 48 #include <sys/mount.h> 49 #include <sys/sysctl.h> 50 #include <getopt.h> 51 #include <libutil.h> 52 #include <locale.h> 53 #include <stdint.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <sysexits.h> 58 #include <unistd.h> 59 #include <libxo/xo.h> 60 61 #define UNITS_SI 1 62 #define UNITS_2 2 63 64 /* Maximum widths of various fields. */ 65 struct maxwidths { 66 int mntfrom; 67 int fstype; 68 int total; 69 int used; 70 int avail; 71 int iused; 72 int ifree; 73 }; 74 75 static void addstat(struct statfs *, struct statfs *); 76 static char *getmntpt(const char *); 77 static const char **makevfslist(char *fslist, int *skip); 78 static int checkvfsname(const char *vfsname, const char **vfslist, int skip); 79 static int checkvfsselected(char *); 80 static int int64width(int64_t); 81 static char *makenetvfslist(void); 82 static void prthuman(const struct statfs *, int64_t); 83 static void prthumanval(const char *, int64_t); 84 static intmax_t fsbtoblk(int64_t, uint64_t, u_long); 85 static void prtstat(struct statfs *, struct maxwidths *); 86 static size_t regetmntinfo(struct statfs **, long); 87 static void update_maxwidths(struct maxwidths *, const struct statfs *); 88 static void usage(void); 89 90 static __inline int 91 imax(int a, int b) 92 { 93 return (a > b ? a : b); 94 } 95 96 static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag; 97 static int thousands; 98 static int skipvfs_l, skipvfs_t; 99 static const char **vfslist_l, **vfslist_t; 100 101 static const struct option long_options[] = 102 { 103 { "si", no_argument, NULL, 'H' }, 104 { NULL, no_argument, NULL, 0 }, 105 }; 106 107 int 108 main(int argc, char *argv[]) 109 { 110 struct stat stbuf; 111 struct statfs statfsbuf, totalbuf; 112 struct maxwidths maxwidths; 113 struct statfs *mntbuf; 114 char *mntpt; 115 int i, mntsize; 116 int ch, rv; 117 118 (void)setlocale(LC_ALL, ""); 119 memset(&maxwidths, 0, sizeof(maxwidths)); 120 memset(&totalbuf, 0, sizeof(totalbuf)); 121 totalbuf.f_bsize = DEV_BSIZE; 122 strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN); 123 124 argc = xo_parse_args(argc, argv); 125 if (argc < 0) 126 exit(1); 127 128 while ((ch = getopt_long(argc, argv, "+abcgHhiklmnPt:T,", long_options, 129 NULL)) != -1) 130 switch (ch) { 131 case 'a': 132 aflag = 1; 133 break; 134 case 'b': 135 /* FALLTHROUGH */ 136 case 'P': 137 /* 138 * POSIX specifically discusses the behavior of 139 * both -k and -P. It states that the blocksize should 140 * be set to 1024. Thus, if this occurs, simply break 141 * rather than clobbering the old blocksize. 142 */ 143 if (kflag) 144 break; 145 setenv("BLOCKSIZE", "512", 1); 146 hflag = 0; 147 break; 148 case 'c': 149 cflag = 1; 150 break; 151 case 'g': 152 setenv("BLOCKSIZE", "1g", 1); 153 hflag = 0; 154 break; 155 case 'H': 156 hflag = UNITS_SI; 157 break; 158 case 'h': 159 hflag = UNITS_2; 160 break; 161 case 'i': 162 iflag = 1; 163 break; 164 case 'k': 165 kflag++; 166 setenv("BLOCKSIZE", "1024", 1); 167 hflag = 0; 168 break; 169 case 'l': 170 /* Ignore duplicate -l */ 171 if (lflag) 172 break; 173 vfslist_l = makevfslist(makenetvfslist(), &skipvfs_l); 174 lflag = 1; 175 break; 176 case 'm': 177 setenv("BLOCKSIZE", "1m", 1); 178 hflag = 0; 179 break; 180 case 'n': 181 nflag = 1; 182 break; 183 case 't': 184 if (vfslist_t != NULL) 185 xo_errx(1, "only one -t option may be specified"); 186 vfslist_t = makevfslist(optarg, &skipvfs_t); 187 break; 188 case 'T': 189 Tflag = 1; 190 break; 191 case ',': 192 thousands = 1; 193 break; 194 case '?': 195 default: 196 usage(); 197 } 198 argc -= optind; 199 argv += optind; 200 201 rv = EXIT_SUCCESS; 202 if (!*argv) { 203 /* everything (modulo -t) */ 204 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 205 mntsize = regetmntinfo(&mntbuf, mntsize); 206 } else { 207 /* just the filesystems specified on the command line */ 208 mntbuf = malloc(argc * sizeof(*mntbuf)); 209 if (mntbuf == NULL) 210 xo_err(1, "malloc()"); 211 mntsize = 0; 212 /* continued in for loop below */ 213 } 214 215 xo_open_container("storage-system-information"); 216 xo_open_list("filesystem"); 217 218 /* iterate through specified filesystems */ 219 for (; *argv; argv++) { 220 if (stat(*argv, &stbuf) < 0) { 221 if ((mntpt = getmntpt(*argv)) == NULL) { 222 xo_warn("%s", *argv); 223 rv = EXIT_FAILURE; 224 continue; 225 } 226 } else if (S_ISCHR(stbuf.st_mode)) { 227 mntpt = getmntpt(*argv); 228 if (mntpt == NULL) { 229 xo_warnx("%s: not mounted", *argv); 230 rv = EXIT_FAILURE; 231 continue; 232 } 233 } else { 234 mntpt = *argv; 235 } 236 237 /* 238 * Statfs does not take a `wait' flag, so we cannot 239 * implement nflag here. 240 */ 241 if (statfs(mntpt, &statfsbuf) < 0) { 242 xo_warn("%s", mntpt); 243 rv = EXIT_FAILURE; 244 continue; 245 } 246 247 /* 248 * Check to make sure the arguments we've been given are 249 * satisfied. Return an error if we have been asked to 250 * list a mount point that does not match the other args 251 * we've been given (-l, -t, etc.). 252 */ 253 if (checkvfsselected(statfsbuf.f_fstypename) != 0) { 254 rv = EXIT_FAILURE; 255 continue; 256 } 257 258 /* the user asked for it, so ignore the ignore flag */ 259 statfsbuf.f_flags &= ~MNT_IGNORE; 260 261 /* add to list */ 262 mntbuf[mntsize++] = statfsbuf; 263 } 264 265 memset(&maxwidths, 0, sizeof(maxwidths)); 266 for (i = 0; i < mntsize; i++) { 267 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) { 268 update_maxwidths(&maxwidths, &mntbuf[i]); 269 if (cflag) 270 addstat(&totalbuf, &mntbuf[i]); 271 } 272 } 273 for (i = 0; i < mntsize; i++) 274 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) 275 prtstat(&mntbuf[i], &maxwidths); 276 277 xo_close_list("filesystem"); 278 279 if (cflag) 280 prtstat(&totalbuf, &maxwidths); 281 282 xo_close_container("storage-system-information"); 283 if (xo_finish() < 0) 284 rv = EXIT_FAILURE; 285 exit(rv); 286 } 287 288 static char * 289 getmntpt(const char *name) 290 { 291 size_t mntsize, i; 292 struct statfs *mntbuf; 293 294 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 295 for (i = 0; i < mntsize; i++) { 296 if (!strcmp(mntbuf[i].f_mntfromname, name)) 297 return (mntbuf[i].f_mntonname); 298 } 299 return (NULL); 300 } 301 302 static const char ** 303 makevfslist(char *fslist, int *skip) 304 { 305 const char **av; 306 int i; 307 char *nextcp; 308 309 if (fslist == NULL) 310 return (NULL); 311 *skip = 0; 312 if (fslist[0] == 'n' && fslist[1] == 'o') { 313 fslist += 2; 314 *skip = 1; 315 } 316 for (i = 0, nextcp = fslist; *nextcp; nextcp++) 317 if (*nextcp == ',') 318 i++; 319 if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) { 320 xo_warnx("malloc failed"); 321 return (NULL); 322 } 323 nextcp = fslist; 324 i = 0; 325 av[i++] = nextcp; 326 while ((nextcp = strchr(nextcp, ',')) != NULL) { 327 *nextcp++ = '\0'; 328 av[i++] = nextcp; 329 } 330 av[i++] = NULL; 331 return (av); 332 } 333 334 static int 335 checkvfsname(const char *vfsname, const char **vfslist, int skip) 336 { 337 338 if (vfslist == NULL) 339 return (0); 340 while (*vfslist != NULL) { 341 if (strcmp(vfsname, *vfslist) == 0) 342 return (skip); 343 ++vfslist; 344 } 345 return (!skip); 346 } 347 348 /* 349 * Without -l and -t option, all file system types are enabled. 350 * The -l option selects the local file systems, if present. 351 * A -t option modifies the selection by adding or removing further 352 * file system types, based on the argument that is passed. 353 */ 354 static int 355 checkvfsselected(char *fstypename) 356 { 357 int result; 358 359 if (vfslist_t) { 360 /* if -t option used then select passed types */ 361 result = checkvfsname(fstypename, vfslist_t, skipvfs_t); 362 if (vfslist_l) { 363 /* if -l option then adjust selection */ 364 if (checkvfsname(fstypename, vfslist_l, skipvfs_l) == skipvfs_t) 365 result = skipvfs_t; 366 } 367 } else { 368 /* no -t option then -l decides */ 369 result = checkvfsname(fstypename, vfslist_l, skipvfs_l); 370 } 371 return (result); 372 } 373 374 /* 375 * Make a pass over the file system info in ``mntbuf'' filtering out 376 * file system types not in vfslist_{l,t} and possibly re-stating to get 377 * current (not cached) info. Returns the new count of valid statfs bufs. 378 */ 379 static size_t 380 regetmntinfo(struct statfs **mntbufp, long mntsize) 381 { 382 int error, i, j; 383 struct statfs *mntbuf; 384 385 if (vfslist_l == NULL && vfslist_t == NULL) 386 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT)); 387 388 mntbuf = *mntbufp; 389 for (j = 0, i = 0; i < mntsize; i++) { 390 if (checkvfsselected(mntbuf[i].f_fstypename) != 0) 391 continue; 392 /* 393 * XXX statfs(2) can fail for various reasons. It may be 394 * possible that the user does not have access to the 395 * pathname, if this happens, we will fall back on 396 * "stale" filesystem statistics. 397 */ 398 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]); 399 if (nflag || error < 0) 400 if (i != j) { 401 if (error < 0) 402 xo_warnx("%s stats possibly stale", 403 mntbuf[i].f_mntonname); 404 mntbuf[j] = mntbuf[i]; 405 } 406 j++; 407 } 408 return (j); 409 } 410 411 static void 412 prthuman(const struct statfs *sfsp, int64_t used) 413 { 414 415 prthumanval(" {:blocks/%6s}", sfsp->f_blocks * sfsp->f_bsize); 416 prthumanval(" {:used/%6s}", used * sfsp->f_bsize); 417 prthumanval(" {:available/%6s}", sfsp->f_bavail * sfsp->f_bsize); 418 } 419 420 static void 421 prthumanval(const char *fmt, int64_t bytes) 422 { 423 char buf[6]; 424 int flags; 425 426 flags = HN_B | HN_NOSPACE | HN_DECIMAL; 427 if (hflag == UNITS_SI) 428 flags |= HN_DIVISOR_1000; 429 430 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 431 bytes, "", HN_AUTOSCALE, flags); 432 433 xo_attr("value", "%lld", (long long) bytes); 434 xo_emit(fmt, buf); 435 } 436 437 /* 438 * Print an inode count in "human-readable" format. 439 */ 440 static void 441 prthumanvalinode(const char *fmt, int64_t bytes) 442 { 443 char buf[6]; 444 int flags; 445 446 flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000; 447 448 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 449 bytes, "", HN_AUTOSCALE, flags); 450 451 xo_attr("value", "%lld", (long long) bytes); 452 xo_emit(fmt, buf); 453 } 454 455 /* 456 * Convert statfs returned file system size into BLOCKSIZE units. 457 */ 458 static intmax_t 459 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs) 460 { 461 return (num * (intmax_t) fsbs / (int64_t) bs); 462 } 463 464 /* 465 * Print out status about a file system. 466 */ 467 static void 468 prtstat(struct statfs *sfsp, struct maxwidths *mwp) 469 { 470 static long blocksize; 471 static int headerlen, timesthrough = 0; 472 static const char *header; 473 int64_t used, availblks, inodes; 474 const char *format; 475 476 if (++timesthrough == 1) { 477 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem")); 478 mwp->fstype = imax(mwp->fstype, (int)strlen("Type")); 479 if (thousands) { /* make space for commas */ 480 mwp->total += (mwp->total - 1) / 3; 481 mwp->used += (mwp->used - 1) / 3; 482 mwp->avail += (mwp->avail - 1) / 3; 483 mwp->iused += (mwp->iused - 1) / 3; 484 mwp->ifree += (mwp->ifree - 1) / 3; 485 } 486 if (hflag) { 487 header = " Size"; 488 mwp->total = mwp->used = mwp->avail = 489 (int)strlen(header); 490 } else { 491 header = getbsize(&headerlen, &blocksize); 492 mwp->total = imax(mwp->total, headerlen); 493 } 494 mwp->used = imax(mwp->used, (int)strlen("Used")); 495 mwp->avail = imax(mwp->avail, (int)strlen("Avail")); 496 497 xo_emit("{T:/%-*s}", mwp->mntfrom, "Filesystem"); 498 if (Tflag) 499 xo_emit(" {T:/%-*s}", mwp->fstype, "Type"); 500 xo_emit(" {T:/%*s} {T:/%*s} {T:/%*s} {T:Capacity}", 501 mwp->total, header, 502 mwp->used, "Used", mwp->avail, "Avail"); 503 if (iflag) { 504 mwp->iused = imax(hflag ? 0 : mwp->iused, 505 (int)strlen(" iused")); 506 mwp->ifree = imax(hflag ? 0 : mwp->ifree, 507 (int)strlen("ifree")); 508 xo_emit(" {T:/%*s} {T:/%*s} {T:\%iused}", 509 mwp->iused - 2, "iused", mwp->ifree, "ifree"); 510 } 511 xo_emit(" {T:Mounted on}\n"); 512 } 513 514 xo_open_instance("filesystem"); 515 /* Check for 0 block size. Can this happen? */ 516 if (sfsp->f_bsize == 0) { 517 xo_warnx ("File system %s does not have a block size, assuming 512.", 518 sfsp->f_mntonname); 519 sfsp->f_bsize = 512; 520 } 521 xo_emit("{tk:name/%-*s}", mwp->mntfrom, sfsp->f_mntfromname); 522 if (Tflag) 523 xo_emit(" {:type/%-*s}", mwp->fstype, sfsp->f_fstypename); 524 used = sfsp->f_blocks - sfsp->f_bfree; 525 availblks = sfsp->f_bavail + used; 526 if (hflag) { 527 prthuman(sfsp, used); 528 } else { 529 if (thousands) 530 format = " {t:total-blocks/%*j'd} {t:used-blocks/%*j'd} " 531 "{t:available-blocks/%*j'd}"; 532 else 533 format = " {t:total-blocks/%*jd} {t:used-blocks/%*jd} " 534 "{t:available-blocks/%*jd}"; 535 xo_emit(format, 536 mwp->total, fsbtoblk(sfsp->f_blocks, 537 sfsp->f_bsize, blocksize), 538 mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize), 539 mwp->avail, fsbtoblk(sfsp->f_bavail, 540 sfsp->f_bsize, blocksize)); 541 } 542 xo_emit(" {:used-percent/%5.0f}{U:%%}", 543 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0); 544 if (iflag) { 545 inodes = sfsp->f_files; 546 used = inodes - sfsp->f_ffree; 547 if (hflag) { 548 xo_emit(" "); 549 prthumanvalinode(" {:inodes-used/%5s}", used); 550 prthumanvalinode(" {:inodes-free/%5s}", sfsp->f_ffree); 551 } else { 552 if (thousands) 553 format = " {:inodes-used/%*j'd} {:inodes-free/%*j'd}"; 554 else 555 format = " {:inodes-used/%*jd} {:inodes-free/%*jd}"; 556 xo_emit(format, mwp->iused, (intmax_t)used, 557 mwp->ifree, (intmax_t)sfsp->f_ffree); 558 } 559 if (inodes == 0) 560 xo_emit(" {:inodes-used-percent/ -}{U:} "); 561 else { 562 xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ", 563 (double)used / (double)inodes * 100.0); 564 } 565 } else 566 xo_emit(" "); 567 if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0) 568 xo_emit(" {:mounted-on}", sfsp->f_mntonname); 569 xo_emit("\n"); 570 xo_close_instance("filesystem"); 571 } 572 573 static void 574 addstat(struct statfs *totalfsp, struct statfs *statfsp) 575 { 576 uint64_t bsize; 577 578 bsize = statfsp->f_bsize / totalfsp->f_bsize; 579 totalfsp->f_blocks += statfsp->f_blocks * bsize; 580 totalfsp->f_bfree += statfsp->f_bfree * bsize; 581 totalfsp->f_bavail += statfsp->f_bavail * bsize; 582 totalfsp->f_files += statfsp->f_files; 583 totalfsp->f_ffree += statfsp->f_ffree; 584 } 585 586 /* 587 * Update the maximum field-width information in `mwp' based on 588 * the file system specified by `sfsp'. 589 */ 590 static void 591 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp) 592 { 593 static long blocksize = 0; 594 int dummy; 595 596 if (blocksize == 0) 597 getbsize(&dummy, &blocksize); 598 599 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname)); 600 mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename)); 601 mwp->total = imax(mwp->total, int64width( 602 fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize))); 603 mwp->used = imax(mwp->used, 604 int64width(fsbtoblk((int64_t)sfsp->f_blocks - 605 (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize))); 606 mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail, 607 sfsp->f_bsize, blocksize))); 608 mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files - 609 sfsp->f_ffree)); 610 mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree)); 611 } 612 613 /* Return the width in characters of the specified value. */ 614 static int 615 int64width(int64_t val) 616 { 617 int len; 618 619 len = 0; 620 /* Negative or zero values require one extra digit. */ 621 if (val <= 0) { 622 val = -val; 623 len++; 624 } 625 while (val > 0) { 626 len++; 627 val /= 10; 628 } 629 630 return (len); 631 } 632 633 static void 634 usage(void) 635 { 636 637 xo_error( 638 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n" 639 " [file | filesystem ...]\n"); 640 exit(EX_USAGE); 641 } 642 643 static char * 644 makenetvfslist(void) 645 { 646 char *str, *strptr, **listptr; 647 struct xvfsconf *xvfsp, *keep_xvfsp; 648 size_t buflen; 649 int cnt, i, maxvfsconf; 650 651 if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) { 652 xo_warn("sysctl(vfs.conflist)"); 653 return (NULL); 654 } 655 xvfsp = malloc(buflen); 656 if (xvfsp == NULL) { 657 xo_warnx("malloc failed"); 658 return (NULL); 659 } 660 keep_xvfsp = xvfsp; 661 if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) { 662 xo_warn("sysctl(vfs.conflist)"); 663 free(keep_xvfsp); 664 return (NULL); 665 } 666 maxvfsconf = buflen / sizeof(struct xvfsconf); 667 668 if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) { 669 xo_warnx("malloc failed"); 670 free(keep_xvfsp); 671 return (NULL); 672 } 673 674 for (cnt = 0, i = 0; i < maxvfsconf; i++) { 675 if (xvfsp->vfc_flags & VFCF_NETWORK) { 676 listptr[cnt++] = strdup(xvfsp->vfc_name); 677 if (listptr[cnt-1] == NULL) { 678 xo_warnx("malloc failed"); 679 free(listptr); 680 free(keep_xvfsp); 681 return (NULL); 682 } 683 } 684 xvfsp++; 685 } 686 687 if (cnt == 0 || 688 (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) { 689 if (cnt > 0) 690 xo_warnx("malloc failed"); 691 free(listptr); 692 free(keep_xvfsp); 693 return (NULL); 694 } 695 696 *str = 'n'; *(str + 1) = 'o'; 697 for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) { 698 strlcpy(strptr, listptr[i], 32); 699 strptr += strlen(listptr[i]); 700 *strptr = ','; 701 free(listptr[i]); 702 } 703 *(--strptr) = '\0'; 704 705 free(keep_xvfsp); 706 free(listptr); 707 return (str); 708 } 709