示例#1
0
void initialize_user_observe(const parameters p, const int argc, char **const argv) {
    observe = fopen_or_die(rtout, "w");

    fprintf(observe, "t%23s%23s%24s\n", "R", "T", "RT");

    atexit(cleanup);
}
示例#2
0
文件: rss.c 项目: kybernetyk/Blogtard
void make_rss ()
{
	printf("making rss ...\n");

	FILE *f_out = fopen_or_die ("out/rss.xml", "wt");
	
	append_file_to_fp ("templates/rss/header.template", f_out);

	struct dirent **dir_entries;
	int n = scandir(POSTS_DIR, &dir_entries, 0, alphasort);
	if (n < 0)
	{
		fprintf (stderr, "couldn't open posts dir for reading\n");
		exit (8);
	}

	while (n--)
	{
		if (dir_entries[n]->d_name[0] == '.')
			continue;

		current_dir = POSTS_DIR;
		append_elements_from_dir_to_fp (dir_entries[n]->d_name, f_out);
	}
	free (dir_entries);

	append_file_to_fp ("templates/rss/footer.template", f_out);
	fclose (f_out);
}
示例#3
0
void cleanup() {
    fclose(observe);

    FILE *fp = fopen_or_die(k0out, "a");

    fprintf(fp, "%3.0f %e\n", k0, Tlast);

    fclose(fp);
}
示例#4
0
// ================================================================
static int read_file_mlr_get_line(char* filename) {
	FILE* fp = fopen_or_die(filename);
	int bc = 0;
	while (1) {
		char* line = mlr_get_line(fp, '\n');
		if (line == NULL)
			break;
		bc += strlen(line);
		free(line);
	}
	fclose(fp);
	return bc;
}
示例#5
0
static int read_file_fgetc(char* filename) {
	FILE* fp = fopen_or_die(filename);
	char* irs = "\n";
	int irs_len = strlen(irs);

	int bc = 0;

	while (TRUE) {
		char* line = read_line_fgetc(fp, irs, irs_len);
		if (line == NULL)
			break;
		bc += strlen(line);
	}
	fclose(fp);
	return bc;
}
示例#6
0
文件: getlines.c 项目: indera/miller
// ================================================================
static int read_file_mlr_get_line(char* filename, int do_write) {
	FILE* fp = fopen_or_die(filename);
	int bc = 0;
	while (1) {
		char* line = mlr_get_cline(fp, '\n');
		if (line == NULL)
			break;
		bc += strlen(line);
		if (do_write) {
			fputs(line, stdout);
			fputc('\n', stdout);
		}
		free(line);
	}
	fclose(fp);
	return bc;
}
示例#7
0
文件: getlines.c 项目: indera/miller
// ================================================================
static int read_file_mlr_getcdelim(char* filename, int do_write) {
	FILE* fp = fopen_or_die(filename);
	char irs = '\n';
	int bc = 0;
	while (1) {
		char* line = mlr_get_cline2(fp, irs);
		if (line == NULL)
			break;
		//bc += linelen; // available by API, but make a fair comparison
		bc += strlen(line);
		if (do_write) {
			fputs(line, stdout);
			fputc('\n', stdout);
		}
		free(line);
	}
	fclose(fp);
	return bc;
}
示例#8
0
文件: getlines.c 项目: indera/miller
static int read_file_getc_unlocked_fixed_len(char* filename, int do_write) {
	FILE* fp = fopen_or_die(filename);
	char* irs = "\n";

	int bc = 0;

	while (TRUE) {
		char* line = read_line_getc_unlocked(fp, irs);
		if (line == NULL)
			break;
		if (do_write) {
			fputs(line, stdout);
			fputc('\n', stdout);
		}
		bc += strlen(line);
		free(line);
	}
	fclose(fp);
	return bc;
}
示例#9
0
static int read_file_fgetc_psb(char* filename) {
	FILE* fp = fopen_or_die(filename);
	char* irs = "\n";
	int irs_len = strlen(irs);

	string_builder_t  sb;
	string_builder_t* psb = &sb;
	sb_init(&sb, STRING_BUILDER_INIT_SIZE);

	int bc = 0;

	while (TRUE) {
		char* line = read_line_fgetc_psb(fp, psb, irs, irs_len);
		if (line == NULL)
			break;
		bc += strlen(line);
	}
	fclose(fp);
	return bc;
}
示例#10
0
文件: getlines.c 项目: indera/miller
static int read_file_fgetc_psb(char* filename, int do_write) {
	FILE* fp = fopen_or_die(filename);
	string_builder_t* psb = sb_alloc(STRING_BUILDER_INIT_SIZE);
	char* irs = "\n";
	int bc = 0;

	while (TRUE) {
		char* line = read_line_fgetc_psb(fp, psb, irs);
		if (line == NULL)
			break;
		if (do_write) {
			fputs(line, stdout);
			fputc('\n', stdout);
		}
		bc += strlen(line);
		free(line);
	}
	sb_free(psb);
	fclose(fp);
	return bc;
}
示例#11
0
文件: rss.c 项目: kybernetyk/Blogtard
static void append_element_to_fp (const char *filename, FILE *f_out)
{
	printf("\t\tappending element %s ...\n", filename);

	/* append header */
	append_file_to_fp ("templates/rss/element/header.template", f_out);


	/* append description */
	append_file_to_fp ("templates/rss/element/description_header.template", f_out);


	/* append the post */
	size_t len = strlen(current_dir) + strlen("/") + strlen(filename) + 1;
	char absname[len];
	sprintf(absname,"%s/%s", current_dir, filename);
	append_file_to_fp (absname, f_out);
	//append_str_to_fp (absname, f_out);

	/* desc end */
	append_file_to_fp ("templates/rss/element/description_footer.template", f_out);

	/* append title */
	append_file_to_fp ("templates/rss/element/title_header.template", f_out);

	FILE *el = fopen_or_die (absname, "rt");
	char title[32];
	fread(title, 28, 1, el);
	title[27] = '.';
	title[28] = '.';
	title[29] = '.';
	title[30] = '\0';
	fclose (el);

	append_str_to_fp (title, f_out);

	/* title end */
	append_file_to_fp ("templates/rss/element/title_footer.template", f_out);





	/* append link */
	append_file_to_fp ("templates/rss/element/link_header.template", f_out);
	append_str_to_fp ("http://fettemama.org", f_out);
	append_file_to_fp ("templates/rss/element/link_footer.template", f_out);

	/* append guid */
	append_file_to_fp ("templates/rss/element/guid_header.template", f_out);
	append_str_to_fp ("http://fettemama.org/", f_out);
	append_str_to_fp (absname, f_out);
	append_file_to_fp ("templates/rss/element/guid_footer.template", f_out);

	/* append date */
	append_file_to_fp ("templates/rss/element/date_header.template", f_out);

	struct stat attribs;
	stat (absname, &attribs);

	char s_time[255];
	rfc822_from_tstamp (attribs.st_mtimespec.tv_sec, s_time, 255);

	append_str_to_fp (s_time, f_out);

	append_file_to_fp ("templates/rss/element/date_footer.template", f_out);

	/* append footer */
	append_file_to_fp ("templates/rss/element/footer.template", f_out);
}
示例#12
0
int main(int argc, char* argv[]) {
  Config config;
  int socket;
  int returnStatus = 0;
  struct sockaddr_in tcpServer;

  /* server is running */
  server_running = 1;
  server_pid = getpid();

  /* no requests yet! */
  total_requests = 0;

  /* make sure we've got a config file */
  if(argc < 2) {
    fprintf(stderr, "Usage: %s <config_file>\n", argv[0]);
    exit(1);
  }

  /* load the config file, abort on error */
  if(load_config(&config, argv[1]) < 0) {
    fprintf(stderr, "Error parsing config file %s\n", argv[1]);
    exit(1);
  }

  /* create a UDP socket or die */
  if((socket = e_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    exit(1);
  }

  /* allow re-use of socket addresses to avoid 'already bound' problems */
  int v = 1;
  returnStatus = setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v));
  if(returnStatus < 0) {
    perror("Setting socket option failed");
    exit(1);
  }
  
  /* set up the TCP server */
  tcpServer.sin_family = AF_INET;
  tcpServer.sin_port = htons(config.port);
  tcpServer.sin_addr.s_addr = INADDR_ANY;

  /* bind socket to the server */
  returnStatus = bind(socket, (struct sockaddr*)&tcpServer, sizeof(tcpServer));
  if(returnStatus < 0) {
    perror("Bind Failed");
    if(close(socket) < 0) {
      perror("Error closing socket");
    }
    exit(1);
  }
  
  /* allow a given signal to terminate server */
  struct sigaction act;
  memset(&act, 0, sizeof(act));
  act.sa_handler = sigshutdown_handler;
  sigaction(config.shutdown_signal, &act, 0);

  /* open log file if enabled */
  if(config.logging) {
    logfile = fopen_or_die(config.logfile, "w");
  }
  
  /* run indefinitely */
  int connect_fd;
  if(config.logging) {
    slog(logfile, "Server started\n");
    slog(logfile, "Listening on port %d\n", config.port); 
  }

  /* listen for connections */
  returnStatus = listen(socket, BACK_LOG);
  if(returnStatus < 0) {
    perror("Error listening for connection");
  }

  while(server_running) {
    /* accept an incoming connection from queue */
    if((connect_fd = accept(socket, NULL, NULL)) < 0) {
      /* may have been interrupted, safe to try again */
      if(errno == EINTR || errno == EAGAIN) {
        continue;
      } else {
        perror("Unable to accept connection");
        exit(EXIT_FAILURE);
      }
    } else {
      /* read in an entire HTTP request */
      FILE* record_file = NULL;
      if(config.recording) {
        record_file = fopen_or_die(config.recordfile, "w");
      }
      char* request = readRequest(connect_fd, record_file);
      /* ensure is a valid request i.e is a GET and hostname matches */
      char* file_path = malloc_or_die(BUFFER_LEN);
      if(parseRequest(connect_fd, request, file_path, &config)) {
        ReplyParams r;
        r.connect_fd = connect_fd;
        strncpy(r.file_path, file_path, BUFFER_LEN);
        r.c = &config;
        r.logfile = logfile;
        sendHttpReply(&r);
      }

      /* successfully handled a request, count it */
      total_requests++;

      /* shutdown & close client connection */
      close(connect_fd);
    }
  }

  if(close(socket) < 0) {
    perror("Error closing socket");
  }
  /* cleanup & exit */
  if(config.logging) {
    slog(logfile, "Terminating server\n");
    fclose(logfile);
  }
  return 0;
}