Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
  long characters, words, lines;

  if (argc == 1) {
    wc(stdin, &characters, &words, &lines);
    print_totals(characters, words, lines);
    putchar('\n');
    return 0;
  }

  long c_total = 0, w_total = 0, l_total = 0;
  FILE *fp;

  for (int i = 1; i < argc; i++) {
    if ((fp = fopen(argv[i], "rb")) == NULL)
      print_e(errno, argv[0], argv[i]);

    wc(fp, &characters, &words, &lines);

    if (fclose(fp) == EOF)
      print_e(errno, argv[0], argv[i]);

    print_totals(characters, words, lines);
    printf("\t%s\n", argv[i]);
    if (argc > 2)
      c_total += characters; w_total += words; l_total += lines;
  }
  if (argc > 2) {
    print_totals(c_total, w_total, l_total);
    printf("\ttotal\n");
  }

  return 0;
}
Ejemplo n.º 2
0
static void wc(FILE *stream, long *characters, long *words, long *lines)
{
  int ch;
  bool in_token = false;
  *characters = 0, *words = 0, *lines = 0;
  while ((ch = fgetc(stream)) != EOF) {
    (*characters)++;
    if (ch == '\n')
      (*lines)++;

    if (isspace(ch)) {
      if (in_token)
        (*words)++;  /* first char after the token */
      in_token = false;
    } else
      in_token = true;

  }
  if (ferror(stream)) {
    print_e(errno, __FILE__, "wc");
    exit(EXIT_FAILURE);
  }
  if (in_token)
    (*words)++;  /* last char of last token followed by EOF */
}
Ejemplo n.º 3
0
int print_g(GraphType* g) {
    if(g==NULL)
        return 0;
    GList* vlist = get_g_allv(g);
    GList* elist = get_g_alle(g);
    int l,n;
    printf("\nGraph------------------------------------------------------------\n");
    l = g_list_length(vlist);
    printf("Vertices: \n");
    for(n=0; n<l; n++) {
        VertexType* v = g_list_nth_data(vlist, n);
        print_v(v);
        printf(" | ");
    }
    printf("\n");

    l = g_list_length(elist);
    printf("Edges: \n");
    for(n=0; n<l; n++) {
        EdgeType* e = g_list_nth_data(elist, n);
        print_e(e);
        printf(" | ");
    }
    printf("\n");
    printf("-----------------------------------------------------------------\n");
    g_list_free(vlist);
    g_list_free(elist);
    return 0;
}
Ejemplo n.º 4
0
Archivo: leaf.c Proyecto: shaoner/leaf
static leaf_error_t leaf_init()
{
	int first_screen = 0;
	xcb_screen_iterator_t iter;
	struct sigaction action;
	uint32_t values;
	xcb_void_cookie_t cookie;
	xcb_generic_error_t *status = NULL;

	gconf.running = false;
	action.sa_handler = stop_signal_handler;
	action.sa_flags = 0;
	sigemptyset(&action.sa_mask);

	/* Catch signals */
	/*
	 * TODO: Catch sighup
	 */
	sigaction(SIGINT, &action, NULL);
	sigaction(SIGTERM, &action, NULL);
	/* Initialize connection to the X server */
	gconf.conn = xcb_connect(NULL, &first_screen);
	if (xcb_connection_has_error(gconf.conn)) {
		print_e("Cannot initialize connection to the X server");
		return ERR_CONN;
	}
	print_d("First screen = %i", first_screen);
	/* Get back X data */
	gconf.xsetup = xcb_get_setup(gconf.conn);
	/* Find the first screen */
	iter = xcb_setup_roots_iterator(gconf.xsetup);
	while (first_screen--)
		xcb_screen_next(&iter);
	gconf.screen = iter.data;

	/* Configure the root screen to get window's events */
	values = \
		XCB_EVENT_MASK_STRUCTURE_NOTIFY |
		XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
	cookie = xcb_change_window_attributes_checked(gconf.conn,
	                                              gconf.screen->root,
	                                              XCB_CW_EVENT_MASK,
	                                              &values);
	status = xcb_request_check(gconf.conn, cookie);
	xcb_flush(gconf.conn);

    if (status)
    {
	    print_d("X is busy, another window manager may be running (%d)",
	            status->error_code);
	    free(status);
	    return ERR_CONN;
    }

	return ERR_NONE;
}
Ejemplo n.º 5
0
Archivo: leaf.c Proyecto: shaoner/leaf
static int leaf_exit(int code)
{
	print_d("");
	gconf.running = false;
	/* Close connection to X and release memory */
	if (gconf.conn)
		xcb_disconnect(gconf.conn);
	if (code)
		print_e("exit with error %i", code);
	return code;
}
Ejemplo n.º 6
0
int destroy_edge(EdgeType* e) {
    if(e==NULL) return 0;
    // GC
    int nref;
    if ( (nref=gcDef( (void *) e, EDGE_T )) > 0 ) return nref;
#ifdef _DEBUG
    fprintf(stdout, "DEBUG: DESTROY EDGE: ");
    print_e(e);
    fprintf(stdout, "\n");
    fprintf(stdout, "------ REMOVE ATTR\n");
#endif
    if(g_hash_table_size(e->attributes) > 0)
        g_hash_table_foreach(e->attributes, &destroy_attr_from_table, NULL);
    g_hash_table_destroy(e->attributes);
#ifdef _DEBUG
    fprintf(stdout, "------ REMOVE me FROM v1 and v2\n");
#endif
    /*
        VertexType* v1 = e->start;
        VertexType* v2 = e->end;
        if(v1 != NULL) v1->outEdges = g_list_remove(v1->outEdges, e);
        if(v2 != NULL) v2->inEdges = g_list_remove(v2->inEdges, e);
        e->start = NULL;
        e->end = NULL;
    */ //bug !!!
#ifdef _DEBUG
    fprintf(stdout, "------ REMOVE me FROM all Gs\n");
#endif
    /*    int l = g_list_length(e->ings);
        int n = 0;
        for(n; n<l; n++){
            GraphType* g = g_list_nth_data(e->ings, n);
            g_remove_edge(g, e);
        }*/
#ifdef _DEBUG
    fprintf(stdout, "------ REMOVE INGs\n");
#endif
    g_list_free(e->ings);
    free(e);
    return 0;
}
Ejemplo n.º 7
0
int	print_es(const char *format, int *i, va_list list)
{
	int	nb;
	int	ret;

	ret = 0;
	(*i)++;
	if (format[(*i)] == '0')
	{
		nb = ft_atoi(nbr(format, i));
		ret += print_e(format, i, list, nb);
	}
	else if (format[(*i)] == '+')
		ret += print_ps(format, i, list);
	else
	{
		nb = ft_atoi(nbr(format, i));
		ret += print(format, i, list, nb);
	}
	return(ret);
}
Ejemplo n.º 8
0
int print_list(ListType* list){	
	if(list==NULL)
		die(-1, "list is NULL if function: print_list()\n");
    int i;
	int type = list->type;
	int length = g_list_length(list->list);
    printf("<List>: ");
	for(i=0; i<length; i++){
		switch(type){
			case VERTEX_T:
				print_v((VertexType*)g_list_nth_data(list->list, i));
				break;
			case EDGE_T:
				print_e((EdgeType*)g_list_nth_data(list->list, i));
				break;
            default: die(-1, "print_list: list print wrong type\n");
				break;
		}
        printf(" ");
	}
	printf("\n");
	return 0;
}
Ejemplo n.º 9
0
print_e(expr e, int lev)
{
	struct expression *ep = e;
	register int i, slev=lev;

	if(!ep) { printf("() "); return; }
	switch(ep->func) {

	/* Literals */
	case 257:	printf("$%d ", ep->values[0].num); return;
	case OP_NUMBER:	printf("%d ", ep->values[0].num); return;
	case OP_STRING:	printf("%s ", ep->values[0].data); return;
	case OP_NULL:	printf("NULL "); return;

	/* Names */
	case OP_COLNAME:
			printf("COLUMN:%s ", ep->values[0].name); return;
	case OP_TABLENAME:
			printf("TABLE:%s ", ep->values[0].name); return;
	case OP_FNAME:
			printf("FUNC:%s ", ep->values[0].name); return;

	/* Relational operators */
	case OP_PROJECTROW:
			printf("(PROJECT1 \n"); break;
	case OP_PROJECTION:
			printf("(PROJECT \n"); break;
	case OP_SELECTROW:
			printf("(SELECT1 \n"); break;
	case OP_SELECTION:
			printf("(SELECT \n"); break;
	case OP_PRODUCT:
			printf("(PRODUCT \n"); break;
	case OP_SORT:
			printf("(SORT \n"); break;
	case OP_GROUP:
			printf("(GROUP \n"); break;
	case OP_DELTA:
			printf("(DELTA \n"); break;
	case OP_CREATETABLE:
			printf("(CREATETABLE \n"); break;
	case OP_INSERTROW:
			printf("(INSERTROW \n"); break;
	case OP_GETNEXT:
			printf("(GETNEXT \n"); break;
	case OP_ITERATE:
			printf("(ITERATE \n"); break;
	
	case OP_PLUS:	printf("(+ \n"); break;
	case OP_BMINUS:	printf("(- \n"); break;
	case OP_TIMES:	printf("(* \n"); break;
	case OP_DIVIDE:	printf("(/ \n"); break;

	case OP_AND:	printf("(AND \n"); break;
	case OP_OR:	printf("(OR \n"); break;
	case OP_NOT:	printf("(! \n"); break;
	case OP_GT:	printf("(> \n"); break;
	case OP_LT:	printf("(< \n"); break;
	case OP_EQUAL:	printf("(== \n"); break;
	case OP_NOTEQ:	printf("(<> \n"); break;
	case OP_GEQ:	printf("(>= \n"); break;
	case OP_LEQ:	printf("(<= \n"); break;

	case OP_SORTSPEC:
			printf("(SORTSPEC \n"); break;

	case OP_COLUMNDEF:
			printf("(COLSPEC \n"); break;

	case OP_OUTCOLNAME:
			printf("(AS \n"); break;

	case OP_RLIST:	printf("(RLIST ");
			print_e(ep->values[0].ep, lev+7);
			putchar('\n');
			for(i=0;i<lev;i++) putchar(' ');
			print_e(ep->values[1].ep, lev);
			putchar(')');
			return;
	default:	printf("(%s \n", op_name(ep->func)); break;
	}
	lev += 2;
	for(i=0; i<lev; i++) putchar(' ');
	for(i=0; i<ep->count; i++) {
		print_e(ep->values[i].ep, lev+2); putchar(' ');
	}
	putchar('\n');
	for(i=0; i<slev; i++) putchar(' ');
	putchar(')');
}