Annotation of embedaddon/miniupnpc/src/testminiwget.c, revision 1.1.1.1

1.1       misho       1: /* $Id: testminiwget.c,v 1.7 2018/01/16 01:01:05 nanard Exp $ */
                      2: /* vim: tabstop=4 shiftwidth=4 noexpandtab
                      3:  * Project : miniupnp
                      4:  * Author : Thomas Bernard
                      5:  * Copyright (c) 2005-2018 Thomas Bernard
                      6:  * This software is subject to the conditions detailed in the
                      7:  * LICENCE file provided in this distribution.
                      8:  * */
                      9: #include <stdio.h>
                     10: #include <stdlib.h>
                     11: #include "miniwget.h"
                     12: 
                     13: /**
                     14:  * This program uses the miniwget / miniwget_getaddr function
                     15:  * from miniwget.c in order to retrieve a web ressource using
                     16:  * a GET HTTP method, and store it in a file.
                     17:  */
                     18: int main(int argc, char * * argv)
                     19: {
                     20:    void * data;
                     21:    int size, writtensize;
                     22:    FILE *f;
                     23:    char addr[64];
                     24:    int status_code = -1;
                     25: 
                     26:    if(argc < 3) {
                     27:        fprintf(stderr, "Usage:\t%s url file\n", argv[0]);
                     28:        fprintf(stderr, "Example:\t%s http://www.google.com/ out.html\n", argv[0]);
                     29:        return 1;
                     30:    }
                     31:    data = miniwget_getaddr(argv[1], &size, addr, sizeof(addr), 0, &status_code);
                     32:    if(!data || (status_code != 200)) {
                     33:        if(data) free(data);
                     34:        fprintf(stderr, "Error %d fetching %s\n", status_code, argv[1]);
                     35:        return 1;
                     36:    }
                     37:    printf("local address : %s\n", addr);
                     38:    printf("got %d bytes\n", size);
                     39:    f = fopen(argv[2], "wb");
                     40:    if(!f) {
                     41:        fprintf(stderr, "Cannot open file %s for writing\n", argv[2]);
                     42:        free(data);
                     43:        return 1;
                     44:    }
                     45:    writtensize = fwrite(data, 1, size, f);
                     46:    if(writtensize != size) {
                     47:        fprintf(stderr, "Could only write %d bytes out of %d to %s\n",
                     48:                writtensize, size, argv[2]);
                     49:    } else {
                     50:        printf("%d bytes written to %s\n", writtensize, argv[2]);
                     51:    }
                     52:    fclose(f);
                     53:    free(data);
                     54:    return 0;
                     55: }
                     56: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>