From 1c2fdcc245551183bbddfaa80bd9328062fc4ed1 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Tue, 15 Dec 2009 05:51:38 +0100 Subject: [PATCH] Added a few previously unversioned utils. --- capitalize.c | 89 ++++++++++++++++++++++ cidd.c | 245 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dumpmem.c | 52 +++++++++++++ perror.c | 26 +++++++ sztest | 45 +++++++++++ trunc.c | 22 ++++++ usleep.c | 14 ++++ 7 files changed, 493 insertions(+) create mode 100644 capitalize.c create mode 100644 cidd.c create mode 100644 dumpmem.c create mode 100644 perror.c create mode 100755 sztest create mode 100644 trunc.c create mode 100644 usleep.c diff --git a/capitalize.c b/capitalize.c new file mode 100644 index 0000000..abcd0d5 --- /dev/null +++ b/capitalize.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include + +wchar_t *exclude[] = { + L"a", + L"an", + L"the", + L"in", + L"to", + L"from", + L"of", + L"and", + L"or", + NULL +}; + +wchar_t *seps = L"-.?!"; +wchar_t *nonseps = L"\'"; + +void capitalize(wchar_t *str) +{ + wchar_t *p, *p2, **ce; + int f; + + p = str; + f = 1; + while(1) { + while(!iswalnum(*p)) { + if(*p == L'\0') + return; + if(wcschr(seps, *p) != NULL) + f = 1; + p++; + } + p2 = p; + while(iswalnum(*p) || (wcschr(nonseps, *p) != NULL)) { + *p = towlower(*p); + p++; + } + if(f) { + *p2 = towupper(*p2); + f = 0; + } else { + for(ce = exclude; *ce != NULL; ce++) { + if(!wcsncmp(*ce, p2, p - p2) && ((*ce)[p - p2] == L'\0')) + break; + } + if(*ce == NULL) + *p2 = towupper(*p2); + } + } +} + +int main(int argc, char **argv) +{ + int i; + wchar_t buf[256]; + + setlocale(LC_ALL, ""); + if(argc > 1) { + for(i = 1; i < argc; i++) { + mbstowcs(buf, argv[i], sizeof(buf) / sizeof(*buf)); + capitalize(buf); + fputws(buf, stdout); + fputwc(L'\n', stdout); + } + } else { + while(1) { + if(fgetws(buf, sizeof(buf) / sizeof(*buf), stdin) == NULL) { + if(feof(stdin)) + break; + perror("reading input"); + return(1); + } + capitalize(buf); + fputws(buf, stdout); + } + } + return(0); +} + +/* + * Local Variables: + * compile-command: "gcc -Wall -g -o capitalize capitalize.c" + * End: + */ diff --git a/cidd.c b/cidd.c new file mode 100644 index 0000000..be2e9e7 --- /dev/null +++ b/cidd.c @@ -0,0 +1,245 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MODEM_DEV "/dev/modem" +#define PIDFILE "/var/run/cidd.pid" +#define PORT 5001 + +int mdm, sock; +int ibuflen = 0; +unsigned char ibuf[1024]; +unsigned char tmpbuf[1024]; +fd_set cfds; +int isdaemon; + +void write2(int fd, unsigned char *buf, int len) +{ + int ret; + + while(len) + { + ret = write(fd, buf, len); + if(ret < 0) + { + perror("write"); + exit(1); + } + buf += ret; + len -= ret; + } +} + +void sendcmd(unsigned char *cmd) +{ + unsigned char *lbuf; + + lbuf = strcpy(malloc(strlen(cmd) + 3), cmd); + strcat(lbuf, "\r\n"); + write2(mdm, lbuf, strlen(cmd) + 2); + free(lbuf); +} + +unsigned char *getline(int block) +{ + int i; + unsigned char *p; + int ret, len; + static unsigned char retbuf[1024]; + struct pollfd pf; + + while(1) + { + while((p = memchr(ibuf, '\r', ibuflen)) != NULL) + { + memcpy(retbuf, ibuf, len = (p - ibuf)); + retbuf[len] = 0; + memmove(ibuf, p + 1, ibuflen -= (len + 1)); + for(p = retbuf; *p; p++) + { + while(*p == '\n') + memmove(p, p + 1, len--); + } + if(len && (memcmp(retbuf, "AT", 2))) + return(retbuf); + } + if(!block) + { + pf.fd = mdm; + pf.events = POLLIN; + pf.revents = 0; + if(poll(&pf, 1, 0) == 0) + return(NULL); + } + ret = read(mdm, ibuf + ibuflen, 1024 - ibuflen); + if(ret < 0) + { + perror("read"); + exit(1); + } + ibuflen += ret; + } +} + +void broadcast(unsigned char *buf, ...) +{ + int i; + va_list args; + + va_start(args, buf); + vsprintf(tmpbuf, buf, args); + va_end(args); + strcat(tmpbuf, "\n"); + for(i = 0; i < FD_SETSIZE; i++) + { + if(FD_ISSET(i, &cfds) && (i != mdm) && (i != sock)) + write2(i, tmpbuf, strlen(tmpbuf)); + } +} + +void initmodem(void) +{ + sendcmd("ATZ"); + if(strcmp(getline(1), "OK")) + { + if(isdaemon) + syslog(LOG_CRIT, "Could not reset modem"); + else + fprintf(stderr, "Could not reset modem\n"); + exit(1); + } + sendcmd("AT#CID=1"); + if(strcmp(getline(1), "OK")) + { + if(isdaemon) + syslog(LOG_CRIT, "Could not activate called ID\n"); + else + fprintf(stderr, "Could not activate caller ID\n"); + exit(1); + } +} + +void sighandler(int sig) +{ + if(sig == SIGHUP) + { + initmodem(); + } +} + +int main(int argc, unsigned char **argv) +{ + int i; + unsigned char *p; + int nsock; + int ret; + struct sockaddr_in name; + struct termios tio; + fd_set rfds; + FILE *pidst; + + isdaemon = 0; + if((mdm = open(MODEM_DEV, O_RDWR)) < 0) + { + perror(MODEM_DEV); + return(1); + } + if(tcgetattr(mdm, &tio) < 0) + { + perror("tcgetattr"); + return(1); + } + cfmakeraw(&tio); + cfsetispeed(&tio, B0); + cfsetospeed(&tio, B38400); + if(tcsetattr(mdm, TCSANOW, &tio) < 0) + { + perror("tcsetattr"); + return(1); + } + if((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) + { + perror("socket"); + return(1); + } + name.sin_family = AF_INET; + name.sin_port = htons(PORT); + name.sin_addr.s_addr = 0; + if(bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) + { + perror("bind"); + return(1); + } + if(listen(sock, 16) < 0) + { + perror("listen"); + return(1); + } + initmodem(); + signal(SIGHUP, sighandler); + if((pidst = fopen(PIDFILE, "w")) == NULL) + { + perror("open " PIDFILE); + exit(1); + } + daemon(0, 0); + fprintf(pidst, "%i\n", getpid()); + fclose(pidst); + openlog("cidd", LOG_PID, LOG_DAEMON); + isdaemon = 1; + FD_ZERO(&cfds); + FD_SET(mdm, &cfds); + FD_SET(sock, &cfds); + while(1) + { + rfds = cfds; + ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL); + if((ret < 0) && (errno != EINTR)) + { + syslog(LOG_CRIT, "select return %m"); + return(1); + } + if(ret > 0) + { + for(i = 0; i < FD_SETSIZE; i++) + { + if(FD_ISSET(i, &rfds)) + { + if(i == mdm) + { + while((p = getline(0)) != NULL) + { + if(!strcmp(p, "RING")) + broadcast("R"); + if(!memcmp(p, "NMBR = ", 7)) + { + p += 7; + broadcast("N%s", p); + } + } + } else if(i == sock) { + if((nsock = accept(sock, NULL, 0)) >= 0) + FD_SET(nsock, &cfds); + } else { + if(read(i, tmpbuf, 1024) == 0) + { + FD_CLR(i, &cfds); + close(i); + } + } + } + } + } + } +} diff --git a/dumpmem.c b/dumpmem.c new file mode 100644 index 0000000..4f30fa2 --- /dev/null +++ b/dumpmem.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +unsigned char buf[1024]; + +int main(int argc, unsigned char **argv) +{ + pid_t pid; + unsigned long addr1, addr2; + int buf; + + if(argc < 4) + { + fprintf(stderr, "Not enough args\n"); + return(1); + } + pid = strtol(argv[1], NULL, 0); + addr1 = (unsigned long)strtoll(argv[2], NULL, 0); + addr2 = (unsigned long)strtoll(argv[3], NULL, 0); + if(ptrace(PTRACE_ATTACH, pid, NULL, NULL)) + { + perror("PTRACE_ATTACH"); + return(1); + } + for(; addr1 < addr2; addr1 += sizeof(int)) + { + errno = 0; + if(((buf = ptrace(PTRACE_PEEKDATA, pid, (void *)addr1, NULL)) == -1) && errno) + { + perror("PTRACE_PEEKDATA"); + if(ptrace(PTRACE_DETACH, pid, NULL, NULL)) + perror("PTRACE_DETACH"); + return(1); + } + if(write(1, &buf, sizeof(buf)) != 4) + { + perror("write"); + if(ptrace(PTRACE_DETACH, pid, NULL, NULL)) + perror("PTRACE_DETACH"); + return(1); + } + } + if(ptrace(PTRACE_DETACH, pid, NULL, NULL)) + { + perror("PTRACE_DETACH"); + return(1); + } + return(0); +} diff --git a/perror.c b/perror.c new file mode 100644 index 0000000..b094445 --- /dev/null +++ b/perror.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int i; + + setlocale(LC_ALL, ""); + + if(argc < 2) + { + fprintf(stderr, "usage: perror errno...\n"); + exit(1); + } + + for(i = 1; i < argc; i++) + { + if(argc > 2) + printf("%i: ", atoi(argv[i])); + printf("%s\n", strerror(atoi(argv[i]))); + } + + return(0); +} diff --git a/sztest b/sztest new file mode 100755 index 0000000..fd7505c --- /dev/null +++ b/sztest @@ -0,0 +1,45 @@ +#!/bin/sh + +if [ $# -lt 1 ]; then + echo "usage: sztest [-H HEADER] TYPE [CCFLAGS...]" >&2 + exit 1 +fi + +headers= +while [ "${1:0:1}" = "-" ]; do + opt="$1" + shift + case "$opt" in + "-H") + headers="$headers $1" + shift + ;; + esac +done +type="$1" +shift +file="$(mktemp /tmp/sztestXXXXXX)" +cat >"$file.c" < +#include +#include +#include +#include +EOF +for header in $headers; do + echo "#include <$header>" >>"$file.c" +done +cat >>"$file.c" < +#include +#include + +int main(int argc, char **argv) +{ + if(argc < 3) { + fprintf(stderr, "usage: trunc FILE SIZE\n"); + exit(1); + } + if(truncate(argv[1], atoll(argv[2]))) { + perror(argv[1]); + exit(1); + } + return(0); +} + +/* + * Local Variables: + * compile-command: "gcc -Wall -g -o trunc trunc.c -D_FILE_OFFSET_BITS=64" + * End: + */ diff --git a/usleep.c b/usleep.c new file mode 100644 index 0000000..1e265d9 --- /dev/null +++ b/usleep.c @@ -0,0 +1,14 @@ +#include +#include +#include + +int main(int argc, char **argv) +{ + if(argc < 2) + { + fprintf(stderr, "usage: usleep usecs\n"); + return(1); + } + usleep(atoi(argv[1])); + return(0); +} -- 2.11.0