-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.c
81 lines (66 loc) · 2.03 KB
/
Table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Erik Phillips
* CPE-357 - Systems Programming
* California Polytechnic State Unviversity
* San Luis Obispo, California
*
* Final Project: MyTar - tape archive
* Presented: 07 June 2016
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include "Tar.h"
#include "Table.h"
#include "SmartAlloc.h"
#define MODE_SIZE 10
typedef struct {
int flag;
int index;
int val;
} ModeChar;
static ModeChar MODECHRS[] = {
{S_IRUSR, 1, 'r'}, {S_IWUSR, 2, 'w'}, {S_IXUSR, 3, 'x'},
{S_IRGRP, 4, 'r'}, {S_IWGRP, 5, 'w'}, {S_IXGRP, 6, 'x'},
{S_IROTH, 7, 'r'}, {S_IWOTH, 8, 'w'}, {S_IXOTH, 9, 'x'},
{S_ISUID, 3, 's'}, {S_ISGID, 6, 's'}, {S_IFDIR, 0, 'd'},
{0, 0, 0}
};
void Table(Tar *tar) {
Header header;
ModeChar *modeChr;
int fdTar, filesize, size, ndx, mode;
char *end, modeStr[MODE_SIZE + 1], *timeStr;
time_t mtime;
fdTar = open(tar->tarfile, O_RDONLY);
if (fdTar < 0) {
OpenError(tar->tarfile, fdTar);
exit(EXIT_FAILURE);
}
while ((size = read(fdTar, &header, sizeof(Header))) != 0) {
if (strcmp(MAGIC, header.magic)) {
fprintf(stderr, "Header reading error: '%s'\n", MAGIC);
Terminate(tar);
}
filesize = strtol(header.size, &end, OCT);
lseek(fdTar, filesize, SEEK_CUR);
if (tar->opts[V_FLAG]) {
mode = strtol(header.mode, &end, OCT);
for (ndx = 0; ndx < MODE_SIZE; ndx++)
modeStr[ndx] = '-';
modeStr[ndx] = 0;
for (modeChr = MODECHRS; modeChr->flag; modeChr++)
if (mode & modeChr->flag)
modeStr[modeChr->index] = modeChr->val;
mtime = strtol(header.mtime, &end, OCT);
timeStr = asctime(gmtime(&mtime));
timeStr[strlen(timeStr) - 1] = 0;
printf("%s %s %s %9u %s ",
modeStr, header.uname, header.gname, filesize, timeStr);
}
printf("%s\n", header.name);
}
}