summaryrefslogtreecommitdiffstats
path: root/mbbsd/kaede.c
blob: 3f284adb7cd09f3641cff1da8c1d1db282b46da6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* $Id$ */
#include "bbs.h"

char           *
Ptt_prints(char *str, size_t size, int mode)
{
    char           *strbuf = alloca(size);
    int             r, w;
    for( r = w = 0 ; str[r] != 0 && w < (size - 1) ; ++r )
    if( str[r] != ESC_CHR )
        strbuf[w++] = str[r];
    else{
        if( str[++r] != '*' ){
        if(w+2>=size-1) break;
        strbuf[w++] = ESC_CHR;
        strbuf[w++] = str[r];
        }
        else{
        /* Note, w will increased by copied length after */
        switch( str[++r] ){
        case 's':
            strlcpy(strbuf+w, cuser.userid, size-w);
            w += strlen(strbuf+w);
            break;
        case 't':
            strlcpy(strbuf+w, Cdate(&now), size-w);
            w += strlen(strbuf+w);
            break;
        case 'u':
            w += snprintf(&strbuf[w], size - w,
                  "%d", SHM->UTMPnumber);
            break;

            /* disabled for security issue.
             * we support only entries can be queried by others now.
             */
#ifdef LOW_SECURITY
        case 'b':
            w += snprintf(&strbuf[w], size - w,
                  "%d/%d", cuser.month, cuser.day);
            break;
        case 'm':
            w += snprintf(&strbuf[w], size - w,
                  "%d", cuser.money);
            break;
#else

#if 0
        case 'm':
            w += snprintf(&strbuf[w], size - w,
                  "%s", money_level(cuser.money));
            break;
#endif

#endif

        case 'l':
            w += snprintf(&strbuf[w], size - w,
                  "%d", cuser.numlogins);
            break;
        case 'p':
            w += snprintf(&strbuf[w], size - w,
                  "%d", cuser.numposts);
            break;
        case 'n':
            strlcpy(strbuf+w, cuser.nickname, size-w);
            w += strlen(strbuf+w);
            break;
        /* It's saver not to send these undefined escape string. 
        default:
            strbuf[w++] = ESC_CHR;
            strbuf[w++] = '*';
            strbuf[w++] = str[r];
            */
        }
        }
    }
    strbuf[w] = 0;
    strip_ansi(str, strbuf, mode);
    return str;
}

int
Rename(const char *src, const char *dst)
{
    char            buf[256];
    if (rename(src, dst) == 0)
    return 0;
    if (!strchr(src, ';') && !strchr(dst, ';'))
    // Ptt 防不正常指令 // XXX 這樣是不夠的
    {
    snprintf(buf, sizeof(buf), "/bin/mv %s %s", src, dst);
    system(buf);
    }
    return -1;
}

int
Copy(const char *src, const char *dst)
{
    int fi, fo, bytes;
    char buf[8192];
    fi=open(src, O_RDONLY);
    if(fi<0) return -1;
    fo=open(dst, O_WRONLY | O_TRUNC | O_CREAT, 0600);
    if(fo<0) {close(fi); return -1;}
    while((bytes=read(fi, buf, sizeof(buf)))>0)
         write(fo, buf, bytes);
    close(fo);
    close(fi);
    return 0;  
}

int
CopyN(const char *src, const char *dst, int n)
{
    int fi, fo, bytes;
    char buf[8192];

    fi=open(src, O_RDONLY);
    if(fi<0) return -1;

    fo=open(dst, O_WRONLY | O_TRUNC | O_CREAT, 0600);
    if(fo<0) {close(fi); return -1;}

    while(n > 0 && (bytes=read(fi, buf, sizeof(buf)))>0)
    {
     n -= bytes;
     if (n < 0)
         bytes += n;
         write(fo, buf, bytes);
    }
    close(fo);
    close(fi);
    return 0;  
}

/* append data from tail of src (starting point=off) to dst */
int
AppendTail(const char *src, const char *dst, int off)
{
    int fi, fo, bytes;
    char buf[8192];

    fi=open(src, O_RDONLY);
    if(fi<0) return -1;

    fo=open(dst, O_WRONLY | O_APPEND | O_CREAT, 0600);
    if(fo<0) {close(fi); return -1;}
    
    if(off > 0)
    lseek(fi, (off_t)off, SEEK_SET);

    while((bytes=read(fi, buf, sizeof(buf)))>0)
    {
         write(fo, buf, bytes);
    }
    close(fo);
    close(fi);
    return 0;  
}

int
Link(const char *src, const char *dst)
{
    if (strcmp(src, BBSHOME "/home") == 0)
    return 1;
    if (symlink(src, dst) == 0)
    return 0;

    return Copy(src, dst);
}

char           *
my_ctime(const time4_t * t, char *ans, int len)
{
    struct tm      *tp;

    tp = localtime4((time4_t*)t);
    snprintf(ans, len,
         "%02d/%02d/%02d %02d:%02d:%02d", (tp->tm_year % 100),
         tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
    return ans;
}