is_tar.c 2.3 KB

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