swapon.c revision 24359
1208963Srdivacky/* 2208963Srdivacky * Copyright (c) 1980, 1993 3239614Sdim * The Regents of the University of California. All rights reserved. 4208963Srdivacky * 5239614Sdim * Redistribution and use in source and binary forms, with or without 6239614Sdim * modification, are permitted provided that the following conditions 7239614Sdim * are met: 8239614Sdim * 1. Redistributions of source code must retain the above copyright 9239614Sdim * notice, this list of conditions and the following disclaimer. 10208963Srdivacky * 2. Redistributions in binary form must reproduce the above copyright 11246259Sdim * notice, this list of conditions and the following disclaimer in the 12246259Sdim * documentation and/or other materials provided with the distribution. 13246259Sdim * 3. All advertising materials mentioning features or use of this software 14246259Sdim * must display the following acknowledgement: 15246259Sdim * This product includes software developed by the University of 16246259Sdim * California, Berkeley and its contributors. 17229169Snwhitehorn * 4. Neither the name of the University nor the names of its contributors 18239614Sdim * may be used to endorse or promote products derived from this software 19209153Sed * without specific prior written permission. 20208963Srdivacky * 21239462Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22246705Sandrew * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23246705Sandrew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24246705Sandrew * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25246705Sandrew * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26246705Sandrew * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27246705Sandrew * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28246705Sandrew * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29246705Sandrew * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30246705Sandrew * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31239462Sdim * SUCH DAMAGE. 32239462Sdim */ 33251662Sdim 34243830Sdim#ifndef lint 35243830Sdimstatic char copyright[] = 36208963Srdivacky"@(#) Copyright (c) 1980, 1993\n\ 37208963Srdivacky The Regents of the University of California. All rights reserved.\n"; 38208963Srdivacky#endif /* not lint */ 39239614Sdim 40239614Sdim#ifndef lint 41208963Srdivackystatic char sccsid[] = "@(#)swapon.c 8.1 (Berkeley) 6/5/93"; 42249423Sdim#endif /* not lint */ 43249423Sdim 44249423Sdim#include <fstab.h> 45249423Sdim#include <errno.h> 46249423Sdim#include <stdio.h> 47249423Sdim#include <stdlib.h> 48249423Sdim#include <unistd.h> 49249423Sdim 50249423Sdimvoid usage __P((void)); 51249423Sdimint add __P((char *name, int ignoreebusy)); 52249423Sdim 53249423Sdimint 54208963Srdivackymain(int argc, char **argv) 55208963Srdivacky{ 56208963Srdivacky extern char *optarg; 57208963Srdivacky extern int optind; 58208963Srdivacky register struct fstab *fsp; 59208963Srdivacky register int stat; 60208963Srdivacky int ch, doall; 61208963Srdivacky 62208963Srdivacky doall = 0; 63212904Sdim while ((ch = getopt(argc, argv, "a")) != -1) 64208963Srdivacky switch((char)ch) { 65224145Sdim case 'a': 66218893Sdim doall = 1; 67224145Sdim break; 68224145Sdim case '?': 69224145Sdim default: 70208963Srdivacky usage(); 71249423Sdim } 72224145Sdim argv += optind; 73224145Sdim 74208963Srdivacky stat = 0; 75208963Srdivacky if (doall) 76208963Srdivacky while ((fsp = getfsent()) != NULL) { 77210299Sed if (strcmp(fsp->fs_type, FSTAB_SW)) 78249423Sdim continue; 79249423Sdim if (strstr(fsp->fs_mntops, "noauto")) 80210299Sed continue; 81249423Sdim if (add(fsp->fs_spec, 1)) 82249423Sdim stat = 1; 83249423Sdim else 84249423Sdim printf("swapon: adding %s as swap device\n", 85251662Sdim fsp->fs_spec); 86251662Sdim } 87251662Sdim else if (!*argv) 88251662Sdim usage(); 89212904Sdim for (; *argv; ++argv) 90249423Sdim stat |= add(*argv, 0); 91249423Sdim exit(stat); 92226633Sdim} 93249423Sdim 94249423Sdimint 95226633Sdimadd(char *name, int ignoreebusy) 96210299Sed{ 97249423Sdim extern int errno; 98249423Sdim 99210299Sed if (swapon(name) == -1) { 100234353Sdim switch (errno) { 101249423Sdim case EBUSY: 102249423Sdim if (!ignoreebusy) 103234353Sdim fprintf(stderr, 104234353Sdim "swapon: %s: device already in use\n", 105249423Sdim name); 106249423Sdim break; 107234353Sdim default: 108212904Sdim fprintf(stderr, "swapon: %s: ", name); 109249423Sdim perror((char *)NULL); 110249423Sdim break; 111212904Sdim } 112212904Sdim return(1); 113249423Sdim } 114249423Sdim return(0); 115212904Sdim} 116218893Sdim 117249423Sdimvoid 118249423Sdimusage() 119218893Sdim{ 120249423Sdim fprintf(stderr, "usage: swapon [-a] [special_file ...]\n"); 121249423Sdim exit(1); 122249423Sdim} 123249423Sdim