caesar.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* This program outputs a file (or stdin) in a caesar cipher.
  2. *
  3. * Copyright @ 2020 Joshua Branson <jbranso@dismail.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * It is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "encrypt.h"
  19. #define DEFAULT_SHIFT 1
  20. char fileName [128];
  21. short int shift = DEFAULT_SHIFT;
  22. static const struct argp_option options [] =
  23. {
  24. {"file" , 'f', "FILE", 0, "Output the caesar cipher of FILE." },
  25. {"shift", 's', "INT", 0, "Specify the shift of the cipher." },
  26. { 0 }
  27. };
  28. //define an argp parse function
  29. error_t argp_parser (int opt, char *arg, struct argp_state *state)
  30. {
  31. extern char fileName [];
  32. extern short int shift;
  33. switch (opt)
  34. {
  35. // if this parser function is called on an option that it doesn't recognize, then don't do anything.
  36. default:
  37. return ARGP_ERR_UNKNOWN;
  38. case 'f':
  39. {
  40. memcpy (fileName, arg, strlen (arg));
  41. break;
  42. }
  43. case 's':
  44. {
  45. shift = abs ((int) strtol (arg, NULL, 0));
  46. if (shift >= 26)
  47. shift = shift % 26;
  48. break;
  49. }
  50. }
  51. return 0;
  52. }
  53. /* a string containing the basic usage of this program. */
  54. struct argp argp =
  55. {
  56. options, argp_parser, 0,
  57. "A simple program implementing a caesar cipher.\nThe default shift is 1."
  58. };
  59. int main (int argc, char **argv)
  60. {
  61. extern short int shift;
  62. argp_parse (&argp, argc, argv, 0, 0, 0);
  63. FILE * stream = maybe_open_file ();
  64. encrypt (shift, stream);
  65. fclose (stream);
  66. return 0;
  67. }