file.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * $Id: file.c,v 1.6 2008/12/20 18:35:27 bkorb Exp $
  3. * Time-stamp: "2008-12-06 10:15:33 bkorb"
  4. *
  5. * This file is part of AutoOpts, a companion to AutoGen.
  6. * AutoOpts is free software.
  7. * AutoOpts is copyright (c) 1992-2008 by Bruce Korb - all rights reserved
  8. * AutoOpts is copyright (c) 1992-2008 by Bruce Korb - all rights reserved
  9. *
  10. * AutoOpts is available under any one of two licenses. The license
  11. * in use must be one of these two and the choice is under the control
  12. * of the user of the license.
  13. *
  14. * The GNU Lesser General Public License, version 3 or later
  15. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  16. *
  17. * The Modified Berkeley Software Distribution License
  18. * See the file "COPYING.mbsd"
  19. *
  20. * These files have the following md5sums:
  21. *
  22. * 239588c55c22c60ffe159946a760a33e pkg/libopts/COPYING.gplv3
  23. * fa82ca978890795162346e661b47161a pkg/libopts/COPYING.lgplv3
  24. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  25. */
  26. /*=export_func optionFileCheck
  27. * private:
  28. *
  29. * what: Decipher a boolean value
  30. * arg: + tOptions* + pOpts + program options descriptor +
  31. * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
  32. * arg: + teOptFileType + ftype + File handling type +
  33. * arg: + tuFileMode + mode + file open mode (if needed) +
  34. *
  35. * doc:
  36. * Make sure the named file conforms with the file type mode.
  37. * The mode specifies if the file must exist, must not exist or may
  38. * (or may not) exist. The mode may also specify opening the
  39. * file: don't, open just the descriptor (fd), or open as a stream
  40. * (FILE* pointer).
  41. =*/
  42. void
  43. optionFileCheck(tOptions* pOpts, tOptDesc* pOD,
  44. teOptFileType ftype, tuFileMode mode)
  45. {
  46. if (pOpts <= OPTPROC_EMIT_LIMIT) {
  47. if (pOpts != OPTPROC_EMIT_USAGE)
  48. return;
  49. switch (ftype & FTYPE_MODE_EXIST_MASK) {
  50. case FTYPE_MODE_MUST_NOT_EXIST:
  51. fputs(zFileCannotExist, option_usage_fp);
  52. break;
  53. case FTYPE_MODE_MUST_EXIST:
  54. fputs(zFileMustExist, option_usage_fp);
  55. break;
  56. }
  57. return;
  58. }
  59. if ((pOD->fOptState & OPTST_RESET) != 0) {
  60. if (pOD->optCookie != NULL)
  61. AGFREE(pOD->optCookie);
  62. return;
  63. }
  64. {
  65. struct stat sb;
  66. errno = 0;
  67. switch (ftype & FTYPE_MODE_EXIST_MASK) {
  68. case FTYPE_MODE_MUST_NOT_EXIST:
  69. if ( (stat(pOD->optArg.argString, &sb) == 0)
  70. || (errno != ENOENT) ){
  71. if (errno == 0)
  72. errno = EINVAL;
  73. fprintf(stderr, zFSOptError, errno, strerror(errno),
  74. zFSOptErrNoExist, pOD->optArg.argString, pOD->pz_Name);
  75. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  76. /* NOTREACHED */
  77. }
  78. /* FALLTHROUGH */
  79. default:
  80. case FTYPE_MODE_MAY_EXIST:
  81. {
  82. char * p = strrchr(pOD->optArg.argString, DIRCH);
  83. if (p != NULL)
  84. *p = NUL;
  85. if ( (stat(pOD->optArg.argString, &sb) != 0)
  86. || (errno = EINVAL, ! S_ISDIR(sb.st_mode)) ){
  87. fprintf(stderr, zFSOptError, errno, strerror(errno),
  88. zFSOptErrMayExist, pOD->optArg.argString, pOD->pz_Name);
  89. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  90. /* NOTREACHED */
  91. }
  92. *p = '/';
  93. break;
  94. }
  95. case FTYPE_MODE_MUST_EXIST:
  96. if ( (stat(pOD->optArg.argString, &sb) != 0)
  97. || (errno = EINVAL, ! S_ISREG(sb.st_mode)) ){
  98. fprintf(stderr, zFSOptError, errno, strerror(errno),
  99. zFSOptErrMustExist, pOD->optArg.argString,
  100. pOD->pz_Name);
  101. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  102. /* NOTREACHED */
  103. }
  104. break;
  105. }
  106. }
  107. switch (ftype & FTYPE_MODE_OPEN_MASK) {
  108. default:
  109. case FTYPE_MODE_NO_OPEN:
  110. break;
  111. case FTYPE_MODE_OPEN_FD:
  112. {
  113. int fd = open(pOD->optArg.argString, mode.file_flags);
  114. if (fd < 0) {
  115. fprintf(stderr, zFSOptError, errno, strerror(errno),
  116. zFSOptErrOpen, pOD->optArg.argString, pOD->pz_Name);
  117. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  118. /* NOTREACHED */
  119. }
  120. if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
  121. pOD->optCookie = (void *)pOD->optArg.argString;
  122. else
  123. AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name");
  124. pOD->optArg.argFd = fd;
  125. pOD->fOptState &= ~OPTST_ALLOC_ARG;
  126. break;
  127. }
  128. case FTYPE_MODE_FOPEN_FP:
  129. {
  130. FILE* fp = fopen(pOD->optArg.argString, mode.file_mode);
  131. if (fp == NULL) {
  132. fprintf(stderr, zFSOptError, errno, strerror(errno),
  133. zFSOptErrFopen, pOD->optArg.argString, pOD->pz_Name);
  134. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  135. /* NOTREACHED */
  136. }
  137. if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
  138. pOD->optCookie = (void *)pOD->optArg.argString;
  139. else
  140. AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name");
  141. pOD->optArg.argFp = fp;
  142. pOD->fOptState &= ~OPTST_ALLOC_ARG;
  143. break;
  144. }
  145. }
  146. }
  147. /*
  148. * Local Variables:
  149. * mode: C
  150. * c-file-style: "stroustrup"
  151. * indent-tabs-mode: nil
  152. * End:
  153. * end of autoopts/file.c */