ubase/swapoff.c

60 lines
1019 B
C
Raw Normal View History

2013-08-09 14:42:49 +00:00
/* See LICENSE file for copyright and license details. */
#include <sys/swap.h>
2014-02-08 17:54:04 +00:00
#include <mntent.h>
2013-08-09 14:42:49 +00:00
#include <errno.h>
#include <stdio.h>
2013-10-07 18:11:40 +00:00
#include <stdlib.h>
2013-08-09 14:42:49 +00:00
#include <string.h>
#include "util.h"
static void
usage(void)
{
2014-02-08 17:54:04 +00:00
eprintf("usage: %s [-a] device\n", argv0);
2013-08-09 14:42:49 +00:00
}
int
main(int argc, char *argv[])
{
int i;
2013-10-07 18:11:40 +00:00
int ret = EXIT_SUCCESS;
2014-02-08 17:54:04 +00:00
int all = 0;
2013-08-09 14:42:49 +00:00
ARGBEGIN {
2014-02-08 17:54:04 +00:00
case 'a':
all = 1;
break;
2013-08-09 14:42:49 +00:00
default:
usage();
} ARGEND;
2014-02-08 17:54:04 +00:00
if (!all && argc < 1)
2013-08-09 14:42:49 +00:00
usage();
2014-02-08 17:54:04 +00:00
if (all) {
struct mntent *me = NULL;
FILE *fp;
fp = setmntent("/etc/fstab", "r");
while ((me = getmntent(fp)) != NULL) {
if (strcmp(me->mnt_type, MNTTYPE_SWAP) == 0) {
if (swapoff(me->mnt_fsname) < 0) {
fprintf(stderr, "swapoff %s: %s\n",
me->mnt_fsname, strerror(errno));
ret = EXIT_FAILURE;
}
}
}
endmntent(fp);
} else {
for (i = 0; i < argc; i++) {
if (swapoff(argv[i]) < 0) {
fprintf(stderr, "swapoff %s: %s\n",
argv[i], strerror(errno));
ret = EXIT_FAILURE;
}
2013-08-09 14:42:49 +00:00
}
}
return ret;
}