File: get_newgroups.C

package info (click to toggle)
peruser 4b33-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,952 kB
  • ctags: 1,064
  • sloc: cpp: 22,367; perl: 2,733; makefile: 345; sh: 335
file content (112 lines) | stat: -rw-r--r-- 2,690 bytes parent folder | download | duplicates (2)
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
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "npserver.h"

int NP_Server::get_newgroups( char *name )
{
   FILE *stamp_file, *list_file;
   char buffer[ 1024 ], timestamp[ 64 ];
   unsigned int count;

   snprintf( buffer, sizeof buffer, "%s/.peruser_spool/%s-LIST", 
             home, name );
   if (( list_file = fopen( buffer, "a" )) == NULL )
   {
      snprintf( error_message, sizeof error_message,
                "NP_Server: get_newgroups(): fopen() returned error: %s.",
                strerror( errno ));
      return 1;
   }

   snprintf( buffer, sizeof buffer, "%s/.peruser_spool/%s-TIMESTAMP", 
             home, name );
   if (( stamp_file = fopen( buffer, "r" )) == NULL )
   {
      fclose( list_file );

      if ( errno == ENOENT )
      {
         snprintf( error_message, sizeof error_message,
                   "NP_Server: get_newgroups(): no timestamp file for %s.",
                   buffer );
         return 3;
      }
      else
      {
         snprintf( error_message, sizeof error_message,
                   "NP_Server: get_newgroups(): fopen() returned error: %s.",
                   strerror( errno ));
         return 1;
      }
   }
   
   if ( fgets( timestamp, 64, stamp_file ) == NULL )
   {
      snprintf( error_message, sizeof error_message,
                "NP_Server: get_newgroups(): empty timestamp file for %s.",
                name );
      fclose( list_file );
      return 1;
   }

   snprintf( buffer, sizeof buffer,
             "newgroups %s GMT\r\n", strtok( timestamp, "\n" ));
   np_fputs( buffer );
   
   if ( np_fgets( buffer, sizeof buffer ))
   {
      fclose( list_file );
      return -1;
   }

   if ( strncmp( buffer, "231", 3 ))
   {
      snprintf( error_message, sizeof error_message,
                "NP_Server: get_newgroups(): server response: %s.",
                buffer );
      fclose( list_file );
      return 1;
   }

   count = 0;

   for( ; ; )
   {
      if ( np_fgets( buffer, sizeof buffer ))
      {
         fclose( list_file );
         return 1;
      }

      if ( !strncmp( buffer, ".\r\n", 3 ))
         break;

      if ( !strncmp( buffer, "junk", 4 ) ||
           !strncmp( buffer, "control", 7 ))
         continue;

      ++count;

      if ( busy_callback != NULL )
         if ( busy_callback( busy_callback_data, -1, count ))
            break;

      fprintf( list_file, "%s", strtok( buffer, " " ));
      strtok( NULL, " " );
      long x = atol( strtok( NULL, " " ));
      fprintf( list_file, ": %ld\n", x );
   }

   fclose( list_file );

   if ( write_timestamp( name ))
      return 1;

   if ( !count )
      return 2;

   return 0;
}