xref: /csrg-svn/sbin/swapon/swapon.c (revision 61554)
121180Sdist /*
2*61554Sbostic  * Copyright (c) 1980, 1993
3*61554Sbostic  *	The Regents of the University of California.  All rights reserved.
434922Sbostic  *
542712Sbostic  * %sccs.include.redist.c%
621180Sdist  */
721180Sdist 
821180Sdist #ifndef lint
9*61554Sbostic static char copyright[] =
10*61554Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*61554Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1234922Sbostic #endif /* not lint */
1321180Sdist 
1421180Sdist #ifndef lint
15*61554Sbostic static char sccsid[] = "@(#)swapon.c	8.1 (Berkeley) 06/05/93";
1634922Sbostic #endif /* not lint */
1721180Sdist 
181417Sbill #include <fstab.h>
1917878Sbloom #include <errno.h>
2034922Sbostic #include <stdio.h>
211113Sbill 
main(argc,argv)221113Sbill main(argc, argv)
231113Sbill 	int argc;
2434922Sbostic 	char **argv;
251113Sbill {
2634922Sbostic 	extern char *optarg;
2734922Sbostic 	extern int optind;
2834922Sbostic 	register struct fstab *fsp;
2934922Sbostic 	register int stat;
3034922Sbostic 	int ch, doall;
311113Sbill 
3234922Sbostic 	doall = 0;
3334922Sbostic 	while ((ch = getopt(argc, argv, "a")) != EOF)
3434922Sbostic 		switch((char)ch) {
3534922Sbostic 		case 'a':
3634922Sbostic 			doall = 1;
3734922Sbostic 			break;
3834922Sbostic 		case '?':
3934922Sbostic 		default:
4034922Sbostic 			usage();
4134922Sbostic 		}
4234922Sbostic 	argv += optind;
4334922Sbostic 
4434922Sbostic 	stat = 0;
4534922Sbostic 	if (doall)
4634922Sbostic 		while (fsp = getfsent()) {
4734922Sbostic 			if (strcmp(fsp->fs_type, FSTAB_SW))
481417Sbill 				continue;
4934922Sbostic 			if (add(fsp->fs_spec, 1))
5034922Sbostic 				stat = 1;
5134922Sbostic 			else
5234922Sbostic 				printf("swapon: adding %s as swap device\n",
5318444Skarels 				    fsp->fs_spec);
541417Sbill 		}
5534922Sbostic 	else if (!*argv)
5634922Sbostic 		usage();
5734922Sbostic 	for (; *argv; ++argv)
5834922Sbostic 		stat |= add(*argv, 0);
5934922Sbostic 	exit(stat);
6034922Sbostic }
6117878Sbloom 
add(name,ignoreebusy)6234922Sbostic add(name, ignoreebusy)
6334922Sbostic 	char *name;
6434922Sbostic 	int ignoreebusy;
6534922Sbostic {
6634922Sbostic 	extern int errno;
6717878Sbloom 
6834922Sbostic 	if (swapon(name) == -1) {
6934922Sbostic 		switch (errno) {
7034922Sbostic 		case EINVAL:
7134922Sbostic 			fprintf(stderr, "swapon: %s: device not configured\n",
7234922Sbostic 			    name);
7334922Sbostic 			break;
7434922Sbostic 		case EBUSY:
7534922Sbostic 			if (!ignoreebusy)
7634922Sbostic 				fprintf(stderr,
7734922Sbostic 				    "swapon: %s: device already in use\n",
7834922Sbostic 				     name);
7934922Sbostic 			break;
8034922Sbostic 		default:
8134922Sbostic 			fprintf(stderr, "swapon: %s: ", name);
8234922Sbostic 			perror((char *)NULL);
8334922Sbostic 			break;
841113Sbill 		}
8534922Sbostic 		return(1);
8634922Sbostic 	}
8734922Sbostic 	return(0);
881113Sbill }
8934922Sbostic 
usage()9034922Sbostic usage()
9134922Sbostic {
9234922Sbostic 	fprintf(stderr, "usage: swapon [-a] [special_file ...]\n");
9334922Sbostic 	exit(1);
9434922Sbostic }
95