xref: /original-bsd/sbin/swapon/swapon.c (revision 1ff91bf0)
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.5 (Berkeley) 02/27/91";
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 add(name, ignoreebusy)
63 	char *name;
64 	int ignoreebusy;
65 {
66 	extern int errno;
67 
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;
84 		}
85 		return(1);
86 	}
87 	return(0);
88 }
89 
90 usage()
91 {
92 	fprintf(stderr, "usage: swapon [-a] [special_file ...]\n");
93 	exit(1);
94 }
95