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;
347 if (strcmp(argv[1], "_sysvar") == 0) {
350 return (SetSysEnv(argc, argv));
353 if (strnicmp(argv[1], "_dir", 4) == 0) { /*get current directory routine */
357 if (strnicmp(argv[1], "_isequal", 4) == 0) { /*get current directory routine */
358 return isequal(argv[2], argv[3], argv[4]);
360 if (stricmp(argv[1], "_del") == 0) { /*DELETE routine */
362 if ((argc > iargc) && (stricmp(argv[iargc], "/s") == 0)) {
366 if ((argc > iargc) && (stricmp(argv[iargc], "/q") == 0)) {
370 if ((argc > iargc) && (stricmp(argv[iargc], "/s") == 0)) {
374 while (iargc < argc) {
375 doremove(bRecurse, bQuiet, argv[iargc]);
380 if (strcmp(argv[1], "_ver") == 0) {
385 if (strcmp(argv[1], "_isOS") == 0)
386 return CheckVersion(argc, argv);
387 if (strcmp(argv[1], "}") == 0) {
388 char v1[4], v2[4], v3[4], v4[4];
398 if ((ptr = strtok(argv[2], ". \n")) == NULL)
401 if ((ptr = strtok(NULL, ". \n")) == NULL)
404 if ((ptr = strtok(NULL, ". \n")) == NULL)
407 switch (strlen(ptr)) {
412 if (isdigit(*ptr) != 0)
415 case 2: //ONLY 1.0.44 is interpreted as 1.0.4.4 or 1.0.4a as 1.0.4.a
416 if (isdigit(*ptr) == 0)
420 if (isalpha(*ptr) == 0) {
422 } else if (isalpha(*ptr) != 0) {
423 pat2 = tolower(*ptr) - 'a' + 1;
427 case 3: //1.0.401 or 1.0.40a are the same;
428 if ((isdigit(*ptr) == 0) // first 2 must be digit
429 || (isdigit(*(ptr + 1)) == 0)
430 || (*(ptr + 1) != '0' && isdigit(*(ptr + 2)) == 0) // disallow 1.0.4b0 or 1.0.41a
438 pat2 = tolower(*ptr) - 'a' + 1;
443 // last can be 1-2 digits or one alpha (if pat2 hasn't been set)
444 if ((ptr = strtok(NULL, ". \n")) != NULL) {
447 switch (strlen(ptr)) {
449 pat2 = (isdigit(*ptr)) ? atoi(ptr) : tolower(*ptr) - 'a' + 1;
452 if (isdigit(*ptr) == 0 || isdigit(*(ptr + 1)) == 0)
460 file = fopen(argv[3], "r");
463 len = filelength(_fileno(file));
464 save = (char *)malloc(len + 1);
466 len = fread(buf, sizeof(char), len, file);
467 buf[len] = 0; //set eof
469 file = fopen(argv[4], "w");
472 sprintf(v1, "%i", maj);
473 sprintf(v2, "%i", min);
474 sprintf(v3, "%i", pat);
475 sprintf(v4, "%02i", pat2);
477 ptr = strstr(buf, "%");
478 fwrite(buf, 1, (ptr) ? ptr - buf : strlen(buf), file); //write file if no % found or up to %
481 switch (*(ptr + 1)) //skip first scan if buf="1...."
484 fwrite(v1, 1, strlen(v1), file);
488 fwrite(v2, 1, strlen(v2), file);
492 fwrite(v3, 1, strlen(v3), file);
496 fwrite(v4, 1, strlen(v4), file);
500 fwrite(v5, 1, strlen(v5), file);
504 fwrite("%", 1, 1, file); //either % at end of file or no %1...
513 if (strcmp(argv[1], "~") == 0) { //check for file presence
514 if (fopen(argv[2], "r"))
517 printf("ERROR --- File not present %s\n", argv[2]);
519 printf("Error---%s\n", argv[3]);
522 if (strcmp(argv[1], "*") == 0) { /* "[HKEY_CLASSES_ROOT\CLSID\{DC515C27-6CAC-11D1-BAE7-00C04FD140D2}] @=AFS Client Shell Extension" */
525 for (i = 2; argc >= 3; i++) {
526 char *ssub = strtok(argv[i], "[");
528 char *skey = strtok(NULL, "]");
530 if ((ssub == NULL) || (skey == NULL)) {
531 printf("format error parameter %s\n", argv[i]);
534 option = (*ssub == '-');
535 stag = strtok(NULL, "\0");
539 ssub = strtok(skey, "\\");
540 ssub = strtok(NULL, "\0");
541 sval = strtok(stag, "=");
542 sval = strtok(NULL, "\0");
545 Addkey(skey, ssub, stag, sval);
549 Addkey(skey, ssub, stag, "");
559 if (strcmp(argv[1], "@") == 0) {
560 char msg[256], msgt[256];
564 for (i = 3; argc >= 4; i++) {
566 char *ssect = strstr(argv[i], "[");
567 char *skey = strstr(ssect, "]");
569 if ((ssect == NULL) || (skey == NULL)) {
570 printf("format error parameter %s\n", argv[i]);
575 if ((strlen(skey + 1) == 0) || (strlen(ssect) == 0)) {
576 printf("format error parameter %s\n", argv[i]);
579 while (*++skey == ' ');
580 sval = strstr(skey, "=");
582 printf("format error parameter %s\n", argv[i]);
586 while (*--ptr == ' ');
588 while (*++sval == ' ');
589 if (ptr = strstr(sval, "*DatE*")) { // ok so lets substitute date in this string;
596 strcat(msg, ptr + 6);
599 if (ptr = strstr(sval, "*TimE*")) {
604 strncat(msgt, tmpbuf, 5);
605 strcat(msgt, ptr + 6);
608 if (WritePrivateProfileString(ssect, skey, sval, argv[2]) == 0) {
610 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
611 FORMAT_MESSAGE_FROM_SYSTEM |
612 FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
613 GetLastError(), MAKELANGID(LANG_NEUTRAL,
615 (LPTSTR) & lpMsgBuf, 0, NULL);
616 printf("Error writing profile string - %s", lpMsgBuf);
624 strcpy(fname, argv[2]);
625 if (strcmp(argv[1], "+") == 0) {
626 file = fopen(fname, "rb");
629 len = filelength(_fileno(file));
630 save = (char *)malloc(len + 2);
632 *ch++ = 0; /* a small hack to allow matching /r/n if /n is first character */
633 len = fread(ch, sizeof(char), len, file);
634 file = freopen(fname, "wb", file);
636 if ((*ch == '\n') && (*(ch - 1) != '\r')) { /*line feed alone */
646 if (strcmp(argv[1], "-") == 0) {
647 strcpy(fname, argv[2]);
648 file = fopen(fname, "rb");
651 len = filelength(_fileno(file));
652 save = (char *)malloc(len + 1);
654 len = fread(ch, sizeof(char), len, file);
655 file = freopen(fname, "wb", file);
665 if (strstr(fname, ".et") == NULL)
666 strcat(fname, ".et");
667 file = fopen(fname, "rb");
670 len = filelength(_fileno(file));
671 save = (char *)malloc(len + 1);
673 len = fread(ch, sizeof(char), len, file);
674 file = freopen(fname, "wb", file);
681 pvar = (char **)malloc(argc * sizeof(char *));
682 for (i = 1; i < argc - 1; i++)
683 pvar[i] = argv[i + 1];
684 pvar[argc - 1] = NULL;
686 (void)_spawnvp(_P_WAIT, argv[1], pvar);