static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) { struct connection *conn = userdata; int bytes = size * nmemb; if (conn->out == -1) { char *fname; if (conn->regexp && !conn->matched) fname = conn->regfname; else { if (want_extensions) /* We allocated space for the extension in add_outname */ strcat(conn->outname, lazy_imgtype(ptr)); fname = conn->outname; } if ((conn->out = creat(fname, 0644)) < 0) { my_perror(fname); fail_connection(conn); return bytes; } conn->connected = 1; } int n = write(conn->out, ptr, bytes); if (n != bytes) printf("Write error\n"); return n; }
/* This is the only place we write to the output file */ static int write_output(struct connection *conn, int bytes) { int n; if (conn->out == -1) { /* deferred open */ /* We alloced space for the extension in add_outname */ char *buf = conn->zs ? (char *)conn->zs_buf : conn->curp; if (want_extensions) strcat(conn->outname, lazy_imgtype(buf)); conn->out = open(conn->outname, WRITE_FLAGS, 0664); if (conn->out < 0) { my_perror(conn->outname); return 0; } if (verbose > 1) printf("Output %s -> %s\n", conn->url, conn->outname); } if (conn->zs) n = write(conn->out, conn->zs_buf, bytes); else n = write(conn->out, conn->curp, bytes); if (n != bytes) { if (n < 0) printf("%s: Write error: %s\n", conn->outname, strerror(errno)); else printf("%s: Write error: %d/%d\n", conn->outname, n, bytes); return 0; } return bytes; }