-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchList.c
executable file
·121 lines (83 loc) · 1.76 KB
/
searchList.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
/*
TITLE: OPERATING SYSTEMS LABS
AUTHOR 1: MARTIÑO RIVERA DOURADO
AUTHOR 2: CARMEN CORRALES CAMELLO
DATE: 13/12/2017
*/
/*
* Searchlist implementation
* */
#include "searchList.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAXNOMBRE 1024
#define MAXSEARCHLIST 128
char *searchlist[MAXSEARCHLIST]={NULL};
int SearchListAddDir(char * dir){
int i=0;
char * p;
while (i<MAXSEARCHLIST-2 && searchlist[i]!=NULL)
i++;
if (i==MAXSEARCHLIST-2){
errno=ENOSPC;
return -1;
} /*does not fit*/
if ((p=strdup(dir))==NULL)
return -1;
searchlist[i]=p;
searchlist[i+1]=NULL;
return 0;
}
void SearchListNew(){
int i;
for (i=0; searchlist[i]!=NULL; i++){
free(searchlist[i]);
searchlist[i]=NULL;
}
}
void SearchListShow(){
int i;
for (i=0; searchlist[i]!=NULL; i++)
printf ("\t- %s\n",searchlist[i]);
}
void SearchListAddPath(){
char *aux;
char *p;
if ((p=getenv("PATH"))==NULL){
printf ("Impossible to obtain PATH of the system\n");
return;
}
aux=strdup(p);
if ((p=strtok (aux,":"))!=NULL && SearchListAddDir(p)==-1)
printf ("Impossible to add %s: %s\n", p, strerror(errno));
while ((p=strtok(NULL,":"))!=NULL)
if (SearchListAddDir(p)==-1){
printf ("Impossible to add %s: %s\n", p, strerror(errno));
break;
}
free(aux);
}
char * SearchExecutable(char * ejec){
static char aux[MAXNOMBRE];
int i;
struct stat s;
if (ejec==NULL)
return NULL;
if (ejec[0]=='/' || !strncmp (ejec,"./",2) || !strncmp (ejec,"../",2)){
if (stat(ejec,&s)!=-1)
return ejec;
else
return NULL;
}
for (i=0;searchlist[i]!=NULL; i++){
sprintf(aux,"%s/%s",searchlist[i],ejec);
if (stat(aux,&s)!=-1)
return aux;
}
return NULL;
}