| 1 | /* |
| 2 | * Line printer queue |
| 3 | */ |
| 4 | |
| 5 | #include <sys/types.h> |
| 6 | #include <dir.h> |
| 7 | #include <stat.h> |
| 8 | #include <stdio.h> |
| 9 | #include <sccs.h> |
| 10 | |
| 11 | #define MAXJOBS 100 |
| 12 | |
| 13 | struct dir dirent; |
| 14 | struct stat stbuf; |
| 15 | char lpddir[] = "/usr/spool/lpd"; |
| 16 | int nextflag; |
| 17 | int linecnt; |
| 18 | FILE *df; |
| 19 | FILE *jf; |
| 20 | char line[100]; |
| 21 | char username[10]; |
| 22 | int cnt; |
| 23 | int isdown; |
| 24 | |
| 25 | main() |
| 26 | { |
| 27 | |
| 28 | if (access("/usr/bin/lpr", 1) && access("/bin/lpr", 1) |
| 29 | && access("/usr/ucb/lpr", 1)) |
| 30 | isdown++; |
| 31 | if (chdir(lpddir) < 0) { |
| 32 | perror(lpddir); |
| 33 | exit(1); |
| 34 | } |
| 35 | oloop: |
| 36 | df = fopen(".", "r"); |
| 37 | if (df == NULL) { |
| 38 | perror(lpddir); |
| 39 | exit(1); |
| 40 | } |
| 41 | loop: |
| 42 | fseek(df, 0l, 0); |
| 43 | linecnt = 0; |
| 44 | cnt = 0; |
| 45 | while (fread(&dirent, sizeof dirent, 1, df) == 1) { |
| 46 | if (dirent.d_ino == 0) |
| 47 | continue; |
| 48 | if (dirent.d_name[0] != 'd') |
| 49 | continue; |
| 50 | if (dirent.d_name[1] != 'f') |
| 51 | continue; |
| 52 | if (stat(dirent.d_name, &stbuf) < 0) |
| 53 | continue; |
| 54 | if (cnt == 0) |
| 55 | printf("Owner\t Id Chars Filename\n"); |
| 56 | cnt++; |
| 57 | process(); |
| 58 | } |
| 59 | if (cnt == 0) { |
| 60 | if (isdown) |
| 61 | printf("Line printer is down.\n"); |
| 62 | else |
| 63 | printf("Line printer queue is empty.\n"); |
| 64 | } |
| 65 | exit(0); |
| 66 | } |
| 67 | |
| 68 | process() |
| 69 | { |
| 70 | |
| 71 | jf = fopen(dirent.d_name, "r"); |
| 72 | if (jf == NULL) |
| 73 | return; |
| 74 | while (getline()) { |
| 75 | switch (line[0]) { |
| 76 | |
| 77 | case 'L': |
| 78 | strcpy(username, line+1); |
| 79 | break; |
| 80 | |
| 81 | case 'B': |
| 82 | case 'F': |
| 83 | if (stat(line+1, &stbuf) < 0) |
| 84 | stbuf.st_size = 0; |
| 85 | printf("%-10s%5s%8d %s\n", username, dirent.d_name+3, |
| 86 | stbuf.st_size, line+1); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | close(jf); |
| 91 | } |
| 92 | |
| 93 | getline() |
| 94 | { |
| 95 | register int i, c; |
| 96 | |
| 97 | i = 0; |
| 98 | while ((c = getc(jf)) != '\n') { |
| 99 | if (c <= 0) |
| 100 | return(0); |
| 101 | if (i < 100) |
| 102 | line[i++] = c; |
| 103 | } |
| 104 | line[i++] = 0; |
| 105 | return (1); |
| 106 | } |