Initial import from FreeBSD RELENG_4:
[games.git] / share / examples / isdn / contrib / answer.c
1 // $FreeBSD: src/share/examples/isdn/contrib/answer.c,v 1.1.2.1 2001/08/10 14:59:47 obrien Exp $
2 // changed ".g711a" to ".al" (-hm)
3 // Tue Mar  3 02:42:14 MET 1998         dave@turbocat.de
4 // started
5
6 #define BLK_SIZE        2048
7 #define SOX             "/usr/local/bin/sox"
8 #define ALAWULAW        "/usr/local/bin/alaw2ulaw"
9
10 #include <stdio.h>
11 #include <time.h>
12
13         FILE    *device;
14         FILE    *logfile;
15         char    srcNum[30];
16         char    destNum[30];
17         char argbuf[255];
18         char tmpBuf[1024] = "";
19
20
21 void writeToPhone (char *path)
22 {
23         char    buf[BLK_SIZE];
24         FILE    *srcfile;
25         int             i = 0;
26         int             readcount = 0;
27
28         srcfile = fopen(path,"r");
29         if (srcfile) {
30                 for (i=0;i<BLK_SIZE;i++) {
31                         buf[i] = '\0';
32                 }
33                 readcount = BLK_SIZE;
34                 i = 0;
35                 do {
36                 readcount = fread(buf,1, BLK_SIZE, srcfile);
37                 fwrite(buf, 1, readcount, device);
38                         i = readcount + i;
39 //                      fprintf(logfile,"%d read (%d)\n",i,readcount);
40                 } while (readcount == BLK_SIZE);
41         
42                 fclose(srcfile);
43         } else {
44                 fprintf(logfile,"Can't open file '%s'\n",path);
45         }
46 }
47
48 void readFromPhone (char *path)
49 {
50         char    buf[BLK_SIZE];
51         FILE    *destfile;
52         int             i = 0;
53         int             readcount = 0;
54
55         destfile = fopen(path,"a");
56         if (destfile) {
57                 for (i=0;i<BLK_SIZE;i++) {
58                         buf[i] = '\0';
59                 }
60                 readcount = BLK_SIZE;
61                 i = 0;
62                 do {
63                 readcount = fread(buf,1, BLK_SIZE, device);
64                 fwrite(buf, 1, readcount, destfile);
65                         i = readcount + i;
66 //                      fprintf(logfile,"%d read (%d)\n",i,readcount);
67                 } while (readcount == BLK_SIZE);
68         
69                 fclose(destfile);
70         } else {
71                 fprintf(logfile,"Can't open file '%s'\n",path);
72         }
73 }
74
75 void usage (void)
76 {
77         fprintf(stderr,"Usage: answer -D device -d destination -s source\n");
78         exit(1); 
79 }
80
81 const char * argWithName (const char* aName)
82 {
83         // '-D /dev/null -d 82834 -s 3305682834'
84         int i = 0;
85         int optionSeen = 0;
86         int startpos = 0;
87
88         for (i = 0; i < sizeof(tmpBuf);i++) {
89                 tmpBuf[i] = '\0';
90         }
91
92         for (i = 0; i<strlen(argbuf);i++) {
93                 if (optionSeen) {
94                         for (;(i<strlen(argbuf) && (argbuf[i] != ' '));i++) {
95                         }
96                         i++;
97                         startpos = i;
98
99                         for (;(i<strlen(argbuf) && (argbuf[i] != ' '));i++) {
100                         }
101                         strncpy(tmpBuf,&argbuf[startpos], i-startpos);
102
103                         return tmpBuf;
104                 }
105                 if (0 == strncmp(aName,&argbuf[i], strlen(aName))) {
106                         optionSeen = 1;
107                 }
108         }
109
110         usage();
111         return NULL;
112 }
113
114 int main (int argc, const char *argv[]) {
115
116         int i,pos = 0;
117         extern char *optarg;
118         extern int optind;
119         int bflag, ch;
120         char timeStr[50];
121         char outfileName[1024] = "";
122         char cmdStr[2048] = "";
123         time_t now;
124
125         now=time(NULL);
126
127         strftime(timeStr,40,I4B_TIME_FORMAT,localtime(&now));
128
129         logfile = fopen("/var/log/answer.log","a");
130
131         fprintf(logfile,"%s Started\n",timeStr);
132
133         pos=0;
134         for (i=1;i<argc;i++) {
135                 sprintf(&argbuf[strlen(argbuf)],"%s ",argv[i]);
136         }
137         if (strlen(argbuf) > 2) {
138                 argbuf[strlen(argbuf)-1] = '\0';
139         }
140
141
142         device = fopen(argWithName("-D"),"r+");
143         strcpy(destNum, argWithName("-d"));
144         strcpy(srcNum, argWithName("-s"));
145
146                 fprintf(logfile,"device '%s'\n", argWithName("-D"));
147                 fprintf(logfile,"srcNum '%s'\n", srcNum);
148                 fprintf(logfile,"destNum '%s'\n", destNum);
149
150
151         if (device) {
152
153                 strftime(timeStr,40,I4B_TIME_FORMAT,localtime(&now));
154
155                 sprintf(outfileName,"/var/isdn/%s_%s_%s", timeStr, srcNum, destNum);
156
157                 writeToPhone ("/usr/local/lib/isdn/msg.al");
158                 readFromPhone (outfileName);
159
160                 sprintf(cmdStr,"/bin/cat %s | %s | %s -t raw -U -b -r 8000 - -t .au %s.snd", outfileName, ALAWULAW, SOX, outfileName);
161                 fprintf(logfile,"%s\n",cmdStr);
162                 system(cmdStr);
163                 unlink(outfileName);
164
165                 fclose(device);
166         } else {
167                 fprintf(logfile,"Can't open file '%s'\n",argWithName("-D"));
168         }
169
170         now=time(NULL);
171
172         strftime(timeStr,40,I4B_TIME_FORMAT,localtime(&now));
173
174         fprintf(logfile,"%s Done\n",timeStr);
175         fclose(logfile);
176     exit(0);       // insure the process exit status is 0
177     return 0;      // ...and make main fit the ANSI spec.
178 }