~ubuntu-branches/ubuntu/lucid/isomd5sum/lucid

« back to all changes in this revision

Viewing changes to checkisomd5.c

  • Committer: Bazaar Package Importer
  • Author(s): Ryan Finnie
  • Date: 2008-02-12 20:50:55 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080212205055-ok7t538if9lctyws
Tags: 1:1.0.4-1
* New upstream version.
* Epoch added, upstream re-worked into standalone package
* debian/control: new homepage/git
* debian/control: clarified description
* Removed sgml documentation (manpages moved upstream)
* Added watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* simple program to check implanted md5sum in an iso 9660 image   */
2
 
/* Copyright 2001 Red Hat, Inc.                                    */
3
 
/* Michael Fulbright msf@redhat.com                                */
 
1
/*
 
2
 * checkosmd5 - simple program to check implanted md5sum
 
3
 * Copyright (C) 2001-2007 Red Hat, Inc.
 
4
 * Michael Fulbright <msf@redhat.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 */
4
20
 
5
21
#include <stdio.h>
6
22
#include <stdlib.h>
7
23
#include <string.h>
 
24
#include <popt.h>
8
25
 
9
26
#include "md5.h"
10
27
#include "libcheckisomd5.h"
11
28
 
 
29
struct progressCBData {
 
30
    int verbose;
 
31
    int gauge;
 
32
    int gaugeat;
 
33
};
 
34
 
 
35
static void outputCB(void *co, long long offset, long long total) {
 
36
    struct progressCBData *data = co;
 
37
    int gaugeval = -1;
 
38
 
 
39
    if (data->verbose) {
 
40
        printf("\rChecking: %05.1f%%", (100.0*offset)/(total));
 
41
        fflush(stdout);
 
42
    }
 
43
    if (data->gauge) {
 
44
        gaugeval = (100.0*offset)/(total);
 
45
        if (gaugeval != data->gaugeat) {
 
46
            printf("%d\n", gaugeval);
 
47
            fflush(stdout);
 
48
            data->gaugeat = gaugeval;
 
49
        }
 
50
    }
 
51
}
 
52
 
 
53
static void usage(void) {
 
54
    fprintf(stderr, "Usage: checkisomd5 [--md5sumonly] [--verbose] [--gauge] <isofilename>|<blockdevice>\n\n");
 
55
    exit(1);
 
56
}
 
57
 
12
58
int main(int argc, char **argv) {
13
 
    int i;
14
59
    int rc;
15
 
    int flags;
16
 
    int verbose;
17
 
    int gauge;
 
60
    const char **args;
18
61
    int md5only;
19
 
    int filearg;
 
62
    int help;
 
63
    struct progressCBData data;
 
64
    char * result;
 
65
    poptContext optCon;
20
66
 
21
 
    if (argc < 2) {
22
 
        printf("Usage: checkisomd5 [--md5sumonly] [--verbose] [--gauge] <isofilename>|<blockdevice>\n\n");
23
 
        exit(1);
24
 
    }
 
67
    memset(&data, 0, sizeof(struct progressCBData));
25
68
 
26
69
    md5only = 0;
27
 
    flags = 1; /* mediaCheckFile defaults to verbose, not quiet, so prepopulate the "quiet" bit */
28
 
    verbose = 0;
29
 
    gauge = 1;
30
 
    filearg = 1;
31
 
    for (i=1; i < argc; i++) {
32
 
        if (strcmp(argv[i], "--md5sumonly") == 0) {
33
 
            md5only = 1;
34
 
            filearg++;
35
 
        } else if (strcmp(argv[i], "--verbose") == 0) {
36
 
            filearg++;
37
 
            flags ^= 1;
38
 
            verbose = 1;
39
 
        } else if (strcmp(argv[i], "--gauge") == 0) {
40
 
            filearg++;
41
 
            flags ^= 2;
42
 
            gauge = 1;
43
 
        } else 
44
 
            break;
 
70
    help = 0;
 
71
    data.verbose = 0;
 
72
    data.gauge = 0;
 
73
 
 
74
    struct poptOption options[] = {
 
75
        { "md5sumonly", 'o', POPT_ARG_NONE, &md5only, 0 },
 
76
        { "verbose", 'v', POPT_ARG_NONE, &data.verbose, 0 },
 
77
        { "gauge", 'g', POPT_ARG_NONE, &data.gauge, 0},
 
78
        { "help", 'h', POPT_ARG_NONE, &help, 0},
 
79
        { 0, 0, 0, 0, 0}
 
80
    };
 
81
 
 
82
    optCon = poptGetContext("checkisomd5", argc, (const char **)argv, options, 0);
 
83
 
 
84
    if ((rc = poptGetNextOpt(optCon)) < -1) {
 
85
        fprintf(stderr, "bad option %s: %s\n",
 
86
                poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
 
87
                poptStrerror(rc));
 
88
        exit(1);
45
89
    }
46
90
 
47
 
    if (md5only|verbose)
48
 
        printMD5SUM(argv[filearg]);
 
91
    if (help)
 
92
        usage();
 
93
 
 
94
    args = poptGetArgs(optCon);
 
95
    if (!args || !args[0] || !args[0][0])
 
96
        usage();
 
97
 
 
98
    if (md5only|data.verbose)
 
99
        printMD5SUM((char *)args[0]);
49
100
 
50
101
    if (md5only)
51
102
        exit(0);
52
103
 
53
 
    rc = mediaCheckFile(argv[filearg], flags);
54
 
 
55
 
    /* 1 means it passed, 0 means it failed, -1 means we couldnt find chksum */
56
 
    if (rc == 1)
57
 
        exit(0);
 
104
    rc = mediaCheckFile((char *)args[0], outputCB, &data);
 
105
 
 
106
    if (data.verbose)
 
107
        printf("\n");
 
108
 
 
109
    if (rc == 0)
 
110
        result = "FAIL.\n\nIt is not recommended to use this media.";
 
111
    else if (rc > 0)
 
112
        result = "PASS.\n\nIt is OK to use this media.";
58
113
    else
59
 
        exit(1);
 
114
        result = "NA.\n\nNo checksum information available, unable to verify media.";
 
115
 
 
116
    fprintf(stderr, "\nThe media check is complete, the result is: %s\n", result);
 
117
 
 
118
    exit (rc == 0);
60
119
}
61
120