-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.c
More file actions
114 lines (94 loc) · 2.41 KB
/
func.c
File metadata and controls
114 lines (94 loc) · 2.41 KB
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
/*
* Author: vyouzhi <vyouzhi@163.com>
* http://www.xda.cn
*
* File: func.c
* Create Date: 2012-07-17 15:15:58
*
*/
#include <string.h>
#include <zlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "func.h"
void file_put_contents(const char *filename, const char *value, unsigned long val_len){
FILE *fp;
fp = fopen(filename, "a+");
fwrite(value, sizeof(char), val_len, fp);
fclose(fp);
}
void mv(const char *dst, const char *src){
rename(src, dst);
unlink(src);
}
char *frand(void){
char str[64] = "t00uAv1wB2xCy3Dz4EF5Gs6HaIb7cJdK8eLf9gMhNOiPjQkRmSnTUoVWqXYpZr";
int i, start, end;
char tmp[2];
unsigned long l;
char str_rand[9], *p;
l = time(0);
start = l%50 + 1;
end = start + 9;
str_rand[0]='\0';
srand((unsigned int)(l%50));
for(i=start; i<end; i++){
snprintf(tmp, 2, "%c",str[(rand()%i)+1]);
strcat(str_rand, tmp);
}
p = strdup(str_rand);
return p;
}
unsigned long files_size(char *filename){
struct stat buf;
if(stat(filename, &buf)<0){
return 0;
}
return (unsigned long)buf.st_size;
}
int files_uid(char *filename){
struct stat buf;
if(stat(filename, &buf)<0){
return -1;
}
return buf.st_uid;
}
int files_iswrite(char *filename){
struct stat buf;
if(stat(filename, &buf)<0){
return -1;
}
return ((buf.st_mode & S_IWGRP) != 0)?1:0;
}
int decompress_one_file(char *infilename, char *outfilename){
gzFile infile = gzopen(infilename, "rb");
/*FILE *outfile = fopen(outfilename, "wb");*/
char buffer[128];
int num_read = 0;
if (!infile ) return -1;
while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0) {
gzwrite(buffer, outfilename, num_read);
}
gzclose(infile);
/*fclose(outfile);*/
return 0;
}
int mem_uncompress(const char *src,unsigned long src_len, char **dst , unsigned long *dst_len){
int factor=1, status;
do {
*dst_len = src_len * (1 << factor++);
*dst = (char *)calloc(sizeof(char), *dst_len + 1);
status = uncompress((unsigned char *)*dst, dst_len, (unsigned const char *)src, src_len);
} while (status == Z_BUF_ERROR && factor < 16);
if(status == Z_OK){
return 0;
}
free(*dst);
return status;
}
/* vim: set ts=4 sw=4: */