summaryrefslogtreecommitdiffstats
path: root/innbbsd/file.c
blob: 7016f940cb941ccef9a37326eaa3ffa3bbbfb3ec (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
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdarg.h>
#define MAXARGS     100

/*   isfile is called by
 *   isfile(filenamecomp1, filecomp2,  filecomp3, ..., (char *)0);
 *   extern "C" int isfile(const char *, const char *[]) ;
*/


char FILEBUF[4096];


static char DOLLAR_[8192];
char *getstream(fp)
FILE *fp;
{
    return fgets(DOLLAR_, sizeof(DOLLAR_) -1 , fp);
}

/*
    The same as sprintf, but return the new string
    fileglue("%s/%s",home,".newsrc");
*/

char *fileglue(char *fmt, ...)
{
    va_list ap;
    static char gluebuffer[8192];
    va_start(ap, fmt);
    vsprintf(gluebuffer, fmt, ap);
    va_end(ap);
    return gluebuffer;
}

long 
filesize(filename)
char* filename;
{
    struct stat st;

    if (stat(filename,&st)) return 0; 
    return st.st_size;
}

int iszerofile(filename)
char* filename;
{
    struct stat st;

    if (stat(filename,&st)) return 0; 
    if (st.st_size == 0) return 1;
    return 0;
}

int isfile(filename)
char* filename;
{
    struct stat st;

    if (stat(filename,&st)) return 0; 
    if (!S_ISREG(st.st_mode)) return 0;
    return 1;
}

#ifdef TEST
int isfilev(va_alist)
{
    va_list ap;
    struct stat st;
    char *p;
    va_start(ap);

    FILEBUF[0]='\0';
        while ((p = va_arg(ap, char *)) != (char *)0) {
        strcat(FILEBUF,p);
    }
    printf("file %s\n",FILEBUF);

    va_end(ap);
    return isfile(FILEBUF);
}
#endif


int isdir(filename)
char* filename;
{
    struct stat st;

    if (stat(filename,&st)) return 0; 
    if (!S_ISDIR(st.st_mode)) return 0;
    return 1;
}

#ifdef TEST
int isdirv(va_alist)
{
    va_list ap;
    struct stat st;
    char *p;
    va_start(ap);

    FILEBUF[0]='\0';
        while ((p = va_arg(ap, char *)) != (char *)0) {
        strcat(FILEBUF,p);
    }

    va_end(ap);
    return isdir(FILEBUF);
}
#endif

unsigned long  mtime(filename)
char* filename;
{
    struct stat st;
    if (stat(filename,&st)) return 0; 
        return st.st_mtime;
}

#ifdef TEST
unsigned long mtimev(va_alist)
{
    va_list ap;
    struct stat st;
    char *p;
    va_start(ap);

    FILEBUF[0]='\0';
        while ((p = va_arg(ap, char *)) != (char *)0) {
        strcat(FILEBUF,p);
    }

    va_end(ap);
    return mtime(FILEBUF);
}
#endif

unsigned long  atime(filename)
char *filename;
{
    struct stat st;
    if (stat(filename,&st)) return 0; 
        return st.st_atime;
}

#ifdef TEST
unsigned long atimev(va_alist)
{
    va_list ap;
    struct stat st;
    char *p;
    va_start(ap);

    FILEBUF[0]='\0';
        while ((p = va_arg(ap, char *)) != (char *)0) {
        strcat(FILEBUF,p);
    }

    va_end(ap);
    return atime(FILEBUF);
}
#endif

/*#undef TEST*/
#ifdef TEST
main(argc,argv)
int argc;
char **argv;
{
    int i;
    if (argc > 3) {
      if (isfilev(argv[1],argv[2],(char*)0))    printf("%s %s %s is file\n",argv[1],argv[2],argv[3]);
      if (isdirv(argv[1],argv[2],(char*)0)) printf("%s %s %s is dir\n",argv[1],argv[2],argv[3]);
      printf("mtime %d\n",mtimev(argv[1],argv[2],(char*)0));
      printf("atime %d\n",atimev(argv[1],argv[2],(char*)0));
    }
    printf("fileglue %s\n", fileglue("%s/%s","home",".test"));
}
#endif