file.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * $Id: file.c,v 4.9 2009/08/01 17:43:06 bkorb Exp $
  3. * Time-stamp: "2009-07-23 17:23:46 bkorb"
  4. *
  5. * This file is part of AutoOpts, a companion to AutoGen.
  6. * AutoOpts is free software.
  7. * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
  8. *
  9. * AutoOpts is available under any one of two licenses. The license
  10. * in use must be one of these two and the choice is under the control
  11. * of the user of the license.
  12. *
  13. * The GNU Lesser General Public License, version 3 or later
  14. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  15. *
  16. * The Modified Berkeley Software Distribution License
  17. * See the file "COPYING.mbsd"
  18. *
  19. * These files have the following md5sums:
  20. *
  21. * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
  22. * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
  23. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  24. */
  25. /*=export_func optionFileCheck
  26. * private:
  27. *
  28. * what: Decipher a boolean value
  29. * arg: + tOptions* + pOpts + program options descriptor +
  30. * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
  31. * arg: + teOptFileType + ftype + File handling type +
  32. * arg: + tuFileMode + mode + file open mode (if needed) +
  33. *
  34. * doc:
  35. * Make sure the named file conforms with the file type mode.
  36. * The mode specifies if the file must exist, must not exist or may
  37. * (or may not) exist. The mode may also specify opening the
  38. * file: don't, open just the descriptor (fd), or open as a stream
  39. * (FILE* pointer).
  40. =*/
  41. void
  42. optionFileCheck(tOptions* pOpts, tOptDesc* pOD,
  43. teOptFileType ftype, tuFileMode mode)
  44. {
  45. if (pOpts <= OPTPROC_EMIT_LIMIT) {
  46. if (pOpts != OPTPROC_EMIT_USAGE)
  47. return;
  48. switch (ftype & FTYPE_MODE_EXIST_MASK) {
  49. case FTYPE_MODE_MUST_NOT_EXIST:
  50. fputs(zFileCannotExist, option_usage_fp);
  51. break;
  52. case FTYPE_MODE_MUST_EXIST:
  53. fputs(zFileMustExist, option_usage_fp);
  54. break;
  55. }
  56. return;
  57. }
  58. if ((pOD->fOptState & OPTST_RESET) != 0) {
  59. if (pOD->optCookie != NULL)
  60. AGFREE(pOD->optCookie);
  61. return;
  62. }
  63. {
  64. struct stat sb;
  65. errno = 0;
  66. switch (ftype & FTYPE_MODE_EXIST_MASK) {
  67. case FTYPE_MODE_MUST_NOT_EXIST:
  68. if ( (stat(pOD->optArg.argString, &sb) == 0)
  69. || (errno != ENOENT) ){
  70. if (errno == 0)
  71. errno = EINVAL;
  72. fprintf(stderr, zFSOptError, errno, strerror(errno),
  73. zFSOptErrNoExist, pOD->optArg.argString, pOD->pz_Name);
  74. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  75. /* NOTREACHED */
  76. }
  77. /* FALLTHROUGH */
  78. default:
  79. case FTYPE_MODE_MAY_EXIST:
  80. {
  81. char * p = strrchr(pOD->optArg.argString, DIRCH);
  82. if (p != NULL)
  83. *p = NUL;
  84. if ( (stat(pOD->optArg.argString, &sb) != 0)
  85. || (errno = EINVAL, ! S_ISDIR(sb.st_mode)) ){
  86. fprintf(stderr, zFSOptError, errno, strerror(errno),
  87. zFSOptErrMayExist, pOD->optArg.argString, pOD->pz_Name);
  88. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  89. /* NOTREACHED */
  90. }
  91. if (p != NULL)
  92. *p = DIRCH;
  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 */