1 /* crlf.c : Defines the entry point for the console application.*/
3 /* Copyright 2000, International Business Machines Corporation and others.
6 This software has been released under the terms of the IBM Public
7 License. For details, see the LICENSE file in the top-level source
8 directory or online at http://www.openafs.org/dl/license10.html
12 #undef _CRTDBG_MAP_ALLOC
31 printf("util_cr file ;remove cr (from crlf)\n\
32 OR util_cr } ProductVersion in_filename out_filename ; substitute for %%1-%%5 in file\n\
33 %%1=Major version, %%2=Minor version, %%3=Patch(first digit) %%4=(last two digits) %%5=Version display string \n\
34 ProductVersion=maj.min.pat.pat2 ;maj=numeric, min=numeric pat,pat2 are not more than 3 digits or 1-2 digits and one alpha \n\
35 e.g 1.0.4.1, 1.0.4 a 1.0.401, 1.0.4a all represent the same version\n\
36 OR util_cr + file ;add cr\n \
37 OR util_cr * \"+[register key value] x=y\" ; add register key value\n\
38 OR util_cr * \"-[register key value]\" ; aremove register key value\n\
39 OR util_cr @ file.ini \"[SectionKey]variable=value\" ; update ini-ipr-pwf file\n\
40 OR util_cr @ file.ini \"[SectionKey]variable=value*DatE*\" ; update ini-ipr-pwf file, insert date\n\
41 OR util_cr ~ ;force error\n\
42 OR util_cr _del [/q=quiet /s=recurese] [*. *. *.] ;delete \n\
43 OR util_cr _cpy [/q=quiet ] [*. *. *.] destinationFolder;\n\
44 OR util_cr _isOS [nt xp 98 9x w2] ;test for OS, return 1 if match else 0\n\
45 OR util_cr _dir !build type!source!object! set current directory in file->home base is used for offset\n\
46 OR unil_cr _ver return compiler version\n");
51 WORD langID; // language ID
52 WORD charset; // character set (code page)
56 CheckVersion(int argc, char *argv[])
58 OSVERSIONINFO VersionInfo;
60 memset(&VersionInfo, 0, sizeof(VersionInfo));
61 VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
62 if (!GetVersionEx(&VersionInfo)) {
65 for (i = 2; i < argc; i++) {
66 if (stricmp(argv[i], "nt") == 0) {
67 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
68 && (VersionInfo.dwMajorVersion == 4)
69 && (VersionInfo.dwMinorVersion == 0))
72 if (stricmp(argv[i], "xp") == 0) {
73 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
74 && (VersionInfo.dwMajorVersion == 5)
75 && (VersionInfo.dwMinorVersion == 1))
78 if (stricmp(argv[i], "w2") == 0) {
79 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
80 && (VersionInfo.dwMajorVersion == 5)
81 && (VersionInfo.dwMinorVersion == 0))
84 if (stricmp(argv[i], "98") == 0) {
85 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
86 && (VersionInfo.dwMinorVersion == 10))
89 if (stricmp(argv[i], "95") == 0) {
90 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
91 && (VersionInfo.dwMinorVersion == 0))
95 if (stricmp(argv[i], "9x") == 0) {
96 if (VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
99 if (stricmp(argv[i], "_") == 0)
106 Addkey(const char *hkey, const char *subkey, const char *stag,
109 DWORD disposition, result;
110 HKEY kPkey, kHkey = 0;
111 if (strcmp(hkey, "HKEY_CLASSES_ROOT") == 0)
112 kHkey = HKEY_CLASSES_ROOT;
113 if (strcmp(hkey, "HKEY_CURRENT_USER") == 0)
114 kHkey = HKEY_CURRENT_USER;
115 if (strcmp(hkey, "HKEY_LOCAL_MACHINE") == 0)
116 kHkey = HKEY_LOCAL_MACHINE;
119 result = (RegCreateKeyEx(kHkey /*HKEY_LOCAL_MACHINE */
120 , subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
121 KEY_ALL_ACCESS, NULL, &kPkey,
122 &disposition) == ERROR_SUCCESS);
124 printf("AFS Error - Could Not create a registration key\n");
129 if ((sval) && (strlen(sval))) {
132 RegSetValueEx(kPkey, "", 0, REG_SZ, (CONST BYTE *) sval,
136 RegSetValueEx(kPkey, stag, 0, REG_SZ, (CONST BYTE *) sval,
142 (RegSetValueEx(kPkey, "", 0, REG_SZ, (CONST BYTE *) "", 0));
145 (RegSetValueEx(kPkey, stag, 0, REG_SZ, (CONST BYTE *) "", 0));
147 if (result != ERROR_SUCCESS) {
148 printf("AFS Error - Could Not create a registration key\n");
154 Subkey(const char *hkey, const char *subkey)
158 if (strcmp(hkey, "HKEY_CLASSES_ROOT") == 0)
159 kHkey = HKEY_CLASSES_ROOT;
160 if (strcmp(hkey, "HKEY_CURRENT_USER") == 0)
161 kHkey = HKEY_CURRENT_USER;
162 if (strcmp(hkey, "HKEY_LOCAL_MACHINE") == 0)
163 kHkey = HKEY_LOCAL_MACHINE;
166 result = RegDeleteKey(kHkey, subkey);
167 if (result != ERROR_SUCCESS) {
168 printf("AFS Error - Could Not create a registration key\n");
174 doremove(BOOL bRecurse, BOOL bQuiet, char *argv)
177 char parm[MAX_PATH + 1];
178 char basdir[MAX_PATH + 1];
181 GetCurrentDirectory(sizeof(basdir), basdir);
182 if (strrchr(parm, '\\') != NULL) { /*jump to base directory */
183 pParm = strrchr(parm, '\\');
185 if (!SetCurrentDirectory(parm))
190 struct _finddata_t fileinfo;
193 char basdir[MAX_PATH + 1];
194 GetCurrentDirectory(sizeof(basdir), basdir);
195 hfile = _findfirst(pParm, &fileinfo);
196 bmore = (hfile != -1);
198 if ((DeleteFile(fileinfo.name) == 1) && (!bQuiet))
199 printf("Remove %s\\%s\n", basdir, fileinfo.name);
200 bmore = (_findnext(hfile, &fileinfo) == 0);
204 /*RECURSIVE LOOP - SCAN directories */
205 struct _finddata_t fileinfo;
208 doremove(FALSE, bQuiet, pParm);
209 hfile = _findfirst("*.*", &fileinfo);
210 bmore = (hfile != -1);
212 if (fileinfo.attrib & _A_SUBDIR) {
213 if ((strcmp(fileinfo.name, ".") != 0)
214 && (strcmp(fileinfo.name, "..") != 0)) {
215 if (SetCurrentDirectory(fileinfo.name))
216 doremove(TRUE, bQuiet, pParm);
217 SetCurrentDirectory(basdir);
220 bmore = (_findnext(hfile, &fileinfo) == 0);
224 SetCurrentDirectory(basdir);
230 char bld[MAX_PATH + 1], parm[MAX_PATH + 1], src[MAX_PATH + 1],
231 obj[MAX_PATH + 1], dir[MAX_PATH + 1], curdir[MAX_PATH + 1];
234 BOOL isObjAbs, isSrcAbs; /* two flags to determine if either string is not relative */
235 strcpy(bld, val + 1); /*it better be checked or free */
236 strcpy(src, strchr(bld, '!') + 1);
237 strcpy(obj, strchr(src, '!') + 1);
238 *strchr(bld, '!') = 0;
239 *strchr(obj, '!') = 0;
240 *strchr(src, '!') = 0;
241 isObjAbs = ((*obj == '\\') || (strstr(obj, ":")));
242 isSrcAbs = ((*src == '\\') || (strstr(src, ":")));
243 GetCurrentDirectory(MAX_PATH, dir);
249 strcat(curdir, "\\ \n");
250 if (GetTempPath(MAX_PATH, parm) == 0)
252 strcat(parm, "home");
253 if ((f = fopen(parm, "w")) == NULL)
257 if (obj[strlen(obj) - 1] != '\\')
259 if (src[strlen(src) - 1] != '\\')
261 do { /* try to match src or obj */
262 if ((p = strstr(dir, src)) && isSrcAbs) {
265 } else if ((p = strstr(dir, src)) && !isSrcAbs
266 && *(p - 1) == '\\') {
269 } else if ((p = strstr(dir, obj)) && isObjAbs) {
272 } else if ((p = strstr(dir, obj)) && !isObjAbs
273 && *(p - 1) == '\\') {
277 } while (strstr(p, src) || strstr(p, obj));
279 sprintf(parm, "OJT=%s%s\\%s \n", obj, bld, p);
282 sprintf(parm, "OJT=%s%s%s\\%s \n", dir, obj, bld, p);
284 if (strcmp(curdir, parm + 4) == 0) /* current directory is object */
285 strcpy(parm, "OJT= \n");
286 fwrite(parm, strlen(parm), 1, f);
288 sprintf(parm, "SRT=%s%s\\ \n", src, p);
290 *(p - strlen(src)) = 0;
291 sprintf(parm, "SRT=%s%s%s\\ \n", dir, src, p);
293 if (strcmp(curdir, parm + 4) == 0) /* current directory is object */
294 strcpy(parm, "SRT= \n");
295 fwrite(parm, strlen(parm), 1, f);
296 /* now lets set the AFS_LASTCMP environment variable */
297 sprintf(parm, "AFS_LASTCMP=%s\\%s", src, p);
299 __except(EXCEPTION_EXECUTE_HANDLER) {
309 isequal(char *msg1, char *msg2, char *disp)
313 if (strcmp(msg1, msg2) != 0)
315 printf("ERROR -- %s \n", disp);
320 SetSysEnv(int argc, char *argv[])
323 printf("assignment %s %s\n", argv[2], argv[3]);
324 Addkey("HKEY_LOCAL_MACHINE",
325 "System\\CurrentControlSet\\Control\\Session Manager\\Environment",
329 SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
330 (DWORD) "Environment", SMTO_NORMAL, 1, &dwResult);
335 main(int argc, char *argv[])
337 /* typedef char * CHARP;*/
341 char **pvar, *ch, *save;
343 BOOL bRecurse = FALSE;
348 /* RSM4: Add an "ECHO" that doesn't append a new line... */
349 if (strcmp(argv[1], "_echo") == 0) {
352 printf("%s",argv[2]);
356 if (strcmp(argv[1], "_sysvar") == 0) {
359 return (SetSysEnv(argc, argv));
362 if (strnicmp(argv[1], "_dir", 4) == 0) { /*get current directory routine */
366 if (strnicmp(argv[1], "_isequal", 4) == 0) { /*get current directory routine */
367 return isequal(argv[2], argv[3], argv[4]);
369 if (stricmp(argv[1], "_del") == 0) { /*DELETE routine */
371 if ((argc > iargc) && (stricmp(argv[iargc], "/s") == 0)) {
375 if ((argc > iargc) && (stricmp(argv[iargc], "/q") == 0)) {
379 if ((argc > iargc) && (stricmp(argv[iargc], "/s") == 0)) {
383 while (iargc < argc) {
384 doremove(bRecurse, bQuiet, argv[iargc]);
389 if (strcmp(argv[1], "_ver") == 0) {
394 if (strcmp(argv[1], "_isOS") == 0)
395 return CheckVersion(argc, argv);
396 if (strcmp(argv[1], "}") == 0) {
397 char v1[4], v2[4], v3[4], v4[4];
407 if ((ptr = strtok(argv[2], ". \n")) == NULL)
410 if ((ptr = strtok(NULL, ". \n")) == NULL)
413 if ((ptr = strtok(NULL, ". \n")) == NULL)
416 switch (strlen(ptr)) {
421 if (isdigit(*ptr) != 0)
424 case 2: //ONLY 1.0.44 is interpreted as 1.0.4.4 or 1.0.4a as 1.0.4.a
425 if (isdigit(*ptr) == 0)
429 if (isalpha(*ptr) == 0) {
431 } else if (isalpha(*ptr) != 0) {
432 pat2 = tolower(*ptr) - 'a' + 1;
436 case 3: //1.0.401 or 1.0.40a are the same;
437 if ((isdigit(*ptr) == 0) // first 2 must be digit
438 || (isdigit(*(ptr + 1)) == 0)
439 || (*(ptr + 1) != '0' && isdigit(*(ptr + 2)) == 0) // disallow 1.0.4b0 or 1.0.41a
447 pat2 = tolower(*ptr) - 'a' + 1;
452 // last can be 1-2 digits or one alpha (if pat2 hasn't been set)
453 if ((ptr = strtok(NULL, ". \n")) != NULL) {
456 switch (strlen(ptr)) {
458 pat2 = (isdigit(*ptr)) ? atoi(ptr) : tolower(*ptr) - 'a' + 1;
461 if (isdigit(*ptr) == 0 || isdigit(*(ptr + 1)) == 0)
469 file = fopen(argv[3], "r");
472 len = filelength(_fileno(file));
473 save = (char *)malloc(len + 1);
475 len = fread(buf, sizeof(char), len, file);
476 buf[len] = 0; //set eof
478 file = fopen(argv[4], "w");
481 sprintf(v1, "%i", maj);
482 sprintf(v2, "%i", min);
483 sprintf(v3, "%i", pat);
484 sprintf(v4, "%02i", pat2);
486 ptr = strstr(buf, "%");
487 fwrite(buf, 1, (ptr) ? ptr - buf : strlen(buf), file); //write file if no % found or up to %
490 switch (*(ptr + 1)) //skip first scan if buf="1...."
493 fwrite(v1, 1, strlen(v1), file);
497 fwrite(v2, 1, strlen(v2), file);
501 fwrite(v3, 1, strlen(v3), file);
505 fwrite(v4, 1, strlen(v4), file);
509 fwrite(v5, 1, strlen(v5), file);
513 fwrite("%", 1, 1, file); //either % at end of file or no %1...
522 if (strcmp(argv[1], "~") == 0) { //check for file presence
523 if (fopen(argv[2], "r"))
526 printf("ERROR --- File not present %s\n", argv[2]);
528 printf("Error---%s\n", argv[3]);
531 if (strcmp(argv[1], "*") == 0) { /* "[HKEY_CLASSES_ROOT\CLSID\{DC515C27-6CAC-11D1-BAE7-00C04FD140D2}] @=AFS Client Shell Extension" */
534 for (i = 2; argc >= 3; i++) {
535 char *ssub = strtok(argv[i], "[");
537 char *skey = strtok(NULL, "]");
539 if ((ssub == NULL) || (skey == NULL)) {
540 printf("format error parameter %s\n", argv[i]);
543 option = (*ssub == '-');
544 stag = strtok(NULL, "\0");
548 ssub = strtok(skey, "\\");
549 ssub = strtok(NULL, "\0");
550 sval = strtok(stag, "=");
551 sval = strtok(NULL, "\0");
554 Addkey(skey, ssub, stag, sval);
558 Addkey(skey, ssub, stag, "");
568 if (strcmp(argv[1], "@") == 0) {
569 char msg[256], msgt[256];
573 for (i = 3; argc >= 4; i++) {
575 char *ssect = strstr(argv[i], "[");
576 char *skey = strstr(ssect, "]");
578 if ((ssect == NULL) || (skey == NULL)) {
579 printf("format error parameter %s\n", argv[i]);
584 if ((strlen(skey + 1) == 0) || (strlen(ssect) == 0)) {
585 printf("format error parameter %s\n", argv[i]);
588 while (*++skey == ' ');
589 sval = strstr(skey, "=");
591 printf("format error parameter %s\n", argv[i]);
595 while (*--ptr == ' ');
597 while (*++sval == ' ');
598 if (ptr = strstr(sval, "*DatE*")) { // ok so lets substitute date in this string;
605 strcat(msg, ptr + 6);
608 if (ptr = strstr(sval, "*TimE*")) {
613 strncat(msgt, tmpbuf, 5);
614 strcat(msgt, ptr + 6);
617 if (WritePrivateProfileString(ssect, skey, sval, argv[2]) == 0) {
619 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
620 FORMAT_MESSAGE_FROM_SYSTEM |
621 FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
622 GetLastError(), MAKELANGID(LANG_NEUTRAL,
624 (LPTSTR) & lpMsgBuf, 0, NULL);
625 printf("Error writing profile string - %s", lpMsgBuf);
633 strcpy(fname, argv[2]);
634 if (strcmp(argv[1], "+") == 0) {
635 file = fopen(fname, "rb");
638 len = filelength(_fileno(file));
639 save = (char *)malloc(len + 2);
641 *ch++ = 0; /* a small hack to allow matching /r/n if /n is first character */
642 len = fread(ch, sizeof(char), len, file);
643 file = freopen(fname, "wb", file);
645 if ((*ch == '\n') && (*(ch - 1) != '\r')) { /*line feed alone */
655 if (strcmp(argv[1], "-") == 0) {
656 strcpy(fname, argv[2]);
657 file = fopen(fname, "rb");
660 len = filelength(_fileno(file));
661 save = (char *)malloc(len + 1);
663 len = fread(ch, sizeof(char), len, file);
664 file = freopen(fname, "wb", file);
674 if (strstr(fname, ".et") == NULL)
675 strcat(fname, ".et");
676 file = fopen(fname, "rb");
679 len = filelength(_fileno(file));
680 save = (char *)malloc(len + 1);
682 len = fread(ch, sizeof(char), len, file);
683 file = freopen(fname, "wb", file);
690 pvar = (char **)malloc(argc * sizeof(char *));
691 for (i = 1; i < argc - 1; i++)
692 pvar[i] = argv[i + 1];
693 pvar[argc - 1] = NULL;
695 (void)_spawnvp(_P_WAIT, argv[1], pvar);