Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
7.7 KiB

  1. /****************************************************************************
  2. * Author: Aaron Lee
  3. * Purpose: do a directory minus a bunch of other directories
  4. ******************************************************************************/
  5. #include <direct.h>
  6. #include <tchar.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <io.h>
  10. #include <string.h>
  11. #include <windows.h>
  12. #include <sys\types.h>
  13. #include <sys\stat.h>
  14. #include <iostream.h>
  15. #include <fstream.h>
  16. #include <winbase.h>
  17. #include "filefind.h"
  18. #define MAX_ARRAY_SIZE 5000
  19. #define ALL_FILES 0xff
  20. struct arrayrow
  21. {
  22. long total;
  23. long nextuse;
  24. } GlobalMinusArrayIndex, GlobalFileInputMinusListIndex;
  25. char GlobalMinusArray[MAX_ARRAY_SIZE][_MAX_FNAME];
  26. char GlobalFileInputMinusList[MAX_ARRAY_SIZE][_MAX_FNAME];
  27. // Globals
  28. char g_szinput_filename_full[_MAX_PATH];
  29. char g_szinput_filename[_MAX_FNAME];
  30. // prototypes
  31. int __cdecl main(int ,char *argv[]);
  32. void Do_Process(void);
  33. void aFileToMinus(char * TheFileToMinus);
  34. int GlobalMinusArray_Add(char * FileNameToAdd);
  35. void GlobalMinusArray_Fill(void);
  36. void GlobalMinusArray_Print(void);
  37. int GlobalMinusArray_Check(char * FileNameToCheck);
  38. void ShowHelp(void);
  39. //-------------------------------------------------------------------
  40. // purpose: main
  41. //-------------------------------------------------------------------
  42. int __cdecl main(int argc,char *argv[])
  43. {
  44. int argno = 0;
  45. int nflags = 0;
  46. char filename_dir[_MAX_PATH];
  47. char filename_only[_MAX_FNAME];
  48. char filename_ext[_MAX_EXT];
  49. filename_only[0]='\0';
  50. // process command line arguments
  51. for(argno=1; argno<argc; argno++)
  52. {
  53. if ( argv[argno][0] == '-' || argv[argno][0] == '/' )
  54. {
  55. nflags++;
  56. switch (argv[argno][1])
  57. {
  58. case 'm':
  59. aFileToMinus(&argv[argno][2]);
  60. break;
  61. case '?':
  62. goto exit_with_help;
  63. break;
  64. }
  65. } // if switch character found
  66. else
  67. {
  68. if ( *filename_only == '\0' )
  69. {
  70. // if no arguments, then
  71. // get the filename_dir and put it into
  72. strcpy(g_szinput_filename_full, argv[argno]);
  73. filename_dir[0] = '\0';
  74. // split up this path
  75. _splitpath( g_szinput_filename_full, NULL, filename_dir, filename_only, filename_ext);
  76. strcat(filename_only, filename_ext);
  77. strcpy(g_szinput_filename,filename_only);
  78. // if we're missing dir, then get it.
  79. if (*filename_dir == '\0')
  80. {
  81. // Get current directory
  82. TCHAR szCurrentDir[_MAX_PATH];
  83. GetCurrentDirectory( _MAX_PATH, szCurrentDir);
  84. // stick it into our variable
  85. strcpy(filename_dir, szCurrentDir);
  86. strcpy(g_szinput_filename_full, szCurrentDir);
  87. strcat(g_szinput_filename_full, "\\");
  88. strcat(g_szinput_filename_full, filename_only);
  89. }
  90. }
  91. else
  92. {
  93. // Additional filenames (or arguments without a "-" or "/" preceding)
  94. //goto exit_with_help;
  95. // should be the section to execute.
  96. }
  97. } // non-switch char found
  98. } // for all arguments
  99. if ( *filename_only == '\0')
  100. {
  101. printf("Too few arguments, argc=%d\n\n",argc);
  102. goto exit_with_help;
  103. }
  104. // run the function to do everything
  105. Do_Process();
  106. return TRUE;
  107. exit_with_help:
  108. ShowHelp();
  109. return FALSE;
  110. }
  111. void aFileToMinus(char * TheFileToMinus)
  112. {
  113. // blankout the array values if any.
  114. GlobalFileInputMinusList[GlobalFileInputMinusListIndex.nextuse][0]= '\0';
  115. // move info into global array
  116. strcpy(GlobalFileInputMinusList[GlobalFileInputMinusListIndex.nextuse],TheFileToMinus);
  117. // increment counter to array
  118. // increment next use space
  119. ++GlobalFileInputMinusListIndex.total;
  120. ++GlobalFileInputMinusListIndex.nextuse;
  121. return;
  122. }
  123. void ShowHelp()
  124. {
  125. printf("\n");
  126. printf("DirMinus - does a dir minus other files\n");
  127. printf("----------------------------------------\n");
  128. printf(" Usage: Dir *.* -m*.cab -mFilename.exe\n");
  129. printf("----------------------------------------\n");
  130. return;
  131. }
  132. void Do_Process(void)
  133. {
  134. // Get stuff that we want to minus out into a huge array
  135. GlobalMinusArray_Fill();
  136. //GlobalMinusArray_Print();
  137. // Ok now, loop thru the directory and lookup each entry
  138. // in our globalminusarray, if it's there, then don't print it out!
  139. int attr;
  140. char filename_dir[_MAX_PATH];
  141. char filename_only[_MAX_FNAME];
  142. long hFile;
  143. finddata datareturn;
  144. if (!(g_szinput_filename)) {return;}
  145. // Get the filename portion
  146. _splitpath( g_szinput_filename, NULL, filename_dir, filename_only, NULL);
  147. attr= 0;
  148. if (strcmp(filename_only, "*.*") == 0)
  149. {attr=ALL_FILES;}
  150. InitStringTable(STRING_TABLE_SIZE);
  151. if ( FindFirst(g_szinput_filename, attr, &hFile, &datareturn) )
  152. {
  153. // check if it's a sub dir
  154. if (!( datareturn.attrib & _A_SUBDIR))
  155. {
  156. // is This Entry in our minus list?
  157. if (GlobalMinusArray_Check(datareturn.name) == FALSE)
  158. {
  159. // print it out
  160. printf(datareturn.name);
  161. printf ("\n");
  162. }
  163. }
  164. while(FindNext(attr, hFile, &datareturn))
  165. {
  166. // check if it's a sub dir
  167. if (!(datareturn.attrib & _A_SUBDIR))
  168. // is This Entry in our minus list?
  169. if (GlobalMinusArray_Check(datareturn.name) == FALSE)
  170. {
  171. // print it out
  172. printf(datareturn.name);
  173. printf ("\n");
  174. }
  175. }
  176. } // we didn't find the specified file.
  177. EndStringTable();
  178. return;
  179. }
  180. void GlobalMinusArray_Fill(void)
  181. {
  182. int outerindex;
  183. int attr;
  184. char filename_dir[_MAX_PATH];
  185. char filename_only[_MAX_FNAME];
  186. long hFile;
  187. finddata datareturn;
  188. if (!(GlobalFileInputMinusList[0][0])) {return;}
  189. for( outerindex = 0; outerindex < GlobalFileInputMinusListIndex.total;outerindex++)
  190. {
  191. // Get the filename portion
  192. _splitpath( GlobalFileInputMinusList[outerindex], NULL, filename_dir, filename_only, NULL);
  193. attr= 0;
  194. if (strcmp(filename_only, "*.*") == 0)
  195. {attr=ALL_FILES;}
  196. InitStringTable(STRING_TABLE_SIZE);
  197. if ( FindFirst(GlobalFileInputMinusList[outerindex], attr, &hFile, &datareturn) )
  198. {
  199. // check if it's a sub dir
  200. if (!( datareturn.attrib & _A_SUBDIR))
  201. {
  202. // ok we found one.
  203. // now take this entry and try to add it to the global array!!!
  204. GlobalMinusArray_Add(datareturn.name);
  205. }
  206. while(FindNext(attr, hFile, &datareturn))
  207. {
  208. // check if it's a sub dir
  209. if (!(datareturn.attrib & _A_SUBDIR))
  210. {
  211. // ok we found one.
  212. // now take this entry and try to add it to the global array!!!
  213. GlobalMinusArray_Add(datareturn.name);
  214. }
  215. }
  216. } // we didn't find the specified file.
  217. }
  218. EndStringTable();
  219. }
  220. int GlobalMinusArray_Add(char * FileNameToAdd)
  221. {
  222. // blankout the array values if any.
  223. GlobalMinusArray[GlobalMinusArrayIndex.nextuse][0] = '\0';
  224. // move info into global array
  225. strcpy(GlobalMinusArray[GlobalMinusArrayIndex.nextuse],FileNameToAdd);
  226. // increment counter to array
  227. // increment next use space
  228. ++GlobalMinusArrayIndex.total;
  229. ++GlobalMinusArrayIndex.nextuse;
  230. return TRUE;
  231. }
  232. void GlobalMinusArray_Print(void)
  233. {
  234. int i0;
  235. for( i0 = 0; i0 < GlobalMinusArrayIndex.total;i0++)
  236. {
  237. printf("-");
  238. printf(GlobalMinusArray[i0]);
  239. printf("\n");
  240. }
  241. return;
  242. }
  243. int GlobalMinusArray_Check(char * FileNameToCheck)
  244. {
  245. int i0;
  246. for( i0 = 0; i0 < GlobalMinusArrayIndex.total;i0++)
  247. {
  248. if (_stricmp(GlobalMinusArray[i0], FileNameToCheck) == 0)
  249. {return TRUE;}
  250. }
  251. return FALSE;
  252. }