ref: 7bc4d4f57bc35956c69039e92ebef1737f973e95
dir: /toe.c/
#include <u.h> #include <libc.h> #include <bio.h> typedef struct Query { char *domain; char *user; } Query; void toe(Query *query) { char *naddr; char *line; int fd; Biobuf out, body; naddr = netmkaddr(query->domain, "tcp", "79"); fd = dial(naddr, 0, 0, 0); if(fd < 0){ print("unable to connect to %s: %r", query->domain); return; } if (query->user == nil) { fprint(fd, "\r\n"); } else { fprint(fd, "%s\r\n", query->user); } Binit(&body, fd, OREAD); Binit(&out, 1, OWRITE); while((line = Brdstr(&body, '\n', 0)) != nil){ Bprint(&out, "%s", line); free(line); } Bflush(&out); Bflush(&body); close(fd); free(query); } Query * mk_query(char *query_str) { char *user, *domain; Query *new_query = malloc(sizeof(Query)); if (query_str[0] == '@') { user = nil; domain = query_str + 1; } else if (strstr(query_str, "@") == nil) { user = query_str; domain = "localhost"; } else { user = strtok(query_str, "@"); domain = strtok(nil, "@"); } new_query->user = user; new_query->domain = domain; return new_query; } void main(int argc, char **argv) { if( argc == 2 ) { Query *query = mk_query(argv[1]); toe(query); exits(0); } else if( argc > 2 ) { print("Too many arguments supplied.\n"); } else { print("Usage: toe [user]@server.\n"); } }