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
24 #define intptr_t INT_PTR
30 printf("util_cr file ;remove cr (from crlf)\n\
31 OR util_cr } ProductVersion in_filename out_filename ; substitute for %%1-%%5 in file\n\
32 %%1=Major version, %%2=Minor version, %%3=Patch(first digit) %%4=(last two digits) %%5=Version display string \n\
33 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\
34 e.g 1.0.4.1, 1.0.4 a 1.0.401, 1.0.4a all represent the same version\n\
35 OR util_cr + file ;add cr\n \
36 OR util_cr * \"+[register key value] x=y\" ; add register key value\n\
37 OR util_cr * \"-[register key value]\" ; aremove register key value\n\
38 OR util_cr @ file.ini \"[SectionKey]variable=value\" ; update ini-ipr-pwf file\n\
39 OR util_cr @ file.ini \"[SectionKey]variable=value*DatE*\" ; update ini-ipr-pwf file, insert date\n\
40 OR util_cr ~ ;force error\n\
41 OR util_cr _del [/q=quiet /s=recurese] [*. *. *.] ;delete \n\
42 OR util_cr _cpy [/q=quiet ] [*. *. *.] destinationFolder;\n\
43 OR util_cr _isOS [nt xp 98 9x w2] ;test for OS, return 1 if match else 0\n\
44 OR util_cr _dir !build type!source!object! set current directory in file->home base is used for offset\n\
45 OR unil_cr _ver return compiler version\n");
50 WORD langID; // language ID
51 WORD charset; // character set (code page)
55 CheckVersion(int argc, char *argv[])
57 OSVERSIONINFO VersionInfo;
59 memset(&VersionInfo, 0, sizeof(VersionInfo));
60 VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
61 if (!GetVersionEx(&VersionInfo)) {
64 for (i = 2; i < argc; i++) {
65 if (stricmp(argv[i], "nt") == 0) {
66 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
67 && (VersionInfo.dwMajorVersion == 4)
68 && (VersionInfo.dwMinorVersion == 0))
71 if (stricmp(argv[i], "xp") == 0) {
72 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
73 && (VersionInfo.dwMajorVersion == 5)
74 && (VersionInfo.dwMinorVersion == 1))
77 if (stricmp(argv[i], "w2") == 0) {
78 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
79 && (VersionInfo.dwMajorVersion == 5)
80 && (VersionInfo.dwMinorVersion == 0))
83 if (stricmp(argv[i], "98") == 0) {
84 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
85 && (VersionInfo.dwMinorVersion == 10))
88 if (stricmp(argv[i], "95") == 0) {
89 if ((VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
90 && (VersionInfo.dwMinorVersion == 0))
94 if (stricmp(argv[i], "9x") == 0) {
95 if (VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
98 if (stricmp(argv[i], "_") == 0)
105 Addkey(const char *hkey, const char *subkey, const char *stag,
108 DWORD disposition, result;
109 HKEY kPkey, kHkey = 0;
110 if (strcmp(hkey, "HKEY_CLASSES_ROOT") == 0)
111 kHkey = HKEY_CLASSES_ROOT;
112 if (strcmp(hkey, "HKEY_CURRENT_USER") == 0)
113 kHkey = HKEY_CURRENT_USER;
114 if (strcmp(hkey, "HKEY_LOCAL_MACHINE") == 0)
115 kHkey = HKEY_LOCAL_MACHINE;
118 result = (RegCreateKeyEx(kHkey /*HKEY_LOCAL_MACHINE */
119 , subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
120 KEY_ALL_ACCESS, NULL, &kPkey,
121 &disposition) == ERROR_SUCCESS);
123 printf("AFS Error - Could Not create a registration key\n");
128 if ((sval) && (strlen(sval))) {
131 RegSetValueEx(kPkey, "", 0, REG_SZ, (CONST BYTE *) sval,
132 (DWORD)strlen(sval));
135 RegSetValueEx(kPkey, stag, 0, REG_SZ, (CONST BYTE *) sval,
136 (DWORD)strlen(sval));
141 (RegSetValueEx(kPkey, "", 0, REG_SZ, (CONST BYTE *) "", 0));
144 (RegSetValueEx(kPkey, stag, 0, REG_SZ, (CONST BYTE *) "", 0));
146 if (result != ERROR_SUCCESS) {
147 printf("AFS Error - Could Not create a registration key\n");
153 Subkey(const char *hkey, const char *subkey)
157 if (strcmp(hkey, "HKEY_CLASSES_ROOT") == 0)
158 kHkey = HKEY_CLASSES_ROOT;
159 if (strcmp(hkey, "HKEY_CURRENT_USER") == 0)
160 kHkey = HKEY_CURRENT_USER;
161 if (strcmp(hkey, "HKEY_LOCAL_MACHINE") == 0)
162 kHkey = HKEY_LOCAL_MACHINE;
165 result = RegDeleteKey(kHkey, subkey);
166 if (result != ERROR_SUCCESS) {
167 printf("AFS Error - Could Not create a registration key\n");
173 doremove(BOOL bRecurse, BOOL bQuiet, char *argv)
176 char parm[MAX_PATH + 1];
177 char basdir[MAX_PATH + 1];
180 GetCurrentDirectory(sizeof(basdir), basdir);
181 if (strrchr(parm, '\\') != NULL) { /*jump to base directory */
182 pParm = strrchr(parm, '\\');
184 if (!SetCurrentDirectory(parm))
189 struct _finddata_t fileinfo;
192 char basdir[MAX_PATH + 1];
193 GetCurrentDirectory(sizeof(basdir), basdir);
194 hfile = _findfirst(pParm, &fileinfo);
195 bmore = (hfile != -1);
197 if ((DeleteFile(fileinfo.name) == 1) && (!bQuiet))
198 printf("Remove %s\\%s\n", basdir, fileinfo.name);
199 bmore = (_findnext(hfile, &fileinfo) == 0);
203 /*RECURSIVE LOOP - SCAN directories */
204 struct _finddata_t fileinfo;
207 doremove(FALSE, bQuiet, pParm);
208 hfile = _findfirst("*.*", &fileinfo);
209 bmore = (hfile != -1);
211 if (fileinfo.attrib & _A_SUBDIR) {
212 if ((strcmp(fileinfo.name, ".") != 0)
213 && (strcmp(fileinfo.name, "..") != 0)) {
214 if (SetCurrentDirectory(fileinfo.name))
215 doremove(TRUE, bQuiet, pParm);
216 SetCurrentDirectory(basdir);
219 bmore = (_findnext(hfile, &fileinfo) == 0);
223 SetCurrentDirectory(basdir);
229 char bld[MAX_PATH + 1], parm[MAX_PATH + 1], src[MAX_PATH + 1],
230 obj[MAX_PATH + 1], dir[MAX_PATH + 1], curdir[MAX_PATH + 1];
233 BOOL isObjAbs, isSrcAbs; /* two flags to determine if either string is not relative */
234 strcpy(bld, val + 1); /*it better be checked or free */
235 strcpy(src, strchr(bld, '!') + 1);
236 strcpy(obj, strchr(src, '!') + 1);
237 *strchr(bld, '!') = 0;
238 *strchr(obj, '!') = 0;
239 *strchr(src, '!') = 0;
240 isObjAbs = ((*obj == '\\') || (strstr(obj, ":")));
241 isSrcAbs = ((*src == '\\') || (strstr(src, ":")));
242 GetCurrentDirectory(MAX_PATH, dir);
248 strcat(curdir, "\\ \n");
249 if (GetTempPath(MAX_PATH, parm) == 0)
251 strcat(parm, "home");
252 if ((f = fopen(parm, "w")) == NULL)
256 if (obj[strlen(obj) - 1] != '\\')
258 if (src[strlen(src) - 1] != '\\')
260 do { /* try to match src or obj */
261 if ((p = strstr(dir, src)) && isSrcAbs) {
264 } else if ((p = strstr(dir, src)) && !isSrcAbs
265 && *(p - 1) == '\\') {
268 } else if ((p = strstr(dir, obj)) && isObjAbs) {
271 } else if ((p = strstr(dir, obj)) && !isObjAbs
272 && *(p - 1) == '\\') {
276 } while (strstr(p, src) || strstr(p, obj));
278 sprintf(parm, "OJT=%s%s\\%s \n", obj, bld, p);
281 sprintf(parm, "OJT=%s%s%s\\%s \n", dir, obj, bld, p);
283 if (strcmp(curdir, parm + 4) == 0) /* current directory is object */
284 strcpy(parm, "OJT= \n");
285 fwrite(parm, strlen(parm), 1, f);
287 sprintf(parm, "SRT=%s%s\\ \n", src, p);
289 *(p - strlen(src)) = 0;
290 sprintf(parm, "SRT=%s%s%s\\ \n", dir, src, p);
292 if (strcmp(curdir, parm + 4) == 0) /* current directory is object */
293 strcpy(parm, "SRT= \n");
294 fwrite(parm, strlen(parm), 1, f);
295 /* now lets set the AFS_LASTCMP environment variable */
296 sprintf(parm, "AFS_LASTCMP=%s\\%s", src, p);
298 __except(EXCEPTION_EXECUTE_HANDLER) {
308 isequal(char *msg1, char *msg2, char *disp)
312 if (strcmp(msg1, msg2) != 0)
314 printf("ERROR -- %s \n", disp);
319 SetSysEnv(int argc, char *argv[])
322 printf("assignment %s %s\n", argv[2], argv[3]);
323 Addkey("HKEY_LOCAL_MACHINE",
324 "System\\CurrentControlSet\\Control\\Session Manager\\Environment",
328 SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
329 (DWORD) "Environment", SMTO_NORMAL, 1, &dwResult);
334 main(int argc, char *argv[])
339 char **pvar, *ch, *save;
341 BOOL bRecurse = FALSE;
346 /* RSM4: Add an "ECHO" that doesn't append a new line... */
347 if (strcmp(argv[1], "_echo") == 0) {
350 printf("%s",argv[2]);
354 if (strcmp(argv[1], "_sysvar") == 0) {
357 return (SetSysEnv(argc, argv));
360 if (strnicmp(argv[1], "_dir", 4) == 0) { /*get current directory routine */
364 if (strnicmp(argv[1], "_isequal", 4) == 0) { /*get current directory routine */
365 return isequal(argv[2], argv[3], argv[4]);
367 if (stricmp(argv[1], "_del") == 0) { /*DELETE routine */
369 if ((argc > iargc) && (stricmp(argv[iargc], "/s") == 0)) {
373 if ((argc > iargc) && (stricmp(argv[iargc], "/q") == 0)) {
377 if ((argc > iargc) && (stricmp(argv[iargc], "/s") == 0)) {
381 while (iargc < argc) {
382 doremove(bRecurse, bQuiet, argv[iargc]);
387 if (strcmp(argv[1], "_ver") == 0) {
392 if (strcmp(argv[1], "_isOS") == 0)
393 return CheckVersion(argc, argv);
394 if (strcmp(argv[1], "}") == 0) {
395 char v1[4], v2[4], v3[4], v4[4];
405 if ((ptr = strtok(argv[2], ". \n")) == NULL)
408 if ((ptr = strtok(NULL, ". \n")) == NULL)
411 if ((ptr = strtok(NULL, ". \n")) == NULL)
414 switch (strlen(ptr)) {
419 if (isdigit(*ptr) != 0)
422 case 2: //ONLY 1.0.44 is interpreted as 1.0.4.4 or 1.0.4a as 1.0.4.a
423 if (isdigit(*ptr) == 0)
427 if (isalpha(*ptr) == 0) {
429 } else if (isalpha(*ptr) != 0) {
430 pat2 = tolower(*ptr) - 'a' + 1;
434 case 3: //1.0.401 or 1.0.40a are the same;
435 if ((isdigit(*ptr) == 0) // first 2 must be digit
436 || (isdigit(*(ptr + 1)) == 0)
437 || (*(ptr + 1) != '0' && isdigit(*(ptr + 2)) == 0) // disallow 1.0.4b0 or 1.0.41a
445 pat2 = tolower(*ptr) - 'a' + 1;
450 // last can be 1-2 digits or one alpha (if pat2 hasn't been set)
451 if ((ptr = strtok(NULL, ". \n")) != NULL) {
454 switch (strlen(ptr)) {
456 pat2 = (isdigit(*ptr)) ? atoi(ptr) : tolower(*ptr) - 'a' + 1;
459 if (isdigit(*ptr) == 0 || isdigit(*(ptr + 1)) == 0)
467 file = fopen(argv[3], "r");
470 len = filelength(_fileno(file));
471 save = (char *)malloc(len + 1);
473 len = fread(buf, sizeof(char), len, file);
474 buf[len] = 0; //set eof
476 file = fopen(argv[4], "w");
479 sprintf(v1, "%i", maj);
480 sprintf(v2, "%i", min);
481 sprintf(v3, "%i", pat);
482 sprintf(v4, "%02i", pat2);
484 ptr = strstr(buf, "%");
485 fwrite(buf, 1, (ptr) ? ptr - buf : strlen(buf), file); //write file if no % found or up to %
488 switch (*(ptr + 1)) //skip first scan if buf="1...."
491 fwrite(v1, 1, strlen(v1), file);
495 fwrite(v2, 1, strlen(v2), file);
499 fwrite(v3, 1, strlen(v3), file);
503 fwrite(v4, 1, strlen(v4), file);
507 fwrite(v5, 1, strlen(v5), file);
511 fwrite("%", 1, 1, file); //either % at end of file or no %1...
520 if (strcmp(argv[1], "~") == 0) { //check for file presence
521 if (fopen(argv[2], "r"))
524 printf("ERROR --- File not present %s\n", argv[2]);
526 printf("Error---%s\n", argv[3]);
529 if (strcmp(argv[1], "*") == 0) { /* "[HKEY_CLASSES_ROOT\CLSID\{DC515C27-6CAC-11D1-BAE7-00C04FD140D2}] @=AFS Client Shell Extension" */
532 for (i = 2; argc >= 3; i++) {
533 char *ssub = strtok(argv[i], "[");
535 char *skey = strtok(NULL, "]");
537 if ((ssub == NULL) || (skey == NULL)) {
538 printf("format error parameter %s\n", argv[i]);
541 option = (*ssub == '-');
542 stag = strtok(NULL, "\0");
546 ssub = strtok(skey, "\\");
547 ssub = strtok(NULL, "\0");
548 sval = strtok(stag, "=");
549 sval = strtok(NULL, "\0");
552 Addkey(skey, ssub, stag, sval);
556 Addkey(skey, ssub, stag, "");
566 if (strcmp(argv[1], "@") == 0) {
567 char msg[256], msgt[256];
571 for (i = 3; argc >= 4; i++) {
573 char *ssect = strstr(argv[i], "[");
574 char *skey = strstr(ssect, "]");
576 if ((ssect == NULL) || (skey == NULL)) {
577 printf("format error parameter %s\n", argv[i]);
582 if ((strlen(skey + 1) == 0) || (strlen(ssect) == 0)) {
583 printf("format error parameter %s\n", argv[i]);
586 while (*++skey == ' ');
587 sval = strstr(skey, "=");
589 printf("format error parameter %s\n", argv[i]);
593 while (*--ptr == ' ');
595 while (*++sval == ' ');
596 if (ptr = strstr(sval, "*DatE*")) { // ok so lets substitute date in this string;
603 strcat(msg, ptr + 6);
606 if (ptr = strstr(sval, "*TimE*")) {
611 strncat(msgt, tmpbuf, 5);
612 strcat(msgt, ptr + 6);
615 if (WritePrivateProfileString(ssect, skey, sval, argv[2]) == 0) {
617 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
618 FORMAT_MESSAGE_FROM_SYSTEM |
619 FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
620 GetLastError(), MAKELANGID(LANG_NEUTRAL,
622 (LPTSTR) & lpMsgBuf, 0, NULL);
623 printf("Error writing profile string - %s", lpMsgBuf);
631 strcpy(fname, argv[2]);
632 if (strcmp(argv[1], "+") == 0) {
633 file = fopen(fname, "rb");
636 len = filelength(_fileno(file));
637 save = (char *)malloc(len + 2);
639 *ch++ = 0; /* a small hack to allow matching /r/n if /n is first character */
640 len = fread(ch, sizeof(char), len, file);
641 file = freopen(fname, "wb", file);
643 if ((*ch == '\n') && (*(ch - 1) != '\r')) { /*line feed alone */
653 if (strcmp(argv[1], "-") == 0) {
654 strcpy(fname, argv[2]);
655 file = fopen(fname, "rb");
658 len = filelength(_fileno(file));
659 save = (char *)malloc(len + 1);
661 len = fread(ch, sizeof(char), len, file);
662 file = freopen(fname, "wb", file);
672 if (strstr(fname, ".et") == NULL)
673 strcat(fname, ".et");
674 file = fopen(fname, "rb");
677 len = filelength(_fileno(file));
678 save = (char *)malloc(len + 1);
680 len = fread(ch, sizeof(char), len, file);
681 file = freopen(fname, "wb", file);
688 pvar = (char **)malloc(argc * sizeof(char *));
689 for (i = 1; i < argc - 1; i++)
690 pvar[i] = argv[i + 1];
691 pvar[argc - 1] = NULL;
693 (void)_spawnvp(_P_WAIT, argv[1], pvar);