swapon.c revision 6661
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
351558Srgrimesstatic char copyright[] =
361558Srgrimes"@(#) Copyright (c) 1980, 1993\n\
371558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381558Srgrimes#endif /* not lint */
391558Srgrimes
401558Srgrimes#ifndef lint
411558Srgrimesstatic char sccsid[] = "@(#)swapon.c	8.1 (Berkeley) 6/5/93";
421558Srgrimes#endif /* not lint */
431558Srgrimes
441558Srgrimes#include <fstab.h>
451558Srgrimes#include <errno.h>
461558Srgrimes#include <stdio.h>
476661Sphk#include <stdlib.h>
486661Sphk#include <unistd.h>
491558Srgrimes
506661Sphkvoid	usage __P((void));
516661Sphkint	add __P((char *name, int ignoreebusy));
526661Sphk
536661Sphkint
546661Sphkmain(int argc, char **argv)
551558Srgrimes{
561558Srgrimes	extern char *optarg;
571558Srgrimes	extern int optind;
581558Srgrimes	register struct fstab *fsp;
591558Srgrimes	register int stat;
601558Srgrimes	int ch, doall;
611558Srgrimes
621558Srgrimes	doall = 0;
631558Srgrimes	while ((ch = getopt(argc, argv, "a")) != EOF)
641558Srgrimes		switch((char)ch) {
651558Srgrimes		case 'a':
661558Srgrimes			doall = 1;
671558Srgrimes			break;
681558Srgrimes		case '?':
691558Srgrimes		default:
701558Srgrimes			usage();
711558Srgrimes		}
721558Srgrimes	argv += optind;
731558Srgrimes
741558Srgrimes	stat = 0;
751558Srgrimes	if (doall)
766661Sphk		while ((fsp = getfsent()) != NULL) {
771558Srgrimes			if (strcmp(fsp->fs_type, FSTAB_SW))
781558Srgrimes				continue;
791558Srgrimes			if (add(fsp->fs_spec, 1))
801558Srgrimes				stat = 1;
811558Srgrimes			else
821558Srgrimes				printf("swapon: adding %s as swap device\n",
831558Srgrimes				    fsp->fs_spec);
841558Srgrimes		}
851558Srgrimes	else if (!*argv)
861558Srgrimes		usage();
871558Srgrimes	for (; *argv; ++argv)
881558Srgrimes		stat |= add(*argv, 0);
891558Srgrimes	exit(stat);
901558Srgrimes}
911558Srgrimes
926661Sphkint
936661Sphkadd(char *name, int ignoreebusy)
941558Srgrimes{
951558Srgrimes	extern int errno;
961558Srgrimes
971558Srgrimes	if (swapon(name) == -1) {
981558Srgrimes		switch (errno) {
991558Srgrimes		case EINVAL:
1001558Srgrimes			fprintf(stderr, "swapon: %s: device not configured\n",
1011558Srgrimes			    name);
1021558Srgrimes			break;
1031558Srgrimes		case EBUSY:
1041558Srgrimes			if (!ignoreebusy)
1051558Srgrimes				fprintf(stderr,
1061558Srgrimes				    "swapon: %s: device already in use\n",
1071558Srgrimes				     name);
1081558Srgrimes			break;
1091558Srgrimes		default:
1101558Srgrimes			fprintf(stderr, "swapon: %s: ", name);
1111558Srgrimes			perror((char *)NULL);
1121558Srgrimes			break;
1131558Srgrimes		}
1141558Srgrimes		return(1);
1151558Srgrimes	}
1161558Srgrimes	return(0);
1171558Srgrimes}
1181558Srgrimes
1196661Sphkvoid
1201558Srgrimesusage()
1211558Srgrimes{
1221558Srgrimes	fprintf(stderr, "usage: swapon [-a] [special_file ...]\n");
1231558Srgrimes	exit(1);
1241558Srgrimes}
125