int main(int argc, char *argv[]) { pthread_t tid; struct iofiles *iofp; if (argc != 3) err_quit("Usage: %s src_dir dst_dir", argv[0]); tid = 0; iofp = Malloc(sizeof(struct iofiles)); iofp->source = s_dup(argv[1]); iofp->destination = s_dup(argv[2]); Pthread_create(&tid, NULL, copydirectory, (void *)iofp); (void)pthread_join(tid, NULL); return 0; }
char *RQ_UnEscapeDup(char *input, int mask) { char *buf = s_dup(input); int blen = array_len(buf)*2; array_resize(buf, blen); RQ_UnEscapePath(input, buf, blen, mask); return buf; }
/* Standard variable set, given context */ int prmvp_set(struct prmvcx_s *px) { struct prmvar_s *p = px->prmvcx_var; union prmval *vp = &px->prmvcx_val; FILE *of = px->prmvcx_of; FILE *ef = px->prmvcx_ef; if (of) fprintf(of," %s: ", p->prmv_name); switch (p->prmv_typf & PRMVF_TYPE) { case PRMVT_BOO: printf("%s => %s\n", *(int *)(p->prmv_loc) ? "On" : "Off", vp->vi ? "On" : "Off"); *(int *)(p->prmv_loc) = vp->vi; break; case PRMVT_OCT: printf("%#.0o => %#.0o\n", *(int *)(p->prmv_loc), vp->vi); *(int *)(p->prmv_loc) = vp->vi; break; case PRMVT_DEC: printf("%d. => %d.\n", *(int *)(p->prmv_loc), vp->vi); *(int *)(p->prmv_loc) = vp->vi; break; case PRMVT_WRD: printf("%lo,,%lo => %lo,,%lo\n", (long)LHGET(*(w10_t *)(p->prmv_loc)), (long)RHGET(*(w10_t *)(p->prmv_loc)), (long)LHGET(vp->vw), (long)RHGET(vp->vw)); *(w10_t *)(p->prmv_loc) = vp->vw; break; case PRMVT_STR: if (*(char **)(p->prmv_loc)) printf("\"%s\"", *(char **)(p->prmv_loc)); else printf("NULL"); if (vp->vs) printf(" => \"%s\"\n", vp->vs); else printf("NULL\n"); { /* Ensure have new string before flushing old one! */ char *tmp = NULL; if (vp->vs && !(tmp = s_dup(vp->vs))) { printf(" Error: malloc failed for new string, var not set!\n"); return FALSE; } /* OK, free up old if necessary */ if ((p->prmv_typf & PRMVF_DYNS) && *(char **)(p->prmv_loc)) free(*(char **)(p->prmv_loc)); *(char **)(p->prmv_loc) = tmp; } p->prmv_typf |= PRMVF_DYNS; break; } return TRUE; }
/* ci_init: allocate memory for new copyinfo ci */ static copyinfo_t *ci_init(const char *name, pthread_t tid) { copyinfo_t *cip; cip = Malloc(sizeof(copyinfo_t)); bzero(cip, sizeof(cip)); cip->namestring = s_dup(name); cip->tid = tid; cip->next = NULL; return cip; }
/* getinput: read input from stdin, returned memory allocated with malloc */ char *getinput(void) { char buf[MAX_CANON]; char *s; /* for now just one line */ if (fgets(buf, MAX_CANON, stdin) == NULL) exit(EXIT_SUCCESS); if (*(buf + strlen(buf)-1) == '\n') *(buf + strlen(buf)-1) = '\0'; s = s_dup(buf); return s; }
HandlerInfo *HL_New(ReqInfo *req) { HandlerInfo *hl = (HandlerInfo*) MEM_calloc(sizeof(HandlerInfo), "HandlerInfo"); char *buf; hl->docroot = s_dup(docroot); hl->servroot = s_dup(servroot); hl->req = req; RQ_SplitQuery(req->path, NULL, &buf, NULL); hl->query = RQ_ParseQuery(buf); s_free(buf); if (!strcncmp(req->method, "POST", 5, 5) && req->body != NULL) { char *encoding = RQ_GetHeader(req, "Content-Type"); if (encoding && s_find(encoding, "urlencoded") >= 0) { //turn binary body data into 0-terminated C string array_append(req->body, 0); hl->query2 = RQ_ParseQuery(req->body); } } return hl; }
/* parse: pares input line into cmd free with free_cmd() */ struct command *cmd_creat(const char *input) { struct command *cmd; char *s, *t; int background = 0; /* 1 if background process */ cmd = Malloc(sizeof(struct command)); bzero(cmd, sizeof(struct command)); s = s_dup(input); if (*(s+strlen(s)-1) == '&') { *(s+strlen(s)-1) = '\0'; background = 1; } if (strchr(s, '|') != '\0') { if (pipeline_parse(cmd, s) == -1) { cmd_free(cmd); free(s); DP("%s", "pipeline_parse error"); return (struct command *)0; } } else { /* remove redirection options from s */ t = io_parse(cmd, s); if (argv_parse(cmd, t) == -1) { cmd_free(cmd); free(s); DP("%s", "argv_parse error"); return (struct command *)0; } } free(s); free(t); cmd->background = background; return cmd; }
int RQ_SplitQuery(char *path, char **pathout, char **query, char **label) { char buf[MAXURL]; char *c = path; int i=0, j=0, qstart, lstart, plen; if (path == NULL) { fprintf(stderr, "NULL passed to RQ_SplitQuery()!"); if (pathout) *pathout = s_dup(""); if (query) *query = s_dup(""); if (label) *label = s_dup(""); return 0; } plen = strlen(path); qstart = lstart = plen; if (strlen(path) >= MAXURL) { path[MAXURL-1] = 0; fprintf(stderr, "%s\n", path); fprintf(stderr, "corrupted path detected\n"); } j = strcspn(c, "?"); c += j; i += j; qstart = i; if (i < plen && buf[i] == '&') { } if (i < plen) { int lasti = i; while (i < plen) { lasti = i; i++; c++; j = strcspn(c, "#"); i += j; c += j; } if (lasti < plen && path[lasti] == '#') lstart = lasti; } if (pathout && qstart != 0 && qstart <= plen) { memcpy(buf, path, qstart); buf[qstart] = 0; *pathout = s_dup(buf); } else if (pathout) { *pathout = s_dup(""); } if (query && qstart < plen) { memcpy(buf, path+qstart, lstart-qstart); buf[lstart-qstart] = 0; *query = s_dup(buf); } else if (query) { *query = s_dup(""); } if (label && lstart < plen) { memcpy(buf, path+lstart, plen-lstart); buf[plen-lstart] = 0; *label = s_dup(buf); } else if (label) { *label = s_dup(""); } return 1; }
void RQ_AddHeader(ReqInfo *req, char *header, char *val) { array_append(req->headers, s_dup(header)); array_append(req->headers, s_dup(val)); }
int main(int argc, char *argv[]) { int i, rc, nerr = 0, e; char *to, *msg, *cp; BUFFER buf; #if HAVE_DOORS struct door_info dib; #endif for (i = 1; i < argc && argv[i][0] == '-'; i++) switch (argv[i][1]) { case 'V': p_header(); exit(0); case 'm': ++mailmode; break; case 'd': ++debug; break; #if HAVE_DOORS case 'D': door_path = strdup(argv[i]+2); break; #endif case 'F': fifo_path = strdup(argv[i]+2); break; case '-': goto EndOptions; case 'h': usage(stdout, argv[0]); exit(0); default: fprintf(stderr, "%s: unknown switch: %s\n", argv[0], argv[i]); exit(1); } EndOptions: if (i >= argc) { fprintf(stderr, "%s: Missing recipient of message\n", argv[0]); exit(1); } #if HAVE_DOORS door_fd = open(door_path, O_RDONLY); if (door_fd < 0) { e = errno; if (errno != ENOENT) { syslog(LOG_ERR, "%s: open: %s\n", door_path, strerror(e)); fprintf(stderr, "%s: open(%s): %s\n", argv[0], door_path, strerror(e)); exit(1); } } if (door_fd > 0) { if (door_info(door_fd, &dib) < 0) { e = errno; syslog(LOG_ERR, "%s: door_info: %s\n", door_path, strerror(e)); fprintf(stderr, "%s: %s: door_info: %s\n", argv[0], door_path, strerror(e)); exit(1); } if (debug) { printf("door_info() -> server pid = %ld, uniquifier = %ld", (long) dib.di_target, (long) dib.di_uniquifier); if (dib.di_attributes & DOOR_LOCAL) printf(", LOCAL"); if (dib.di_attributes & DOOR_PRIVATE) printf(", PRIVATE"); if (dib.di_attributes & DOOR_REVOKED) printf(", REVOKED"); if (dib.di_attributes & DOOR_UNREF) printf(", UNREF"); putchar('\n'); } if (dib.di_attributes & DOOR_REVOKED) { syslog(LOG_ERR, "%s: door revoked\n", door_path); fprintf(stderr, "%s: door revoked\n", argv[0]); exit(1); } } #endif if (isatty(fileno(stdin))) puts("Enter message:"); buf_init(&buf); buf_load(&buf, stdin); msg = buf_getall(&buf); if (mailmode) { while (*msg && !((msg[0] == '\n' && msg[1] == '\n') || (msg[0] == '\r' && msg[1] == '\n' && msg[2] == '\r' && msg[3] == '\n'))) ++msg; switch (*msg) { case '\r': msg += 4; break; case '\n': msg += 2; } } while (i < argc) { to = s_dup(argv[i]); if (!to) { fprintf(stderr, "%s: %s: s_dup: %s\n", argv[0], argv[i], strerror(errno)); exit(1); } cp = strchr(to, '@'); if (cp) *cp = '\0'; rc = send_sms(to, msg); if (rc != 0) { ++nerr; e = errno; syslog(LOG_ERR, "%s: send failed (door_path=%s, rc=%d): %s", argv[i], door_path, strerror(e)); fprintf(stderr, "%s: %s: send failed (rc=%d): %s\n", argv[0], argv[i], rc, strerror(e)); } ++i; } exit(nerr); }
int server_init( char *machine, const char *cservice, /* usually a literal */ int port, char *text, size_t mlen) { # ifndef INET6 char temp[256]; char *service = strncpy(temp, cservice, 255); /* ...calls non-const funcs */ # endif /* !INET6 */ # ifndef VMS int sockt_rd, sockt_wr; # endif /* !VMS */ # ifdef M_AMIGA if (!s_init()) /* some initialisation ... */ return -1; # endif /* M_AMIGA */ # ifdef DECNET char *cp; cp = strchr(machine, ':'); if (cp && cp[1] == ':') { *cp = '\0'; sockt_rd = get_dnet_socket(machine, service); } else sockt_rd = get_tcp_socket(machine, service, port); # else # ifdef INET6 sockt_rd = get_tcp6_socket(machine, (unsigned short) port); # else sockt_rd = get_tcp_socket(machine, service, (unsigned short) port); # endif /* INET6 */ # endif /* DECNET */ if (sockt_rd < 0) return sockt_rd; # ifndef VMS /* * Now we'll make file pointers (i.e., buffered I/O) out of * the socket file descriptor. Note that we can't just * open a fp for reading and writing -- we have to open * up two separate fp's, one for reading, one for writing. */ if ((nntp_rd_fp = (TCP *) s_fdopen(sockt_rd, "r")) == NULL) { perror("server_init: fdopen() #1"); return -errno; } if ((sockt_wr = s_dup(sockt_rd)) < 0) { perror("server_init: dup()"); return -errno; } # ifdef TLI /* Transport Level Interface */ if (t_sync(sockt_rd) < 0) { /* Sync up new fd with TLI */ t_error("server_init: t_sync()"); nntp_rd_fp = NULL; return -EPROTO; } # else if ((nntp_wr_fp = (TCP *) s_fdopen(sockt_wr, "w")) == NULL) { perror("server_init: fdopen() #2"); nntp_rd_fp = NULL; return -errno; } # endif /* TLI */ # else sockt_wr = sockt_rd; # endif /* !VMS */ last_put[0] = '\0'; /* no retries in get_respcode */ /* * Now get the server's signon message */ return (get_respcode(text, mlen)); }
/* copydirectory: copy all regular files in src to dst, arg is of form 'src\0dst\0' */ static void *copydirectory(void *arg) { struct iofiles *iofp, *newiofp; char *src_dir, *dst_dir; char *src_path, *dst_path; struct dirent *dirp; DIR *dp; int infd, outfd; pthread_t tid; struct stat statbuf; copyinfo_t *cip, *newci; /* get inputs */ iofp = (struct iofiles *)arg; src_dir = iofp->source; dst_dir = iofp->destination; fprintf(stderr, "copydirectory got: %s %s\n", src_dir, dst_dir); tid = 0; if ((dp = opendir(src_dir)) == NULL) { err_quit("Failed to opendir: %s", src_dir); } if (stat(dst_dir, &statbuf) == -1) { if (mkdir(dst_dir, DIR_MODE) == -1) err_sys("mkdir error"); } while ((dirp = readdir(dp)) != NULL) { if ((!strcmp(dirp->d_name, ".")) || (!strcmp(dirp->d_name, ".."))) continue; src_path = buildpath(src_dir, dirp->d_name); if (stat(src_path, &statbuf) == -1) err_sys("stat error"); if ((infd = open(src_path, O_RDONLY)) < 0) err_sys("failed to open %s", src_path); dst_path = buildpath(dst_dir, dirp->d_name); if (S_ISDIR(statbuf.st_mode)) { newiofp = Malloc(sizeof(struct iofiles)); newiofp->source = s_dup(src_path); newiofp->destination = s_dup(dst_path); copydirectory((void *)newiofp); continue; } if ((outfd = open(dst_path, OFLAGS, FILE_MODE)) < 0) err_sys("failed to open %s", dst_path); newci = ci_init(dst_path, tid); newci->sourcefd = infd; newci->destinationfd = outfd; newci->next = head; head = newci; Pthread_create(&newci->tid, NULL, copyfilepass, (void *)newci); for (cip = head; cip != NULL; cip = cip->next) { (void)pthread_join(cip->tid, NULL); } free(src_path); free(dst_path); } if (closedir(dp) < 0) err_msg("failed to close directory"); return (void *)0; }