File: pscanf.h

package info (click to toggle)
papi 5.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 28,064 kB
  • ctags: 38,805
  • sloc: ansic: 404,236; makefile: 3,440; fortran: 3,282; xml: 2,460; sh: 821; python: 515; perl: 269; asm: 24
file content (32 lines) | stat: -rw-r--r-- 633 bytes parent folder | download | duplicates (3)
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
/* This file was taken from the tacc_stats utility, which is distributed 
 * under a GPL license.
 */
#ifndef _PSCANF_H_
#define _PSCANF_H_
#include <stdio.h>
#include <stdarg.h>

__attribute__((format(scanf, 2, 3)))
  static inline int pscanf(const char *path, const char *fmt, ...)
{
  int rc = -1;
  FILE *file = NULL;
  char file_buf[4096];
  va_list arg_list;
  va_start(arg_list, fmt);

  file = fopen(path, "r");
  if (file == NULL)
    goto out;
  setvbuf(file, file_buf, _IOFBF, sizeof(file_buf));

  rc = vfscanf(file, fmt, arg_list);

 out:
  if (file != NULL)
    fclose(file);
  va_end(arg_list);
  return rc;
}

#endif