-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprinter_minimal.c
87 lines (73 loc) · 1.84 KB
/
printer_minimal.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
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/usb/g_printer.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#define PRINTER_FILE "/dev/g_printer0"
#define PRINTJOB_FILE_EXTENSION "pcl"
int main(int argc, char *argv[])
{
/* Open device file for printer gadget. */
struct pollfd fd[1];
fd[0].fd = open(PRINTER_FILE, O_RDWR);
if (fd[0].fd < 0) {
printf("[!] Error\t%d opening %s\n", fd[0].fd, PRINTER_FILE);
close(fd[0].fd);
return(-1);
}
fd[0].events = POLLIN | POLLRDNORM;
int rec = -1;
char filename[100];
char file_path[256];
FILE* cur_job;
while (1) {
static char buf[512];
int bytes_read;
int retval;
/* Wait for up to 1 second for data. */
retval = poll(fd, 1, 1000);
if (retval && (fd[0].revents & POLLRDNORM)) {
/* Data received - Create new file if needed */
if(rec == -1) {
time_t now;
struct tm ts;
// Build file name
time(&now);
ts = *localtime(&now);
strftime(filename, sizeof(filename), "%Y-%m-%d-%H-%M-%S", &ts);
snprintf(file_path, sizeof(file_path), "%s.pcl", filename);
// Create PCL file
cur_job = fopen(file_path, "w");
if(cur_job == NULL){
printf("[!] Error\tOpening %s. Exiting\n", file_path);
return -1;
}
rec = 0;
}
/* Read data from printer gadget driver. */
bytes_read = read(fd[0].fd, buf, 512);
if (bytes_read < 0) {
printf("[!] Error\t%d reading from %s\n",
fd[0].fd, PRINTER_FILE);
close(fd[0].fd);
return(-1);
} else if (bytes_read > 0) {
rec = 1;
fwrite(buf, 1, bytes_read, cur_job);
fflush(stdout);
}
} else if(rec == 1) {
/* Save printjob */
rec = -1;
fclose(cur_job);
printf("[*] RECEIVED\tPrint job %s\n", file_path);
}
}
/* Close the device file. */
close(fd[0].fd);
return 0;
}