Пример #1
0
void
expire_grp()
{
  int pos, fd;
  char fgrp[80], fold[80], fnew[80];
  SLOT grp;

  pos = 0;
  sprintf(fgrp, "2nd/%s", FN_GRP);
  sprintf(fnew, "2nd/%s.new", FN_GRP);
  fd = open(fnew, O_CREAT | O_TRUNC, 0600);	// touch a new file
  if(fd > 0) close(fd);

  while(rec_get(fgrp, &grp, sizeof(SLOT), pos) != -1)
  {
    if(grp.prop & PROP_G_CANCEL)
    {
      sprintf(fold, "2nd/%s", grp.fn);
      f_rm(fold);
    }
    else
    {
      rec_add(fnew, &grp, sizeof(SLOT));
    }
    pos++;
  }
  sprintf(fold, "2nd/%s.old", FN_GRP);
  f_cp(fgrp, fold, O_TRUNC);
  f_mv(fnew, fgrp);

  return;
}
Пример #2
0
Файл: fileio.c Проект: fbbs/fbbs
// Makes a hard link 'dst' pointing to 'src'.
// If hard link cannot be created, try to copy 'src' to 'dst'.
// Returns 0 on success, -1 on error.
int f_ln(const char *src, const char *dst)
{
	if (link(src, dst) != 0) {
		if (errno != EEXIST)
			return f_cp(src, dst, O_EXCL);
	}
	return 0;
}
Пример #3
0
/* Function `f_mv`
 *
 * Behavs: Move file `src` to file `dst`
 * Params:
 *   `src`: Name of the source file
 *   `dst`: Name of the destination file
 * Return:
 *       0: Success
 *   Other: Failed
 *
*/
int f_mv(const char *src,const char *dst) {
    if(!rename(src,dst))
        return 0;
    if(!f_cp(src,dst,0)) {
        unlink(src);
        return 0;
    }
    return -1;
}
Пример #4
0
int f_mv(char *src, char *dst)
{
  int ret;

  if ((ret = rename(src, dst)) != 0)
  {
    ret = f_cp(src, dst, O_TRUNC);
    if (!ret)
      unlink(src);
  }
  return ret;
}
Пример #5
0
Файл: tmpl.c Проект: zhouqt/kbs
/* make a deep copy of one specified template, caller guarantees permission
 * @to_board, from_board - board name, no NULL.
 **/
static int deepcopy(struct a_template *to,   const char *to_board,
                    struct a_template *from, const char *from_board)
{
    if (to == from)
        return -1;

    /* copy basic structure */
    if (!(to->tmpl = malloc(sizeof(struct s_template))))
        return -1;

    memcpy(to->tmpl, from->tmpl, sizeof(struct s_template));

    /* copy questions */
    if (to->tmpl->content_num > 0) {
        int size = (to->tmpl->content_num + 1) * sizeof(struct s_content);
        if (!(to->cont = malloc(size))) {
            free(to->tmpl); to->tmpl = NULL;
            return -1;
        }

        memcpy(to->cont, from->cont, size);

    } else {
        to->cont = NULL;
    }

    /* copy template text */
    if (from->tmpl->filename[0] != '\0') {
        char to_filepath[STRLEN];
        char from_filepath[STRLEN];

        setbfile(to_filepath, to_board, "");
        if (GET_POSTFILENAME(to->tmpl->filename, to_filepath) != 0) {
            free(to->tmpl); to->tmpl = NULL;
            free(to->cont); to->cont = NULL;
            return -2;
        }

        modify_user_mode(uinfo.mode);

        setbfile(to_filepath, to_board, to->tmpl->filename);
        setbfile(from_filepath, from_board, from->tmpl->filename);
        if (f_cp(from_filepath, to_filepath, 0)) {
            free(to->tmpl); to->tmpl = NULL;
            free(to->cont); to->cont = NULL;
            return -3;
        }
    }

    return 0;
}
Пример #6
0
int refriend(struct userec* user,char* arg)
{
   char buf[256],buf2[256];
   char cmd[1024];
   sethomefile(buf,user->userid,"friends");
   sethomefile(buf2,user->userid,"friends.sav");
   f_cp(buf,buf2,O_TRUNC);
   sethomefile(buf2,user->userid,"newfriends");
   newfd=open(buf2,O_WRONLY|O_TRUNC|O_CREAT,0644);
   apply_record(buf,(APPLY_FUNC_ARG)conver,sizeof(struct oldfriend),NULL,0,false);
   close(newfd);
   f_rm(buf);
   f_mv(buf2,buf);
}
Пример #7
0
void
expire_item()
{
  int pos, pos1, fd;
  char fgrp[80], fitem[80], fname[80], fold[80], fnew[80];
  time_t expire;
  SLOT grp, item;

  expire = time(0) - EXP * 86400;
  pos = pos1 = 0;
  sprintf(fgrp, "2nd/%s", FN_GRP);

  while(rec_get(fgrp, &grp, sizeof(SLOT), pos) != -1)
  {
    sprintf(fitem, "2nd/%s/%s", grp.fn, FN_ITEM);
    sprintf(fnew, "2nd/%s/%s.new", grp.fn, FN_ITEM);

    fd = open(fnew, O_CREAT | O_TRUNC, 0600);	// touch a new file
    if(fd > 0) close(fd);
    fd = 0;

    while(rec_get(fitem, &item, sizeof(SLOT), pos1) != -1)
    {
      if((item.prop & PROP_I_CANCEL) || (item.chrono < expire))
      {
        sprintf(fname, "2nd/%s/%c/%s", grp.fn, item.fn[7], item.fn);
        f_rm(fname);
      }
      else
      {
        fd++;
        rec_add(fnew, &item, sizeof(SLOT));
      }
      pos1++;
    }
    sprintf(fold, "2nd/%s/%s.old", grp.fn, FN_ITEM);
    f_cp(fitem, fold, O_TRUNC);
    f_mv(fnew, fitem);

    grp.reply = fd;
    rec_put(fgrp, &grp, sizeof(SLOT), pos);
    pos++;
  }

  return;
}
Пример #8
0
int main(int argc, char **argv)
{
    char mdir[256];
    char mdir_bak[256];
    char mdir_new[256];
    struct stat st;
    int fd;
    char touser[20];
    int len;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s fromuser\n", argv[0]);
        return -1;
    }
    chdir(BBSHOME);
    resolve_ucache();
    while (!feof(stdin)) {
        if (fgets(touser, sizeof(touser) - 1, stdin) == NULL)
            return -1;
        touser[sizeof(touser) - 1] = '\0';
        len = strlen(touser);
        if (touser[len - 1] == '\n')
            touser[len - 1] = '\0';
        setmailfile(mdir, touser, ".DIR");
        if (stat(mdir, &st) < 0) {
            fprintf(stderr, "User '%s' not found.\n", touser);
            continue;
        }
        sprintf(mdir_bak, "%s.BAK", mdir);
        f_cp(mdir, mdir_bak, 0);
        sprintf(mdir_new, "%s.NEW", mdir);
        if ((fd = open(mdir_new, O_RDWR | O_CREAT, 0644)) > 0) {
            delete_all_mail(fd, touser, mdir, sizeof(struct fileheader),
                            (RECORD_FUNC_ARG)cmpauthor, argv[1]);
            close(fd);
            f_mv(mdir_new, mdir);
        }
    }
    return 0;
}
Пример #9
0
/*合并版面处理*/
void process(char *prog,struct fileheader *ddir,struct fileheader *dir,unsigned int *offset,
             unsigned int dpos,unsigned int pos,char *dpath,char *dp,char *path,char *p)
{
    unsigned int ret;
    memcpy(&ddir[dpos],&dir[pos],sizeof(struct fileheader));
    /*存入当前对应id并分配新的顺序id*/
    offset[pos]=dpos;
    ddir[dpos].id=dpos+1;
    /*寻找对应的groupid*/
    if ((ret=getipos(dir,pos,ddir[dpos].groupid))!=(unsigned int)-1)/*groupid对应的fileheader存在*/
        ddir[dpos].groupid=offset[ret]+1;
    else if ((ret=getgpos(dir,pos,ddir[dpos].groupid))!=(unsigned int)-1)/*寻找向前最近的同主题fileheader*/
        ddir[dpos].groupid=ddir[offset[ret]].groupid;
    else
        ddir[dpos].groupid=ddir[dpos].id;/*设置groupid=id*/
    /*寻找对应的reid*/
    if (ddir[dpos].groupid==ddir[dpos].id)/*主题起始文章,reid=id*/
        ddir[dpos].reid=ddir[dpos].id;
    else if ((ret=getipos(dir,pos,ddir[dpos].reid))!=(unsigned int)-1)/*reid对应的fileheader存在*/
        ddir[dpos].reid=offset[ret]+1;
    else
        ddir[dpos].reid=ddir[dpos].id;/*设置reid=id*/
    /*对丢失主题起始文章而出现的新的主题起始文标题处理*/
    if (ddir[dpos].groupid==ddir[dpos].id&&!strncmp(ddir[dpos].title,"Re: ",4))
        memmove(ddir[dpos].title,ddir[dpos].title+4,ARTICLE_TITLE_LEN-4);
    sprintf(p,"%s",ddir[dpos].filename);
    sprintf(dp,"%s",ddir[dpos].filename);
#ifdef USE_HARD_LINK
    /*硬链接对应文件*/
    if (link(path,dpath)) {
        if (errno==EEXIST) {
            char *ptr;int i;
            ptr=ddir[dpos].filename+strlen(ddir[dpos].filename)-2;
            srand(getpid());
            /*失败的时候在同一时间点上更换后缀*/
            for (i=0;i<TRIES_ON_SAME_NAME;i++) {
                *ptr=suffix[rand()%62];*(ptr+1)=suffix[rand()%62];
                sprintf(dp,"%s",ddir[dpos].filename);
                if (!link(path,dpath)) {
                    printf("[%s] 更改重名硬链接: %s -> %s\n",prog,path,ddir[dpos].filename);
                    return;
                }
            }
        }
        printf("[%s] 硬链接 %d (%s) 失败...\n",prog,pos,path);
    }
#else
    /*复制对应文件*/
    if (f_cp(path,dpath,O_EXCL)==-1) {
        char *ptr;int i;
        ptr=ddir[dpos].filename+strlen(ddir[dpos].filename)-2;
        srand(getpid());
        /*失败的时候在同一时间点上更换后缀*/
        for (i=0;i<TRIES_ON_SAME_NAME;i++) {
            *ptr=suffix[rand()%62];*(ptr+1)=suffix[rand()%62];
            sprintf(dp,"%s",ddir[dpos].filename);
            if (f_cp(path,dpath,O_EXCL)!=-1) {
                printf("[%s] 更改重名文件: %s -> %s\n",prog,path,ddir[dpos].filename);
                return;
            }
        }
        printf("[%s] 复制 %d (%s) 失败...\n",prog,pos,path);
    }
#endif
    return;
}
Пример #10
0
/* Function `f_ln`
 *
 * Behavs: Hard Link/Copy file `src` to file `dst`
 * Params:
 *   `src`: Name of the source file
 *   `dst`: Name of the destination file
 * Return:
 *       0: Success
 *      -1: Failed for file read error
 *      -2: Failed for file `src` open error
 *      -4: Failed for file `dst` open error
 *      -8: Failed for memory allocation error
 *     -16: Failed for link error
 *       +: Failed for file write error
 *
*/
int f_ln(const char *src,const char *dst) {
    if(!link(src,dst))
        return 0;
    return ((errno==EMLINK||errno==EXDEV)?f_cp(src,dst,0):-16);
}
Пример #11
0
int scan_register_form(char* regfile)
{
    static char *field[] = {"usernum", "userid", "realname", "dept",
                            "addr", "phone", "regip", NULL
                           };
    static char *finfo[] = {"帐号位置", "申请帐号", "真实姓名", "学校系级",
                            "目前住址", "连络电话", "注册ip  ", NULL
                           };
    static char *reason[] = {"请确实填写真实姓名(不可使用拼音).",
                             "请详填学校院系与年级 (注明系和年级或者工作单位和工作部门).",
                             "请填写完整的住址资料 (完整详细,能够通信).",
                             "请详填连络电话 (固定电话需填写区号).",
                             "请勿穿梭注册.",
                             "请如实详细填写注册申请表.",
                             "请用中文填写申请表.",
                             "同一个用户注册了过多ID.", NULL
                            };
    struct userec uinfo;
    FILE   *fn, *fout, *freg;
    char    fdata[7][STRLEN];
    char    fname[STRLEN], buf[STRLEN];
    char    ans[5], *ptr, *uid;
    int     n, unum;
    uid = currentuser.userid;
    char numofreason[2];//tdhlshx for refuse.
    int total;
    int iis;
    int realnumofrea[8];//add end

    stand_title("依序设定所有新注册资料");
    sprintf(fname, "%s.tmp", regfile);
    move(2, 0);
    if (dashf(fname))
    {
        move(1, 0);
        prints("其他 SYSOP 正在查看注册申请单, 请检查使用者状态.\n");
        getdata(2, 0, "你确定没有其他 SYSOP 在审核注册单吗 ? [y/N]: ", ans, 2, DOECHO, YEA);
        if (ans[0] == 'Y' || ans[0] == 'y')
            f_cp(fname, regfile, O_APPEND);
        else
        {
            pressreturn();
            return -1;
        }
    }
    rename(regfile, fname);
    if ((fn = fopen(fname, "r")) == NULL)
    {
        move(2, 0);//请管理员输入未通过原因:
        prints("系统错误, 无法读取注册资料档: %s\n", fname);
        pressreturn();
        return -1;
    }
    memset(fdata, 0, sizeof(fdata));
    while (fgets(genbuf, STRLEN, fn) != NULL)
    {
        if ((ptr = (char *) strstr(genbuf, ": ")) != NULL)
        {
            *ptr = '\0';
            for (n = 0; field[n] != NULL; n++)
            {
                if (strcmp(genbuf, field[n]) == 0)
                {
                    strcpy(fdata[n], ptr + 2);
                    if ((ptr = (char *) strchr(fdata[n], '\n')) != NULL)
                        *ptr = '\0';
                }
            }
        }
        else if ((unum = getuser(fdata[1])) == 0)
        {
            move(2, 0);
            clrtobot();
            prints("系统错误, 查无此帐号.\n\n");
            for (n = 0; field[n] != NULL; n++)
                prints("%s     : %s\n", finfo[n], fdata[n]);
            pressreturn();
            memset(fdata, 0, sizeof(fdata));
        }
        else
        {
            memcpy(&uinfo, &lookupuser, sizeof(uinfo));
            move(1, 0);
            prints("帐号位置     : %d\n", unum);
            disply_userinfo(&uinfo);
            move(15, 0);
            printdash(NULL);
            for (n = 0; field[n] != NULL; n++)
                prints("%s     : %s\n", finfo[n], fdata[n]);
            if (uinfo.userlevel & PERM_LOGINOK)
            {
                move(t_lines - 1, 0);
                prints("此帐号不需再填写注册单.\n");
                igetkey();
                ans[0] = 'D';
            }
            else
            {
                getdata(t_lines - 1, 0, "是否接受此资料 (Y/N/Q/Del/Skip)? [S]: ",
                        ans, 3, DOECHO, YEA);
            }
            move(1, 0);
            clrtobot();
            switch (ans[0])
            {
            case 'D':
            case 'd':
                break;
            case 'Y':
            case 'y':
                prints("以下使用者资料已经更新:\n");
                n = strlen(fdata[5]);
                if (n + strlen(fdata[3]) > 60)
                {
                    if (n > 40)
                        fdata[5][n = 40] = '\0';
                    fdata[3][60 - n] = '\0';
                }
                strncpy(uinfo.realname, fdata[2], NAMELEN);
                strncpy(uinfo.address, fdata[4], NAMELEN);
                sprintf(genbuf, "%s$%s@%s", fdata[3], fdata[5], uid);
                genbuf[STRLEN - 16] = '\0';
                strncpy(uinfo.reginfo, genbuf, STRLEN - 17);
#ifdef ALLOWGAME

                uinfo.money = 1000;
#endif

                uinfo.lastjustify = time(0);
                substitute_record(PASSFILE, &uinfo, sizeof(uinfo), unum);
                sethomefile(buf, uinfo.userid, "register");
                if (dashf(buf))
                {
                    sethomefile(genbuf, uinfo.userid, "register.old");
                    rename(buf, genbuf);
                }
                if ((fout = fopen(buf, "w")) != NULL)
                {
                    for (n = 0; field[n] != NULL; n++)
                        fprintf(fout, "%s: %s\n", field[n], fdata[n]);
                    n = time(NULL);
                    getdatestring(n,NA);
                    fprintf(fout, "Date: %s\n", datestring);
                    fprintf(fout, "Approved: %s\n", uid);
                    fclose(fout);
                }
                mail_file("etc/s_fill", uinfo.userid, "恭禧你,你已经完成注册。");
                mail_file("etc/fornewcomers",uinfo.userid,"欢迎加入 "BBSNAME" 大家庭");//tdhlshx 新手FAQ信件
                sethomefile(buf, uinfo.userid, "mailcheck");
                unlink(buf);
                sprintf(genbuf, "让 %s 通过身份确认.", uinfo.userid);
                securityreport4(genbuf);
                break;
            case 'Q':
            case 'q':
                if ((freg = fopen(regfile, "a")) != NULL)
                {
                    for (n = 0; field[n] != NULL; n++)
                        fprintf(freg, "%s: %s\n", field[n], fdata[n]);
                    fprintf(freg, "----\n");
                    while (fgets(genbuf, STRLEN, fn) != NULL)
                        fputs(genbuf, freg);
                    fclose(freg);
                }
                break;
            case 'N':
            case 'n':
                for(total=0; total<8; total++)
                    realnumofrea[total]=0;
                for (n = 0; field[n] != NULL; n++)
                    prints("%s: %s\n", finfo[n], fdata[n]);
                printdash(NULL);
                move(9, 0);
                prints("请依次输入退回申请表原因的序号并回车确认,直接回车结束.\n\n");
                //	prints("请选择/输入退回申请表原因, 按 <enter> 取消.\n\n");
                for (n = 0; reason[n] != NULL; n++)
                    prints("%d) %s\n", n, reason[n]);
                getdata(12+n,0,"退回原因:",numofreason,2,DOECHO,YEA);
                //	getdata(12 + n, 0, "退回原因: ", buf, 60, DOECHO, YEA);
                if(numofreason[0]!='\0'&&numofreason[0]>='0'&&numofreason[0]<='7')
                {
                    total=atoi(numofreason);
                    realnumofrea[total]=1;
                    move(14+n,0);
                    prints("您已经选择的理由有:\n%d  ",total);
                }
                while(numofreason[0]!='\0')
                {
                    getdata(12+n,0,"退回原因:",numofreason,2,DOECHO,YEA);
                    if(numofreason[0]>='0'&&numofreason[0]<='7')
                    {
                        total=atoi(numofreason);
                        if(realnumofrea[total]==0)
                            realnumofrea[total]=1;
                        else
                            realnumofrea[total]=0;
                        move(14+n,0);
                        clrtobot();
                        prints("您已经选择的理由有:\n");
                        for(iis=0; iis<8; iis++)
                        {
                            if(realnumofrea[iis]==1)
                                prints("%d  ",iis);
                        }
                    }
                }
                system("cp /home/bbs/etc/f_fill  /home/bbs/tmp/fletter");
                char FL[STRLEN];
                sprintf(FL,"/home/bbs/tmp/fletter");
                FILE * fail=fopen(FL,"a");
                if(fail==NULL)
                {
                    prints("Error,创建临时文件错误.请与系统维护联系或再次尝试!");
                    pressanykey();
                    return -1;
                }

                {
                    fprintf(fail,"\n\n您被拒绝注册的原因大概如下:\n");
                    for(total=0; total<8; total++)
                    {
                        if(realnumofrea[total]==1)
                        {
                            strcpy(buf,reason[total]);
                            fprintf(fail,"\n%s",buf);
                        }
                    }
                    sprintf(genbuf,"很抱歉的告诉您,您这次注册失败.:(");
                    strncpy(uinfo.address, genbuf, NAMELEN);
                    substitute_record(PASSFILE, &uinfo, sizeof(uinfo), unum);
                    fclose(fail);
                    mail_file(FL,uinfo.userid, uinfo.address);
                    system("rm -f /home/bbs/tmp/fletter");

                }
                break;
            /*if (buf[0] != '\0') {
            if (buf[0] >= '0' && buf[0] < '0' + n) {
            	strcpy(buf, reason[buf[0] - '0']);
            }
            sprintf(genbuf, "<注册失败> - %s", buf);
            strncpy(uinfo.address, genbuf, NAMELEN);
            substitute_record(PASSFILE, &uinfo, sizeof(uinfo), unum);
            mail_file("etc/f_fill", uinfo.userid, uinfo.address);*/
            /* user_display( &uinfo, 1 ); */
            /* pressreturn(); */
            /*	break;
            }
            move(10, 0);
            clrtobot();
            prints("取消退回此注册申请表.\n");*/
            /* run default -- put back to regfile */
            default:
                if ((freg = fopen(regfile, "a")) != NULL)
                {
                    for (n = 0; field[n] != NULL; n++)
                        fprintf(freg, "%s: %s\n", field[n], fdata[n]);
                    fprintf(freg, "----\n");
                    fclose(freg);
                }
            }
            memset(fdata, 0, sizeof(fdata));
        }
    }
    fclose(fn);
    unlink(fname);
    return (0);
}