void _test_read_write(char *seq, char mode[2]){ FILE *fh; size_t seq_len = strlen(seq); fh = fopen(TEST_FILE, mode); size_t file_pos = ftell(fh); size_t pos = fencode(fh, (unsigned char *)seq); TEST_OK((seq_len + 1) / 2 == pos - file_pos); fflush(fh); fclose(fh); fh = fopen(TEST_FILE, "rb"); //printf("%i:%i\n", file_pos, pos); char *read_seq = (char *)fdecode(fh, file_pos, pos); //printf("%s, %s\n", seq, read_seq); TEST_OK(!strcmp(seq, read_seq)); free(read_seq); fclose(fh); }
int main(int argc, char *argv[]) { FILE *in, *out; if(strstr(argv[0], "dec") != 0) mode = DECODE; else mode = ENCODE; if ( mode == ENCODE ) { in = stdin; out = stdout; } else { in = stdin; out = stdout; } if ( argc < 2 ) mode == ENCODE ? fencode(in, out) : fdecode(in, out); return 0; }
int main(int argc, char *argv[]) { int ch, lflags; char *p, *codeset; while ((ch = getopt(argc, argv, GETOPTOPTS)) != -1) switch ((char) ch) { case 'c': cpm = atoi(optarg); break; case 'd': device = optarg; break; case 'e': eflag = 1; setvbuf(stdout, 0, _IONBF, 0); break; case 'f': freq = atoi(optarg); break; case 'l': lflag = 1; break; #ifdef SPEAKER case 'p': pflag = 1; break; #endif case 'r': rflag = 1; break; case 's': sflag = 1; break; case 'w': wpm = atoi(optarg); break; case '?': default: errx(1, USAGE); } if ((sflag && lflag) || (sflag && rflag) || (lflag && rflag)) { errx(1, "morse: only one of -l, -s, and -r allowed\n"); } if ((pflag || device) && (sflag || lflag)) { errx(1, "morse: only one of -p, -d and -l, -s allowed\n"); } if (cpm == 0) { cpm = wpm; } if ((pflag || device) && ((wpm < 1) || (wpm > 60) || (cpm < 1) || (cpm > 60))) { errx(1, "morse: insane speed\n"); } if ((pflag || device) && (freq == 0)) { freq = FREQUENCY; } #ifdef SPEAKER if (pflag) { if ((spkr = open(SPEAKER, O_WRONLY, 0)) == -1) { err(1, SPEAKER); } } else #endif if (device) { if ((line = open(device, O_WRONLY | O_NONBLOCK)) == -1) { err(1, "open tty line"); } if (tcgetattr(line, &otty) == -1) { err(1, "tcgetattr() failed"); } ntty = otty; ntty.c_cflag |= CLOCAL; tcsetattr(line, TCSANOW, &ntty); lflags = fcntl(line, F_GETFL); lflags &= ~O_NONBLOCK; fcntl(line, F_SETFL, &lflags); ioctl(line, TIOCMGET, &lflags); lflags &= ~TIOCM_RTS; olflags = lflags; ioctl(line, TIOCMSET, &lflags); (void)signal(SIGHUP, sighandler); (void)signal(SIGINT, sighandler); (void)signal(SIGQUIT, sighandler); (void)signal(SIGTERM, sighandler); } if (pflag || device) { dot_clock = wpm / 2.4; /* dots/sec */ dot_clock = 1 / dot_clock; /* duration of a dot */ dot_clock = dot_clock / 2; /* dot_clock runs at twice */ /* the dot rate */ dot_clock = dot_clock * 100; /* scale for ioctl */ cdot_clock = cpm / 2.4; /* dots/sec */ cdot_clock = 1 / cdot_clock; /* duration of a dot */ cdot_clock = cdot_clock / 2; /* dot_clock runs at twice */ /* the dot rate */ cdot_clock = cdot_clock * 100; /* scale for ioctl */ } argc -= optind; argv += optind; if (setlocale(LC_CTYPE, "") != NULL && *(codeset = nl_langinfo(CODESET)) != '\0') { if (strcmp(codeset, "KOI8-R") == 0) hightab = koi8rtab; else if (strcmp(codeset, "ISO8859-1") == 0 || strcmp(codeset, "ISO8859-15") == 0) hightab = iso8859_1tab; else if (strcmp(codeset, "ISO8859-7") == 0) hightab = iso8859_7tab; } if (lflag) { printf("m"); } if (rflag) { if (*argv) { do { p = strtok(*argv, DELIMITERS); if (p == NULL) { decode(*argv); } else { while (p) { decode(p); p = strtok(NULL, DELIMITERS); } } } while (*++argv); putchar('\n'); } else { fdecode(stdin); } } else if (*argv) { do { for (p = *argv; *p; ++p) { if (eflag) putchar(*p); morse(*p); } if (eflag) putchar(' '); morse(' '); } while (*++argv); } else { while ((ch = getchar()) != EOF) { if (eflag) putchar(ch); morse(ch); } } if (device) tcsetattr(line, TCSANOW, &otty); exit(0); }