1
0
mirror of https://github.com/DoctorWkt/unix-jun72.git synced 2026-04-11 23:12:59 +00:00
Files
DoctorWkt.unix-jun72/src/cmd/glob.c
warren.toomey 45ebdd66d8 Warren and Doug Merritt have worked on rebuilding the code fragments from
the s1-bits tape. Doug has provided as11.s as12.s as13.s as14.s as15.s as16.s
as17.s as18.s as19.s as21.s as22.s as23.s as24.s as25.s as26.s as27.s as28.s
as29.s, and Warren has compared against his production, and against V5 (to
determine the correct trailing blank line). These files can be considered
authentic. The other file's are Warren's production only, so we need to
compare against Doug's versions to ensure correctness.
2008-05-15 00:59:49 +00:00

194 lines
2.9 KiB
C

/* global command --
glob params
"*" in params matches r.e ".*"
"?" in params matches r.e. "."
"[...]" in params matches character class
"[...a-z...]" in params matches a through z.
perform command with argument list
constructed as follows:
if param does not contain "*", "[", or "?", use it as is
if it does, find all files in current directory
which match the param, sort them, and use them
prepend the command name with "/bin" or "/usr/bin"
as required.
*/
char ab[2000]; /* generated characters */
char *ava[200]; /* generated arguments */
char **av ava;
char *string ab;
main(argc, argv)
char *argv[];
{
int i, j, c;
int inode, dirf, ap;
int fb[5], sb[17];
char *cp, *cpo;
if (argc < 3) {
write(1, "Arg count\n", 10);
return;
}
ap = 0;
av++;
fb[4] = 0;
loop:
cpo = cp = *++argv;
while(c = *cp++) if (c=='*' | c=='?' | c=='[') goto compl;
av[ap++] = copy(cpo);
if (--argc>=2) goto loop;
goto donow;
compl:
if(*--cp == '/') {
*cp = '\0';
if((dirf=open(cp==cpo? "/" : cpo, 0))<0)
goto oper;
*cp++ = '/';
goto compl1;
}
if(cp != cpo) goto compl;
if((dirf=open(".",0)) >= 0) goto compl1;
oper:
write(1, "No directory\n", 13);
return;
compl1:
j = ap;
l2:
while (read(dirf, &inode, 2)>0) {
read(dirf, fb, 8);
if (inode==0) goto l2;
if (match(fb, cp)) {
c = *cp;
*cp = '\0';
av[ap++] = cat(cpo, fb);
*cp = c;
}
}
close(dirf);
i = j;
while(i<ap-1) {
j = i;
while(++j<ap) {
if (compar(av[i],av[j])) {
c = av[i];
av[i] = av[j];
av[j] = c;
}
}
i++;
}
if (--argc>=2) goto loop;
donow:
if (ap<=1) {
write(1, "No match\n", 9);
return;
}
av[ap] = 0;
execv(av[0], av);
i = cat("/bin/", av[0]);
execv(i, av);
i = cat("/usr", i);
execv(i, av);
if (stat(i, sb) == 0) {
*av = i;
*--av = "/bin/sh";
execv(av[0], av);
}
write(1, "No command\n", 11);
}
match(s, p)
char *s, *p; {
if (*s=='.' & *p!='.') return(0);
return(amatch(s, p));
}
amatch(s, p)
char *s, *p;
{
int c, cc, ok, lc, scc;
scc = *s;
lc = 077777;
switch (c = *p) {
case '[':
ok = 0;
while (cc = *++p) {
switch (cc) {
case ']':
if (ok)
return(amatch(++s, ++p));
else
return(0);
case '-':
ok =| lc <= scc & scc <= (cc=p[1]);
}
if (scc==(lc=cc)) ok++;
}
return(0);
case '?':
caseq:
if(scc) return(amatch(++s, ++p));
return(0);
case '*':
return(umatch(s, ++p));
case 0:
return(!scc);
}
if (c==scc) goto caseq;
return(0);
}
umatch(s, p)
char *s, *p;
{
if(*p==0) return(1);
while(*s)
if (amatch(s++,p)) return(1);
return(0);
}
compar(s1,s2)
char *s1, *s2;
{
int c1,c2;
loop:
if ((c1 = *s1++)==0) return(0);
if ((c2 = *s2++)==0) return(1);
if (c1==c2) goto loop;
return(c1>c2);
}
copy(s1)
char *s1;
{
char *ts;
ts = string;
while(*string++ = *s1++);
return(ts);
}
cat(s1, s2)
char *s1, *s2;
{
char *ts;
ts = string;
while(*string++ = *s1++);
string--;
while(*string++ = *s2++);
return(ts);
}