patch-uulib_uunconc_c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. $OpenBSD: patch-uulib_uunconc_c,v 1.1.1.1 2014/11/20 21:14:54 jca Exp $
  2. - mkstemp(3) support
  3. - Debian patch
  4. --- uulib/uunconc.c.orig Mon Mar 1 23:52:27 2004
  5. +++ uulib/uunconc.c Mon Jan 27 21:44:47 2014
  6. @@ -1311,6 +1311,11 @@ UUDecode (uulist *data)
  7. char *mode, *ntmp;
  8. uufile *iter;
  9. size_t bytes;
  10. +#ifdef HAVE_MKSTEMP
  11. + int tmpfd;
  12. + const char *tmpprefix = "uuXXXXXXXXXX";
  13. + char *tmpdir = NULL;
  14. +#endif /* HAVE_MKSTEMP */
  15. if (data == NULL || data->thisfile == NULL)
  16. return UURET_ILLVAL;
  17. @@ -1329,13 +1334,35 @@ UUDecode (uulist *data)
  18. else
  19. mode = "wb"; /* otherwise in binary */
  20. +#ifdef HAVE_MKSTEMP
  21. + if ((getuid()==geteuid()) && (getgid()==getegid())) {
  22. + tmpdir=getenv("TMPDIR");
  23. + }
  24. +
  25. + if (!tmpdir) {
  26. + tmpdir = "/tmp";
  27. + }
  28. + data->binfile = malloc(strlen(tmpdir)+strlen(tmpprefix)+2);
  29. +
  30. + if (!data->binfile) {
  31. +#else
  32. if ((data->binfile = tempnam (NULL, "uu")) == NULL) {
  33. +#endif /* HAVE_MKSTEMP */
  34. UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
  35. uustring (S_NO_TEMP_NAME));
  36. return UURET_NOMEM;
  37. }
  38. +#ifdef HAVE_MKSTEMP
  39. + strcpy(data->binfile, tmpdir);
  40. + strcat(data->binfile, "/");
  41. + strcat(data->binfile, tmpprefix);
  42. +
  43. + if ((tmpfd = mkstemp(data->binfile)) == -1 ||
  44. + (dataout = fdopen(tmpfd, mode)) == NULL) {
  45. +#else
  46. if ((dataout = fopen (data->binfile, mode)) == NULL) {
  47. +#endif /* HAVE_MKSTEMP */
  48. /*
  49. * we couldn't create a temporary file. Usually this means that TMP
  50. * and TEMP aren't set
  51. @@ -1343,6 +1370,12 @@ UUDecode (uulist *data)
  52. UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
  53. uustring (S_WR_ERR_TARGET),
  54. data->binfile, strerror (uu_errno = errno));
  55. +#ifdef HAVE_MKSTEMP
  56. + if (tmpfd != -1) {
  57. + unlink(data->binfile);
  58. + close(tmpfd);
  59. + }
  60. +#endif /* HAVE_MKSTEMP */
  61. _FP_free (data->binfile);
  62. data->binfile = NULL;
  63. uu_errno = errno;
  64. @@ -1437,6 +1470,9 @@ UUDecode (uulist *data)
  65. res = UURET_IOERR;
  66. break;
  67. }
  68. + UUMessage (uunconc_id, __LINE__, UUMSG_MESSAGE,
  69. + uustring (S_OPEN_FILE),
  70. + iter->data->sfname);
  71. _FP_strncpy (uugen_fnbuffer, iter->data->sfname, 1024);
  72. }
  73. @@ -1496,7 +1532,13 @@ UUDecode (uulist *data)
  74. */
  75. if (data->uudet == BH_ENCODED && data->binfile) {
  76. +#ifdef HAVE_MKSTEMP
  77. + ntmp = malloc(strlen(tmpdir)+strlen(tmpprefix)+2);
  78. +
  79. + if (ntmp == NULL) {
  80. +#else
  81. if ((ntmp = tempnam (NULL, "uu")) == NULL) {
  82. +#endif /* HAVE_MKSTEMP */
  83. UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
  84. uustring (S_NO_TEMP_NAME));
  85. progress.action = 0;
  86. @@ -1510,12 +1552,27 @@ UUDecode (uulist *data)
  87. free (ntmp);
  88. return UURET_IOERR;
  89. }
  90. +
  91. +#ifdef HAVE_MKSTEMP
  92. + strcpy(ntmp, tmpdir);
  93. + strcat(ntmp, "/");
  94. + strcat(ntmp, tmpprefix);
  95. + if ((tmpfd = mkstemp(ntmp)) == -1 ||
  96. + (dataout = fdopen(tmpfd, "wb")) == NULL) {
  97. +#else
  98. if ((dataout = fopen (ntmp, "wb")) == NULL) {
  99. +#endif /* HAVE_MKSTEMP */
  100. UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
  101. uustring (S_NOT_OPEN_TARGET),
  102. ntmp, strerror (uu_errno = errno));
  103. progress.action = 0;
  104. fclose (datain);
  105. +#ifdef HAVE_MKSTEMP
  106. + if (tmpfd != -1) {
  107. + unlink(ntmp);
  108. + close(tmpfd);
  109. + }
  110. +#endif /* HAVE_MKSTEMP */
  111. free (ntmp);
  112. return UURET_IOERR;
  113. }