예제 #1
0
파일: gsr.c 프로젝트: wesgarland/gpsee
static FILE *openScriptFile(JSContext *cx, const char *scriptFilename, int skipSheBang)
{
  FILE 	*file = fopen(scriptFilename, "r");
  char	line[64]; /* #! args can be longer than 64 on some unices, but no matter */

  if (!file)
    return NULL;

  gpsee_flock(fileno(file), GPSEE_LOCK_SH);

  if (!skipSheBang)
    return file;

  if (fgets(line, sizeof(line), file))
  {
    if ((line[0] != '#') || (line[1] != '!'))
    {
      gpsee_log(cx, GLOG_NOTICE, PRODUCT_SHORTNAME ": Warning: First line of "
		"file-interpreter script does not contain #!");
      rewind(file);
    }
    else
    {
      do  /* consume entire first line, regardless of length */
      {
        if (strchr(line, '\n'))
          break;
      } while(fgets(line, sizeof(line), file));

      ungetc('\n', file);       /* Make spidermonkey think the script starts with a blank line, to keep line numbers in sync */
    }
  }

  return file;
}
예제 #2
0
파일: rpmjs.c 프로젝트: avokhmin/RPM5
static FILE * rpmjsOpenFile(rpmjs js, const char * fn, const char ** msgp)
	/*@modifies js @*/
{
    FILE * fp = NULL;

    fp = fopen(fn, "r");
    if (fp == NULL || ferror(fp)) {
	if (fp) {
	    if (msgp)
		*msgp = xstrdup(strerror(errno));
	    (void) fclose(fp);
	    fp = NULL;
	} else {
	    if (msgp)	/* XXX FIXME: add __FUNCTION__ identifier? */
		*msgp = xstrdup("unknown error");
	}
	goto exit;
    }

    gpsee_flock(fileno(fp), GPSEE_LOCK_SH);

    if (F_ISSET(js->flags, SKIPSHEBANG)) {
	char buf[BUFSIZ];
	
	if (fgets(buf, sizeof(buf), fp)) {
	    if (!(buf[0] == '#' && buf[1] == '!')) {
		/* XXX FIXME: return through *msgp */
		rpmlog(RPMLOG_WARNING, "%s: %s: no \'#!\' on 1st line\n",
			__FUNCTION__, fn);
		rewind(fp);
	    } else {
#ifdef	NOTYET	/* XXX FIXME */
gpsee_interpreter_t * I = js->I;
		I->linenoOffset += 1;
#endif	/* NOTYET */
		do {	/* consume entire first line, regardless of length */
		    if (strchr(buf, '\n'))
			break;
		} while (fgets(buf, sizeof(buf), fp));
		/*
		 * Make spidermonkey think the script starts with a blank line,
		 * to keep line numbers in sync.
		 */
		ungetc('\n', fp);
	    }
	}
    }

exit:

if (_rpmjs_debug)
fprintf(stderr, "<== %s(%p,%s,%p) fp %p\n", __FUNCTION__, js, fn, msgp, fp);

    return fp;
}