maildirdelfolder.c
上传用户:s81996212
上传日期:2007-01-04
资源大小:722k
文件大小:2k
源码类别:

WEB邮件程序

开发平台:

C/C++

  1. /*
  2. ** Copyright 2000 Double Precision, Inc.
  3. ** See COPYING for distribution information.
  4. */
  5. #if HAVE_CONFIG_H
  6. #include "config.h"
  7. #endif
  8. #include <sys/types.h>
  9. #if HAVE_DIRENT_H
  10. #include <dirent.h>
  11. #define NAMLEN(dirent) strlen((dirent)->d_name)
  12. #else
  13. #define dirent direct
  14. #define NAMLEN(dirent) (dirent)->d_namlen
  15. #if HAVE_SYS_NDIR_H
  16. #include <sys/ndir.h>
  17. #endif
  18. #if HAVE_SYS_DIR_H
  19. #include <sys/dir.h>
  20. #endif
  21. #if HAVE_NDIR_H
  22. #include <ndir.h>
  23. #endif
  24. #endif
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <time.h>
  30. #if HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #include <stdio.h>
  34. #include <ctype.h>
  35. #include <errno.h>
  36. #include "maildirmisc.h"
  37. static int delsubdir(const char *s)
  38. {
  39. DIR *dirp;
  40. struct dirent *de;
  41. char *p;
  42. dirp=opendir(s);
  43. while (dirp && (de=readdir(dirp)) != 0)
  44. {
  45. if (strcmp(de->d_name, ".") == 0 ||
  46. strcmp(de->d_name, "..") == 0)
  47. continue;
  48. p=malloc(strlen(s)+strlen(de->d_name)+2);
  49. if (!p)
  50. {
  51. if (dirp) closedir(dirp);
  52. return (-1);
  53. }
  54. strcat(strcat(strcpy(p, s), "/"), de->d_name);
  55. unlink(p);
  56. free(p);
  57. }
  58. if (dirp) closedir(dirp);
  59. rmdir(s);
  60. return (0);
  61. }
  62. int maildir_mddelete(const char *s)
  63. {
  64. char *q;
  65. q=malloc(strlen(s)+sizeof("/cur"));
  66. if (!q) return (-1);
  67. strcat(strcpy(q, s), "/cur");
  68. if (delsubdir(q) == 0)
  69. {
  70. strcat(strcpy(q, s), "/new");
  71. if (delsubdir(q) == 0)
  72. {
  73. strcat(strcpy(q, s), "/tmp");
  74. if ( delsubdir(q) == 0 && delsubdir(s) == 0)
  75. {
  76. free(q);
  77. return (0);
  78. }
  79. }
  80. }
  81. free(q);
  82. return (-1);
  83. }
  84. int maildir_deletefolder(const char *maildir, const char *folder)
  85. {
  86. char *s;
  87. int rc;
  88. if (*folder == '.')
  89. {
  90. errno=EINVAL;
  91. return (-1);
  92. }
  93. s=malloc(strlen(maildir)+strlen(folder)+3);
  94. if (!s) return (-1);
  95. strcat(strcat(strcpy(s, maildir), "/."), folder);
  96. rc=maildir_mddelete(s);
  97. free(s);
  98. return (rc);
  99. }