xref: /original-bsd/sbin/swapon/swapon.c (revision 6cca134b)
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 are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)swapon.c	5.3 (Berkeley) 06/30/88";
26 #endif /* not lint */
27 
28 #include <fstab.h>
29 #include <errno.h>
30 #include <stdio.h>
31 
32 main(argc, argv)
33 	int argc;
34 	char **argv;
35 {
36 	extern char *optarg;
37 	extern int optind;
38 	register struct fstab *fsp;
39 	register int stat;
40 	int ch, doall;
41 
42 	doall = 0;
43 	while ((ch = getopt(argc, argv, "a")) != EOF)
44 		switch((char)ch) {
45 		case 'a':
46 			doall = 1;
47 			break;
48 		case '?':
49 		default:
50 			usage();
51 		}
52 	argv += optind;
53 
54 	stat = 0;
55 	if (doall)
56 		while (fsp = getfsent()) {
57 			if (strcmp(fsp->fs_type, FSTAB_SW))
58 				continue;
59 			if (add(fsp->fs_spec, 1))
60 				stat = 1;
61 			else
62 				printf("swapon: adding %s as swap device\n",
63 				    fsp->fs_spec);
64 		}
65 	else if (!*argv)
66 		usage();
67 	for (; *argv; ++argv)
68 		stat |= add(*argv, 0);
69 	exit(stat);
70 }
71 
72 static
73 add(name, ignoreebusy)
74 	char *name;
75 	int ignoreebusy;
76 {
77 	extern int errno;
78 
79 	if (swapon(name) == -1) {
80 		switch (errno) {
81 		case EINVAL:
82 			fprintf(stderr, "swapon: %s: device not configured\n",
83 			    name);
84 			break;
85 		case EBUSY:
86 			if (!ignoreebusy)
87 				fprintf(stderr,
88 				    "swapon: %s: device already in use\n",
89 				     name);
90 			break;
91 		default:
92 			fprintf(stderr, "swapon: %s: ", name);
93 			perror((char *)NULL);
94 			break;
95 		}
96 		return(1);
97 	}
98 	return(0);
99 }
100 
101 static
102 usage()
103 {
104 	fprintf(stderr, "usage: swapon [-a] [special_file ...]\n");
105 	exit(1);
106 }
107