home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / config / mkswapconf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-01  |  5.7 KB  |  216 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)mkswapconf.c    5.10 (Berkeley) 7/1/91";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * Build a swap configuration file.
  40.  */
  41. #include "config.h"
  42.  
  43. #include <stdio.h>
  44. #include <ctype.h>
  45.  
  46. swapconf()
  47. {
  48.     register struct file_list *fl;
  49.     struct file_list *do_swap();
  50.  
  51.     fl = conf_list;
  52.     while (fl) {
  53.         if (fl->f_type != SYSTEMSPEC) {
  54.             fl = fl->f_next;
  55.             continue;
  56.         }
  57.         fl = do_swap(fl);
  58.     }
  59. }
  60.  
  61. struct file_list *
  62. do_swap(fl)
  63.     register struct file_list *fl;
  64. {
  65.     FILE *fp;
  66.     char  swapname[80];
  67.     register struct file_list *swap;
  68.     dev_t dev;
  69.  
  70.     if (eq(fl->f_fn, "generic")) {
  71.         fl = fl->f_next;
  72.         return (fl->f_next);
  73.     }
  74.     (void) sprintf(swapname, "swap%s.c", fl->f_fn);
  75.     fp = fopen(path(swapname), "w");
  76.     if (fp == 0) {
  77.         perror(path(swapname));
  78.         exit(1);
  79.     }
  80.     fprintf(fp, "#include \"sys/param.h\"\n");
  81.     fprintf(fp, "#include \"sys/conf.h\"\n");
  82.     fprintf(fp, "\n");
  83.     /*
  84.      * If there aren't any swap devices
  85.      * specified, just return, the error
  86.      * has already been noted.
  87.      */
  88.     swap = fl->f_next;
  89.     if (swap == 0 || swap->f_type != SWAPSPEC) {
  90.         (void) unlink(path(swapname));
  91.         fclose(fp);
  92.         return (swap);
  93.     }
  94.     fprintf(fp, "dev_t\trootdev = makedev(%d, %d);\n",
  95.         major(fl->f_rootdev), minor(fl->f_rootdev));
  96.     fprintf(fp, "dev_t\tdumpdev = makedev(%d, %d);\n",
  97.         major(fl->f_dumpdev), minor(fl->f_dumpdev));
  98.     fprintf(fp, "\n");
  99.     fprintf(fp, "struct\tswdevt swdevt[] = {\n");
  100.     do {
  101.         dev = swap->f_swapdev;
  102.         fprintf(fp, "\t{ makedev(%d, %d),\t0,\t%d },\t/* %s */\n",
  103.             major(dev), minor(dev), swap->f_swapsize, swap->f_fn);
  104.         swap = swap->f_next;
  105.     } while (swap && swap->f_type == SWAPSPEC);
  106.     fprintf(fp, "\t{ 0, 0, 0 }\n");
  107.     fprintf(fp, "};\n");
  108.     fclose(fp);
  109.     return (swap);
  110. }
  111.  
  112. static    int devtablenotread = 1;
  113. static    struct devdescription {
  114.     char    *dev_name;
  115.     int    dev_major;
  116.     struct    devdescription *dev_next;
  117. } *devtable;
  118.  
  119. /*
  120.  * Given a device name specification figure out:
  121.  *    major device number
  122.  *    partition
  123.  *    device name
  124.  *    unit number
  125.  * This is a hack, but the system still thinks in
  126.  * terms of major/minor instead of string names.
  127.  */
  128. dev_t
  129. nametodev(name, defunit, defpartition)
  130.     char *name;
  131.     int defunit;
  132.     char defpartition;
  133. {
  134.     char *cp, partition;
  135.     int unit;
  136.     register struct devdescription *dp;
  137.  
  138.     cp = name;
  139.     if (cp == 0) {
  140.         fprintf(stderr, "config: internal error, nametodev\n");
  141.         exit(1);
  142.     }
  143.     while (*cp && !isdigit(*cp))
  144.         cp++;
  145.     unit = *cp ? atoi(cp) : defunit;
  146.     if (unit < 0 || unit > 31) {
  147.         fprintf(stderr,
  148. "config: %s: invalid device specification, unit out of range\n", name);
  149.         unit = defunit;            /* carry on more checking */
  150.     }
  151.     if (*cp) {
  152.         *cp++ = '\0';
  153.         while (*cp && isdigit(*cp))
  154.             cp++;
  155.     }
  156.     partition = *cp ? *cp : defpartition;
  157.     if (partition < 'a' || partition > 'h') {
  158.         fprintf(stderr,
  159. "config: %c: invalid device specification, bad partition\n", *cp);
  160.         partition = defpartition;    /* carry on */
  161.     }
  162.     if (devtablenotread)
  163.         initdevtable();
  164.     for (dp = devtable; dp; dp = dp->dev_next)
  165.         if (eq(name, dp->dev_name))
  166.             break;
  167.     if (dp == 0) {
  168.         fprintf(stderr, "config: %s: unknown device\n", name);
  169.         return (NODEV);
  170.     }
  171.     return (makedev(dp->dev_major, (unit << 3) + (partition - 'a')));
  172. }
  173.  
  174. char *
  175. devtoname(dev)
  176.     dev_t dev;
  177. {
  178.     char buf[80]; 
  179.     register struct devdescription *dp;
  180.  
  181.     if (devtablenotread)
  182.         initdevtable();
  183.     for (dp = devtable; dp; dp = dp->dev_next)
  184.         if (major(dev) == dp->dev_major)
  185.             break;
  186.     if (dp == 0)
  187.         dp = devtable;
  188.     (void) sprintf(buf, "%s%d%c", dp->dev_name,
  189.         minor(dev) >> 3, (minor(dev) & 07) + 'a');
  190.     return (ns(buf));
  191. }
  192.  
  193. initdevtable()
  194. {
  195.     char buf[BUFSIZ];
  196.     int maj;
  197.     register struct devdescription **dp = &devtable;
  198.     FILE *fp;
  199.  
  200.     (void) sprintf(buf, "../conf/devices.%s", machinename);
  201.     fp = fopen(buf, "r");
  202.     if (fp == NULL) {
  203.         fprintf(stderr, "config: can't open %s\n", buf);
  204.         exit(1);
  205.     }
  206.     while (fscanf(fp, "%s\t%d\n", buf, &maj) == 2) {
  207.         *dp = (struct devdescription *)malloc(sizeof (**dp));
  208.         (*dp)->dev_name = ns(buf);
  209.         (*dp)->dev_major = maj;
  210.         dp = &(*dp)->dev_next;
  211.     }
  212.     *dp = 0;
  213.     fclose(fp);
  214.     devtablenotread = 0;
  215. }
  216.