is_tar.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * is_tar() -- figure out whether file is a tar archive.
  3. *
  4. * Stolen (by the author!) from the public domain tar program:
  5. * Pubic Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
  6. *
  7. * @(#)list.c 1.18 9/23/86 Public Domain - gnu
  8. * $Id: is_tar.c,v 1.10 1998/06/27 13:23:39 christos Exp $
  9. *
  10. * Comments changed and some code/comments reformatted
  11. * for file command by Ian Darwin.
  12. */
  13. #ifdef HAVE_CONFIG_H
  14. #include <config.h>
  15. #endif
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include <sys/types.h>
  19. #include "tar.h"
  20. #include "file.h"
  21. #ifndef lint
  22. FILE_RCSID("@(#)$Id: is_tar.c,v 1.10 1998/06/27 13:23:39 christos Exp $")
  23. #endif
  24. #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
  25. #if defined(__STDC__) || defined(__cplusplus)
  26. static int from_oct(int, char*); /* Decode octal number */
  27. #else
  28. static int from_oct();
  29. #endif
  30. /*
  31. * Return
  32. * 0 if the checksum is bad (i.e., probably not a tar archive),
  33. * 1 for old UNIX tar file,
  34. * 2 for Unix Std (POSIX) tar file.
  35. */
  36. int
  37. is_tar(buf, nbytes)
  38. unsigned char *buf;
  39. int nbytes;
  40. {
  41. register union record *header = (union record *)buf;
  42. register int i;
  43. register int sum, recsum;
  44. register char *p;
  45. if (nbytes < sizeof(union record))
  46. return 0;
  47. recsum = from_oct(8, header->header.chksum);
  48. sum = 0;
  49. p = header->charptr;
  50. for (i = sizeof(union record); --i >= 0;) {
  51. /*
  52. * We can't use unsigned char here because of old compilers,
  53. * e.g. V7.
  54. */
  55. sum += 0xFF & *p++;
  56. }
  57. /* Adjust checksum to count the "chksum" field as blanks. */
  58. for (i = sizeof(header->header.chksum); --i >= 0;)
  59. sum -= 0xFF & header->header.chksum[i];
  60. sum += ' '* sizeof header->header.chksum;
  61. if (sum != recsum)
  62. return 0; /* Not a tar archive */
  63. if (0==strcmp(header->header.magic, TMAGIC))
  64. return 2; /* Unix Standard tar archive */
  65. return 1; /* Old fashioned tar archive */
  66. }
  67. /*
  68. * Quick and dirty octal conversion.
  69. *
  70. * Result is -1 if the field is invalid (all blank, or nonoctal).
  71. */
  72. static int
  73. from_oct(digs, where)
  74. register int digs;
  75. register char *where;
  76. {
  77. register int value;
  78. while (isspace(*where)) { /* Skip spaces */
  79. where++;
  80. if (--digs <= 0)
  81. return -1; /* All blank field */
  82. }
  83. value = 0;
  84. while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */
  85. value = (value << 3) | (*where++ - '0');
  86. --digs;
  87. }
  88. if (digs > 0 && *where && !isspace(*where))
  89. return -1; /* Ended on non-space/nul */
  90. return value;
  91. }