static int xml_cb(void *_fp, int argc, char **argv, char **ColName) { FILE *fp = (FILE *) _fp; char *msgid; if (argc < 5) { printf("argc -lt 4\n"); return 0; } /* delete < and > from the msgid */ msgid = argv[0]; msgid = msgid + 1; msgid[strlen(msgid) - 1] = '\0'; fprintf(fp, "<item>\n <msgid>"); write_html(fp, msgid); fprintf(fp, "</msgid>\n <author>"); write_html(fp, argv[1]); fprintf(fp, "</author>\n <groups>"); write_html(fp, argv[2]); fprintf(fp, "</groups>\n <subject>"); write_html(fp, argv[3]); fprintf(fp, "</subject>\n <date>"); write_html(fp, argv[4]); fprintf(fp, "</date>\n</item>"); return 0; }
void Html_File::create_file(void) { if (!tu_file_) tu_file_ = new TU_File(argc_, argv_, idx_, source_filename_, object_dir_, prefix_); write_html(); }
void notfound_404(struct hitArgs *args, char *info) { write_html(args->socketfd, "HTTP/1.1 404 Not Found\nServer: dweb\nConnection: close\nContent-Type: text/html", "<html><head>\n<title>404 Not Found</title>\n" "</head><body>\n<h1>Not Found</h1>\nThe requested URL was not found on this server.\n</body></html>"); args->logger_function(LOG, "404 NOT FOUND", info, args->socketfd); }
void forbidden_403(struct hitArgs *args, char *info) { write_html(args->socketfd, "HTTP/1.1 403 Forbidden\nServer: dweb\nConnection: close\nContent-Type: text/html", "<html><head>\n<title>403 Forbidden</title>\n" "</head><body>\n<h1>Forbidden</h1>\nThe requested URL, file type or operation is not allowed on this simple webserver.\n</body>" "</html>"); args->logger_function(LOG, "403 FORBIDDEN", info, args->socketfd); }
void ok_200(struct hitArgs *args, char *custom_headers, char *html, char *path) { STRING *headers = new_string(255); string_add(headers, "HTTP/1.1 200 OK\nServer: dweb\nCache-Control: no-cache\nPragma: no-cache"); if (custom_headers != NULL) { string_add(headers, custom_headers); } write_html(args->socketfd, string_chars(headers), html); string_free(headers); args->logger_function(LOG, "200 OK", path, args->socketfd); }
int main(int argc, char *argv[]) { uint8_t byte[100]; uint8_t message[256]; int fd = serialport_init(); //write(fd, "X", 1); int state = 0; // Wait for a recognized pattern int msgOffset = 0; int needed = 0; int index; int logFd = open("vistalog.bin", (O_CREAT | O_RDWR), (S_IRUSR | S_IWUSR)); //int txtFd = open("vistalog.txt", (O_CREAT | O_RDWR), (S_IRUSR | S_IWUSR)); while(1) { ssize_t size = read(fd, byte, 100); if (size < 0) printf("Read error\n"); if (size == 0) continue; //printf("%zu:", size); int rc = write(logFd, byte, size); if (rc == -1) printf("Log write failure\n"); #if 0 int rc = write(logFd, byte, size); if (rc == -1) printf("Log write failure\n"); #endif #if 1 struct timeval time; gettimeofday(&time, NULL); printf("[%ld.%06ld]", time.tv_sec, time.tv_usec); dump_message(byte, size); printf("\n"); #else for (index = 0; index < size; index++) { //printf(" %02X", byte[index]); switch (state) { case 0: // No message if (byte[index] == 0xF7) { // Keypad message state = 1; // Read message needed = 44; //printf("state = Read keypad message header (%d)\n", needed); message[0] = 0xF7; msgOffset = 1; } else if (byte[index] == 0x9E) { // 9E message, whatever that is state = 1; // Read message needed = 4; //printf("state = Read 9E message (%d)\n", needed); message[0] = 0x9E; msgOffset = 1; } else if (byte[index] == 0xF6) { // Appears to be a keypress message but without any indication of what key was pressed, maybe byte 1 is keypad address state = 1; // Read message needed = 3; message[0] = 0xF6; msgOffset = 1; } else if (byte[index] == 0x00) { // Keep looking } else { printf("Unknown message type (0x%02X)\n", byte[index]); } break; case 1: message[msgOffset] = byte[index]; msgOffset++; needed--; if (needed == 0) { //printf("Read needed bytes...\n"); // Entire message received struct timeval time; gettimeofday(&time, NULL); #if 0 time_t ltime; /* calendar time */ ltime=time(NULL); /* get current cal time */ char timeStr[256]; strftime(timeStr, sizeof(timeStr), "", localtime(<ime))); #endif printf("[%ld.%06ld]", time.tv_sec, time.tv_usec); // Handle message if (message[0] == 0xF7) { // keypad message printf("Keypad : "); dump_message(message, 12); message[msgOffset - 1] = 0x00; message[12] &= 0x7F; // The upper bit has some unknown meaning, masked for display printf("'%s'\n", &(message[12])); write_html((char *)&(message[12])); } else if (message[0] == 0x9E) { printf("9E : "); dump_message(message, msgOffset); printf("\n"); } else if (message[0] == 0xF6) { printf("Keypress : "); dump_message(message, msgOffset); printf("\n"); } else { printf("Trying to process unknown message type (0x%02X)\n", message[0]); } state = 0; // Wait for known message pattern msgOffset = 0; needed = 0; } } //printf("\n"); } #endif } return EXIT_SUCCESS; }
main( int argc, char * argv[]) { int buf[Chunk / IntSize]; int bufindex; int chars[256]; int child; char * dir; int html = 0; int fd; double first_start; double last_stop; int lseek_count = 0; char * machine; char name[Chunk]; int next; int seek_control[2]; int seek_feedback[2]; char seek_tickets[Seeks + SeekProcCount]; double seeker_report[3]; off_t size; FILE * stream; off_t words; fd = -1; basetime = (int) time((time_t *) NULL); size = 100; dir = "."; machine = ""; /* pick apart args */ for (next = 1; next < argc; next++) if (strcmp(argv[next], "-d") == 0) dir = argv[++next]; else if (strcmp(argv[next], "-s") == 0) size = atol(argv[++next]); else if (strcmp(argv[next], "-m") == 0) machine = argv[++next]; else if (strcmp(argv[next], "-html") == 0) html = 1; else usage(); if (size < 1) usage(); /* sanity check - 32-bit machines can't handle more than 2047 Mb */ if (sizeof(off_t) <= 4 && size > 2047) { fprintf(stderr, "File too large for 32-bit machine, sorry\n"); exit(1); } sprintf(name, "%s/Bonnie.%d", dir, getpid()); /* size is in meg, rounded down to multiple of Chunk */ size *= (1024 * 1024); size = Chunk * (size / Chunk); fprintf(stderr, "File '%s', size: %ld\n", name, size); /* Fill up a file, writing it a char at a time with the stdio putc() call */ fprintf(stderr, "Writing with putc()..."); newfile(name, &fd, &stream, 1); timestamp(); for (words = 0; words < size; words++) if (putc(words & 0x7f, stream) == EOF) io_error("putc"); /* * note that we always close the file before measuring time, in an * effort to force as much of the I/O out as we can */ if (fclose(stream) == -1) io_error("fclose after putc"); get_delta_t(Putc); fprintf(stderr, "done\n"); /* Now read & rewrite it using block I/O. Dirty one word in each block */ newfile(name, &fd, &stream, 0); if (lseek(fd, (off_t) 0, 0) == (off_t) -1) io_error("lseek(2) before rewrite"); fprintf(stderr, "Rewriting..."); timestamp(); bufindex = 0; if ((words = read(fd, (char *) buf, Chunk)) == -1) io_error("rewrite read"); while (words == Chunk) { /* while we can read a block */ if (bufindex == Chunk / IntSize) bufindex = 0; buf[bufindex++]++; if (lseek(fd, (off_t) -words, 1) == -1) io_error("relative lseek(2)"); if (write(fd, (char *) buf, words) == -1) io_error("re write(2)"); if ((words = read(fd, (char *) buf, Chunk)) == -1) io_error("rwrite read"); } /* while we can read a block */ if (close(fd) == -1) io_error("close after rewrite"); get_delta_t(ReWrite); fprintf(stderr, "done\n"); /* Write the whole file from scratch, again, with block I/O */ newfile(name, &fd, &stream, 1); fprintf(stderr, "Writing intelligently..."); for (words = 0; words < Chunk / IntSize; words++) buf[words] = 0; timestamp(); for (words = bufindex = 0; words < (size / Chunk); words++) { /* for each word */ if (bufindex == (Chunk / IntSize)) bufindex = 0; buf[bufindex++]++; if (write(fd, (char *) buf, Chunk) == -1) io_error("write(2)"); } /* for each word */ if (close(fd) == -1) io_error("close after fast write"); get_delta_t(FastWrite); fprintf(stderr, "done\n"); /* read them all back with getc() */ newfile(name, &fd, &stream, 0); for (words = 0; words < 256; words++) chars[words] = 0; fprintf(stderr, "Reading with getc()..."); timestamp(); for (words = 0; words < size; words++) { /* for each byte */ if ((next = getc(stream)) == EOF) io_error("getc(3)"); /* just to fool optimizers */ chars[next]++; } /* for each byte */ if (fclose(stream) == -1) io_error("fclose after getc"); get_delta_t(Getc); fprintf(stderr, "done\n"); /* use the frequency count */ for (words = 0; words < 256; words++) sprintf((char *) buf, "%d", chars[words]); /* Now suck it in, Chunk at a time, as fast as we can */ newfile(name, &fd, &stream, 0); if (lseek(fd, (off_t) 0, 0) == -1) io_error("lseek before read"); fprintf(stderr, "Reading intelligently..."); timestamp(); do { /* per block */ if ((words = read(fd, (char *) buf, Chunk)) == -1) io_error("read(2)"); chars[buf[abs(buf[0]) % (Chunk / IntSize)] & 0x7f]++; } /* per block */ while (words); if (close(fd) == -1) io_error("close after read"); get_delta_t(FastRead); fprintf(stderr, "done\n"); /* use the frequency count */ for (words = 0; words < 256; words++) sprintf((char *) buf, "%d", chars[words]); /* * Now test random seeks; first, set up for communicating with children. * The object of the game is to do "Seeks" lseek() calls as quickly * as possible. So we'll farm them out among SeekProcCount processes. * We'll control them by writing 1-byte tickets down a pipe which * the children all read. We write "Seeks" bytes with val 1, whichever * child happens to get them does it and the right number of seeks get * done. * The idea is that since the write() of the tickets is probably * atomic, the parent process likely won't get scheduled while the * children are seeking away. If you draw a picture of the likely * timelines for three children, it seems likely that the seeks will * overlap very nicely with the process scheduling with the effect * that there will *always* be a seek() outstanding on the file. * Question: should the file be opened *before* the fork, so that * all the children are lseeking on the same underlying file object? */ if (pipe(seek_feedback) == -1 || pipe(seek_control) == -1) io_error("pipe"); for (next = 0; next < Seeks; next++) seek_tickets[next] = 1; for ( ; next < (Seeks + SeekProcCount); next++) seek_tickets[next] = 0; /* launch some parallel seek processes */ for (next = 0; next < SeekProcCount; next++) { /* for each seek proc */ if ((child = fork()) == -1) io_error("fork"); else if (child == 0) { /* child process */ /* set up and wait for the go-ahead */ close(seek_feedback[0]); close(seek_control[1]); newfile(name, &fd, &stream, 0); srandom(getpid()); fprintf(stderr, "Seeker %d...", next + 1); /* wait for the go-ahead */ if (read(seek_control[0], seek_tickets, 1) != 1) io_error("read ticket"); timestamp(); seeker_report[StartTime] = time_so_far(); /* loop until we read a 0 ticket back from our parent */ while(seek_tickets[0]) { /* until Mom says stop */ doseek((long) (random() % (size / Chunk)), fd, ((lseek_count++ % UpdateSeek) == 0)); if (read(seek_control[0], seek_tickets, 1) != 1) io_error("read ticket"); } /* until Mom says stop */ if (close(fd) == -1) io_error("close after seek"); /* report to parent */ get_delta_t(Lseek); seeker_report[EndTime] = time_so_far(); seeker_report[CPU] = delta[(int) Lseek][CPU]; if (write(seek_feedback[1], seeker_report, sizeof(seeker_report)) != sizeof(seeker_report)) io_error("pipe write"); exit(0); } /* child process */ } /* for each seek proc */ /* * Back in the parent; in an effort to ensure the children get an even * start, wait a few seconds for them to get scheduled, open their * files & so on. */ close(seek_feedback[1]); close(seek_control[0]); sleep(5); fprintf(stderr, "start 'em..."); if (write(seek_control[1], seek_tickets, sizeof(seek_tickets)) != sizeof(seek_tickets)) io_error("write tickets"); /* read back from children */ for (next = 0; next < SeekProcCount; next++) { /* for each child */ if (read(seek_feedback[0], (char *) seeker_report, sizeof(seeker_report)) != sizeof(seeker_report)) io_error("pipe read"); /* * each child writes back its CPU, start & end times. The elapsed time * to do all the seeks is the time the first child started until the * time the last child stopped */ delta[(int) Lseek][CPU] += seeker_report[CPU]; if (next == 0) { /* first time */ first_start = seeker_report[StartTime]; last_stop = seeker_report[EndTime]; } /* first time */ else { /* not first time */ first_start = (first_start < seeker_report[StartTime]) ? first_start : seeker_report[StartTime]; last_stop = (last_stop > seeker_report[EndTime]) ? last_stop : seeker_report[EndTime]; } /* not first time */ if (wait(&child) == -1) io_error("wait"); fprintf(stderr, "done..."); } /* for each child */ fprintf(stderr, "\n"); delta[(int) Lseek][Elapsed] = last_stop - first_start; if (html) write_html(machine, size); else report(machine, size); unlink(name); }