回精華區
作者itoc (站上人數:802), 信區: plan
標題[功能] 清除個人信箱中的垃圾檔案
時間2005/06/09 Thu 23:48:55 Updated: 2005/06/09
: src/util/Makefile

EXE =   ..... rmtrash

: src/util/rmtrash.c 新增此程式

/*-------------------------------------------------------*/
/* util/rmtrash.c       ( NTHU CS MapleBBS Ver 3.00 )    */
/*-------------------------------------------------------*/
/* target : 清除個人信箱中的垃圾檔案                     */
/* create : 01/09/25                                     */
/* update :   /  /                                       */
/* author : itoc.bbs@bbs.tnfsh.tn.edu.tw                 */
/*-------------------------------------------------------*/


#include "bbs.h"


static int         /* 1: 在 .DIR 中  0: 不在 .DIR 中 */
find_dir(folder, xname)
  char *folder;
  char *xname;
{
  HDR hdr;
  int fd;
  int rc = 0;

  if ((fd = open(folder, O_RDONLY)) >= 0)
  {
    while (read(fd, &hdr, sizeof(HDR)) == sizeof(HDR))
    {
      if (!strcmp(hdr.xname, xname))
      {
        rc = 1;
        break;
      }
    }
    close(fd);
  }
  return rc;
}


static void
reaper(userid)
  char *userid;
{
  int num;
  char *fname, home[64], folder[64], fpath[64];
  struct stat st;
  struct dirent *de;
  DIR *dirp;

  usr_fpath(home, userid, "@");

  if (!(dirp = opendir(home)))
  {
    printf("No such ID: %s\n", userid);
    return;
  }

  usr_fpath(folder, userid, FN_DIR);
  num = 0;

  while (de = readdir(dirp))
  {
    fname = de->d_name;
    if (*fname <= ' ' || *fname == '.')
      continue;

    sprintf(fpath, "%s/%s", home, fname);

    if ((!stat(fpath, &st) && (st.st_size == 0)) ||
      !find_dir(folder, fname)) /* size == 0 或 不在 .DIR 中 */
    {
      unlink(fpath);
      num++;
    }
  }

  printf("%s:總共清除了 %d 個檔案\n", userid, num);
}


int
main(argc, argv)
  int argc;
  char *argv[];
{
  char c, *userid, buf[64];
  struct dirent *de;
  DIR *dirp;

  if (argc > 2)
  {
    printf("Usage: %s [userid]\n", argv[0]);
    return -1;
  }

  chdir(BBSHOME);

  for (c = 'a'; c <= 'z'; c++)
  {
    sprintf(buf, "usr/%c", c);

    if (!(dirp = opendir(buf)))
      continue;

    while (de = readdir(dirp))
    {
      userid = de->d_name;
      if (*userid <= ' ' || *userid == '.')
        continue;

      if ((argc == 2) && str_cmp(userid, argv[1]))
        continue;

      reaper(userid);
    }

    closedir(dirp);
  }

  return 0;
}

--
  這只是 bquota 的簡化版

--
□ 本文章由 itocitoc.Dorm11.NCTU.edu.tw 發表
精華選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻