1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2017 Gordon W. Ross
  14  */
  15 
  16 /*
  17  * OAM User Test program: read/write defaults
  18  */
  19 
  20 #include        <sys/types.h>
  21 #include        <stdio.h>
  22 #include        <string.h>
  23 #include        <stdlib.h>
  24 #include        <stddef.h>
  25 #include        <getopt.h>
  26 #include        <userdefs.h>
  27 
  28 extern const char *__progname;
  29 boolean_t rflag = B_FALSE;
  30 int vflag = 0;
  31 
  32 int
  33 main(int argc, char **argv)
  34 {
  35         struct userdefs *ud;
  36         int c, i, errs = 0;
  37 
  38         while ((c = getopt(argc, argv, "rv")) != EOF) {
  39                 switch (c) {
  40                 case 'r': /* role */
  41                         rflag = B_TRUE;
  42                         break;
  43                 case 'v': /* verbose */
  44                         vflag++;
  45                         break;
  46                 case '?':
  47                         (void) fprintf(stderr,
  48                             "usage: %s [-rv] [file [file...]]\n",
  49                             __progname);
  50                         break;
  51                 }
  52         }
  53 
  54         if (rflag) {
  55                 ud = _get_roledefs();
  56         } else {
  57                 ud = _get_userdefs();
  58         }
  59 
  60         if (vflag) {
  61                 (void) printf("# Defaults:\n");
  62                 if (rflag) {
  63                         (void) fwrite_roledefs(stdout, ud);
  64                 } else {
  65                         (void) fwrite_userdefs(stdout, ud);
  66                 }
  67                 (void) printf("\n");
  68         }
  69 
  70         for (i = optind; i < argc; i++) {
  71                 FILE *fp;
  72 
  73                 fp = fopen(argv[i], "r");
  74                 if (fp == NULL) {
  75                         perror(argv[i]);
  76                         errs++;
  77                 } else {
  78                         fread_defs(fp, ud, rflag);
  79                         (void) fclose(fp);
  80                 }
  81         }
  82 
  83         if (rflag) {
  84                 (void) fwrite_roledefs(stdout, ud);
  85         } else {
  86                 (void) fwrite_userdefs(stdout, ud);
  87         }
  88 
  89         return ((errs == 0) ? 0 : 1);
  90 }