#include "ufk.h" #include #define MAXENT 50 /* max number of files in a help directory */ int file_typed, /* Number of files typed */ noprint, /* don't print now */ pos, /* printed position */ hdr_printed, /* banner printed */ file_printed; /* file printed */ help() { file_typed = 0; /* Setup initial values */ pos = 0; noprint = FALSE; hdr_printed = FALSE; file_printed = FALSE; do_help(HELPDIR,1,numprm-1,params); /* call the workhorse */ if (!file_typed) /* Found anything ? */ prterr(ER_NOTOPIC); /* No, no such topic */ } do_help(directory,firstarg,filecount,filelist) char *directory, *filelist[]; int firstarg, filecount; { int fp, fp1, size; char hlpstr[80], savstr[80], save_file[80], *pt, *save_pt, *calloc(), get_info(); *save_file = '\0'; /* No saved info yet */ if ((fp = open(directory,0)) == -1) /* Open current directory */ return(prterr(ER_DIROPN)); if ((pt = calloc(MAXENT,DIRSIZ)) == 0) /* Get memory for directory info */ return(prterr(ER_NOMEM)); save_pt = pt; /* Save so we can deallocate it later */ size = readdir(fp,pt); /* Read all directory entry's */ while (size--) /* Repeat for every entry */ { strcpy(hlpstr,directory); /* Save current directory */ if ((firstarg - 1 >= filecount) && /* Upper level reached, ignore */ (*pt != '.')) /* files with leading 'dot' */ { if (!noprint) /* We have to print something */ { if (!hdr_printed && (file_typed == 0)) { hdr_printed = TRUE; /* Print header info */ printf("Information available:\n\n"); } prt_name(pt); /* Print available entry */ } } else /* More levels to come */ { if ((*pt != '.') && /* Ignore leading 'dot' entry's */ (hlpmatch(pt,filelist[firstarg]))) /* Check for a match */ { strcat(hlpstr,"/"); /* Create full filespec */ hlpstr[strlen(hlpstr) + DIRSIZ] = '\0'; /* Nice terminator */ strncat(hlpstr,pt,DIRSIZ); if (get_info(hlpstr) == '.')/* If first byte of file == '.' */ { strcpy(savstr,directory); /* More to come */ strcat(savstr,"/."); /* Create sub-directory spec */ savstr[strlen(savstr) + DIRSIZ] = '\0'; /* Nice terminator */ strncat(savstr,pt,DIRSIZ); if (firstarg >= filecount) /* Save info in case ambiguous */ { noprint = TRUE; /* Don't print it yet */ strcpy(save_file,hlpstr); /* Save current spec */ } do_help(savstr,firstarg+1,filecount,filelist); /* Recursive */ noprint = FALSE; } else type_file(hlpstr); /* Show this topic */ } } pt += DIRSIZ; /* Point to next entry */ } if (*save_file) /* Some info left ? */ { type_file(save_file); /* Print it */ if (file_typed <= 1) /* More to come ? */ { printf("\nAdditional information available:\n\n"); do_help(savstr,firstarg+1,filecount,filelist); /* Recursive call */ } } if (pos) /* Printed something ? */ printf("\n"); pos = 0; free(save_pt); /* Return buffer space */ } char get_info(file) char *file; { int fp; char v; if ((fp = open(file,0)) == 0) /* Open data file */ return(prterr(ER_OPENERR)); if (read(fp,&v,1) == 0) /* Get first byte */ return(prterr(ER_NOHELP)); close(fp); return v; /* Give it back */ } readdir(fp,table) int fp; char *table; { extern int qcmp(); char *pt; int nument; struct direct dirent; nument = 0; /* No valid entries yet */ pt = table; /* Point to start of table */ while (read(fp,&dirent,sizeof(struct direct)) != 0) /* Read dir entry */ if (dirent.d_ino != 0) /* If not a deleted one */ { strcpy(pt,dirent.d_name); /* Save for later */ pt += DIRSIZ; /* Point to next entry */ nument++; /* Count this one */ } close(fp); qsort(table,nument,DIRSIZ,qcmp); /* Sort it nicely */ return(nument); /* Return number of entries */ } qcmp(a,b) char *a, *b; { return strncmp(a,b,DIRSIZ); /* Compare entries for sort */ } prt_name(name) char *name; { printf("%-14.14s ",name); /* Print this name */ if (++pos > 4) /* Count position */ { printf("\n"); /* Time for a new line */ pos = 0; } file_typed++; /* Count file displayed */ } hlpmatch(a1,a2) char *a1,*a2; { int cnt, len; cnt = 0; /* No characters matched yet */ len = strlen(a2); while (*a1 && *a2) /* As long as not end of strings */ if (*a1++ == *a2++) cnt++; /* These characters match */ return (cnt == len); /* Match against full string */ } type_file(name) char *name; { FILE *fp; char buf[80]; if ((fp = fopen(name,"r")) == -1) /* Open file to type */ return(prterr(ER_OPENERR)); if (file_printed) /* New line if we were here before */ printf("\n"); else file_printed = TRUE; printf("%s\n\n",rindex(name,'/') + 1); /* Print this filespec */ fgets(buf,80,fp); /* Read 'indicator for subdir' line */ while(fgets(buf,80,fp)) /* Get data */ printf(" %s",buf); /* Send to output */ fclose(fp); file_typed++; /* One more file done */ }