btrfs-progs: Fix getopt on arm/ppc platforms
[platform/upstream/btrfs-progs.git] / btrfs-crc.c
1 /*
2  * Copyright (C) 2013 STRATO.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include "crc32c.h"
23
24 void usage(void)
25 {
26         printf("usage: btrfs-crc filename\n");
27         printf("    print out the btrfs crc for \"filename\"\n");
28         printf("usage: btrfs-crc filename -c crc [-s seed] [-l length]\n");
29         printf("    brute force search for file names with the given crc\n");
30         printf("      -s seed    the random seed (default: random)\n");
31         printf("      -l length  the length of the file names (default: 10)\n");
32         exit(1);
33 }
34
35 int main(int argc, char **argv)
36 {
37         int c;
38         unsigned long checksum = 0;
39         char *str;
40         char *buf;
41         int length = 10;
42         int seed = getpid() ^ getppid();
43         int loop = 0;
44         int i;
45
46         while ((c = getopt(argc, argv, "l:c:s:h")) != -1) {
47                 switch (c) {
48                 case 'l':
49                         length = atol(optarg);
50                         break;
51                 case 'c':
52                         sscanf(optarg, "%li", &checksum);
53                         loop = 1;
54                         break;
55                 case 's':
56                         seed = atol(optarg);
57                         break;
58                 case 'h':
59                         usage();
60                 case '?':
61                         return 255;
62                 }
63         }
64
65         str = argv[optind];
66
67         if (!loop) {
68                 if (optind >= argc) {
69                         fprintf(stderr, "not enough arguments\n");
70                         return 255;
71                 }
72                 printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
73                 return 0;
74         }
75
76         buf = malloc(length);
77         if (!buf)
78                 return -ENOMEM;
79         srand(seed);
80
81         while (1) {
82                 for (i = 0; i < length; i++)
83                         buf[i] = rand() % 94 + 33;
84                 if (crc32c(~1, buf, length) == checksum)
85                         printf("%12lu - %.*s\n", checksum, length, buf);
86         }
87
88         return 0;
89 }