btrfs-progs: add getopt stubs where needed
[platform/upstream/btrfs-progs.git] / btrfs-fragments.c
index 4dd9470..17768c3 100644 (file)
@@ -14,7 +14,6 @@
  * Boston, MA 021110-1307, USA.
  */
 
-#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -191,7 +190,6 @@ list_fragments(int fd, u64 flags, char *dir)
        u64 bgused = 0;
        u64 saved_extent = 0;
        u64 saved_len = 0;
-       u64 saved_flags = 0;
        int saved_color = 0;
        u64 last_end = 0;
        u64 areas = 0;
@@ -202,7 +200,6 @@ list_fragments(int fd, u64 flags, char *dir)
 
        gdImagePtr im = NULL;
        int black = 0;
-       int white = 0;
        int width = 800;
 
        snprintf(name, sizeof(name), "%s/index.html", dir);
@@ -235,7 +232,7 @@ list_fragments(int fd, u64 flags, char *dir)
                ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
                if (ret < 0) {
                        fprintf(stderr, "ERROR: can't perform the search\n");
-                       return ret;
+                       goto out_close;
                }
                /* the ioctl returns the number of item it found in nr_items */
                if (sk->nr_items == 0)
@@ -280,10 +277,9 @@ list_fragments(int fd, u64 flags, char *dir)
                                im = gdImageCreate(width,
                                        (sh->offset / 4096 + 799) / width);
 
-                               white = gdImageColorAllocate(im, 255, 255, 255);
                                black = gdImageColorAllocate(im, 0, 0, 0);  
 
-                               for (j = 0; j < 10; ++j)
+                               for (j = 0; j < ARRAY_SIZE(colors); ++j)
                                        colors[j] = black;
 
                                init_colors(im, colors);
@@ -308,13 +304,11 @@ list_fragments(int fd, u64 flags, char *dir)
                                saved_len = 0;
                        }
                        if (im && sh->type == BTRFS_EXTENT_ITEM_KEY) {
-                               u64 e_flags;
                                int c;
                                struct btrfs_extent_item *item;
 
                                item = (struct btrfs_extent_item *)
                                                (args.buf + off);
-                               e_flags = btrfs_stack_extent_flags(item);
 
                                if (use_color)
                                        c = colors[get_color(item, sh->len)];
@@ -328,7 +322,6 @@ list_fragments(int fd, u64 flags, char *dir)
                                if (sh->objectid == bgend) {
                                        saved_extent = sh->objectid;
                                        saved_len = sh->offset;
-                                       saved_flags = e_flags;
                                        saved_color = c;
                                        goto skip;
                                }
@@ -379,7 +372,10 @@ skip:;
                fprintf(html, "</p>");
        }
        fprintf(html, "</body></html>\n");
-       
+
+out_close:
+       fclose(html);
+
        return ret;
 }
 
@@ -431,18 +427,18 @@ int main(int argc, char **argv)
                }
        }
 
-       if (optind < argc) {
-               path = argv[optind++];
-       } else {
+       set_argv0(argv);
+       argc = argc - optind;
+       if (check_argc_min(argc, 1)) {
                usage();
                exit(1);
        }
 
-       fd = open_file_or_dir(path, &dirstream);
-       if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", path);
+       path = argv[optind++];
+
+       fd = btrfs_open_dir(path, &dirstream, 1);
+       if (fd < 0)
                exit(1);
-       }
 
        if (flags == 0)
                flags = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;