1
0

mv.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*******************************************************************************
  2. * Example source code for using the argtable3 library to implement:
  3. *
  4. * mv [-bfiuv] [--backup=[CONTROL]] [--reply={yes,no,query}]
  5. * [--strip-trailing-slashes] [-S SUFFIX] [--target-directory=DIRECTORY]
  6. * [--help] [--version] SOURCE [SOURCE]... DEST|DIRECTORY
  7. *
  8. * This file is part of the argtable3 library.
  9. *
  10. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  11. * <sheitmann@users.sourceforge.net>
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  29. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  32. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. ******************************************************************************/
  36. #include "argtable3.h"
  37. int mymain(const char *backup_control,
  38. int backup,
  39. int force,
  40. int interactive,
  41. const char *reply,
  42. int strip_trailing_slashes,
  43. const char *suffix,
  44. const char *targetdir,
  45. int update,
  46. int verbose,
  47. const char **files,
  48. int nfiles)
  49. {
  50. int j;
  51. /* if verbose option was given then display all option settings */
  52. if (verbose)
  53. {
  54. printf("backup = %s\n", ((backup)?"YES":"NO"));
  55. printf("backup CONTROL = %s\n", backup_control);
  56. printf("force = %s\n", ((force)?"YES":"NO"));
  57. printf("interactive mode = %s\n", ((interactive)?"YES":"NO"));
  58. printf("reply = %s\n", reply);
  59. printf("strip-trailing-slashes = %s\n", ((strip_trailing_slashes)?"YES":"NO"));
  60. printf("suffix = %s\n", suffix);
  61. printf("target-directory = %s\n", targetdir);
  62. printf("update = %s\n", ((update)?"YES":"NO"));
  63. printf("verbose = %s\n", ((verbose)?"YES":"NO"));
  64. }
  65. /* print the source filenames */
  66. for (j=0; j<nfiles; j++)
  67. printf("file[%d] = \"%s\"\n", j, files[j]);
  68. return 0;
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. const char *progname = "mv";
  73. struct arg_str *backupc = arg_str0(NULL, "backup", "[CONTROL]", "make a backup of each existing destination file");
  74. struct arg_lit *backup = arg_lit0("b", NULL, "like --backup but does not accept an argument");
  75. struct arg_lit *force = arg_lit0("f", "force", "do not prompt before overwriting");
  76. struct arg_rem *force1 = arg_rem (NULL, " equivalent to --reply=yes");
  77. struct arg_lit *interact = arg_lit0("i", "interactive", "Prompt before overwriting");
  78. struct arg_rem *interact1= arg_rem (NULL, " equivalent to --reply=yes");
  79. struct arg_str *reply = arg_str0(NULL,"reply", "{yes,no,query}", "specify how to handle the prompt about an");
  80. struct arg_rem *reply1 = arg_rem (NULL, " existing destination file");
  81. struct arg_lit *strpslsh = arg_lit0(NULL,"strip-trailing-slashes", "remove any trailing slashes from each SOURCE argument");
  82. struct arg_str *suffix = arg_str0("S", "suffix", "SUFFIX", "override the usual backup suffix");
  83. struct arg_str *targetd = arg_str0(NULL,"target-directory", "DIRECTORY", "move all SOURCE arguments into DIRECTORY");
  84. struct arg_lit *update = arg_lit0("u", "update", "copy only when the SOURCE file is newer");
  85. struct arg_rem *update1 = arg_rem (NULL, " than the destination file or when the");
  86. struct arg_rem *update2 = arg_rem (NULL, " destination file is missing");
  87. struct arg_lit *verbose = arg_lit0("v", "verbose", "explain what is being done");
  88. struct arg_lit *help = arg_lit0(NULL,"help", "display this help and exit");
  89. struct arg_lit *version = arg_lit0(NULL,"version", "display version information and exit");
  90. struct arg_file *files = arg_filen(NULL, NULL, "SOURCE", 1, argc+2, NULL);
  91. struct arg_rem *dest = arg_rem ("DEST|DIRECTORY", NULL);
  92. struct arg_end *end = arg_end(20);
  93. void* argtable[] = {backupc,backup,force,force1,interact,interact1,reply,reply1,strpslsh,suffix,targetd,update,update1,update2,verbose,help,version,files,dest,end};
  94. int exitcode=0;
  95. int nerrors;
  96. /* verify the argtable[] entries were allocated sucessfully */
  97. if (arg_nullcheck(argtable) != 0)
  98. {
  99. /* NULL entries were detected, some allocations must have failed */
  100. printf("%s: insufficient memory\n",progname);
  101. exitcode=1;
  102. goto exit;
  103. }
  104. /* Set default argument values prior to parsing */
  105. backupc->sval[0] = "existing"; /* --backup={none,off,numbered,t,existing,nil,simple,never} */
  106. suffix->sval[0] = "~"; /* --suffix=~ */
  107. reply->sval[0] = "query"; /* --reply={yes,no,query} */
  108. targetd->sval[0] = NULL;
  109. /* Parse the command line as defined by argtable[] */
  110. nerrors = arg_parse(argc,argv,argtable);
  111. /* special case: '--help' takes precedence over error reporting */
  112. if (help->count > 0)
  113. {
  114. printf("Usage: %s", progname);
  115. arg_print_syntax(stdout,argtable,"\n");
  116. printf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n\n");
  117. arg_print_glossary(stdout,argtable," %-30s %s\n");
  118. printf("\nThe backup suffix is \"~\", unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n"
  119. "The version control method may be selected via the --backup option or through\n"
  120. "the VERSION_CONTROL environment variable. Here are the values:\n\n"
  121. " none, off never make backups (even if --backup is given)\n"
  122. " numbered, t make numbered backups\n"
  123. " existing, nil numbered if numbered backups exist, simple otherwise\n"
  124. " simple, never always make simple backups\n\n"
  125. "Report bugs to <foo@bar>.\n");
  126. exitcode=0;
  127. goto exit;
  128. }
  129. /* special case: '--version' takes precedence error reporting */
  130. if (version->count > 0)
  131. {
  132. printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname);
  133. printf("September 2003, Stewart Heitmann\n");
  134. exitcode=0;
  135. goto exit;
  136. }
  137. /* If the parser returned any errors then display them and exit */
  138. if (nerrors > 0)
  139. {
  140. /* Display the error details contained in the arg_end struct.*/
  141. arg_print_errors(stdout,end,progname);
  142. printf("Try '%s --help' for more information.\n",progname);
  143. exitcode=1;
  144. goto exit;
  145. }
  146. /* Command line parsing is complete, do the main processing */
  147. exitcode = mymain(backupc->sval[0],
  148. backup->count,
  149. force->count,
  150. interact->count,
  151. reply->sval[0],
  152. strpslsh->count,
  153. suffix->sval[0],
  154. targetd->sval[0],
  155. update->count,
  156. verbose->count,
  157. files->filename,
  158. files->count);
  159. exit:
  160. /* deallocate each non-null entry in argtable[] */
  161. arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
  162. return exitcode;
  163. }