示例#1
0
文件: hake.c 项目: bgold09/psu_cmpsc
static int read_file(char *filename, int quiet)
{
  verify(filename != NULL, "null arg filename");
  verify(filename[0] != '\0', "empty arg filename");

  if (verbose > 0)
    { fprintf(stderr, "%s: read_file(%s)\n", prog, filename); }

  static struct list_names filenames;	// file names come from -f and include
  static int init = 0;
  if (init == 0)
    { list_names_init(&filenames, "filenames"); init++; }

  if (verbose > 1)
    { list_names_print(&filenames); }

  // if (filename is on the list already) { return 1 }
  // else { put filename on the list and continue }
  if (list_names_append_if_new(&filenames, filename) == 1)
    { return 1; }

  if (verbose > 0)
    { list_names_print(&filenames); }

  if (strcmp(filename, "-") == 0)
    { read_lines("[stdin]", stdin); return 1; }

  FILE *fp = fopen(filename, "r");
  if (fp == NULL)
    {
      if (quiet == 0)
	fprintf(stderr, "%s: could not open input file %s: %s\n", prog, filename, strerror(errno));
      return 0;
    }

  read_lines(filename, fp);

  if (fclose(fp) != 0)
    {
      fprintf(stderr, "%s: could not close input file %s: %s\n", prog, filename, strerror(errno));
    }

  return 1;
}
示例#2
0
文件: pr8.c 项目: njb5174/Hake
int pr8_read(char *file)
{
  if (file == NULL) return 0;

  printf("file: %s\n", file);
  
  static struct list_names * filenames = NULL;
  if (filenames == NULL) filenames = list_names_allocate("filenames");
    // by construction, filenames is now not NULL
    
    // if (file is on the list already) { return 1 }
  // else { put filename on the list and continue }
  if (list_names_append_if_new(filenames, file) == 1)
    { return 1; }
    
    if (strcmp(file, "-") == 0)
    { read_lines("[stdin]", stdin); return 1; }
    
    FILE *fp = fopen(file, "r");
    
    if (fp == NULL)
    {
//		fprintf(stderr, "%s: could not open input file %s: %s\n", prog, file, strerror(errno));
      return 0;
    }
    
    read_lines(file, fp);
    
    if (fclose(fp) != 0)
    {
      fprintf(stderr, "%s: could not close input file %s: %s\n", prog, file, strerror(errno));
    }

  return 1;

}