home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.2 / util-lin / util-linux-2.2 / mount / swapon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  1.9 KB  |  110 lines

  1. /*
  2.  * A swapon(8)/swapoff(8) for Linux 0.99.
  3.  * swapon.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
  4.  */
  5.  
  6. #include "sundries.h"
  7.  
  8. /* Nonzero for chatty (-v).  This is a nonstandard flag (not in BSD).  */
  9. int verbose = 0;
  10.  
  11. extern char version[];
  12. static char *program_name;
  13. static struct option longopts[] =
  14. {
  15.   { "all", 0, 0, 'a' },
  16.   { "help", 0, 0, 'h' },
  17.   { "verbose", 0, 0, 'v' },
  18.   { "version", 0, 0, 'V' },
  19.   { NULL, 0, 0, 0 }
  20. };
  21.  
  22. const char *usage_string = "\
  23. usage: %s [-hV]\n\
  24.        %s -a [-v]\n\
  25.        %s [-v] special ...\n\
  26. ";
  27.  
  28. static void
  29. usage (FILE *fp, int n)
  30. {
  31.   fprintf (fp, usage_string, program_name, program_name, program_name);
  32.   exit (n);
  33. }
  34.  
  35. static int
  36. swap (const char *special)
  37. {
  38.   int status;
  39.  
  40.   if (verbose)
  41.     printf("%s on device %s\n", program_name, special);
  42.  
  43.   if (streq (program_name, "swapon"))
  44.     status = swapon (special);
  45.   else
  46.     status = swapoff (special);
  47.  
  48.   if (status < 0)
  49.     fprintf (stderr, "%s: %s: %s\n", program_name, special, strerror (errno));
  50.  
  51.   return status;
  52. }
  53.  
  54. int
  55. main (int argc, char *argv[])
  56. {
  57.   struct fstab *fstab;
  58.   int status;
  59.   int all = 0;
  60.   int c;
  61.  
  62.   if (strrchr (argv[0], '/') != NULL)
  63.     program_name = strrchr (argv[0], '/') + 1;
  64.   else
  65.     program_name = argv[0];
  66.  
  67.   while ((c = getopt_long (argc, argv, "ahvV", longopts, NULL)) != EOF)
  68.     switch (c)
  69.       {
  70.       case 'a':            /* all */
  71.     ++all;
  72.     break;
  73.       case 'h':            /* help */
  74.     usage (stdout, 0);
  75.     break;
  76.       case 'v':            /* be chatty */
  77.     ++verbose;
  78.     break;
  79.       case 'V':            /* version */
  80.     printf ("%s\n", version);
  81.     exit (0);
  82.       case 0:
  83.     break;
  84.       case '?':
  85.       default:
  86.     usage (stderr, 1);
  87.       }
  88.  
  89.   argv += optind;
  90.  
  91.   status = 0;
  92.  
  93.   if (all)
  94.     {
  95.       while ((fstab = getfsent()) != NULL)
  96.     if (streq (fstab->fs_type, FSTAB_SW))
  97.       status |= swap (fstab->fs_spec);
  98.     }
  99.   else if (*argv == NULL)
  100.     {
  101.       usage (stderr, 2);
  102.     }
  103.   else
  104.     {
  105.       while (*argv != NULL)
  106.     status |= swap (*argv++);
  107.     }
  108.   return status;
  109. }
  110.