forked from JohnHau/mis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myudp-dat-processing.c
executable file
·120 lines (95 loc) · 2.43 KB
/
myudp-dat-processing.c
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
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
//#include <curses.h>
#include <unistd.h>
#include <aio.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#define PORT 0xBAC0
#define MAXLINE 1500
#define LOG_FILE "bacnet0.log"
#define DIALOG_WIDTH 40
#define DIALOG_HEIGHT 20
#define CONTENT_WIDTH (DIALOG_WIDTH - 2)
// a simple log function, works similar to printf
void bacnet0_log(const char *format, ... )
{
va_list args; // argument list
static FILE *logfile = NULL; // file pointer to the logfile
char *fformat; // the modified format of the string which will be written to the logfile
int length; // length of the format
if(!(format == NULL && logfile == NULL)) {
// open the logfile if not already opened
if(logfile == NULL) {
logfile = fopen(LOG_FILE, "w");
// if that doesn't work exit with an error message
if(logfile == NULL) {
fprintf(stderr, "Cannot open logfile %s\n", LOG_FILE);
exit(EXIT_FAILURE);
}
}
// if NULL is given as format, close the opened file
if(format != NULL) {
// increase length by 2 (for \n\0
length = strlen(format) + 2;
// allocate memory
fformat = malloc(sizeof(char) * length);
// copy the format over
strncpy(fformat, format, length - 2);
// append \n\0
//fformat[length - 2] = '\n';
fformat[length - 2] = '\0';
fformat[length - 1] = '\0';
// get the rest of the arguments
va_start(args, format);
// use vfprintf() to
vfprintf(logfile, fformat, args);
// forces the logmessage to be written into the file right now
fflush(logfile);
va_end(args);
// free the allocated memory for the format string
free(fformat);
}
else
{
// close the logfile
fclose(logfile);
}
}
}
int val=0;
char mystr[1024]={0};
int main(int argc, char* argv[])
{
#if 1
if(setvbuf(stdin,NULL,_IOLBF,0) != 0)
{
perror("setvbuf failed\n");
}
#endif
while(1)
{
scanf("%s %d",mystr,&val);
//fgets(mystr,strlen(mystr),stdin);
//read(STDIN_FILENO,mystr,sizeof(mystr));
if(strcmp(mystr,"quit") == 0)
{
bacnet0_log("%s\n",mystr);
exit(EXIT_SUCCESS);
}
//fgets(mystr,sizeof(mystr),stdin);
//read(STDIN_FILENO,mystr,sizeof(mystr));
// printf("%s %d",mystr,val+1);
bacnet0_log("%s\n",mystr);
memset(mystr,0,sizeof(mystr));
}
}