util-linux/mount/swapon.c

172 lines
3.2 KiB
C
Raw Normal View History

2006-12-06 17:25:32 -06:00
/*
* A swapon(8)/swapoff(8) for Linux 0.99.
* swapon.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
*/
#include "sundries.h"
/* Nonzero for chatty (-v). This is a nonstandard flag (not in BSD). */
int verbose = 0;
2006-12-06 17:25:33 -06:00
#ifdef SUPPORT_PRIORITIES
int priority = -1; /* non-prioritized swap by default */
#endif
2006-12-06 17:25:32 -06:00
extern char version[];
static char *program_name;
static struct option longopts[] =
{
{ "all", 0, 0, 'a' },
{ "help", 0, 0, 'h' },
2006-12-06 17:25:33 -06:00
#ifdef SUPPORT_PRIORITIES
{ "priority", required_argument, 0, 'p' },
#endif
2006-12-06 17:25:32 -06:00
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ NULL, 0, 0, 0 }
};
2006-12-06 17:25:33 -06:00
#ifdef SUPPORT_PRIORITIES
2006-12-06 17:25:32 -06:00
const char *usage_string = "\
usage: %s [-hV]\n\
%s -a [-v]\n\
2006-12-06 17:25:33 -06:00
%s [-v] [-p priority] special ...\n\
2006-12-06 17:25:32 -06:00
";
2006-12-06 17:25:33 -06:00
#else
const char *usage_string = "\
usage: %s [-hV]\n\
%s -a [-v]\n\
%s [-v] [-p priority] special ...\n\
";
#endif
2006-12-06 17:25:32 -06:00
static void
usage (FILE *fp, int n)
{
fprintf (fp, usage_string, program_name, program_name, program_name);
exit (n);
}
static int
2006-12-06 17:25:33 -06:00
#ifdef SUPPORT_PRIORITIES
swap (const char *special, int prio)
#else
2006-12-06 17:25:32 -06:00
swap (const char *special)
2006-12-06 17:25:33 -06:00
#endif
2006-12-06 17:25:32 -06:00
{
int status;
2006-12-06 17:25:33 -06:00
#ifdef SUPPORT_PRIORITIES
int flags;
#endif
2006-12-06 17:25:32 -06:00
if (verbose)
printf("%s on device %s\n", program_name, special);
2006-12-06 17:25:33 -06:00
if (streq (program_name, "swapon")) {
#ifdef SUPPORT_PRIORITIES
flags = 0;
if (prio >= 0) {
flags = SWAP_FLAG_PREFER
| ((prio & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
}
status = swapon (special, flags);
#else
status = swapon (special);
#endif
} else
status = swapoff (special);
2006-12-06 17:25:32 -06:00
if (status < 0)
fprintf (stderr, "%s: %s: %s\n", program_name, special, strerror (errno));
return status;
}
int
main (int argc, char *argv[])
{
struct fstab *fstab;
int status;
int all = 0;
int c;
if (strrchr (argv[0], '/') != NULL)
program_name = strrchr (argv[0], '/') + 1;
else
program_name = argv[0];
2006-12-06 17:25:33 -06:00
#ifdef SUPPORT_PRIORITIES
while ((c = getopt_long (argc, argv, "ahp:vV", longopts, NULL)) != EOF)
#else
2006-12-06 17:25:32 -06:00
while ((c = getopt_long (argc, argv, "ahvV", longopts, NULL)) != EOF)
2006-12-06 17:25:33 -06:00
#endif
2006-12-06 17:25:32 -06:00
switch (c)
{
case 'a': /* all */
++all;
break;
case 'h': /* help */
usage (stdout, 0);
break;
2006-12-06 17:25:33 -06:00
#ifdef SUPPORT_PRIORITIES
case 'p': /* priority */
priority = atoi(optarg);
break;
#endif
2006-12-06 17:25:32 -06:00
case 'v': /* be chatty */
++verbose;
break;
case 'V': /* version */
2006-12-06 17:25:33 -06:00
printf ("swapon: %s\n", version);
2006-12-06 17:25:32 -06:00
exit (0);
case 0:
break;
case '?':
default:
usage (stderr, 1);
}
argv += optind;
status = 0;
if (all)
{
while ((fstab = getfsent()) != NULL)
if (streq (fstab->fs_type, FSTAB_SW))
2006-12-06 17:25:33 -06:00
{
#ifdef SUPPORT_PRIORITIES
/* parse mount options; */
char *opt, *opts = strdup(fstab->fs_mntopts);
for (opt = strtok (opts, ",");
opt != NULL;
opt = strtok (NULL, ","))
{
if (strncmp(opt, "pri=", 4) == 0)
{
priority = atoi(opt+4);
}
}
status |= swap (fstab->fs_spec, priority);
#else
status |= swap (fstab->fs_spec);
#endif
}
2006-12-06 17:25:32 -06:00
}
else if (*argv == NULL)
{
usage (stderr, 2);
}
else
{
2006-12-06 17:25:33 -06:00
while (*argv != NULL) {
#ifdef SUPPORT_PRIORITIES
status |= swap (*argv++,priority);
#else
status |= swap (*argv++);
#endif
}
2006-12-06 17:25:32 -06:00
}
return status;
}