Beispiel #1
0
int main(int argc, char *argv[]) {
    int N;
    scanf("%d", &N);
    __clear_input_buf();
    int *result = (int *)malloc(sizeof(int) * N);
    char *source = NULL;
    char *patt = NULL;
    size_t n;
    int i;
    for(i=0;i<N;i++) {
        __getline(&patt, &n, stdin);
        __getline(&source, &n, stdin);
        result[i] = kmp_matcher(source, strlen(source), patt, strlen(patt));
    }
    for(i=0;i<N;i++)
        printf("%d\n", result[i]);
    free(source);
    free(patt);
    free(result);
    return 0;
}
Beispiel #2
0
/* find all lines matching pattern */
main()
{
	char line[MAXLINE];
	int found = 0;

	while (__getline(line, MAXLINE) > 0)
		if (strindex(line, pattern) >= 0) {
			printf("%s", line);
			found++;
		}
	return found;
}
Beispiel #3
0
int _RTLENTRY _EXPFUNC gettext(int left, int top, int right, int bottom, void *buffer)
{
        int y, size;

        if (!__validatexy(left, top, right, bottom))
                return 0;

        size = right-left+1;
        for (y = top; y <= bottom; y++)
        {
                __getline(buffer, left, y, size);
                buffer = (void *)((char *)buffer + size*2);
        }
        return 1;
}
Beispiel #4
0
char *
getpass (const char *prompt)
{
  FILE *in, *out;
  struct termios s, t;
  int tty_changed;
  static char *buf;
  static size_t bufsize;
  ssize_t nread;

  /* Try to write to and read from the terminal if we can.
     If we can't open the terminal, use stderr and stdin.  */

  in = fopen ("/dev/tty", "w+ce");
  if (in == NULL)
    {
      in = stdin;
      out = stderr;
    }
  else
    {
      /* We do the locking ourselves.  */
      __fsetlocking (in, FSETLOCKING_BYCALLER);

      out = in;
    }

  /* Make sure the stream we opened is closed even if the thread is
     canceled.  */
  __libc_cleanup_push (call_fclose, in == out ? in : NULL);

  flockfile (out);

  /* Turn echoing off if it is on now.  */

  if (__tcgetattr (fileno (in), &t) == 0)
    {
      /* Save the old one. */
      s = t;
      /* Tricky, tricky. */
      t.c_lflag &= ~(ECHO|ISIG);
      tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
    }
  else
    tty_changed = 0;

  /* Write the prompt.  */
  __fxprintf (out, "%s", prompt);
  __fflush_unlocked (out);

  /* Read the password.  */
  nread = __getline (&buf, &bufsize, in);
  if (buf != NULL)
    {
      if (nread < 0)
	buf[0] = '\0';
      else if (buf[nread - 1] == '\n')
	{
	  /* Remove the newline.  */
	  buf[nread - 1] = '\0';
	  if (tty_changed)
	    /* Write the newline that was not echoed.  */
	    __fxprintf (out, "\n");
	}
    }

  /* Restore the original setting.  */
  if (tty_changed)
    (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);

  funlockfile (out);

  __libc_cleanup_pop (0);

  if (in != stdin)
    /* We opened the terminal; now close it.  */
    fclose (in);

  return buf;
}
Beispiel #5
0
internal_function
nss_parse_file (const char *fname)
{
  FILE *fp;
  name_database *result;
  name_database_entry *last;
  char *line;
  size_t len;

  /* Open the configuration file.  */
  fp = fopen (fname, "r");
  if (fp == NULL)
    return NULL;

  /* No threads use this stream.  */
  __fsetlocking (fp, FSETLOCKING_BYCALLER);

  result = (name_database *) malloc (sizeof (name_database));
  if (result == NULL)
    return NULL;

  result->entry = NULL;
  result->library = NULL;
  last = NULL;
  line = NULL;
  len = 0;
  do
    {
      name_database_entry *this;
      ssize_t n;

      n = __getline (&line, &len, fp);
      if (n < 0)
	break;
      if (line[n - 1] == '\n')
	line[n - 1] = '\0';

      /* Because the file format does not know any form of quoting we
	 can search forward for the next '#' character and if found
	 make it terminating the line.  */
      *__strchrnul (line, '#') = '\0';

      /* If the line is blank it is ignored.  */
      if (line[0] == '\0')
	continue;

      /* Each line completely specifies the actions for a database.  */
      this = nss_getline (line);
      if (this != NULL)
	{
	  if (last != NULL)
	    last->next = this;
	  else
	    result->entry = this;

	  last = this;
	}
    }
  while (!feof_unlocked (fp));

  /* Free the buffer.  */
  free (line);
  /* Close configuration file.  */
  fclose (fp);

  return result;
}
Beispiel #6
0
void iconTitleUpdate (int isdir, const char* name) {
	writeRow (0,name);
	writeRow (1,"");
	writeRow (2,"");
	writeRow (3,"");

	if (isdir) {
		// text
		writeRow (2,"[directory]");
		// icon
		clearIcon();
	} else if(strlen(name) >= 5 && strcasecmp(name + strlen(name) - 5, ".argv") == 0) {
		// look through the argv file for the corresponding nds file
		FILE    *fp;
		char    *line = NULL, *p = NULL;
		size_t  size = 0;
		ssize_t rc;

		// open the argv file
		fp = fopen(name,"rb");
		if(fp == NULL) {
			writeRow(2, "(can't open file!)");
			clearIcon();
			fclose(fp); return;
		}

		// read each line
		while((rc = __getline(&line, &size, fp)) > 0) {
			// remove comments
			if((p = strchr(line, '#')) != NULL)
				*p = 0;

			// skip leading whitespace
			for(p = line; *p && isspace((int)*p); ++p)
			  ;

			if(*p)
				break;
		}

		// done with the file at this point
		fclose(fp);

		if(p && *p) {
			// we found an argument
			struct stat st;

			// truncate everything after first argument
			strtok(p, "\n\r\t ");

			if(strlen(p) < 4 || strcasecmp(p + strlen(p) - 4, ".nds") != 0) {
				// this is not an nds file!
				writeRow(2, "(invalid argv file!)");
				clearIcon();
			} else {
				// let's see if this is a file or directory
				rc = stat(p, &st);
				if(rc != 0) {
					// stat failed
					writeRow(2, "(can't find argument!)");
					clearIcon();
				} else if(S_ISDIR(st.st_mode)) {
					// this is a directory!
					writeRow(2, "(invalid argv file!)");
					clearIcon();
				} else {
					iconTitleUpdate(false, p);
				}
			}
		} else {
			writeRow(2, "(no argument!)");
			clearIcon();
		}
		// clean up the allocated line
		free(line);
	} else {
		// this is an nds file!
		FILE *fp;
		unsigned int Icon_title_offset;
		int ret;

		// open file for reading info
		fp=fopen (name,"rb");
		if (fp==NULL) {
			// text
			writeRow (2,"(can't open file!)");
			// icon
			clearIcon();
			fclose (fp); return;
		}

		ret=fseek (fp, offsetof(tNDSHeader, bannerOffset), SEEK_SET);
		if (ret==0)
			ret=fread (&Icon_title_offset, sizeof(int), 1, fp); // read if seek succeed
		else
			ret=0;  // if seek fails set to !=1

		if (ret!=1) {
			// text
			writeRow (2,"(can't read file!)");
			// icon
			clearIcon();
			fclose (fp); return;
		}

		if (Icon_title_offset==0) {
			// text
			writeRow (2,"(no title/icon)");
			// icon
			clearIcon();
			fclose (fp); return;
		}

		ret=fseek (fp,Icon_title_offset,SEEK_SET);
		if (ret==0)
			ret=fread (&banner, sizeof(banner), 1, fp); // read if seek succeed
		else
			ret=0;  // if seek fails set to !=1

		if (ret!=1) {
			// text
			writeRow (2,"(can't read icon/title!)");
			// icon
			clearIcon();
			fclose (fp); return;
		}

		// close file!
		fclose (fp);

		// turn unicode into ascii (kind of)
		// and convert 0x0A into 0x00
		int i;
		char *p = (char*)banner.titles[0];
		for (i = 0; i < sizeof(banner.titles[0]); i = i+2) {
			if ((p[i] == 0x0A) || (p[i] == 0xFF))
				p[i/2] = 0;
			else
				p[i/2] = p[i];
		}

		// text
		for(i = 0; i < 3; ++i) {
			writeRow (i+1, p);
			p += strlen(p)+1;
		}

		// icon
		DC_FlushAll();
		dmaCopy(banner.icon,    sprite,         sizeof(banner.icon));
		dmaCopy(banner.palette, SPRITE_PALETTE, sizeof(banner.palette));
	}
}
Beispiel #7
0
int
__getdate_r (const char *string, struct tm *tp)
{
  FILE *fp;
  char *line;
  size_t len;
  char *datemsk;
  char *result = NULL;
  time_t timer;
  struct tm tm;
  struct stat64 st;
  int mday_ok = 0;

  datemsk = getenv ("DATEMSK");
  if (datemsk == NULL || *datemsk == '\0')
    return 1;

  if (stat64 (datemsk, &st) < 0)
    return 3;

  if (!S_ISREG (st.st_mode))
    return 4;

  if (__access (datemsk, R_OK) < 0)
    return 2;

  /* Open the template file.  */
  fp = fopen (datemsk, "rce");
  if (fp == NULL)
    return 2;

  /* No threads reading this stream.  */
  __fsetlocking (fp, FSETLOCKING_BYCALLER);

  line = NULL;
  len = 0;
  do
    {
      ssize_t n;

      n = __getline (&line, &len, fp);
      if (n < 0)
	break;
      if (line[n - 1] == '\n')
	line[n - 1] = '\0';

      /* Do the conversion.  */
      tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
      tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN;
      tp->tm_isdst = -1;
      tp->tm_gmtoff = 0;
      tp->tm_zone = NULL;
      result = strptime (string, line, tp);
      if (result && *result == '\0')
	break;
    }
  while (!feof_unlocked (fp));

  /* Free the buffer.  */
  free (line);

  /* Check for errors. */
  if (ferror_unlocked (fp))
    {
      fclose (fp);
      return 5;
    }

  /* Close template file.  */
  fclose (fp);

  if (result == NULL || *result != '\0')
    return 7;

  /* Get current time.  */
  time (&timer);
  __localtime_r (&timer, &tm);

  /* If only the weekday is given, today is assumed if the given day
     is equal to the current day and next week if it is less.  */
  if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN
      && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN)
    {
      tp->tm_year = tm.tm_year;
      tp->tm_mon = tm.tm_mon;
      tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7;
      mday_ok = 1;
    }

  /* If only the month is given, the current month is assumed if the
     given month is equal to the current month and next year if it is
     less and no year is given (the first day of month is assumed if
     no day is given.  */
  if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN)
    {
      if (tp->tm_year == INT_MIN)
	tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0);
      tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday);
      mday_ok = 1;
    }

  /* If no hour, minute and second are given the current hour, minute
     and second are assumed.  */
  if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN)
    {
      tp->tm_hour = tm.tm_hour;
      tp->tm_min = tm.tm_min;
      tp->tm_sec = tm.tm_sec;
    }

  /* Fill in the gaps.  */
  if (tp->tm_hour == INT_MIN)
    tp->tm_hour = 0;
  if (tp->tm_min == INT_MIN)
    tp->tm_min = 0;
  if (tp->tm_sec == INT_MIN)
    tp->tm_sec = 0;

  /* If no date is given, today is assumed if the given hour is
     greater than the current hour and tomorrow is assumed if
     it is less.  */
  if (tp->tm_hour >= 0 && tp->tm_hour <= 23
      && tp->tm_mon == INT_MIN
      && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
    {
      tp->tm_mon = tm.tm_mon;
      tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
      mday_ok = 1;
    }

  /* More fillers.  */
  if (tp->tm_year == INT_MIN)
    tp->tm_year = tm.tm_year;
  if (tp->tm_mon == INT_MIN)
    tp->tm_mon = tm.tm_mon;

  /* Check if the day of month is within range, and if the time can be
     represented in a time_t.  We make use of the fact that the mktime
     call normalizes the struct tm.  */
  if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon,
				tp->tm_mday))
      || mktime (tp) == (time_t) -1)
    return 8;

  return 0;
}