File: file.c

package info (click to toggle)
ganglia-monitor-core 2.5.7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,300 kB
  • ctags: 3,900
  • sloc: ansic: 27,889; sh: 8,492; makefile: 148
file content (133 lines) | stat: -rw-r--r-- 2,967 bytes parent folder | download | duplicates (4)
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
/**
 * @file file.c Some file functions
 */
/* $Id: file.c,v 1.3 2002/08/26 22:45:34 massie Exp $ */
#include "gangliaconf.h"

/**
 * @fn ssize_t readn (int fd, void *vptr, size_t n)
 * Reads "n" bytes from a descriptor
 * @param fd The descriptor
 * @param vptr A void pointer to a data buffer
 * @param n The data buffer size
 * @return ssize_t
 * @retval 0 on success
 * @retval -1 on failure
 */
ssize_t                         
readn (int fd, void *vptr, size_t n)
{
   size_t nleft;
   ssize_t nread;
   char *ptr;
 
   ptr = vptr;
   nleft = n;
   while (nleft > 0)
     {
        nread = read (fd, ptr, nleft);
        if (nread < 0)
          {
             if (errno == EINTR)
                nread = 0;      /* and call read() again */
             else
                return SYNAPSE_FAILURE;
          }
        else if (nread == 0)
           break;               /* EOF */
 
        nleft -= nread;
        ptr   += nread;
     }
   return (n - nleft);          /* return >= 0 */
}                                                 

/**
 * @fn ssize_t writen (int fd, const void *vptr, size_t n)
 * Writes "n" bytes from a descriptor
 * @param fd The descriptor
 * @param vptr A void pointer to a data buffer
 * @param n The data buffer size
 * @return ssize_t
 * @retval 0 on success
 * @retval -1 on failure
 */
ssize_t 
writen (int fd, const void *vptr, size_t n)
{
   size_t nleft;
   ssize_t nwritten;
   const char *ptr;
 
   ptr = vptr;
   nleft = n;
   while (nleft > 0)
     {
        nwritten = write (fd, ptr, nleft);
        if (nwritten <= 0)
          {
             if (errno == EINTR)
                nwritten = 0;   /* and call write() again */
             else
                return SYNAPSE_FAILURE;
          }
        nleft -= nwritten;
        ptr += nwritten;
     }
   return SYNAPSE_SUCCESS;
}                     

/**
 * @fn int slurpfile ( char * filename, char *buffer, int buflen )
 * Reads an entire file into a buffer
 * @param filename The name of the file to read into memory
 * @param buffer A pointer to the data buffer
 * @param buflen The data buffer length
 * @return int
 * @retval 0 on success
 * @retval -1 on failure
 */
int
slurpfile ( char * filename, char *buffer, int buflen )
{
   int  fd, read_len;
 
   fd = open(filename, O_RDONLY);
   if ( fd < 0 )
      {
         err_ret("slurpfile() open() error");      
         return SYNAPSE_FAILURE;
      }
 
  read:
   read_len = read( fd, buffer, buflen );
   if ( read_len <= 0 )
      {
         if ( errno == EINTR )
            goto read;
         err_ret("slurpfile() read() error");
         close(fd);
         return SYNAPSE_FAILURE;
      }
   close(fd);

   buffer[read_len] = '\0';
   return read_len;
}   


char * 
skip_whitespace ( const char *p)
{
    while (isspace(*p)) p++;
    return (char *)p;
}
 
char * 
skip_token ( const char *p)
{
    while (isspace(*p)) p++;
    while (*p && !isspace(*p)) p++;
    return (char *)p;
}