xref: /original-bsd/sbin/swapon/swapon.c (revision 542201aa)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)swapon.c	5.2 (Berkeley) 03/04/86";
15 #endif not lint
16 
17 #include <stdio.h>
18 #include <fstab.h>
19 #include <errno.h>
20 
21 extern int errno;
22 
23 main(argc, argv)
24 	int argc;
25 	char *argv[];
26 {
27 	int stat = 0;
28 
29 	--argc, argv++;
30 	if (argc == 0) {
31 		fprintf(stderr, "usage: swapon name...\n");
32 		exit(1);
33 	}
34 	if (argc == 1 && !strcmp(*argv, "-a")) {
35 		struct	fstab	*fsp;
36 		if (setfsent() == 0)
37 			perror(FSTAB), exit(1);
38 		while ( (fsp = getfsent()) != 0){
39 			if (strcmp(fsp->fs_type, FSTAB_SW) != 0)
40 				continue;
41 			if (swapon(fsp->fs_spec) == -1) {
42 				switch(errno) {
43 				case EINVAL:
44 					fprintf(stderr,
45 						"%s: Device not configured\n",
46 						fsp->fs_spec);
47 					stat = 1;
48 					break;
49 
50 				case EBUSY:	/* ignore already in use */
51 					break;
52 
53 				default:
54 					perror(fsp->fs_spec);
55 					stat = 1;
56 					break;
57 				}
58 			} else
59 				printf("Adding %s as swap device\n",
60 				    fsp->fs_spec);
61 		}
62 		endfsent();
63 		exit(stat);
64 	}
65 	do {
66 		if (swapon(*argv++) == -1) {
67 			stat = 1;
68 			switch (errno) {
69 			case EINVAL:
70 				fprintf(stderr, "%s: Device not configured\n",
71 						argv[-1]);
72 				break;
73 
74 			case EBUSY:
75 				fprintf(stderr, "%s: Device already in use\n",
76 						argv[-1]);
77 				break;
78 
79 			default:
80 				perror(argv[-1]);
81 				break;
82 			}
83 		}
84 		argc--;
85 	} while (argc > 0);
86 	exit(stat);
87 }
88