"Fossies" - the Fresh Open Source Software Archive

Member "cook-2.34/cookfp/main.c" (25 Sep 2010, 5582 Bytes) of package /linux/misc/old/cook-2.34.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *      cook - file construction tool
    3  *      Copyright (C) 1994, 1997-1999, 2001, 2006-2008 Peter Miller
    4  *
    5  *      This program is free software; you can redistribute it and/or modify
    6  *      it under the terms of the GNU General Public License as published by
    7  *      the Free Software Foundation; either version 3 of the License, or
    8  *      (at your option) any later version.
    9  *
   10  *      This program is distributed in the hope that it will be useful,
   11  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *      GNU General Public License for more details.
   14  *
   15  *      You should have received a copy of the GNU General Public License
   16  *      along with this program. If not, see
   17  *      <http://www.gnu.org/licenses/>.
   18  */
   19 
   20 #include <common/ac/errno.h>
   21 #include <common/ac/stdio.h>
   22 #include <common/ac/stdlib.h>
   23 
   24 #include <common/arglex.h>
   25 #include <common/error_intl.h>
   26 #include <common/fp/cksum.h>
   27 #include <common/fp/cksum.h>
   28 #include <common/fp/combined.h>
   29 #include <common/fp/ident.h>
   30 #include <common/fp/md5.h>
   31 #include <common/fp/snefru.h>
   32 #include <common/help.h>
   33 #include <common/progname.h>
   34 #include <common/version.h>
   35 #include <common/str_list.h>
   36 
   37 
   38 enum
   39 {
   40     arglex_token_cksum,
   41     arglex_token_ident,
   42     arglex_token_md5,
   43     arglex_token_snefru
   44 };
   45 
   46 static arglex_table_ty argtab[] =
   47 {
   48     { "-Checksum", arglex_token_cksum },
   49     { "-Identifier", arglex_token_ident },
   50     { "-Message_Digest", arglex_token_md5 },
   51     { "-Snefru", arglex_token_snefru },
   52     { 0, 0 }    /* end marker */
   53 };
   54 
   55 
   56 static void
   57 usage(void)
   58 {
   59     char            *progname;
   60 
   61     progname = progname_get();
   62     fprintf(stderr, "Usage: %s [ <option>... ][ <filename>... ]\n", progname);
   63     fprintf(stderr, "       %s -Help\n", progname);
   64     exit(1);
   65 }
   66 
   67 
   68 int
   69 main(int argc, char **argv)
   70 {
   71     string_ty       *minus;
   72     string_list_ty  file;
   73     size_t          j;
   74     fingerprint_methods_ty *method;
   75     string_ty       *s;
   76 
   77     arglex_init(argc, argv, argtab);
   78     str_initialize();
   79     switch (arglex())
   80     {
   81     case arglex_token_help:
   82         help((char *)0, usage);
   83         exit(0);
   84 
   85     case arglex_token_version:
   86         version();
   87         exit(0);
   88 
   89     default:
   90         break;
   91     }
   92 
   93     method = 0;
   94     string_list_constructor(&file);
   95     minus = str_from_c("-");
   96     while (arglex_token != arglex_token_eoln)
   97     {
   98         switch (arglex_token)
   99         {
  100         default:
  101             generic_argument(usage);
  102             continue;
  103 
  104         case arglex_token_snefru:
  105             if (method)
  106             {
  107                 too_many_methods:
  108                 error_intl(0, i18n("too many methods specified"));
  109                 usage();
  110             }
  111             method = &fp_snefru;
  112             break;
  113 
  114         case arglex_token_ident:
  115             if (method)
  116                 goto too_many_methods;
  117             method = &fp_ident;
  118             break;
  119 
  120         case arglex_token_md5:
  121             if (method)
  122                 goto too_many_methods;
  123             method = &fp_md5;
  124             break;
  125 
  126         case arglex_token_cksum:
  127             if (method)
  128                 goto too_many_methods;
  129             method = &fp_cksum;
  130             break;
  131 
  132         case arglex_token_string:
  133             s = str_from_c(arglex_value.alv_string);
  134             string_list_append(&file, s);
  135             str_free(s);
  136             break;
  137 
  138         case arglex_token_stdio:
  139             if (string_list_member(&file, minus))
  140             {
  141                 error_intl(0, i18n("may only name standard input once"));
  142                 usage();
  143             }
  144             string_list_append(&file, minus);
  145             break;
  146         }
  147         arglex();
  148     }
  149 
  150     /*
  151      * if no files named, read stdin
  152      */
  153     if (!file.nstrings)
  154         string_list_append(&file, minus);
  155 
  156     /*
  157      * by default, use the fp_combined class
  158      */
  159     if (!method)
  160         method = &fp_combined;
  161 
  162     /*
  163      * read the named files
  164      */
  165     for (j = 0; j < file.nstrings; ++j)
  166     {
  167         fingerprint_ty  *p;
  168         char            buf[1000];
  169 
  170         p = fingerprint_new(method);
  171         s = file.string[j];
  172         if (str_equal(s, minus))
  173         {
  174             if (fingerprint_file_sum(p, (char *)0, buf, sizeof(buf)))
  175             {
  176                 int             err;
  177                 string_ty       *s2;
  178                 sub_context_ty  *scp;
  179 
  180                 scp = sub_context_new();
  181                 err = errno;
  182                 s2 = subst_intl(scp, i18n("standard input"));
  183                 /* re-use substitution context */
  184                 sub_errno_setx(scp, err);
  185                 sub_var_set_string(scp, "File_Name", s2);
  186                 fatal_intl(scp, i18n("fingerprint $filename: $errno"));
  187                 /* NOTREACHED */
  188                 sub_context_delete(scp);
  189                 str_free(s2);
  190             }
  191             printf("%s", buf);
  192             if (file.nstrings != 1)
  193                 printf(" stdin");
  194             printf("\n");
  195         }
  196         else
  197         {
  198             if (fingerprint_file_sum(p, s->str_text, buf, sizeof(buf)))
  199             {
  200                 sub_context_ty  *scp;
  201 
  202                 scp = sub_context_new();
  203                 sub_errno_set(scp);
  204                 sub_var_set_string(scp, "File_Name", s);
  205                 fatal_intl(scp, i18n("fingerprint $filename: $errno"));
  206                 /* NOTREACHED */
  207                 sub_context_delete(scp);
  208             }
  209             printf("%s %s\n", buf, s->str_text);
  210         }
  211         fingerprint_delete(p);
  212     }
  213     exit(0);
  214     return 0;
  215 }