Guest User

Untitled

a guest
Apr 14th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. /* lsgbfs.c
  2.    list objects in a GBFS file
  3.  
  4. Copyright (C) 2002  Damian Yerrick
  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
  18.   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.   Boston, MA  02111-1307, USA.
  20. GNU licenses can be viewed online at http://www.gnu.org/copyleft/
  21.  
  22. In addition, as a special exception, Damian Yerrick gives
  23. permission to link the code of this program with import libraries
  24. distributed as part of the plug-in development tools published by
  25. the vendor of any image manipulation program, and distribute
  26. linked combinations including the two.  You must obey the GNU
  27. General Public License in all respects for all of the code used
  28. other than the plug-in interface.  If you modify this file, you
  29. may extend this exception to your version of the file, but you
  30. are not obligated to do so.  If you do not wish to do so, delete
  31. this exception statement from your version.
  32.  
  33. Visit http://www.pineight.com/ for more information.
  34.  
  35. */
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40.  
  41. #define strequ(a,b) (!strcmp(a,b))
  42.  
  43. typedef unsigned short u16;
  44. typedef unsigned long u32;
  45.  
  46. #include "../gbfs.h"
  47.  
  48.  
  49. /* fgeti16() ***************************
  50.    read a 16-bit integer in intel format from a file
  51. */
  52. u32 fgeti16(FILE *fp)
  53. {
  54.   return ((fgetc(fp) & 0x000000ff)      ) |
  55.          ((fgetc(fp) & 0x000000ff) <<  8);
  56. }
  57.  
  58.  
  59. /* fgeti32() ***************************
  60.    read a 32-bit integer in intel format from a file
  61. */
  62. u32 fgeti32(FILE *fp)
  63. {
  64.   return ((fgetc(fp) & 0x000000ff)      ) |
  65.          ((fgetc(fp) & 0x000000ff) <<  8) |
  66.          ((fgetc(fp) & 0x000000ff) << 16) |
  67.          ((fgetc(fp) & 0x000000ff) << 24);
  68. }
  69.  
  70.  
  71. int main(int argc, char **argv)
  72. {
  73.   FILE *fp;
  74.   char filename[32] = {0};
  75.   u32 dir_off, dir_nmemb;
  76.   unsigned int i;
  77.  
  78.   if(argc != 2 || strequ("-h", argv[1]) || strequ("--help", argv[1]))
  79.   {
  80.     fputs("lists the objects in a gbfs file\n"
  81.           "syntax: lsgbfs FILE\n", stderr);
  82.     return 1;
  83.   }
  84.  
  85.   fp = fopen(argv[1], "rb");
  86.   if(!fp)
  87.   {
  88.     fputs("could not open ", stderr);
  89.     perror(argv[1]);
  90.     return 1;
  91.   }
  92.  
  93.   /* read pertinent header fields */
  94.   fseek(fp, 20, SEEK_SET);
  95.   dir_off = fgeti16(fp);
  96.   dir_nmemb = fgeti16(fp);
  97.  
  98.   for(i = 0; i < dir_nmemb; i++)
  99.   {
  100.     unsigned long len;
  101.  
  102.     fseek(fp, dir_off + 32 * i, SEEK_SET);
  103.     fread(filename, 24, 1, fp);
  104.     len = fgeti32(fp);
  105.     printf("%10lu %s\n", (unsigned long)len, filename);
  106.   }
  107.  
  108.   fclose(fp);
  109.   return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment