summaryrefslogtreecommitdiffstats
path: root/ldap/admin/src/namegen.c
blob: bdb8c4e1b35dc97e7392027fc95a7416a1b2d211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
/* namegen.c - utility program to generate name *
 * of backup files in the format YYYY_MM_DD_HMS *
 * and set it up as an environment variable to  *
 * be used by batch files on NT                 *
 *                                              *
 * to use it do the following in your batch file*
 * namegen                                      *
 * call bstart                                   *
 * .......                                      *
 * call bend                                     *
 * rm end.bat                                   *
 *                                              *
 * start and end are batch files generated by   *
 * name gen.                                    *
 * Example: ldif2db.bat                         */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

#define STARTFILE "bstart.bat"
#define ENDFILE   "bend.bat"
#define CMD       "set DATESTR=%0\n"
 

int main (int argc, char **argv)
{
    char      szDate [64];
    char      szDateFile [64];
	char      szCmd [256];
	struct tm *sCurTime;
	long      lCurTime;
    int       rt;
    FILE      *fBatch;

	time( &lCurTime );

	sCurTime = localtime( &lCurTime );

	strftime(szDate, sizeof (szDateFile), "%Y_%m_%d_%H%M%S", 
             sCurTime);

    PR_snprintf (szDateFile, sizeof(szDateFile), "%s.bat", szDate);

    /* create date batch file */
    fBatch = fopen (szDateFile, "w");
    if (fBatch == NULL)
    {
        perror ("Unable to create date file!");
        exit (1);
    }

    rt = fwrite (CMD, strlen (CMD), 1, fBatch);
    if (rt != 1)
    {
        perror ("Unable to write date file\n");
        exit (1);
    }

    fclose (fBatch);

    /* create bstart.bat that executest date batch file */
    fBatch = fopen (STARTFILE, "w");
    if (fBatch == NULL)
    {
        perror ("Unable to bstart file!");
        exit (1);
    }

    PR_snprintf (szCmd, sizeof(szCmd), "call %s", szDate);

    rt = fwrite (szCmd, strlen (szCmd), 1, fBatch);
    if (rt != 1)
    {
        perror ("Unable to write bstart file\n");
        exit (1);
    }

    fclose (fBatch);

    /* create bstart.bat that executest date batch file */
    fBatch = fopen (ENDFILE, "w");
    if (fBatch == NULL)
    {
        perror ("Unable to bend file!");
        exit (1);
    }

    PR_snprintf (szCmd, sizeof(szCmd), "del %s\ndel bstart.bat\nset DATESTR=", szDateFile);

    rt = fwrite (szCmd, strlen(szCmd), 1, fBatch);
    if (rt != 1)
    {
        perror ("Unable to write bend file\n");
        exit (1);
    }

    fclose (fBatch);

    return 0;
}