OSDN Git Service

I was library can be compiled 20140223
[zither/ds-zither.git] / libms / counts.c
1 // counts.c
2 // masashi shimakura 20140223
3
4
5 #include<stdio.h>
6 #include<stdlib.h>
7 #include<string.h>
8 #include "libms.h"
9
10
11 int counts(char * path_name, int min, int max)
12 {
13 FILE * fp;
14 int stat;
15 int count;
16 char * data;
17
18 stat = 0;
19 count = 0;
20
21 if(min >= max){
22    min = 0;
23    max = 1;
24    }
25
26
27 data = (char *)calloc(BUF_LEN, sizeof(char));
28
29 if(0 >= (int)strlen(path_name)){
30    path_name = safe_memcpy(path_name, "/tmp/taro.count", BUF_LEN);
31    }
32
33 if((fp = fopen(path_name, "r+"))!=NULL){
34    data = safe_fpcat(fp, data, &stat, BUF_LEN);
35    if(0 > stat){
36       data = safe_memcpy(data, "0\n", BUF_LEN);
37       }
38    fclose(fp);
39    }
40 else{
41    fprintf(stderr,"counts(): file not read open %s\n", path_name);
42    }
43
44 count = strtol(data, (char **)NULL, 10);
45
46 if(count < min){
47    count = min;
48    }
49 else if(count > max){
50    count = max;
51    }
52 else{
53    }
54
55 count = count + 1;
56
57 if(count > max){
58    count = min;
59    }
60
61 if((fp = fopen(path_name, "w+"))!=NULL){
62    fprintf(fp,"%d\n", count);
63    fclose(fp);
64    }
65 else{
66    fprintf(stderr,"counts(): file not write open %s\n", path_name);
67    }
68
69 free(data);
70
71 return count;
72 }
73
74
75
76
77