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
23 #define intptr_t INT_PTR
29 printf("util_cr file ;remove cr (from crlf)\n\
30 OR util_cr } ProductVersion in_filename out_filename ; substitute for %%1-%%5 in file\n\
31 %%1=Major version, %%2=Minor version, %%3=Patch(first digit) %%4=(last two digits) %%5=Version display string \n\
32 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\
33 e.g 1.0.4.1, 1.0.4 a 1.0.401, 1.0.4a all represent the same version\n\
34 OR util_cr + file ;add cr\n \
35 OR util_cr * \"+[register key value] x=y\" ; add register key value\n\
36 OR util_cr * \"-[register key value]\" ; aremove register key value\n\
37 OR util_cr @ file.ini \"[SectionKey]variable=value\" ; update ini-ipr-pwf file\n\
38 OR util_cr @ file.ini \"[SectionKey]variable=value*DatE*\" ; update ini-ipr-pwf file, insert date\n\
39 OR util_cr ~ ;force error\n\
40 OR util_cr _del [/q=quiet /s=recurese] [*. *. *.] ;delete \n\
41 OR util_cr _cpy [/q=quiet ] [*. *. *.] destinationFolder;\n\
42 OR util_cr _isOS [nt xp 98 9x w2] ;test for OS, return 1 if match else 0\n\
43 OR util_cr _dir !build type!source!object! set current directory in file->home base is used for offset\n\
44 OR unil_cr _ver return compiler version\n");
49 WORD langID
; // language ID
50 WORD charset
; // character set (code page)
54 CheckVersion(int argc
, char *argv
[])
56 OSVERSIONINFO VersionInfo
;
58 memset(&VersionInfo
, 0, sizeof(VersionInfo
));
59 VersionInfo
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
60 if (!GetVersionEx(&VersionInfo
)) {
63 for (i
= 2; i
< argc
; i
++) {
64 if (stricmp(argv
[i
], "nt") == 0) {
65 if ((VersionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
66 && (VersionInfo
.dwMajorVersion
== 4)
67 && (VersionInfo
.dwMinorVersion
== 0))
70 if (stricmp(argv
[i
], "xp") == 0) {
71 if ((VersionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
72 && (VersionInfo
.dwMajorVersion
== 5)
73 && (VersionInfo
.dwMinorVersion
== 1))
76 if (stricmp(argv
[i
], "w2") == 0) {
77 if ((VersionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
78 && (VersionInfo
.dwMajorVersion
== 5)
79 && (VersionInfo
.dwMinorVersion
== 0))
82 if (stricmp(argv
[i
], "98") == 0) {
83 if ((VersionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
)
84 && (VersionInfo
.dwMinorVersion
== 10))
87 if (stricmp(argv
[i
], "95") == 0) {
88 if ((VersionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
)
89 && (VersionInfo
.dwMinorVersion
== 0))
93 if (stricmp(argv
[i
], "9x") == 0) {
94 if (VersionInfo
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
)
97 if (stricmp(argv
[i
], "_") == 0)
104 Addkey(const char *hkey
, const char *subkey
, const char *stag
,
107 DWORD disposition
, result
;
108 HKEY kPkey
, kHkey
= 0;
109 if (strcmp(hkey
, "HKEY_CLASSES_ROOT") == 0)
110 kHkey
= HKEY_CLASSES_ROOT
;
111 if (strcmp(hkey
, "HKEY_CURRENT_USER") == 0)
112 kHkey
= HKEY_CURRENT_USER
;
113 if (strcmp(hkey
, "HKEY_LOCAL_MACHINE") == 0)
114 kHkey
= HKEY_LOCAL_MACHINE
;
117 result
= (RegCreateKeyEx(kHkey
/*HKEY_LOCAL_MACHINE */
118 , subkey
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
119 KEY_ALL_ACCESS
, NULL
, &kPkey
,
120 &disposition
) == ERROR_SUCCESS
);
122 printf("AFS Error - Could Not create a registration key\n");
127 if ((sval
) && (strlen(sval
))) {
130 RegSetValueEx(kPkey
, "", 0, REG_SZ
, (CONST BYTE
*) sval
,
131 (DWORD
)strlen(sval
));
134 RegSetValueEx(kPkey
, stag
, 0, REG_SZ
, (CONST BYTE
*) sval
,
135 (DWORD
)strlen(sval
));
140 (RegSetValueEx(kPkey
, "", 0, REG_SZ
, (CONST BYTE
*) "", 0));
143 (RegSetValueEx(kPkey
, stag
, 0, REG_SZ
, (CONST BYTE
*) "", 0));
145 if (result
!= ERROR_SUCCESS
) {
146 printf("AFS Error - Could Not create a registration key\n");
152 Subkey(const char *hkey
, const char *subkey
)
156 if (strcmp(hkey
, "HKEY_CLASSES_ROOT") == 0)
157 kHkey
= HKEY_CLASSES_ROOT
;
158 if (strcmp(hkey
, "HKEY_CURRENT_USER") == 0)
159 kHkey
= HKEY_CURRENT_USER
;
160 if (strcmp(hkey
, "HKEY_LOCAL_MACHINE") == 0)
161 kHkey
= HKEY_LOCAL_MACHINE
;
164 result
= RegDeleteKey(kHkey
, subkey
);
165 if (result
!= ERROR_SUCCESS
) {
166 printf("AFS Error - Could Not create a registration key\n");
172 doremove(BOOL bRecurse
, BOOL bQuiet
, char *argv
)
175 char parm
[MAX_PATH
+ 1];
176 char basdir
[MAX_PATH
+ 1];
179 GetCurrentDirectory(sizeof(basdir
), basdir
);
180 if (strrchr(parm
, '\\') != NULL
) { /*jump to base directory */
181 pParm
= strrchr(parm
, '\\');
183 if (!SetCurrentDirectory(parm
))
188 struct _finddata_t fileinfo
;
191 char basdir
[MAX_PATH
+ 1];
192 GetCurrentDirectory(sizeof(basdir
), basdir
);
193 hfile
= _findfirst(pParm
, &fileinfo
);
194 bmore
= (hfile
!= -1);
196 if ((DeleteFile(fileinfo
.name
) == 1) && (!bQuiet
))
197 printf("Remove %s\\%s\n", basdir
, fileinfo
.name
);
198 bmore
= (_findnext(hfile
, &fileinfo
) == 0);
202 /*RECURSIVE LOOP - SCAN directories */
203 struct _finddata_t fileinfo
;
206 doremove(FALSE
, bQuiet
, pParm
);
207 hfile
= _findfirst("*.*", &fileinfo
);
208 bmore
= (hfile
!= -1);
210 if (fileinfo
.attrib
& _A_SUBDIR
) {
211 if ((strcmp(fileinfo
.name
, ".") != 0)
212 && (strcmp(fileinfo
.name
, "..") != 0)) {
213 if (SetCurrentDirectory(fileinfo
.name
))
214 doremove(TRUE
, bQuiet
, pParm
);
215 SetCurrentDirectory(basdir
);
218 bmore
= (_findnext(hfile
, &fileinfo
) == 0);
222 SetCurrentDirectory(basdir
);
228 char bld
[MAX_PATH
+ 1], parm
[MAX_PATH
+ 1], src
[MAX_PATH
+ 1],
229 obj
[MAX_PATH
+ 1], dir
[MAX_PATH
+ 1], curdir
[MAX_PATH
+ 1];
232 BOOL isObjAbs
, isSrcAbs
; /* two flags to determine if either string is not relative */
233 strcpy(bld
, val
+ 1); /*it better be checked or free */
234 strcpy(src
, strchr(bld
, '!') + 1);
235 strcpy(obj
, strchr(src
, '!') + 1);
236 *strchr(bld
, '!') = 0;
237 *strchr(obj
, '!') = 0;
238 *strchr(src
, '!') = 0;
239 isObjAbs
= ((*obj
== '\\') || (strstr(obj
, ":")));
240 isSrcAbs
= ((*src
== '\\') || (strstr(src
, ":")));
241 GetCurrentDirectory(MAX_PATH
, dir
);
247 strcat(curdir
, "\\ \n");
248 if (GetTempPath(MAX_PATH
, parm
) == 0)
250 strcat(parm
, "home");
251 if ((f
= fopen(parm
, "w")) == NULL
)
255 if (obj
[strlen(obj
) - 1] != '\\')
257 if (src
[strlen(src
) - 1] != '\\')
259 do { /* try to match src or obj */
260 if ((p
= strstr(dir
, src
)) && isSrcAbs
) {
263 } else if ((p
= strstr(dir
, src
)) && !isSrcAbs
264 && *(p
- 1) == '\\') {
267 } else if ((p
= strstr(dir
, obj
)) && isObjAbs
) {
270 } else if ((p
= strstr(dir
, obj
)) && !isObjAbs
271 && *(p
- 1) == '\\') {
275 } while (strstr(p
, src
) || strstr(p
, obj
));
277 sprintf(parm
, "OJT=%s%s\\%s \n", obj
, bld
, p
);
280 sprintf(parm
, "OJT=%s%s%s\\%s \n", dir
, obj
, bld
, p
);
282 if (strcmp(curdir
, parm
+ 4) == 0) /* current directory is object */
283 strcpy(parm
, "OJT= \n");
284 fwrite(parm
, strlen(parm
), 1, f
);
286 sprintf(parm
, "SRT=%s%s\\ \n", src
, p
);
288 *(p
- strlen(src
)) = 0;
289 sprintf(parm
, "SRT=%s%s%s\\ \n", dir
, src
, p
);
291 if (strcmp(curdir
, parm
+ 4) == 0) /* current directory is object */
292 strcpy(parm
, "SRT= \n");
293 fwrite(parm
, strlen(parm
), 1, f
);
294 /* now lets set the AFS_LASTCMP environment variable */
295 sprintf(parm
, "AFS_LASTCMP=%s\\%s", src
, p
);
297 __except(EXCEPTION_EXECUTE_HANDLER
) {
307 isequal(char *msg1
, char *msg2
, char *disp
)
311 if (strcmp(msg1
, msg2
) != 0)
313 printf("ERROR -- %s \n", disp
);
318 SetSysEnv(int argc
, char *argv
[])
321 printf("assignment %s %s\n", argv
[2], argv
[3]);
322 Addkey("HKEY_LOCAL_MACHINE",
323 "System\\CurrentControlSet\\Control\\Session Manager\\Environment",
327 SendMessageTimeout(HWND_BROADCAST
, WM_SETTINGCHANGE
, 0,
328 (LPARAM
) "Environment", SMTO_NORMAL
, 1, &dwResult
);
333 main(int argc
, char *argv
[])
338 char **pvar
, *ch
, *save
;
340 BOOL bRecurse
= FALSE
;
345 /* RSM4: Add an "ECHO" that doesn't append a new line... */
346 if (strcmp(argv
[1], "_echo") == 0) {
349 printf("%s",argv
[2]);
353 if (strcmp(argv
[1], "_sysvar") == 0) {
356 return (SetSysEnv(argc
, argv
));
359 if (strnicmp(argv
[1], "_dir", 4) == 0) { /*get current directory routine */
363 if (strnicmp(argv
[1], "_isequal", 4) == 0) { /*get current directory routine */
364 return isequal(argv
[2], argv
[3], argv
[4]);
366 if (stricmp(argv
[1], "_del") == 0) { /*DELETE routine */
368 if ((argc
> iargc
) && (stricmp(argv
[iargc
], "/s") == 0)) {
372 if ((argc
> iargc
) && (stricmp(argv
[iargc
], "/q") == 0)) {
376 if ((argc
> iargc
) && (stricmp(argv
[iargc
], "/s") == 0)) {
380 while (iargc
< argc
) {
381 doremove(bRecurse
, bQuiet
, argv
[iargc
]);
386 if (strcmp(argv
[1], "_ver") == 0) {
391 if (strcmp(argv
[1], "_isOS") == 0)
392 return CheckVersion(argc
, argv
);
393 if (strcmp(argv
[1], "}") == 0) {
394 char v1
[4], v2
[4], v3
[4], v4
[4];
404 if ((ptr
= strtok(argv
[2], ". \n")) == NULL
)
407 if ((ptr
= strtok(NULL
, ". \n")) == NULL
)
410 if ((ptr
= strtok(NULL
, ". \n")) == NULL
)
413 switch (strlen(ptr
)) {
418 if (isdigit(*ptr
) != 0)
421 case 2: //ONLY 1.0.44 is interpreted as 1.0.4.4 or 1.0.4a as 1.0.4.a
422 if (isdigit(*ptr
) == 0)
426 if (isalpha(*ptr
) == 0) {
428 } else if (isalpha(*ptr
) != 0) {
429 pat2
= tolower(*ptr
) - 'a' + 1;
433 case 3: //1.0.401 or 1.0.40a are the same;
434 if ((isdigit(*ptr
) == 0) // first 2 must be digit
435 || (isdigit(*(ptr
+ 1)) == 0)
436 || (*(ptr
+ 1) != '0' && isdigit(*(ptr
+ 2)) == 0) // disallow 1.0.4b0 or 1.0.41a
444 pat2
= tolower(*ptr
) - 'a' + 1;
449 // last can be 1-2 digits or one alpha (if pat2 hasn't been set)
450 if ((ptr
= strtok(NULL
, ". \n")) != NULL
) {
453 switch (strlen(ptr
)) {
455 pat2
= (isdigit(*ptr
)) ? atoi(ptr
) : tolower(*ptr
) - 'a' + 1;
458 if (isdigit(*ptr
) == 0 || isdigit(*(ptr
+ 1)) == 0)
466 file
= fopen(argv
[3], "r");
469 len
= filelength(_fileno(file
));
470 save
= malloc(len
+ 1);
472 len
= fread(buf
, sizeof(char), len
, file
);
473 buf
[len
] = 0; //set eof
475 file
= fopen(argv
[4], "w");
478 sprintf(v1
, "%i", maj
);
479 sprintf(v2
, "%i", min
);
480 sprintf(v3
, "%i", pat
);
481 sprintf(v4
, "%02i", pat2
);
483 ptr
= strstr(buf
, "%");
484 fwrite(buf
, 1, (ptr
) ? ptr
- buf
: strlen(buf
), file
); //write file if no % found or up to %
487 switch (*(ptr
+ 1)) //skip first scan if buf="1...."
490 fwrite(v1
, 1, strlen(v1
), file
);
494 fwrite(v2
, 1, strlen(v2
), file
);
498 fwrite(v3
, 1, strlen(v3
), file
);
502 fwrite(v4
, 1, strlen(v4
), file
);
506 fwrite(v5
, 1, strlen(v5
), file
);
510 fwrite("%", 1, 1, file
); //either % at end of file or no %1...
519 if (strcmp(argv
[1], "~") == 0) { //check for file presence
520 if (fopen(argv
[2], "r"))
523 printf("ERROR --- File not present %s\n", argv
[2]);
525 printf("Error---%s\n", argv
[3]);
528 if (strcmp(argv
[1], "*") == 0) { /* "[HKEY_CLASSES_ROOT\CLSID\{DC515C27-6CAC-11D1-BAE7-00C04FD140D2}] @=AFS Client Shell Extension" */
531 for (i
= 2; argc
>= 3; i
++) {
532 char *ssub
= strtok(argv
[i
], "[");
534 char *skey
= strtok(NULL
, "]");
536 if ((ssub
== NULL
) || (skey
== NULL
)) {
537 printf("format error parameter %s\n", argv
[i
]);
540 option
= (*ssub
== '-');
541 stag
= strtok(NULL
, "\0");
545 ssub
= strtok(skey
, "\\");
546 ssub
= strtok(NULL
, "\0");
547 sval
= strtok(stag
, "=");
548 sval
= strtok(NULL
, "\0");
551 Addkey(skey
, ssub
, stag
, sval
);
555 Addkey(skey
, ssub
, stag
, "");
565 if (strcmp(argv
[1], "@") == 0) {
566 char msg
[256], msgt
[256];
570 for (i
= 3; argc
>= 4; i
++) {
572 char *ssect
= strstr(argv
[i
], "[");
573 char *skey
= strstr(ssect
, "]");
575 if ((ssect
== NULL
) || (skey
== NULL
)) {
576 printf("format error parameter %s\n", argv
[i
]);
581 if ((strlen(skey
+ 1) == 0) || (strlen(ssect
) == 0)) {
582 printf("format error parameter %s\n", argv
[i
]);
585 while (*++skey
== ' ');
586 sval
= strstr(skey
, "=");
588 printf("format error parameter %s\n", argv
[i
]);
592 while (*--ptr
== ' ');
594 while (*++sval
== ' ');
595 if (ptr
= strstr(sval
, "*DatE*")) { // ok so lets substitute date in this string;
602 strcat(msg
, ptr
+ 6);
605 if (ptr
= strstr(sval
, "*TimE*")) {
610 strncat(msgt
, tmpbuf
, 5);
611 strcat(msgt
, ptr
+ 6);
614 if (WritePrivateProfileString(ssect
, skey
, sval
, argv
[2]) == 0) {
616 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
617 FORMAT_MESSAGE_FROM_SYSTEM
|
618 FORMAT_MESSAGE_IGNORE_INSERTS
, NULL
,
619 GetLastError(), MAKELANGID(LANG_NEUTRAL
,
621 (LPTSTR
) & lpMsgBuf
, 0, NULL
);
622 printf("Error writing profile string - %s", lpMsgBuf
);
630 strcpy(fname
, argv
[2]);
631 if (strcmp(argv
[1], "+") == 0) {
632 file
= fopen(fname
, "rb");
635 len
= filelength(_fileno(file
));
636 save
= malloc(len
+ 2);
638 *ch
++ = 0; /* a small hack to allow matching /r/n if /n is first character */
639 len
= fread(ch
, sizeof(char), len
, file
);
640 file
= freopen(fname
, "wb", file
);
642 if ((*ch
== '\n') && (*(ch
- 1) != '\r')) { /*line feed alone */
652 if (strcmp(argv
[1], "-") == 0) {
653 strcpy(fname
, argv
[2]);
654 file
= fopen(fname
, "rb");
657 len
= filelength(_fileno(file
));
658 save
= malloc(len
+ 1);
660 len
= fread(ch
, sizeof(char), len
, file
);
661 file
= freopen(fname
, "wb", file
);
671 if (strstr(fname
, ".et") == NULL
)
672 strcat(fname
, ".et");
673 file
= fopen(fname
, "rb");
676 len
= filelength(_fileno(file
));
677 save
= malloc(len
+ 1);
679 len
= fread(ch
, sizeof(char), len
, file
);
680 file
= freopen(fname
, "wb", file
);
687 pvar
= malloc(argc
* sizeof(char *));
688 for (i
= 1; i
< argc
- 1; i
++)
689 pvar
[i
] = argv
[i
+ 1];
690 pvar
[argc
- 1] = NULL
;
692 (void)_spawnvp(_P_WAIT
, argv
[1], pvar
);