Annotation of embedaddon/smartmontools/os_win32/syslogevt.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * os_win32/syslogevt.c
        !             3:  *
        !             4:  * Home page of code is: http://smartmontools.sourceforge.net
        !             5:  *
        !             6:  * Copyright (C) 2004-10 Christian Franke <smartmontools-support@lists.sourceforge.net>
        !             7:  *
        !             8:  * This program is free software; you can redistribute it and/or modify
        !             9:  * it under the terms of the GNU General Public License as published by
        !            10:  * the Free Software Foundation; either version 2, or (at your option)
        !            11:  * any later version.
        !            12:  *
        !            13:  * You should have received a copy of the GNU General Public License
        !            14:  * (for example COPYING); if not, write to the Free
        !            15:  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
        !            16:  *
        !            17:  */
        !            18: 
        !            19: static char rcsid[] = "$Id: syslogevt.c 3257 2011-02-14 22:19:42Z manfred99 $";
        !            20: 
        !            21: #include <stdio.h>
        !            22: #include <string.h>
        !            23: #include <process.h>
        !            24: 
        !            25: #define WIN32_LEAN_AND_MEAN
        !            26: #include <windows.h>
        !            27: 
        !            28: #include "syslogevt.h"
        !            29: // Compile time check for Message Ids, see also syslog_win32.cpp
        !            30: typedef assert_msg_syslog   [MSG_SYSLOG    ==  0 ? 1 : -1];
        !            31: typedef assert_msg_syslog_01[MSG_SYSLOG_01 ==  1 ? 1 : -1];
        !            32: typedef assert_msg_syslog_10[MSG_SYSLOG_10 == 10 ? 1 : -1];
        !            33: 
        !            34: 
        !            35: static int usage()
        !            36: {
        !            37:        puts(
        !            38:                "syslogevt $Revision: 3257 $ Copyright (C) 2004-10 Christian Franke\n"
        !            39:                "Home page is http://smartmontools.sourceforge.net/\n"
        !            40:                "\n"
        !            41:                "Usage: syslogevt [-ru] name [ident ...]\n"
        !            42:                "\n"
        !            43:                "Creates registry files \"name-r.reg\" and \"name-u.reg\" to (un)register\n"
        !            44:                "this program as an event message file for message source(s) \"ident\".\n"
        !            45:                "If \"ident\" is ommited, \"name\" is used. Options:\n"
        !            46:                "\n"
        !            47:                "    -r    run \"regedit name-r.reg\" after creating files\n"
        !            48:                "    -u    run \"regedit name-u.reg\" after creating files\n"
        !            49:                "\n"
        !            50:                "Examples:\n"
        !            51:                "\n"
        !            52:                "syslogevt smartd                     (Create smartd-r.reg and smartd-u.reg)\n"
        !            53:                "regedit smartd-r.reg           (Register syslogevt.exe for smartd messages)\n"
        !            54:                "\n"
        !            55:                "syslogevt -r smartd                             (Same as above in one step)\n"
        !            56:                "\n"
        !            57:                "regedit smartd-u.reg                                (Undo the registration)\n"
        !            58:                "\n"
        !            59:                "CAUTION: A registry entry of an existing event source with the same \"ident\"\n"
        !            60:                "         will be overwritten by regedit without notice."
        !            61:        );
        !            62:        return 1;
        !            63: }
        !            64: 
        !            65: main(int argc, char ** argv)
        !            66: {
        !            67:        int regedit, a1, ai;
        !            68:        char name1[30+1], name2[30+1], mypath[MAX_PATH+1];
        !            69:        const char * ident;
        !            70:        FILE * f1, * f2;
        !            71: 
        !            72:        if (argc < 2)
        !            73:                return usage();
        !            74: 
        !            75:        a1 = 1;
        !            76:        regedit = 0;
        !            77:        if (!strcmp(argv[a1], "-r")) {
        !            78:                regedit = 1; a1++;
        !            79:        }
        !            80:        else if (!strcmp(argv[a1], "-u")) {
        !            81:                regedit = -1; a1++;
        !            82:        }
        !            83: 
        !            84:        for (ai = a1; ai < argc; ai++) {
        !            85:                ident = argv[ai];
        !            86:                if (!(ident[0] && strlen(ident) < sizeof(name1)-10
        !            87:                          && strcspn(ident, "-.:/\\") == strlen(ident) )) {
        !            88:                        return usage();
        !            89:                }
        !            90:        }
        !            91: 
        !            92:        if (!GetModuleFileName(NULL, mypath, sizeof(mypath)-1)) {
        !            93:                fputs("GetModuleFileName failed\n", stderr);
        !            94:                return 1;
        !            95:        }
        !            96: 
        !            97:        ident = argv[a1];
        !            98:        strcpy(name1, ident); strcat(name1, "-r.reg");
        !            99:        strcpy(name2, ident); strcat(name2, "-u.reg");
        !           100: 
        !           101:        if (!(f1 = fopen(name1, "w"))) {
        !           102:                perror(name1); return 1;
        !           103:        }
        !           104:        if (!(f2 = fopen(name2, "w"))) {
        !           105:                perror(name2); fclose(f1); unlink(name1); return 1;
        !           106:        }
        !           107: 
        !           108:        fputs("REGEDIT4\n\n", f1);
        !           109:        fputs("REGEDIT4\n\n", f2);
        !           110: 
        !           111:        for (ai = (argc > a1+1 ? a1+1 : a1); ai < argc; ai++) {
        !           112:                int i;
        !           113:                ident = argv[ai];
        !           114:                fputs("[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\", f1);
        !           115:                fputs(ident, f1); fputs("]\n\"EventMessageFile\"=\"", f1);
        !           116:                for (i = 0; mypath[i]; i++) {
        !           117:                        if (mypath[i] == '\\')
        !           118:                                fputc('\\', f1);
        !           119:                        fputc(mypath[i], f1);
        !           120:                }
        !           121:                fputs("\"\n\"TypesSupported\"=dword:00000007\n\n", f1);
        !           122: 
        !           123:                fputs("[-HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\", f2);
        !           124:                fputs(ident, f2); fputs("]\n\n", f2);
        !           125:        }
        !           126: 
        !           127:        fclose(f1);
        !           128:        fclose(f2);
        !           129: 
        !           130:        if (GetVersion() & 0x80000000) {
        !           131:                puts("Warning: Event log not supported on Win9x/ME\n");
        !           132:                if (regedit)
        !           133:                        return 1;
        !           134:        }
        !           135: 
        !           136:        if (regedit) {
        !           137:                if (spawnlp(P_WAIT, "regedit", "regedit", (regedit > 0 ? name1 : name2), (const char *)0) == -1) {
        !           138:                        fputs("regedit: cannot execute\n", stderr);
        !           139:                        return 1;
        !           140:                }
        !           141:        }
        !           142:        else {
        !           143:                fputs("Files generated. Use\n\n    regedit ", stdout);
        !           144:                puts(name1);
        !           145:                fputs("\nto register event message file, and\n\n    regedit ", stdout);
        !           146:                puts(name2);
        !           147:                fputs("\nto remove registration later.\n\n"
        !           148:                          "Do not remove this program when registered.\n", stdout);
        !           149:        }
        !           150: 
        !           151:        return 0;
        !           152: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>