Ejemplo n.º 1
0
main()
{
  char line[MAXLINE], c;
  int len = 0, i, dec = 0, power;
  char *abcdef = "abcdefABCDEF";
  for (len = getaline(line,MAXLINE); len > 1; len = getaline(line,MAXLINE)){
    if (line[1]=='x'||line[1]=='X'){
      for(i=2;i<len;i++) {
	line[i-2]=line[i];
      }
      len -=2;
    }
       ;//ignores 0x / 0X prefix
    for (i=0; i<len-1; i++){
      dec *=16;
      c = line[i];
      if(isdigit(c)){
	dec +=(c-'0');
      }//if
      else if (index(abcdef,c)){
	dec += ((c-'A')%('a'-'A'))+10;//hex value of a given letter
      }//else if
      else {
	printf("%c is not valid in hexadecimal!\n",c);
	break;
      }
    }//for
    printf("%i\n",dec);
    dec = 0;
  }//for
}//main
Ejemplo n.º 2
0
main()
{
  char line[MAXLINE];
  int len = 0, i;
  for (len = getaline(line,MAXLINE); len > 1; len = getaline(line,MAXLINE)){
    (len >= 80) ? printf("%s",line) : printf("%i\n",len);
  }//for len
  return 0;
}
Ejemplo n.º 3
0
void *utf8fan2jian_init(const char *unihan)
{
  char *data,*hzbuf,*hz,*hzfan,*hzjian;
  TCMAP *map;
  int hznum,hzlen;

  data = loadfile(unihan,NULL);
  if(!data) return NULL;

  hznum = getlinenum(data,"\n");
  map = tcmapnew2(hznum);

  hzbuf = data;
  while((hz = getaline(hzbuf,"\n", &hzlen)) != NULL) {
    hzjian = strchr(hz,'=');
    if(!hzjian) return NULL;
    *hzjian++ = 0;
    hzfan = hz;
    trim(hzfan);
    trim(hzjian);
    tcmapput2(map,hzfan,hzjian);
    hzbuf = hz + hzlen + 1;
  }

  xfree(data);
  return map;
}
Ejemplo n.º 4
0
int main()
{
    char line[MAX];
    int numbers[LIMIT], i = 0;
    double answer;

    while(1)
    {
        printf("Enter a NUMBER (or 'quit' to exit): ");
        getaline(line);
        
        if(strcmp(line, "quit") == 0)
            break;
            
        if(check(line) == DIGITS)
        {
            numbers[i++] = atoi(line);
            if(i == LIMIT) {
                 printf("ARRAY FULL!\n");
                 break;
            }
        }        
        else            
            printf("%s is not all digits\n", line);
    }
    
    answer = average(numbers, i);
    printf("%.2f\n", answer);
}
Ejemplo n.º 5
0
char* solveMoreOfTheProblem() {
    try {
        if (! inFile.eof()) {
            string t = getaline(inFile);
            if (t.size() == 0) {
                inFile.clear();
                inFile.close();
                return error[4];
            } else {
                sprintf(lzbfr, "%s", t.c_str());
            }
            return lzbfr;
        } else {
            inFile.clear();
            inFile.close();
            return error[4];
        }
    } catch (string message) {
        inFile.clear();
        inFile.close();
        throw message;
    } catch (...) {
        inFile.clear();
        inFile.close();
        throw string("More went bad!!");
    }
    return error[3];
}
Ejemplo n.º 6
0
static void
omultlinerep(int savecnt, line *startsrcline, int endsrccol)
{
	int tmpcnt = 2;	/* 1st insert counts as 1 repeat */
	line *srcline, *endsrcline;

	endsrcline = dot;
	/* Save linebuf into temp file before moving off the line. */
	vsave();

	/*
	 * Loop from the first line of the first iteration
	 * through endsrcline, appending after dot.
	 */
	while (tmpcnt <= savecnt) {
		for (srcline = startsrcline; srcline <= endsrcline; ++srcline) {
			getaline(*srcline);
			/* copy linebuf to temp file */
			vdoappend(linebuf);
			vcline++;
		}
		++tmpcnt;
	}
	cursor = linebuf + endsrccol;
}
Ejemplo n.º 7
0
/*
 * process an uncompressed newsbatch
 * return 1 if successful, 0 otherwise
 */
static int
fprocessbatch(FILE * f, char *firstline)
{
    char *l;
    long bytes;
    int ret;

    while ((l = (firstline ? firstline : getaline(f)))) {
	static const char tomatch[] = "#! rnews ";
	firstline = NULL;
	ln_log(LNLOG_SDEBUG, LNLOG_CARTICLE,
		"fprocessbatch: read \"%s\"", l);

	if (!str_isprefix(l, tomatch)) {
	    ln_log(LNLOG_SERR, LNLOG_CARTICLE,
		   "expected `#! rnews', got %-.40s%s", l,
		   strlen(l) > 40 ? "[...]" : "");
	    goto pb_bail;
	}
	if (0 == get_long(l + strlen(tomatch), &bytes)) {
	    ln_log(LNLOG_SERR, LNLOG_CARTICLE,
		   "cannot extract article length from `%-.40s%s'\n", l,
		   strlen(l) > 40 ? "[...]" : "");
	    goto pb_bail;
	}
	ret = store_stream(f, 0, filter, bytes, 0);
	ln_log(LNLOG_SDEBUG, LNLOG_CARTICLE,
			"store_stream returns %d", ret);
    }
    return 1;
  pb_bail:
    return 0;
}
Ejemplo n.º 8
0
int32_t
reply(char *question)
{
	char line[80];

	if (preen)
		pfatal(gettext("INTERNAL ERROR: GOT TO reply()"));
	(void) printf("\n%s? ", question);
	if (nflag || fswritefd < 0) {
		(void) printf(gettext(" no\n\n"));
		iscorrupt = 1;		/* known to be corrupt */
		return (0);
	}
	if (yflag) {
		(void) printf(gettext(" yes\n\n"));
		return (1);
	}
	if (getaline(stdin, line, sizeof (line)) == EOF)
		errexit("\n");
	(void) printf("\n");
	if (line[0] == 'y' || line[0] == 'Y')
		return (1);
	else {
		iscorrupt = 1;		/* known to be corrupt */
		return (0);
	}
}
Ejemplo n.º 9
0
static config_file_t *config_file_new_internal(const char *path, unsigned depth)
{
   struct config_file *conf = (struct config_file*)calloc(1, sizeof(*conf));
   if (!conf)
      return NULL;

   if (!path)
      return conf;

   conf->path = strdup(path);
   if (!conf->path)
   {
      free(conf);
      return NULL;
   }

   conf->include_depth = depth;
   FILE *file = fopen(path, "r");

   if (!file)
   {
      free(conf->path);
      free(conf);
      return NULL;
   }

   while (!feof(file))
   {
      struct entry_list *list = (struct entry_list*)calloc(1, sizeof(*list));
      char *line = getaline(file);

      if (line)
      {
         if (parse_line(conf, list, line))
         {
            if (conf->entries)
            {
               conf->tail->next = list;
               conf->tail = list;
            }
            else
            {
               conf->entries = list;
               conf->tail = list;
            }
         }

         free(line);
      }

      if (list != conf->tail)
         free(list);
   }
   fclose(file);

   return conf;
}
Ejemplo n.º 10
0
/*@null@*/ /*@only@*/ char *
mygetfoldedline(const char *fi, unsigned long ln, FILE * f)
{
    /* what characters are considered whitespace that marks the beginning of
       continuation lines.
       WARNING: NEVER EVER list \n here! */
    static const char white[] = " \t";
    char *l1, *l2;
    int c, len, oldlen;

    l1 = getaline(f);
    if (!l1)
	return NULL;
    l2 = (char *)mycritmalloc(fi, ln, (len = strlen(l1)) + 1, "getfoldedline");
    strcpy(l2, l1);

    /* only try to read continuation if the line is not empty 
     * and not a lone dot */
    if (*l2 && strcmp(l2, ".")) {
	for (;;) {
	    c = fgetc(f);
	    if (c != EOF) {
		ungetc(c, f);
		if (strchr(white, c)) {
		    /* join */
		    l1 = getaline(f);
		    if (l1) {
			oldlen = len;
			len += strlen(l1);
			l2 = (char *)mycritrealloc(fi, ln, l2, len + 1,
						   "getfoldedline");
			strcpy(l2 + oldlen, l1);
		    }
		} else {
		    break;
		}
	    } else {
		break;
	    }
	}
    }
    return l2;
}
Ejemplo n.º 11
0
void read_config()
{
  char* rcfilename;
  char* home_var;
  FILE* pfile;
  char key[256], value[256];

  home_var = getenv("HOME");

  rcfilename = malloc(sizeof(char) * strlen(home_var) + 11 /* / .wmkeysrc + NULL*/);
  strcpy(rcfilename, home_var);
  strcat(rcfilename, "/.wmkeysrc");

  pfile = fopen(rcfilename, "r");
  if(pfile == NULL) {
    /* try to open system-wide configuration */
    strcpy(rcfilename, "/etc/wmkeysrc");
    pfile = fopen(rcfilename, "r");

    if(!pfile) {
      fprintf(stderr, "Error: cannot open ${HOME}/.wmkeysrc or /etc/wmkeysrc\n");
      exit(1);
    }
  }

  while(!feof(pfile)) {
    getaline(pfile, key, 256);

    if(!feof(pfile)) {
      getaline(pfile, value, 256);

      configs[num_configs].name = malloc(sizeof(char)*strlen(key)+1);
      strcpy(configs[num_configs].name, key);
      configs[num_configs].filename = malloc(sizeof(char)*strlen(value)+1);
      strcpy(configs[num_configs].filename, value);
      num_configs++;
    }
  }
  if(num_configs == 0) {
    fprintf(stderr, "Error: no configurations, exiting.\n");
    exit(1);
  }
}
Ejemplo n.º 12
0
int
parse_database(FILE *in)
{
        char *line = NULL;
	char *tmp;
	int sec=0, field;
	list_item item;

	item = item_create();

	for(;;) {
		line = getaline(in);
		if(feof(in)) {
			if(item[field_id(NAME)] && sec) {
				add_item2database(item);
			} else {
				item_empty(item);
			}
			break;
		}

		if(!*line || *line == '\n' || *line == '#') {
			goto next;
		} else if(*line == '[') {
			if(item[field_id(NAME)] && sec ) {
				add_item2database(item);
			} else {
				item_empty(item);
			}
			sec = 1;
			memset(item, 0, ITEM_SIZE);
			if(!(tmp = strchr(line, ']')))
				sec = 0; /*incorrect section lines are skipped*/
		} else if((tmp = strchr(line, '=') ) && sec) {
			*tmp++ = '\0';
			find_field_number(line, &field);
			if(field != -1) {
				item[field] = xstrdup(tmp);
				goto next;
			} else if(!strcasecmp(opt_get_str(STR_PRESERVE_FIELDS),
						"all")){
				declare_unknown_field(line);
				item = xrealloc(item, ITEM_SIZE);
				item[fields_count - 1] = xstrdup(tmp);
				goto next;
			}
		}
next:
		xfree(line);
	}

	xfree(line);
	item_free(&item);
	return 0;
}
int main() {
    int len;
    char line[MAXLINE];

    while((len = getaline(line, MAXLINE)) > 0) {
        printf("Length of line: %d\n", len);
        printf("Line: %s\n", line);
    }

    return 0;
}
Ejemplo n.º 14
0
main() {
  char line[MAXLINE];
  int found = 0;

  while (getaline(line, MAXLINE) > 0)
    if (strindex(line, pattern) >= 0) {
      printf("%s", line);
      found++;
    }
  return found;
}
int main() {
    int len;
    char line[MAXLINE];

    while((len = getaline(line, MAXLINE)) > 0) {
        if (len >= 80) {
            printf("Line: %s\n", line);
        }
    }

    return 0;
}
Ejemplo n.º 16
0
int main(void)
{
  int len;
  char line[MAXLINE];
  while((len = getaline(line,MAXLINE)) > 0)
    {
      char* reversed = reverse(line,len);
      printf("%s\n",reversed);
      free(reversed);
    }
  return 0;
}
Ejemplo n.º 17
0
/* Remove trailing blanks and tabs. Omit empty lines. */
main()
{
	int len;				/* current line length */
	char line[MAXLINE];		/* current input line */

	while ((len = getaline(line, MAXLINE)) > 0) {
		line[len-1] = '\0'; // chop off the newline
		reverse(line);
		printf("%s\n", line);
	}
	return 0;
}
Ejemplo n.º 18
0
/************************************************************************
 * solveTheProblem							*
 *	Assumes the equations and variables have already been entered	*
 *	solves the problem, writing the solution in TEMP_FILE, 		*
 *	including the tags and problem statements if the problem was	*
 *	not completely solved.						*
 *   Then it opens the file for reading, and returns the first line	*
 *	[does not close file]						*
 ************************************************************************/
const char* solveTheProblem() {
  int k;

  if (isFirst) throw string("solveTheProblem called before initialization");
  try {
    // reset the buffer to be empty
    resultBuffer.str(string());
    if(resultBuffer.good()) {
      numsols->assign(canonvars->size(),HUGE_VAL);
      if (solveeqs(resultBuffer)) {
	// should we do checking of solution here?
	bool discrep = false;
	for (k = 0; k < canoneqf->size(); k++) {
	  if (checksol((*canoneqf)[k],numsols,RELERR) > 1) {
	    if (!discrep) {
	      resultBuffer << "<DISCREPANCIES>" << endl;
	      discrep = true;
	    }
	    resultBuffer << (*canoneqf)[k]->getInfix() << endl;
	  }
	} // loop over equations to check
	dimchkeqf(resultBuffer);
	// end of "should we do checking of solution here?"
      }
    } 
    else throw string("unable to create solution buffer");
  } 
  catch (string message) { throw message; } 
  catch (...) { throw string("solveTheProblem went boom!!"); }

  // this would be a good place to insert checksol ? or after returning
  // solution, in solveMoreOfTheProblem ?
  try {
    resultBuffer.clear();
    resultBuffer.seekg(0);
    int tk;
    try { tk = resultBuffer.eof(); } 
    catch (...) { throw string("eof?!?"); }
    if (! tk) {
      string t;
      try { t = getaline(resultBuffer); }
      catch (...) { throw string("getaline fails???"); }
      try { sprintf(lzbfr, "%s", t.c_str()); } 
      catch(...) { throw string("copy is wrong??"); }
      return lzbfr;
    } 
    else return error[4];
  } 
  catch (string message) { throw message; } 
  catch (...) { throw string("Read went bad!!"); }
  return error[3];
}
Ejemplo n.º 19
0
main()
{
	char line[MAXLEN];
	int len;
	char box[MAXLEN];
	//	while ((len = getaline(line, MAXLEN)) > 0)
		len = getaline(line, MAXLEN);
//		printf("%s", line);
		filter(line, box);
		printf("%s",box);
		printf("%s",line);					
	return 0;
}
Ejemplo n.º 20
0
Archivo: ex17.c Proyecto: vyshakn/CKnR
int main()
{
	int len;	/* current line length */
	int max;	/* maximum length */
	char line[MAXLINE];	/* current input line */

	max = 80;	/* set filter*/
	while ((len = getaline(line, MAXLINE)) > 0)
		if (len > max) {
			printf("%s", line);
		}
	return 0;
}
Ejemplo n.º 21
0
/*
 * Replace blanks with tabs and blanks as to keep the same spacing.
 */
int
main(void)
{
	int len;
	char line[BUFSIZ];	/* Current input line. */
	char tabbed[BUFSIZ];	/* Cooked string with tabs. */

	while ((len = getaline(line, BUFSIZ)) != -1) {
		entab(tabbed, line);
		printf("%s", tabbed);
	}

	return (0);
}
Ejemplo n.º 22
0
static offset_t
findline(char *expr, offset_t oset)
{
	static int benhere = 0;
	offset_t lncnt = 0, saveloc;

	saveloc = ftello(infile);
	if (curline != (offset_t)1 || benhere)	/* If first line, first time, */
		(void) getaline(FALSE);		/* then don't skip */
	else
		lncnt--;
	benhere = 1;
	while (getaline(FALSE) != NULL) {
		lncnt++;
		if ((sptr = strrchr(linbuf, '\n')) != NULL)
			*sptr = '\0';
		if (step(linbuf, expr)) {
			(void) fseeko(infile, (offset_t)saveloc, SEEK_SET);
			return (curline+lncnt+oset);
		}
	}
	(void) fseeko(infile, (offset_t)saveloc, SEEK_SET);
	return (curline+lncnt+oset+2);
}
Ejemplo n.º 23
0
static void
to_line(offset_t ln)
{
	outfile = getfile();
	if (ln != LAST) {
		if (curline > ln)
			fatal("%s - out of range\n", targ);
		while (curline < ln) {
			if (getaline(TRUE) == NULL)
				fatal("%s - out of range\n", targ);
			flush();
		}
	} else		/* last file */
		if (getaline(TRUE) != NULL) {
			flush();
			for (;;) {
				if (getaline(TRUE) == NULL)
					break;
				flush();
			}
		} else
			fatal("%s - out of range\n", targ);
	closefile();
}
Ejemplo n.º 24
0
int
main(void)
{
    char *l;
    char **act;
    size_t acount = 0;
    size_t i;
    FILE *f;
    int err = 0;
    mastr *path = mastr_new(LN_PATH_MAX);

    (void)mastr_vcat(path, def_spooldir, "/leaf.node/groupinfo.old", NULL);

    f = fopen(mastr_str(path), "r");
    if (f == NULL) {
	fprintf(stderr, "Error: Can't open \"%s\": %s!\n", mastr_str(path), strerror(errno));
	exit(EXIT_FAILURE);
    }

    act = (char **)critmalloc(sizeof(char *),
			      "Allocating space for active file");

    while ((l = getaline(f))) {
	act = (char **)realloc(act, (acount + 1) * sizeof(char *));
	if (act == NULL) {
	    fprintf(stderr, "out of memory: realloc returned NULL\n");
	    exit(EXIT_FAILURE);
	}
	act[acount] = critstrdup(l, "lsort");
	acount++;
    }
    (void)fclose(f);

    qsort(act, acount, sizeof(char *), &lcomp);

    clearerr(stdout);
    for (i = 0; i < acount; i++) {
	int ok;
	ok = printf("%s\n", act[i]);
	if (ok < 0) { err = 1; break; }
    }

    free(act);
    mastr_delete(path);
    fflush(stdout);
    if (ferror(stdout)) err = 1;
    return err ? EXIT_FAILURE : EXIT_SUCCESS;
}
Ejemplo n.º 25
0
config_file_t *config_file_new(const char *path)
{

	struct config_file *conf = calloc(1, sizeof(*conf));
	if (conf == NULL)
		return NULL;

	if (path == NULL)
		return conf;

	FILE *file = fopen(path, "r");
	if (!file)
	{
		free(conf);
		return NULL;
	}

	struct entry_list *tail = conf->entries;

	while (!feof(file))
	{
		struct entry_list *list = calloc(1, sizeof(*list));
		char *line = getaline(file);

		if (line)
		{
			if (parse_line(list, line))
			{
				if (conf->entries == NULL)
				{
					conf->entries = list;
					tail = list;
				}
				else
				{
					tail->next = list;
					tail = list;
				}
			}
			free(line);
		}
	}
	fclose(file);

	return conf;
}
Ejemplo n.º 26
0
Archivo: 1.9.c Proyecto: adamesque/k-r
/* print longest input line */
main()
{
	int len;				/* current line length */
	int max;				/* maximum length seen so far */
	char line[MAXLINE];		/* current input line */
	char longest[MAXLINE];	/* longest line saved here */

	max = 0;
	while ((len = getaline(line, MAXLINE)) > 0)
		if (len > max) {
			max = len;
			copy(longest, line);
		}
	if (max > 0)	/* there was a line */
		printf("%s", longest);
	return 0;
}
Ejemplo n.º 27
0
void
rop2(void)
{
	line *first, *last, *a;

	deletenone();
	clrstats();
	first = addr2 + 1;
	(void)append(getfile, addr2);
	last = dot;
	if (value(vi_MODELINES))
		for (a=first; a<=last; a++) {
			if (a==first+5 && last-first > 10)
				a = last - 4;
			getaline(*a);
				chkmdln(linebuf);
		}
}
Ejemplo n.º 28
0
/*
 * call getaline with timeout
 */
char *
timeout_getaline(FILE * f, unsigned int timeout)
{
    char *l;
    struct sigaction sa;

    if (sigsetjmp(to, 1)) {
	ln_log(LNLOG_SERR, LNLOG_CTOP, "timeout reading.");
	return NULL;
    }
    sa.sa_handler = timer;
    sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
    sigemptyset(&sa.sa_mask);
    (void)sigaction(SIGALRM, &sa, NULL);
    (void)alarm(timeout);
    l = getaline(f);
    (void)alarm(0U);
    return l;
}
Ejemplo n.º 29
0
/*
 * process any file
 * return 0 if failed, 1 otherwise
 */
static int
fprocessfile(FILE * f)
{
    char *l;
    int i;

    const struct compressors {
	const char *command;	/* read from batch */
	const char *compressor[4];/* command to run */
    } compressors[] = {
	{ "cunbatch", { GZIP, "-c", "-d", NULL} },
	{ "gunbatch", { GZIP, "-c", "-d", NULL} },
	{ "zunbatch", { GZIP, "-c", "-d", NULL} },
	{ "bunbatch", { BZIP2, "-c", "-d", NULL} },
    };

    l = getaline(f);
    if (!l || strlen(l) < 2) {
	return 0;
    }
    if (str_isprefix(l, "#! rnews ")) {
	    return fprocessbatch(f, l);
    } else if (str_isprefix(l, "#!")) {
	char *c;

	c = l + 2;
	SKIPLWS(c);
	for (i = 0;
	     i < (long)(sizeof(compressors) / sizeof(struct compressors));
	     i++) {
	    if (str_isprefix(c, compressors[i].command)) {
		FILE *uc;

		uc = decompresspipe(f, compressors[i].compressor);
		if (uc == NULL)
			return 0;
		return fprocessbatch(uc, NULL);
	    }
	}
    }
    return 1;
}
Ejemplo n.º 30
0
int main()
{
  int i,t;
  int spacecount,len;

  while (( len = getaline()) > 0 )
    {
      spacecount = 0;
      for( i=0; i < len; i++)
	{
	  if(line[i] == ' ')
	    spacecount++; /* increment counter for each space */
	  if(line[i] != ' ')
	    spacecount = 0; /* reset counter */
	  if(spacecount == TAB2SPACE) /* Now we have enough spaces
				      ** to replace them with a tab
				      */
	    {
	      /* Because we are removing 4 spaces and
	      ** replacing them with 1 tab we move back 
	      ** three chars and replace the ' ' with a \t
	      */
	      i -= 3; /* same as "i = i - 3" */
	      len -= 3;
	      line[i] = '\t';
	      /* Now move all the char's to the right into the
	      ** places we have removed.
	      */
	      for(t=i+1;t<len;t++)
		line[t]=line[t+3];
	      /* Now set the counter back to zero and move the 
	      ** end of line back 3 spaces
	      */
	      spacecount = 0;
	      line[len] = '\0'; 
	    }
	}
      printf("%s", line);
    }
  return 0;
}