-
Notifications
You must be signed in to change notification settings - Fork 0
/
lister.c
145 lines (136 loc) · 3.59 KB
/
lister.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* \file lister.c
* \brief Programme qui liste les jobs dans le spool
* \author ROSTAQI Yossef et PENG Zhigang
* \date 7 Novembre 2017
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include "check.h"
#define MONEOF -1
#define LOCK_FILE "verrou"
#define SPOOL "spool"
char * concat(char * a, char * b){
char * dest = (char *) malloc(strlen(a) + strlen(b) + 1);
strcpy(dest, a);
strcat(dest, b);
return dest;
}
/**
* \fn void lister(int lflag, int uflag, char * user)
* \brief Liste les jobs dans le spool sur la sortie standard
*
* \param int lflag, flag de l'option i
* \param int uflag, flag de l'option u
* \param char * user, nom de l'utilisateur si l'option u a été activée
* \return void
*/
void lister(int lflag, int uflag, char * user){
int lfp = open(LOCK_FILE, O_RDWR | O_CREAT, 0666);
while (lockf(lfp, F_TEST, 0) != 0)
{
sleep(1); /* can not lock */
}
lockf(lfp, F_LOCK, 0);
FILE * fd;
struct stat * stbuf = malloc(sizeof(struct stat));
CHECK(stat(SPOOL, stbuf) != MONEOF);
DIR * dp = NULL;
char * chemin_spool = NULL;
if(getenv("PROJETSE") != NULL){
CHECK(stat(getenv("PROJETSE"), stbuf) != MONEOF);
dp = opendir(getenv("PROJETSE"));
chemin_spool = concat(getenv("PROJETSE"), "/");
}
else{
CHECK(stat(SPOOL, stbuf) != MONEOF);
dp = opendir(SPOOL);
chemin_spool = concat(SPOOL, "/");
}
struct dirent * d;
char login[20];
char nomfich[50];
char jour[5];
char mois [5];
int numjour, heure, minute, seconde, annee;
// char * chemin_spool = concat(SPOOL,"/");
if(lflag || uflag){
if(stbuf -> st_uid != getuid()){
fprintf(stderr, "lister: Option -l or -u are restricted to spool owner \n");
free(stbuf);
exit(1);
}
}
while((d = readdir(dp)) != NULL){
//On ignore les fichiers . et .. ainsi que les fichiers commençant par j
if(strcmp (d -> d_name, ".") != 0 && strcmp(d -> d_name, "..") != 0 && d -> d_name[0] != 'j'){
char * chemin = concat(chemin_spool, d->d_name);
char * id = (char *) malloc(strlen(d->d_name) + 1);
//On copie l'id XXXXXX dans id
strcpy(id, &d->d_name[strlen(d->d_name) - 6]);
fd = fopen(chemin, "r");
CHECK(fd != NULL);
fscanf(fd, "%s %*d %s %s %s %d %d%*c%d%*c%d %d", login, nomfich, jour, mois, &numjour, &heure, &minute, &seconde, &annee);
if(lflag){
if(!uflag || (uflag && (strcmp(user, login) == 0)))
printf("%s %s %s\t%s %s %d %d:%d:%d %d\n", id, login, nomfich, jour, mois, numjour, heure, minute, seconde, annee);
else{
fclose(fd);
free(chemin);
free(id);
continue;
}
}
else if(uflag){
if(strcmp(user, login) == 0)
printf("%s %s\t\t%s %s %d %d:%d:%d %d\n", id, login, jour, mois, numjour, heure, minute, seconde, annee);
else{
fclose(fd);
free(chemin);
free(id);
continue;
}
}
else
printf("%s %s\t\t%s %s %d %d:%d:%d %d\n", id, login, jour, mois, numjour, heure, minute, seconde, annee);
fclose(fd);
free(chemin);
free(id);
}
}
free(chemin_spool);
closedir(dp);
free(stbuf);
lockf(lfp, F_ULOCK, 0);
close(lfp);
}
int main (int argc, char ** argv){
int c, lflag = 0, uflag = 0;
extern char * optarg;
char * input;
extern int optind;
while ((c = getopt (argc, argv, "lu:")) != MONEOF){
switch (c){
case 'l' :
lflag++;
break;
case 'u' :
input = optarg;
uflag++;
break;
default :
fprintf(stderr, "Usage : lister -l -u <user> \n");
exit (1);
break;
}
}
lister(lflag, uflag, input);
}