#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

#define PACKAGE "forvavo"

/* recursion */
void recdir(char *dir);

int main(int argc, char *argv[]) {
 if(argc != 2)
  fprintf(stderr, "%s [directory]\n", PACKAGE), exit(EXIT_FAILURE);
 else {
  printf("<HTML>\n");
  printf(" <HEAD>\n");
  printf("  <TITLE>favorites</TITLE>\n");
  printf(" </HEAD>\n");
  printf("<BODY>\n");

  recdir(argv[1]);

  printf("</BODY>\n");
  printf("</HTML>\n");
 }

 return 0;
}

void recdir(char *dir) {
 DIR *dp;
 FILE *fp;
 char *ptr;
 char line[1024];
 struct dirent *entry;
 struct stat statbuf;

 if((dp = opendir(dir)) == NULL) {
  fprintf(stderr, "Cannot open directory: %s\n", dir);
  return;
 }

 chdir(dir);
 while((entry = readdir(dp)) != NULL) {
  stat(entry->d_name, &statbuf);
  if(S_ISDIR(statbuf.st_mode)) {
   if(strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
    continue;

   printf("\n<H1>%s</H1>\n", entry->d_name);
   printf(" <UL>\n");
   recdir(entry->d_name);
  } else {
   if((fp = fopen(entry->d_name, "r")) == NULL)
    perror(PACKAGE);
   else {
    while((fgets(line, 1024, fp)) != NULL) {
     if(strncmp(line, "URL=", 4) == 0) {
      printf("  <LI><A href=\"");
      for(ptr = line, ptr += 4; *ptr; ptr++)
       if(*ptr != '\n' && *ptr != '\r') printf("%c", *ptr);
      printf("\">%s</A>\n", entry->d_name);
     } /* while */
    } /* else */
    fclose(fp);
   } /* else */
  } /* if */
 } /* while */
 chdir("..");
 printf("</UL>\n");
 closedir(dp);
}

Designed By Blogger Templates | Templatelib & Distributed By Blogspot Templates