File: main.c.orig

package info (click to toggle)
wwwcount 2.4-4
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 1,348 kB
  • ctags: 693
  • sloc: ansic: 9,271; sh: 1,372; makefile: 168
file content (1053 lines) | stat: -rw-r--r-- 27,149 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
/*
 *  WWW document access counter
 *
 *  RCS:
 *      $Revision: 2.3 $
 *      $Date: 1996/05/03 02:20:22 $
 *
 *  Security:
 *      Unclassified
 *
 *  Description:
 *     main routine for WWW homepage access counter
 *  
 *  Input Parameters:
 *      type    identifier  description
 *
 *      N/A
 *
 *  Output Parameters:
 *      type    identifier  description
 *
 *      N/A
 *
 *  Return Values:
 *      value   description
 *
 *  Side Effects:
 *      None
 *
 *  Limitations and Comments:
 *      None
 *
 *  Development History:
 *      who                 when        why
 *      muquit@semcor.com   10-Apr-95   first cut
 *      muquit@semcor.com   06-07-95    release 1.2
 *      muquit@semcor.com   07-13-95    release 1.3
 *      ma_muquit@fccc.edu  10-11-97    release 2.3
 */

#define __Main__
#include "cdebug.h"
#include "combine.h"
#include "count.h"


#ifdef SYS_WIN32
#include "configNT.h"
#else
#include "config.h"
#endif


int main (argc, argv)
int
    argc;
char
    **argv;

{
    int
        rc = 0;

    unsigned int
        ignore_site=False;

    register int
        i;

    char
        *browser_type,
        *remote_ip,
        *query_string;

    int
        n;

    int
        counter_length;

    int
        left_pad;

    char
        filename[MaxTextLength];

    char
        tmpbuf[MaxTextLength];

    char
        digitbuf[MaxTextLength];

    int
        fd;

    char
        format[10],
        buf[50];

    int
        count,
        counter;

    int
        tagdigit;

    int
        MaxDigits=10;

    DigitInfo
        digit_info;

    FrameInfo
        frame_info;

    unsigned int
        use_st;
    
    int
        min_dig1,
        min_dig2;

    int
        mon_dig1,
        mon_dig2,
        day_dig1,
        day_dig2,
        year_dig1,
        year_dig2;

     char
        hour[10],
        min[10],
        ampm[10];

    int
        display_what;

    unsigned int
        untrusted_browser=False;


#ifdef ACCESS_AUTH
    char
        *rem_refh;
#endif

/*
**---------initialize globals------Starts---
*/
    for (i=0; i < MaxSites; i++)
    {
        GrefererHost[i] = (char *) NULL;
        GignoreSite[i] = (char *) NULL;
    }
    Grhost=0;
    Gsite=0;
    Gdebug=False;
    Gauto_file_creation=0;
    Gstrict_mode=1;
    GrgbMappingIsError=1;
/*
**---------initialize globals------Ends---
*/
    count=1;
    *hour='\0';
    *min='\0';
    *ampm='\0';
    *format='\0';
    *buf='\0';
    *filename = '\0';
    *digitbuf='\0';
    *tmpbuf = '\0';
    counter = 0;
    tagdigit=9;
    left_pad=MaxDigits;
    use_st=False;
    display_what=SHOW_COUNTER;
    remote_ip=(char *) NULL;

    /*
    ** parse command line, this is only used for testing at commandline
    ** no command line flag is allowed in the web page while calling
    ** the program
    */

    for (i=1; i < argc; i++)
    {
        if (!strncmp(argv[i],"-debug",(int)strlen("-debug")))
        {
            Gdebug=True;
            break;
        }
    }

    Debug2("[%s] Count %s: --Debug--",GetTime(),Version);

#ifdef TEST_RRR
    /*************REMOVE****************/
    env_test=getenv("PATH_INFO");
    if (env_test == (char *) NULL)
    {
        (void) strcpy(tmpbuf,"PATH_INFO is NULL");
    }
    else
    {
        (void) sprintf(tmpbuf,"PATH_INFO is=%s\n",env_test);
    }
    Warning(tmpbuf);

    env_test=getenv("PATH_TRANSLATED");
    if (env_test == (char *) NULL)
    {
        (void) strcpy(tmpbuf,"PATH_TRANSLATED is NULL");
    }
    else
    {
        (void) sprintf(tmpbuf,"PATH_TRANSLATED=%s\n",env_test);
    }
    Warning(tmpbuf);
    /*************REMOVE****************/
#endif
    rc=CheckDirs();
    if (rc == 1)
    {
        *tmpbuf='\0';
        (void) sprintf(tmpbuf,"ConfigDir,DigitDir,DataDir,LogDir must differ");
        Warning(tmpbuf);
        PrintHeader();
        StringImage(tmpbuf);
        exit(1);
    }

    /*
    ** parse the authorization list
    */
    rc = ParseConfig ();

    switch (rc)
    {
        case ConfigOpenFailed:
        {
            *tmpbuf='\0';
            (void) sprintf (tmpbuf, 
                "Faliled to open configuration file: \"%s/%s\"",
                    ConfigDir,ConfigFile);
            Warning (tmpbuf);
            PrintHeader ();
            StringImage (tmpbuf);
            exit(1);
            break;
        }

        case NoAutofcBlock:
        {
            *tmpbuf='\0';
            (void) sprintf (tmpbuf,
                "No auto file creation block in conf file: \"%s/%s\"",
                    ConfigDir,ConfigFile);
            Warning(tmpbuf);
            PrintHeader ();
            StringImage(tmpbuf);
            exit(1);
            break;
        }

        case NoStrictBlock:
        {
            *tmpbuf='\0';
            (void) sprintf (tmpbuf,
                "No Strict mode block in conf file: \"%s/%s\"",
                    ConfigDir,ConfigFile);
            Warning(tmpbuf);
            PrintHeader ();
            StringImage(tmpbuf);
            exit(1);
            break;
        }
        case NoIgnoreHostsBlock:
        {
            *tmpbuf='\0';
            (void) sprintf (tmpbuf,
                "No IgnoreHosts Block in the configuration file: \"%s/%s\"",
                    ConfigDir,ConfigFile);

            Warning(tmpbuf);
            PrintHeader ();
            StringImage(tmpbuf);
            exit(1);
        }

        case NoRefhBlock:
        {
            *tmpbuf='\0';
            (void) sprintf(tmpbuf,"Compiled with -DACCESS_AUTH,but no such block in configuration file!");
            Warning(tmpbuf);
            PrintHeader ();
            StringImage(tmpbuf);
            exit(1);
        }

        case UnpexpectedEof:
        {
            *tmpbuf='\0';
            (void) sprintf (tmpbuf, 
            "Unexpected EOF in configuration file: \"%s/%s\"",
                ConfigDir,ConfigFile);
            Warning(tmpbuf);
            PrintHeader ();
            StringImage(tmpbuf);
            exit(1);
        }

    } /* switch (rc) */


        Debug2("Gsite: %d",Gsite,0);
        Debug2("Gauto_file_creation= %d",Gauto_file_creation,0);
        Debug2("Gstrict_mode= %d",Gstrict_mode,0);

#ifdef ACCESS_AUTH
    /*
    ** check if the referer is a remote host or not. refere should
    ** be the local host.
    */

    rem_refh = (char *) getenv("HTTP_REFERER");
    if (rem_refh != (char *) NULL)
    {
        char
            http_ref[1024],
            Rhost[1024];
        *http_ref = '\0';
        *Rhost = '\0';
        /*
        ** this will be same as your host when accessed from your
        ** page. if the host is anything else, it is an unauthorized
        ** access. handle it.
        */
        *tmpbuf='\0';
        (void) strcpy (http_ref, rem_refh);
        GetRemoteReferer(http_ref, Rhost);

        /*
            (void) sprintf(tmpbuf,"http_ref=%s",http_ref);
            Warning(tmpbuf);
        */

        Debug2("Grhost= %d",Grhost,0);
        Debug2("Rhost= %s",Rhost,0);

        if (*Rhost != '\0')
        {
            if (Grhost > 0)
            {
                for (i=0; i < Grhost; i++)
                {
                    rc=mystrcasecmp(Rhost,GrefererHost[i]);
                    if (rc == 0)
                        break;
                }
            }
            if (rc != 0)
            {
                *tmpbuf='\0';
              (void) sprintf(tmpbuf,
              "Host: \"%s\" is not in the auth block of the conf file",Rhost);
                Warning(tmpbuf);                    
                PrintHeader();
                StringImage(tmpbuf);
                exit(1);
            }
        }
    }

#endif /* ACCESS_AUTH */
    /*
    ** get the user name from query string
    */
    query_string = (char *) getenv("QUERY_STRING");

    if (query_string == (char *) NULL)
    {
        *tmpbuf='\0';
        (void) sprintf(tmpbuf,"%s","Empty QUERY_STRING!");
        Warning(tmpbuf);
        PrintHeader ();
        StringImage(tmpbuf);
        exit(1);
    }

    rc=ParseQueryString(query_string,&digit_info,&frame_info);

    Debug2("\n",0,0);
    Debug2("Parsed QUERY_STRING",0,0); 
    Debug2(" ft=%d",frame_info.width,0);
    Debug4(" rgb=%d,%d,%d", frame_info.matte_color.red,
        frame_info.matte_color.green, frame_info.matte_color.blue,0);
    Debug2(" tr=%d",digit_info.alpha,0);
    Debug4(" trgb=%d,%d,%d",digit_info.alpha_red,
        digit_info.alpha_green,digit_info.alpha_blue,0);
    Debug2(" replace_color=%d",digit_info.replace_color,0);
    Debug4(" srgb=%d,%d,%d",
        digit_info.opaque_red,
        digit_info.opaque_green,
        digit_info.opaque_blue,
        0);
    Debug4(" prgb=%d,%d,%d",
        digit_info.pen_red,
        digit_info.pen_green,
        digit_info.pen_blue,
        0);
    Debug2(" maxdigits=%d",digit_info.maxdigits,0);
    Debug2(" wxh [ignored,exists for compatibity] =%dx%d", 
        digit_info.width, digit_info.height);
    Debug2(" md=%d", digit_info.maxdigits,0);
    Debug2(" pad=%d", digit_info.leftpad,0);
    Debug2(" ddhead=%s",digit_info.ddhead,0);
    Debug2(" st=%d",digit_info.st,0);
    Debug2(" sh=%d", digit_info.show,0);
    Debug2(" df=%s",digit_info.datafile,0);
    Debug2(" lit=%s",digit_info.literal,0);
    Debug2(" incr=%d",digit_info.increment_counter,0);
    Debug2(" negate=%d",digit_info.negate,0);
    Debug2(" rotate=%d",digit_info.rotate,0);
    Debug2(" degrees=%d",digit_info.rotate_degrees,0);
    Debug2(" timezone=%s",digit_info.time_z,0);
    Debug2(" timeformat=%d",digit_info.time_format,0);
    Debug2(" istrip=%d",digit_info.use_strip,0);
    Debug2(" image=%s", digit_info.gif_file,0);
    Debug2(" parsed QS--\n",0,0);

    if (rc)
    {
        Debug2("rc from from ParseQueryString()=%d",rc,0);
        exit(1);
    }

    if (digit_info.leftpad == True)
    {
        MaxDigits=digit_info.maxdigits;

        if ((MaxDigits < 5) || (MaxDigits > 10))
        {
            Warning("Maxdigits (md) must be >= 5 and <= 10");
            PrintHeader ();
            StringImage("Maxdigits (md) must be >= 5 and <= 10");
            exit(1);
        }
    }

    /*
    ** now find out what we'r trying to display
    */
    display_what=digit_info.display_type;

    if (display_what == SHOW_COUNTER)
    {
        if (checkfilename (digit_info.datafile) != 0)
        {
            *tmpbuf='\0';
            (void) sprintf(tmpbuf,"Illegal datafile path!");
            Warning(tmpbuf);
            PrintHeader ();
            StringImage(tmpbuf);
            exit(1);
        }
    }
    /*
    ** get frameinfo
    */

    if (frame_info.width > 1)
        digit_info.Frame=True;
    else
        digit_info.Frame=False;
    /*
    ** get the IP address of the connected machine to check if we need
    ** to increment the counter
    */
    if (display_what == SHOW_COUNTER)
    {
        remote_ip = (char *) getenv("REMOTE_ADDR");
        if (remote_ip == (char *) NULL)
        {
            /*
            ** put a dummy here
            */
            remote_ip = "dummy_ip";
        }
        else
        {
            for (i=0; i < Gsite; i++)
            {
                ignore_site=CheckRemoteIP(remote_ip,GignoreSite[i]);
                if (ignore_site == True)
                {
                    Debug2("Not counting IP: %s",remote_ip,0);
                    Debug2("GignoreSite[%d]:%s",i,GignoreSite[i]);
                    break;
                }
            }
        }
    }

    /*
    ** initialize Limit array
    */


    if (display_what == SHOW_COUNTER)
    {
        Debug2("Maxdigits=%d",MaxDigits,0);
    }

#ifdef ACCESS_AUTH 
    if (Gstrict_mode == 1)
    {
        if (rem_refh == (char *) NULL)
        {
            untrusted_browser=True;
        /*
        ** no HTTP_REFERER env variable found.
        ** display a literal string. this will make sure that even if
        ** someone with a broken browser tries to access the counter
        ** remotely, will not mess up the datafile. of course we'll
        ** miss some counts. because anyone accesses the page with a 
        ** broken browser like this will see a literal string
        */
            (void) strcpy(digit_info.literal,"888888");
        /*
        if (remote_ip != (char *) NULL)
        {
            (void) sprintf(tmpbuf,"No HTTP_REFERER supplied from %s",
                remote_ip);
            Warning(tmpbuf);
        }
        */
        /*
        ** try to get the browser name, it will give us a good
        ** feedback.
        */
            browser_type=(char *) getenv("HTTP_USER_AGENT");
            if (browser_type != (char *) NULL)
            {
                if (remote_ip != (char *) NULL)
                {
                *tmpbuf='\0';
                (void) sprintf(tmpbuf,"Untrusted browser %s",browser_type);
                Warning(tmpbuf);
                }
            }
        }
    }   /* Gstrict_mode == 1 */
#endif

    switch (display_what)
    {

        case SHOW_CLOCK: /* act as a clock*/
        {
            time_t
                daytime,
                tm;

            struct tm
                *Tm=(struct tm *) NULL;

            long
                diff;   /* in seconds */

            if (untrusted_browser == False)
            {
                tm=time(NULL);
                if (*digit_info.time_z != '\0')
                {
                    /*
                    ** no more TZ, 03/26/96
                    */
                    diff=checkTimezone(digit_info.time_z);

                    Tm=gmtime(&tm);
#ifdef HAVE_MKTIME
                    daytime=mktime(Tm);
#else
                    daytime=netMktime(Tm);
#endif
                    daytime += diff;
                    Tm=localtime(&daytime);

                }
                else
                {
                    Debug2("No timezone",0,0);
                    Tm=localtime(&tm);
                } 
                if (digit_info.time_format == 12)
                {
                    if ((Tm)->tm_hour < 12)
                        (void) strcpy(ampm,"A");
                    else
                        (void) strcpy(ampm,"P");
        
                    if ((Tm)->tm_hour > 12)
                        (Tm)->tm_hour = (Tm)->tm_hour-12;
  
                    if ((Tm)->tm_hour == 0)  
                        (Tm)->tm_hour=12;
                }
            /*
            ** take off strftime(), 1/13/95, muquit
            */
                (void) sprintf(hour,"%d",(Tm)->tm_hour);
             
            /*
            ** I like to keep leading zeros for minutes
            */
                min_dig1=(Tm->tm_min - (Tm->tm_min % 10)) / 10;
                min_dig2=Tm->tm_min % 10;
                (void) sprintf(min,"%d%d",min_dig1,min_dig2);
                (void) sprintf(buf,"%s:%s%s",hour,min,ampm);
            } 
            else
            {
               (void) strcpy(buf,"888888");
            }
         
            Debug2(" time buf= %s",buf,0);

            *digitbuf='\0';
            (void) strcpy(digitbuf,buf);
            WriteCounterImage(digitbuf, &digit_info,&frame_info);
            
            /*
            ** we will not be here
            */
            exit(0);
            break;
        }

        case SHOW_DATE:
        {
            time_t
                daytime,
                tm;

            struct tm
                *Tm;

            long
                diff;   /* in seconds */

            if (untrusted_browser == False)
            {
                tm=time(NULL);
                if (*digit_info.time_z != '\0')
                {

                    /*
                    ** no more TZ, 03/26/96
                    */
                    diff=checkTimezone(digit_info.time_z);
                    Tm=gmtime(&tm);
#ifdef HAVE_MKTIME
                    daytime=mktime(Tm);
#else
                    daytime=netMktime(Tm);
#endif
                    daytime += diff;
                    Tm=localtime(&daytime);
                }
                else
                {
                    Tm=localtime(&tm);
                }
                Tm->tm_mon++;

                Debug2(" Inside SHOW_DATE",0,0);

                /*
                ** padding, saw in xdaliclock
                */
                mon_dig1=(Tm->tm_mon-(Tm->tm_mon % 10)) / 10;
                mon_dig2=Tm->tm_mon % 10;

                day_dig1=(Tm->tm_mday-(Tm->tm_mday % 10)) / 10;
                day_dig2=Tm->tm_mday % 10;

                year_dig1=(Tm->tm_year-(Tm->tm_year % 10)) / 10;
                year_dig2=Tm->tm_year % 10;


                switch (digit_info.date_format)
                {
                    case DATE_MMDDYY:
                    default:
                    {
                        (void) sprintf(buf,"%d%d-%d%d-%d%d",
                    mon_dig1,mon_dig2,day_dig1,day_dig2,year_dig1,year_dig2);
                        break;
                    }

                    case DATE_DDMMYY:
                    {
                        (void) sprintf(buf,"%d%d-%d%d-%d%d",
                    day_dig1,day_dig2,mon_dig1,mon_dig2,year_dig1,year_dig2);
                        break;
                    }

                    case DATE_YYMMDD:
                    {
                        (void) sprintf(buf,"%d%d-%d%d-%d%d",
                    year_dig1,year_dig2,mon_dig1,mon_dig2,day_dig1,day_dig2);
                        break;
                    }

                    case DATE_YYDDMM:
                    {
                        (void) sprintf(buf,"%d%d-%d%d-%d%d",
                    year_dig1,year_dig2,day_dig1,day_dig2,mon_dig1,mon_dig2);
                        break;
                    }

                    case DATE_MMYYDD:
                    {
                        (void) sprintf(buf,"%d%d-%d%d-%d%d",
                    mon_dig1,mon_dig2,year_dig1,year_dig2,day_dig1,day_dig2);
                        break;
                    }

                    case DATE_DDYYMM:
                    {
                        (void) sprintf(buf,"%d%d-%d%d-%d%d",
                    day_dig1,day_dig2,year_dig1,year_dig2,mon_dig1,mon_dig2);
                        break;
                    }
                }

                Debug2(" date buf=%s",buf,0);
            }
            else
            {
                (void) strcpy(buf,"888888");
            }

            (void) strcpy(digitbuf,buf);
            WriteCounterImage (digitbuf, &digit_info,&frame_info);
            /*
            ** we won't be here
            */
            break;
        }

        case SHOW_GIF_FILE:
        {
            if (*digit_info.gif_file == '\0')
            {
                PrintHeader();
                StringImage("No GIF file specified to dispaly");
                exit(1);
            }
            /*
            ** security fix. oct-10-1997
            ** first check if the image file specified with image=
            ** parameter has any path in it
            */
            if (checkfilename(digit_info.gif_file) != 0)
            {
                PrintHeader();
                StringImage("Illegal image filename!");
                exit(1);
            }
            (void) sprintf(digitbuf,"%s/%s/%s",
                DigitDir,digit_info.ddhead,digit_info.gif_file);
            WriteCounterImage(digitbuf,&digit_info,&frame_info);
            /*
            ** we won't be here
            */
            break;
        }
        case SHOW_COUNTER:
        default:
        {

            if (digit_info.comma == True)
                digit_info.leftpad=False;
            if ((int) strlen(digit_info.literal) > 0)
            {
                /*
                ** only copy 10 digits, otherwise we take the risk
                ** of buffer overflow
                */
                if (strlen(digit_info.literal) > 10)
                    (void) strncpy(buf, digit_info.literal,10);
                else
                {
                    (void) strcpy(buf, digit_info.literal);
                }
                if (digit_info.comma == True)
                    (void) sprintf(buf,"%d",atoi(buf));
                digit_info.leftpad=False;
            }
            else if (mystrcasecmp(digit_info.datafile,DfForRandom) == 0)
            {
                srand(time(NULL));  /* Seed number generator */
                counter = rand();   /* My psychic prediction of counter value */

                /*
                ** we do not want to overflow buffer
                */
                Debug2("random number=%d",counter,0);

                *tmpbuf='\0';
                (void) sprintf(tmpbuf,"%d",counter);
                if (strlen(tmpbuf) > 10)
                    (void) strncpy(buf,tmpbuf,10);
                else
                    (void) strcpy(buf,tmpbuf);
              Debug2("random number after bound checning=%s",buf,0);
              digit_info.leftpad=False;
            }
            else
            {

            (void) strcpy(filename, DataDir);
            (void) strcat(filename, "/");
            (void) strcat (filename, digit_info.datafile);

            /*
            ** check if the counter file exists or not
            */
        if (Gauto_file_creation == 0)
        {
            if (CheckFile (filename) != 0)
            {
                *tmpbuf='\0';
                (void) sprintf (tmpbuf,
                    "Counter datafile \"%s\" must be created first!", filename);
                Warning(tmpbuf);
                PrintHeader ();
                StringImage (tmpbuf);
                exit(1);
            }

        }
        else
        {
            if (CheckFile (filename) != 0)
                use_st=True;
        }
#ifdef SYS_WIN32
        fd=sopen(filename,_O_RDWR | _O_CREAT, _SH_DENYWR, _S_IREAD |_S_IWRITE);
#else
        fd = open (filename, O_RDWR | O_CREAT,0644);
#endif
        if (fd < 0)
        {
            *tmpbuf='\0';
            if (Gauto_file_creation == 1)
            {
                if (CheckFile (filename) != 0)
                    (void) sprintf(tmpbuf,
                    "Could not create data file: \"%s\"",filename);
                else
                    (void) sprintf (tmpbuf,
                    "Could not write to counter file: \"%s\"", filename);
            }
            else
                (void) sprintf (tmpbuf,
                "Could not write to counter file: \"%s\"", filename);

            Warning(tmpbuf);
            PrintHeader ();
            StringImage(tmpbuf);
            exit(1);
        }

#ifdef SYS_UNIX
        SetLock(fd);
#endif
        /*
        ** try to read from the file
        */

        lseek(fd,0L,0);
        /*        n = read(fd, buf, MaxDigits);*/
        n = read(fd, buf, 10);

        if (n > 0)
        {


            /*
            ** check if the datafile is edited, 
            ** NULL terminate at first non-digit
            */
            for (i=0; i < n; i++)
            {
                if (!isdigit(buf[i]))
                {
                    buf[i]='\0';
                    break;
                }
            }
            if (i == n)
                buf[n]='\0';

            Debug2("In buffer=%s",buf,0);

            if (*buf == '\0')
                counter=0;
            else
                counter = atoi(buf);

            if (counter < 0)
            {
                /*
                ** possibly we reached the limit, the digit didn't fit
                ** in an int
                */
                PrintHeader();
                StringImage("The digit did not fit in a signed int!");
                exit(1);
            }

            *buf='\0';
            (void) sprintf(buf,"%d",counter);
        
            Debug2(" before increment=%d",counter,0);

            if (counter == 0)
            {
                counter_length = 1;
                counter=1;
                (void) sprintf(buf, "%d", counter);
            }
            else
            {

                if (digit_info.increment_counter == True)
                    counter++;
                (void) sprintf(buf, "%d", counter);
                counter_length = (int) strlen(buf);
            } 

            Debug2(" after increment=%d",counter,0);

            if (ignore_site  == False)
            {
                lseek(fd,0L,0);
                (void) write(fd, buf, (int)strlen(buf));
                (void) close (fd); /*unlocks as well */
            }
        }
        else
        {
            if (use_st == True)
            {
                counter=digit_info.st;
                Debug2("counter=%d",counter,0);
            }
            Debug2(" n < 0",0,0);
            Debug2(" before increment=%d",counter,0);

            if ( digit_info.increment_counter==True )
                 counter++;

            (void) sprintf(buf, "%d", counter);
            Debug2(" after increment=%d",counter,0);

            lseek(fd,0L,0);
            write (fd, buf, (int) strlen(buf));
            (void) close (fd);
        }

#ifdef SYS_UNIX
        UnsetLock(fd);
#endif
    }

    counter_length = (int) strlen(buf);

        if (digit_info.show == False)
        {
            Image
                *image;

            image=CreateBaseImage(1,1,0,0,0,DirectClass);
            if (image == (Image *) NULL)
            {
                PrintHeader();
                StringImage("Failed to create 1x1 GIF image");
                exit(1);
            }

            AlphaImage(image,0,0,0);
            PrintHeader();
            (void) WriteGIFImage (image, (char *)NULL);
            DestroyAnyImageStruct (&image);
            exit(0);
        }

       if (digit_info.leftpad == False)    /* no left padding */
       {
            (void) strcpy(digitbuf,buf);
            WriteCounterImage(digitbuf,&digit_info,&frame_info);
            /*
            ** we will not be here, we'll exit from LoadDigits()
            */
        }
        else
        {
            if (counter_length < MaxDigits)
                left_pad = MaxDigits - counter_length;
            else
                left_pad=0;

            Debug2(" MaxDigits=%d",MaxDigits,0);
            Debug2(" left_pad=%d",left_pad,0);

              if ((left_pad < MaxDigits) && (left_pad != 0))
              {

                (void) strcpy(digitbuf,"0");
                for (i=1; i < left_pad; i++)
                {
                    (void) sprintf(digitbuf,"%s0",digitbuf);
                }
                (void) sprintf(digitbuf,"%s%s",digitbuf,buf);
                WriteCounterImage(digitbuf,&digit_info,&frame_info);
                /*
                ** we will not be here
                */
              }
              else    /* MaxDigits*/
              {

                Debug2(" We are in MaxDigits=%d",MaxDigits,0);

                (void) strcpy(digitbuf,buf);
                    WriteCounterImage(digitbuf,&digit_info,&frame_info);
                /*
                ** we won't be here
                */
               }

            }
            exit(0);
        } /* case SHOW_COUNTER*/
    }

    exit(0);
}