home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / amd / fsinfo / fsi_util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-12  |  10.7 KB  |  574 lines

  1. /*
  2.  * Copyright (c) 1989 Jan-Simon Pendry
  3.  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
  4.  * Copyright (c) 1989 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *    This product includes software developed by the University of
  18.  *    California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  *
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  *
  35.  *    @(#)fsi_util.c    5.3 (Berkeley) 5/12/91
  36.  *
  37.  * $Id: fsi_util.c,v 5.2.1.3 91/05/07 22:19:05 jsp Alpha $
  38.  *
  39.  */
  40.  
  41. #include "../fsinfo/fsinfo.h"
  42.  
  43. /*
  44.  * Lots of ways of reporting errors...
  45.  */
  46. void error(s, s1, s2, s3, s4)
  47. char *s, *s1, *s2, *s3, *s4;
  48. {
  49.     col_cleanup(0);
  50.     fprintf(stderr, "%s: Error, ", progname);
  51.     fprintf(stderr, s, s1, s2, s3, s4);
  52.     fputc('\n', stderr);
  53.     errors++;
  54. }
  55.  
  56. void lerror(l, s, s1, s2, s3, s4)
  57. ioloc *l;
  58. char *s, *s1, *s2, *s3, *s4;
  59. {
  60.     col_cleanup(0);
  61.     fprintf(stderr, "%s:%d: ", l->i_file, l->i_line);
  62.     fprintf(stderr, s, s1, s2, s3, s4);
  63.     fputc('\n', stderr);
  64.     errors++;
  65. }
  66.  
  67. void lwarning(l, s, s1, s2, s3, s4)
  68. ioloc *l;
  69. char *s, *s1, *s2, *s3, *s4;
  70. {
  71.     col_cleanup(0);
  72.     fprintf(stderr, "%s:%d: ", l->i_file, l->i_line);
  73.     fprintf(stderr, s, s1, s2, s3, s4);
  74.     fputc('\n', stderr);
  75.     
  76. }
  77.  
  78. void fatal(s, s1, s2, s3, s4)
  79. char *s, *s1, *s2, *s3, *s4;
  80. {
  81.     col_cleanup(1);
  82.     fprintf(stderr, "%s: Fatal, ", progname);
  83.     fprintf(stderr, s, s1, s2, s3, s4);
  84.     fputc('\n', stderr);
  85.     exit(1);
  86. }
  87.  
  88. /*
  89.  * Dup a string
  90.  */
  91. char *strdup(s)
  92. char *s;
  93. {
  94.     int len = strlen(s);
  95.     char *sp = (char *) xmalloc(len+1);
  96.  
  97.     bcopy(s, sp, len);
  98.     sp[len] = 0;
  99.  
  100.     return sp;
  101. }
  102.  
  103. /*
  104.  * Debug log
  105.  */
  106. void log(s, s1, s2, s3, s4)
  107. char *s, *s1, *s2, *s3, *s4;
  108. {
  109.     if (verbose > 0) {
  110.         fputc('#', stdout);
  111.         fprintf(stdout, "%s: ", progname);
  112.         fprintf(stdout, s, s1, s2, s3, s4);
  113.         putc('\n', stdout);
  114.     }
  115. }
  116.  
  117. void info_hdr(ef, info)
  118. FILE *ef;
  119. char *info;
  120. {
  121.     fprintf(ef, "# *** NOTE: This file contains %s info\n", info);
  122. }
  123.  
  124. void gen_hdr(ef, hn)
  125. FILE *ef;
  126. char *hn;
  127. {
  128.     fprintf(ef, "# *** NOTE: Only for use on %s\n", hn);
  129. }
  130.  
  131. static void make_banner(fp)
  132. FILE *fp;
  133. {
  134.     time_t t = time((time_t*) 0);
  135.     char *ctime(), *cp = ctime(&t);
  136.  
  137.     fprintf(fp,
  138. "\
  139. # *** This file was automatically generated -- DO NOT EDIT HERE ***\n\
  140. # \"%s\" run by %s@%s on %s\
  141. #\n\
  142. ",
  143.     progname, username, hostname, cp);
  144. }
  145.  
  146. static int show_range = 10;
  147. static int col = 0;
  148. static int total_shown = 0;
  149. static int total_mmm = 8;
  150.  
  151. static int col_output(len)
  152. int len;
  153. {
  154.     int wrapped = 0;
  155.     col += len;
  156.     if (col > 77) {
  157.         fputc('\n', stdout);
  158.         col = len;
  159.         wrapped = 1;
  160.     }
  161.     return wrapped;
  162. }
  163.  
  164. static void show_total()
  165. {
  166.     if (total_mmm != -show_range+1) {
  167.         char n[8];
  168.         int len;
  169.         if (total_mmm < 0)
  170.             fputc('*', stdout);
  171.         sprintf(n, "%d", total_shown);
  172.         len = strlen(n);
  173.         if (col_output(len))
  174.             fputc(' ', stdout);
  175.         fputs(n, stdout); fflush(stdout);
  176.         total_mmm = -show_range;
  177.     }
  178. }
  179.  
  180. col_cleanup(eoj)
  181. int eoj;
  182. {
  183.     if (verbose < 0) return;
  184.     if (eoj) {
  185.         show_total();
  186.         fputs(")]", stdout);
  187.     }
  188.     if (col) {
  189.         fputc('\n', stdout);
  190.         col = 0;
  191.     }
  192. }
  193.  
  194. void show_new(msg)
  195. char *msg;
  196. {
  197.     if (verbose < 0) return;
  198.     total_shown++;
  199.     if (total_mmm > show_range) {
  200.         show_total();
  201.     } else if (total_mmm == 0) {
  202.         fputc('*', stdout); fflush(stdout);
  203.         col += 1;
  204.     }
  205.     total_mmm++;
  206. }
  207.  
  208. void show_area_being_processed(area, n)
  209. char *area;
  210. int n;
  211. {
  212. static char *last_area = 0;
  213.     if (verbose < 0) return;
  214.     if (last_area) {
  215.         if (total_shown)
  216.             show_total();
  217.         fputs(")", stdout);
  218.         col += 1;
  219.     }
  220.     if (!last_area || strcmp(area, last_area) != 0) {
  221.         if (last_area) {
  222.             col_cleanup(0);
  223.             total_shown = 0;
  224.             total_mmm = show_range+1;
  225.         }
  226.         (void) col_output(strlen(area)+2);
  227.         fprintf(stdout, "[%s", area);
  228.         last_area = area;
  229.     }
  230.  
  231.     fputs(" (", stdout);
  232.     col += 2;
  233.     show_range = n;
  234.     total_mmm = n + 1;
  235.  
  236.     fflush(stdout);
  237. }
  238.  
  239. /*
  240.  * Open a file with the given prefix and name
  241.  */
  242. FILE *pref_open(pref, hn, hdr, arg)
  243. char *pref;
  244. char *hn;
  245. void (*hdr)();
  246. char *arg;
  247. {
  248.     char p[MAXPATHLEN];
  249.     FILE *ef;
  250.     sprintf(p, "%s%s", pref, hn);
  251.     log("Writing %s info for %s to %s", pref, hn, p);
  252.     ef = fopen(p, "w");
  253.     if (ef) {
  254.         (*hdr)(ef, arg);
  255.         make_banner(ef, hn);
  256.     } else {
  257.         error("can't open %s for writing", p);
  258.     }
  259.  
  260.     return ef;
  261. }
  262.  
  263. int pref_close(fp)
  264. FILE *fp;
  265. {
  266.     return fclose(fp) == 0;
  267. }
  268.  
  269. /*
  270.  * Determine where Amd would automount the host/volname pair
  271.  */
  272. void compute_automount_point(buf, hp, vn)
  273. char *buf;
  274. host *hp;
  275. char *vn;
  276. {
  277. #ifdef AMD_USES_HOSTPATH
  278.     sprintf(buf, "%s/%s%s", autodir, hp->h_hostpath, vn);
  279. #else
  280.     sprintf(buf, "%s/%s%s", autodir, hp->h_lochost, vn);
  281. #endif
  282. }
  283.  
  284. char *xcalloc(i, s)
  285. int i;
  286. int s;
  287. {
  288.     char *p = (char *) calloc(i, (unsigned) s);
  289.     if (!p)
  290.         fatal("Out of memory");
  291.     return p;
  292. }
  293.  
  294. char *xmalloc(i)
  295. int i;
  296. {
  297.     char *p = (char *) malloc(i);
  298.     if (!p)
  299.         fatal("Out of memory");
  300.     return p;
  301. }
  302.  
  303. /*
  304.  * Data constructors..
  305.  */
  306.  
  307. automount *new_automount(name)
  308. char *name;
  309. {
  310.     automount *ap = ALLOC(automount);
  311.     ap->a_ioloc = current_location();
  312.     ap->a_name = name;
  313.     ap->a_volname = 0;
  314.     ap->a_mount = 0;
  315.     show_new("automount");
  316.     return ap;
  317. }
  318.  
  319. auto_tree *new_auto_tree(def, ap)
  320. char *def;
  321. qelem *ap;
  322. {
  323.     auto_tree *tp = ALLOC(auto_tree);
  324.     tp->t_ioloc = current_location();
  325.     tp->t_defaults = def;
  326.     tp->t_mount = ap;
  327.     show_new("auto_tree");
  328.     return tp;
  329. }
  330.  
  331. host *new_host()
  332. {
  333.     host *hp = ALLOC(host);
  334.     hp->h_ioloc = current_location();
  335.     hp->h_mask = 0;
  336.     show_new("host");
  337.     return hp;
  338. }
  339.  
  340. void set_host(hp, k, v)
  341. host *hp;
  342. int k;
  343. char *v;
  344. {
  345.     int m = 1 << k;
  346.     if (hp->h_mask & m) {
  347.         yyerror("host field \"%s\" already set", host_strings[k]);
  348.         return;
  349.     }
  350.  
  351.     hp->h_mask |= m;
  352.  
  353.     switch (k) {
  354.     case HF_HOST: {
  355.         char *p = strdup(v);
  356.         dict_ent *de = dict_locate(dict_of_hosts, v);
  357.         if (de)
  358.             yyerror("duplicate host %s!", v);
  359.         else
  360.             dict_add(dict_of_hosts, v, (char *) hp);
  361.         hp->h_hostname = v;
  362.         domain_strip(p, hostname);
  363.         if (strchr(p, '.') != 0)
  364.             free(p);
  365.         else
  366.             hp->h_lochost = p;
  367.     } break;
  368.     case HF_CONFIG: {
  369.         qelem *q;
  370.         qelem *vq = (qelem *) v;
  371.         hp->h_mask &= ~m;
  372.         if (hp->h_config)
  373.             q = hp->h_config;
  374.         else
  375.             q = hp->h_config = new_que();
  376.         ins_que(vq, q->q_back);
  377.     } break;
  378.     case HF_ETHER: {
  379.         qelem *q;
  380.         qelem *vq = (qelem *) v;
  381.         hp->h_mask &= ~m;
  382.         if (hp->h_ether)
  383.             q = hp->h_ether;
  384.         else
  385.             q = hp->h_ether = new_que();
  386.         ins_que(vq, q->q_back);
  387.     } break;
  388.     case HF_ARCH: hp->h_arch = v; break;
  389.     case HF_OS: hp->h_os = v; break;
  390.     case HF_CLUSTER: hp->h_cluster = v; break;
  391.     default: abort(); break;
  392.     }
  393. }
  394.  
  395. ether_if *new_ether_if()
  396. {
  397.     ether_if *ep = ALLOC(ether_if);
  398.     ep->e_mask = 0;
  399.     ep->e_ioloc = current_location();
  400.     show_new("ether_if");
  401.     return ep;
  402. }
  403.  
  404. void set_ether_if(ep,k, v)
  405. ether_if *ep;
  406. int k;
  407. char *v;
  408. {
  409.     int m = 1 << k;
  410.     if (ep->e_mask & m) {
  411.         yyerror("netif field \"%s\" already set", ether_if_strings[k]);
  412.         return;
  413.     }
  414.  
  415.     ep->e_mask |= m;
  416.  
  417.     switch (k) {
  418.     case EF_INADDR: {
  419.         extern u_long inet_addr();
  420.         ep->e_inaddr.s_addr = inet_addr(v);
  421.         if (ep->e_inaddr.s_addr == (u_long) -1)
  422.             yyerror("malformed IP dotted quad: %s", v);
  423.         free(v);
  424.     } break;
  425.     case EF_NETMASK: {
  426.         u_long nm = 0;
  427.         if ((sscanf(v, "0x%lx", &nm) == 1 || sscanf(v, "%lx", &nm) == 1) && nm != 0)
  428.             ep->e_netmask = htonl(nm);
  429.         else
  430.             yyerror("malformed netmask: %s", v);
  431.         free(v);
  432.     } break;
  433.     case EF_HWADDR:
  434.         ep->e_hwaddr = v;
  435.         break;
  436.     default: abort(); break;
  437.     }
  438. }
  439.  
  440. void set_disk_fs(dp, k, v)
  441. disk_fs *dp;
  442. int k;
  443. char *v;
  444. {
  445.     int m = 1 << k;
  446.     if (dp->d_mask & m) {
  447.         yyerror("fs field \"%s\" already set", disk_fs_strings[k]);
  448.         return;
  449.     }
  450.  
  451.     dp->d_mask |= m;
  452.  
  453.     switch (k) {
  454.     case DF_FSTYPE: dp->d_fstype = v; break;
  455.     case DF_OPTS: dp->d_opts = v; break;
  456.     case DF_DUMPSET: dp->d_dumpset = v; break;
  457.     case DF_LOG: dp->d_log = v; break;
  458.     case DF_PASSNO: dp->d_passno = atoi(v); free(v); break;
  459.     case DF_FREQ: dp->d_freq = atoi(v); free(v); break;
  460.     case DF_MOUNT: dp->d_mount = &((mount *) v)->m_q; break;
  461.     default: abort(); break;
  462.     }
  463. }
  464.  
  465. disk_fs *new_disk_fs()
  466. {
  467.     disk_fs *dp = ALLOC(disk_fs);
  468.     dp->d_ioloc = current_location();
  469.     show_new("disk_fs");
  470.     return dp;
  471. }
  472.  
  473. void set_mount(mp, k, v)
  474. mount *mp;
  475. int k;
  476. char *v;
  477. {
  478.     int m = 1 << k;
  479.     if (mp->m_mask & m) {
  480.         yyerror("mount tree field \"%s\" already set", mount_strings[k]);
  481.         return;
  482.     }
  483.  
  484.     mp->m_mask |= m;
  485.  
  486.     switch (k) {
  487.     case DM_VOLNAME:
  488.         dict_add(dict_of_volnames, v, (char *) mp);
  489.         mp->m_volname = v;
  490.         break;
  491.     case DM_EXPORTFS:
  492.         mp->m_exportfs = v;
  493.         break;
  494.     case DM_SEL:
  495.         mp->m_sel = v;
  496.         break;
  497.     default: abort(); break;
  498.     }
  499. }
  500.  
  501. mount *new_mount()
  502. {
  503.     mount *fp = ALLOC(mount);
  504.     fp->m_ioloc = current_location();
  505.     show_new("mount");
  506.     return fp;
  507. }
  508.  
  509. void set_fsmount(fp, k, v)
  510. fsmount *fp;
  511. int k;
  512. char *v;
  513. {
  514.     int m = 1 << k;
  515.     if (fp->f_mask & m) {
  516.         yyerror("mount field \"%s\" already set", fsmount_strings[k]);
  517.         return;
  518.     }
  519.  
  520.     fp->f_mask |= m;
  521.  
  522.     switch (k) {
  523.     case FM_LOCALNAME: fp->f_localname = v; break;
  524.     case FM_VOLNAME: fp->f_volname = v; break;
  525.     case FM_FSTYPE: fp->f_fstype = v; break;
  526.     case FM_OPTS: fp->f_opts = v; break;
  527.     case FM_FROM: fp->f_from = v; break;
  528.     default: abort(); break;
  529.     }
  530. }
  531.  
  532. fsmount *new_fsmount()
  533. {
  534.     fsmount *fp = ALLOC(fsmount);
  535.     fp->f_ioloc = current_location();
  536.     show_new("fsmount");
  537.     return fp;
  538. }
  539.  
  540. void init_que(q)
  541. qelem *q;
  542. {
  543.     q->q_forw = q->q_back = q;
  544. }
  545.  
  546. qelem *new_que()
  547. {
  548.     qelem *q = ALLOC(qelem);
  549.     init_que(q);
  550.     return q;
  551. }
  552.  
  553. void ins_que(elem, pred)
  554. qelem *elem, *pred;
  555. {
  556.     qelem *p;
  557.     p = pred->q_forw;
  558.     elem->q_back = pred;
  559.     elem->q_forw = p;
  560.     pred->q_forw = elem;
  561.     p->q_back = elem;
  562. }
  563.  
  564. void rem_que(elem)
  565. qelem *elem;
  566. {
  567.     qelem *p, *p2;
  568.     p = elem->q_forw;
  569.     p2 = elem->q_back;
  570.  
  571.     p2->q_forw = p;
  572.     p->q_back = p2;
  573. }
  574.