4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / sbin / swapon / swapon.c
CommitLineData
8c5eec2f
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
ef08073c
KB
3 * All rights reserved.
4 *
d60d530a 5 * %sccs.include.redist.c%
8c5eec2f
DF
6 */
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1980 Regents of the University of California.\n\
11 All rights reserved.\n";
ef08073c 12#endif /* not lint */
8c5eec2f
DF
13
14#ifndef lint
f311b613 15static char sccsid[] = "@(#)swapon.c 5.5 (Berkeley) %G%";
ef08073c 16#endif /* not lint */
8c5eec2f 17
f0861f40 18#include <fstab.h>
ad372055 19#include <errno.h>
ef08073c 20#include <stdio.h>
ad372055 21
1885c525
BJ
22main(argc, argv)
23 int argc;
ef08073c 24 char **argv;
1885c525 25{
ef08073c
KB
26 extern char *optarg;
27 extern int optind;
28 register struct fstab *fsp;
29 register int stat;
30 int ch, doall;
1885c525 31
ef08073c
KB
32 doall = 0;
33 while ((ch = getopt(argc, argv, "a")) != EOF)
34 switch((char)ch) {
35 case 'a':
36 doall = 1;
37 break;
38 case '?':
39 default:
40 usage();
41 }
42 argv += optind;
ad372055 43
ef08073c
KB
44 stat = 0;
45 if (doall)
46 while (fsp = getfsent()) {
47 if (strcmp(fsp->fs_type, FSTAB_SW))
48 continue;
49 if (add(fsp->fs_spec, 1))
50 stat = 1;
51 else
52 printf("swapon: adding %s as swap device\n",
df60bdb5 53 fsp->fs_spec);
f0861f40 54 }
ef08073c
KB
55 else if (!*argv)
56 usage();
57 for (; *argv; ++argv)
58 stat |= add(*argv, 0);
59 exit(stat);
60}
ad372055 61
ef08073c
KB
62add(name, ignoreebusy)
63 char *name;
64 int ignoreebusy;
65{
66 extern int errno;
ad372055 67
ef08073c
KB
68 if (swapon(name) == -1) {
69 switch (errno) {
70 case EINVAL:
71 fprintf(stderr, "swapon: %s: device not configured\n",
72 name);
73 break;
74 case EBUSY:
75 if (!ignoreebusy)
76 fprintf(stderr,
77 "swapon: %s: device already in use\n",
78 name);
79 break;
80 default:
81 fprintf(stderr, "swapon: %s: ", name);
82 perror((char *)NULL);
83 break;
1885c525 84 }
ef08073c
KB
85 return(1);
86 }
87 return(0);
88}
89
ef08073c
KB
90usage()
91{
92 fprintf(stderr, "usage: swapon [-a] [special_file ...]\n");
93 exit(1);
1885c525 94}