Today's Code Snippet comes to us from Hank M. Hank writes, "It was my third week on the job, and that morning the custom compiler they wrote for their in-house language (steaming pile of WTFs) quit working." Standard debugging revealed that the executable didn't change, and reverting source control to when it worked, both source files and executable (just to make sure) didn't help.
Hank continues, "Fortunately the source code to the compiler was in source control, so I was able to run a debugger to find the problem. Here are the problem lines. Note that this is C++, not C, so std::string and helpers exist. See if you can guess why it didn't work."
strcpy(tmpstr,"Dir "); strcat(tmpstr,argv[1]); strcat(tmpstr," > "); strcat(tmpstr,JunkFile); system(tmpstr); plog_fp1 = fopen(JunkFile,"r"); if(plog_fp1 == NULL) { printf("Unable to open Junk File\n"); fcloseall(); return 0; } i = 0; while(fgets(tmpstr,200,plog_fp1) != 0) { if(tmpstr[1] == 'D') { int j; j = strlen(tmpstr); tmpstr[j-1] = 0; for(;j>0;j--) { if(tmpstr[j] == ' ') break; } j++; strcpy(Path,&tmpstr[j]); } else if(tmpstr[2] == '/') { int j; j = strlen(tmpstr); tmpstr[j-1] = 0; for(;j>0;j--) { if(tmpstr[j] == ' ') break; } j++; strcpy(Files[i],&tmpstr[j]); i++; if(i > 999) { printf("Too Many Files to Process! Please select fewer files!\n"); fcloseall(); return 0; } Files[i][0] = 0; } } fclose(plog_fp1); strcpy(tmpstr,"Del "); strcat(tmpstr,JunkFile); system(tmpstr);
Hank is one of those weird people who thinks that dates should be YYYYMMDD format, so when he had a few minutes to kill the day before he switched his settings. "I was able to replace the whole mess with FindFirstFile/FindNextFile, and get rid of the 999 files limit in one quick change. (Good thing we got to more than 1000 files to process just a few weeks latter)