xref: /original-bsd/sbin/swapon/swapon.c (revision f754001b)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)swapon.c	5.4 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 #include <fstab.h>
19 #include <errno.h>
20 #include <stdio.h>
21 
22 main(argc, argv)
23 	int argc;
24 	char **argv;
25 {
26 	extern char *optarg;
27 	extern int optind;
28 	register struct fstab *fsp;
29 	register int stat;
30 	int ch, doall;
31 
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;
43 
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",
53 				    fsp->fs_spec);
54 		}
55 	else if (!*argv)
56 		usage();
57 	for (; *argv; ++argv)
58 		stat |= add(*argv, 0);
59 	exit(stat);
60 }
61 
62 static
63 add(name, ignoreebusy)
64 	char *name;
65 	int ignoreebusy;
66 {
67 	extern int errno;
68 
69 	if (swapon(name) == -1) {
70 		switch (errno) {
71 		case EINVAL:
72 			fprintf(stderr, "swapon: %s: device not configured\n",
73 			    name);
74 			break;
75 		case EBUSY:
76 			if (!ignoreebusy)
77 				fprintf(stderr,
78 				    "swapon: %s: device already in use\n",
79 				     name);
80 			break;
81 		default:
82 			fprintf(stderr, "swapon: %s: ", name);
83 			perror((char *)NULL);
84 			break;
85 		}
86 		return(1);
87 	}
88 	return(0);
89 }
90 
91 static
92 usage()
93 {
94 	fprintf(stderr, "usage: swapon [-a] [special_file ...]\n");
95 	exit(1);
96 }
97