--- /dev/null
+
+#include "c.h"
+#include "nls.h"
+#include "swapon-common.h"
+#include "xalloc.h"
+
+/*
+ * content of /proc/swaps and /etc/fstab
+ */
+static struct libmnt_table *swaps, *fstab;
+
+struct libmnt_cache *mntcache;
+
+struct libmnt_table *get_fstab(void)
+{
+ if (!fstab) {
+ fstab = mnt_new_table();
+ if (!fstab)
+ return NULL;
+ mnt_table_set_cache(fstab, mntcache);
+ if (mnt_table_parse_fstab(fstab, NULL) != 0)
+ return NULL;
+ }
+
+ return fstab;
+}
+
+struct libmnt_table *get_swaps(void)
+{
+ if (!swaps) {
+ swaps = mnt_new_table();
+ if (!swaps)
+ return NULL;
+ mnt_table_set_cache(swaps, mntcache);
+ if (mnt_table_parse_swaps(swaps, NULL) != 0)
+ return NULL;
+ }
+
+ return swaps;
+}
+
+void free_tables(void)
+{
+ mnt_free_table(swaps);
+ mnt_free_table(fstab);
+}
+
+int match_swap(struct libmnt_fs *fs, void *data __attribute__((unused)))
+{
+ return fs && mnt_fs_is_swaparea(fs);
+}
+
+int is_active_swap(const char *filename)
+{
+ struct libmnt_table *st = get_swaps();
+ return st && mnt_table_find_srcpath(st, filename, MNT_ITER_BACKWARD);
+}
+
+
+int cannot_find(const char *special)
+{
+ warnx(_("cannot find the device for %s"), special);
+ return -1;
+}
+
+/*
+ * Lists with -L and -U option
+ */
+static const char **llist;
+static size_t llct;
+static const char **ulist;
+static size_t ulct;
+
+
+void add_label(const char *label)
+{
+ llist = (const char **) xrealloc(llist, (++llct) * sizeof(char *));
+ llist[llct - 1] = label;
+}
+
+const char *get_label(size_t i)
+{
+ return i < llct ? llist[i] : NULL;
+}
+
+size_t numof_labels(void)
+{
+ return llct;
+}
+
+void add_uuid(const char *uuid)
+{
+ ulist = (const char **) xrealloc(ulist, (++ulct) * sizeof(char *));
+ ulist[ulct - 1] = uuid;
+}
+
+const char *get_uuid(size_t i)
+{
+ return i < ulct ? ulist[i] : NULL;
+}
+
+size_t numof_uuids(void)
+{
+ return ulct;
+}
+
#include "nls.h"
#include "pathnames.h"
#include "swapheader.h"
-#include "mangle.h"
#include "xalloc.h"
#include "c.h"
#include "closestream.h"
+#include "swapon-common.h"
+
#define PATH_MKSWAP "/sbin/mkswap"
#ifdef HAVE_SYS_SWAP_H
static const struct option *longswapoffopts = &longswaponopts[4];
-static int cannot_find(const char *special);
-
#define PRINT_USAGE_SPECIAL(_fp) \
fputs(_("\nThe <spec> parameter:\n" \
" -L <label> LABEL of device to be used\n" \
exit(n);
}
-/*
- * contents of /proc/swaps
- */
-static struct libmnt_table *swaps, *fstab;
-static struct libmnt_cache *mntcache;
-
-static struct libmnt_table *get_fstab(void)
-{
- if (!fstab) {
- fstab = mnt_new_table();
- if (!fstab)
- return NULL;
- mnt_table_set_cache(fstab, mntcache);
- if (mnt_table_parse_fstab(fstab, NULL) != 0)
- return NULL;
- }
-
- return fstab;
-}
-
-static struct libmnt_table *get_swaps(void)
-{
- if (!swaps) {
- swaps = mnt_new_table();
- if (!swaps)
- return NULL;
- mnt_table_set_cache(swaps, mntcache);
- if (mnt_table_parse_swaps(swaps, NULL) != 0)
- return NULL;
- }
-
- return swaps;
-}
-
-static int match_swap(struct libmnt_fs *fs, void *data __attribute__((unused)))
-{
- return fs && mnt_fs_is_swaparea(fs);
-}
-
-static int is_active_swap(const char *filename)
-{
- struct libmnt_table *st = get_swaps();
- return st && mnt_table_find_srcpath(st, filename, MNT_ITER_BACKWARD);
-}
-
static int
display_summary(void)
{
return status;
}
-static int
-cannot_find(const char *special) {
- warnx(_("cannot find the device for %s"), special);
- return -1;
-}
-
static int
swapon_by_label(const char *label, int prio, int dsc) {
const char *special = mnt_resolve_tag("LABEL", label, mntcache);
return status;
}
-static const char **llist = NULL;
-static int llct = 0;
-static const char **ulist = NULL;
-static int ulct = 0;
-
-static void addl(const char *label) {
- llist = (const char **) xrealloc(llist, (++llct) * sizeof(char *));
- llist[llct-1] = label;
-}
-
-static void addu(const char *uuid) {
- ulist = (const char **) xrealloc(ulist, (++ulct) * sizeof(char *));
- ulist[ulct-1] = uuid;
-}
-
static int
main_swapon(int argc, char *argv[]) {
int status = 0;
- int c, i;
+ int c;
+ size_t i;
while ((c = getopt_long(argc, argv, "ahdefp:svVL:U:",
longswaponopts, NULL)) != -1) {
priority = atoi(optarg);
break;
case 'L':
- addl(optarg);
+ add_label(optarg);
break;
case 'U':
- addu(optarg);
+ add_uuid(optarg);
break;
case 'd':
discard = 1;
}
argv += optind;
- if (!all && !llct && !ulct && *argv == NULL)
+ if (!all && !numof_labels() && numof_uuids() && *argv == NULL)
swapon_usage(stderr, 2);
if (ifexists && (!all || strcmp(progname, "swapon")))
if (all)
status |= swapon_all();
- for (i = 0; i < llct; i++)
- status |= swapon_by_label(llist[i], priority, discard);
+ for (i = 0; i < numof_labels(); i++)
+ status |= swapon_by_label(get_label(i), priority, discard);
- for (i = 0; i < ulct; i++)
- status |= swapon_by_uuid(ulist[i], priority, discard);
+ for (i = 0; i < numof_uuids(); i++)
+ status |= swapon_by_uuid(get_uuid(i), priority, discard);
while (*argv != NULL)
status |= do_swapon(*argv++, priority, discard, !CANONIC);
FILE *fp;
struct mntent *fstab;
int status = 0;
- int c, i;
+ int c;
+ size_t i;
while ((c = getopt_long(argc, argv, "ahvVL:U:",
longswapoffopts, NULL)) != -1) {
printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
exit(EXIT_SUCCESS);
case 'L':
- addl(optarg);
+ add_label(optarg);
break;
case 'U':
- addu(optarg);
+ add_uuid(optarg);
break;
case 0:
break;
}
argv += optind;
- if (!all && !llct && !ulct && *argv == NULL)
+ if (!all && !numof_labels() && !numof_uuids() && *argv == NULL)
swapoff_usage(stderr, 2);
/*
* swapoff any explicitly given arguments.
* Complain in case the swapoff call fails.
*/
- for (i = 0; i < llct; i++)
- status |= swapoff_by_label(llist[i], !QUIET);
+ for (i = 0; i < numof_labels(); i++)
+ status |= swapoff_by_label(get_label(i), !QUIET);
- for (i = 0; i < ulct; i++)
- status |= swapoff_by_uuid(ulist[i], !QUIET);
+ for (i = 0; i < numof_uuids(); i++)
+ status |= swapoff_by_uuid(get_uuid(i), !QUIET);
while (*argv != NULL)
status |= do_swapoff(*argv++, !QUIET, !CANONIC);
errx(EXIT_FAILURE, _("'%s' is unsupported program name "
"(must be 'swapon' or 'swapoff')."), progname);
- mnt_free_table(swaps);
+ free_tables();
mnt_free_cache(mntcache);
return status;
}