コード例 #1
0
ファイル: ui_input.c プロジェクト: HieuLsw/iphonefrotz
/* Read a line, processing commands (lines that start with a backslash
 * (that isn't the start of a special character)), and write the
 * first non-command to s.
 * Return true if timed-out.  */
static bool ui_read_line(char *s, char *prompt, bool show_cursor,
                           int timeout, enum input_type type,
                           zchar *continued_line_chars)
{
    unix_set_global_timeout(timeout);
    if (prompt)
	iphone_puts(prompt);
    zgetline(s);
    translate_special_chars(s);
    if (*s == ZC_AUTOSAVE)
	return FALSE;
    if (*s == ZC_TIME_OUT)
	return TRUE;
    return FALSE;
}
コード例 #2
0
static int
fsshare_main(const char *file, const char *mountpoint, const char *shareopts,
    int share)
{
	char tmpfile[PATH_MAX];
	char *line;
	FILE *newfd, *oldfd;
	int fd, error;

	newfd = oldfd = NULL;
	error = 0;

	/*
	 * Create temporary file in the same directory, so we can atomically
	 * rename it.
	 */
	if (strlcpy(tmpfile, file, sizeof(tmpfile)) >= sizeof(tmpfile))
		return (ENAMETOOLONG);
	if (strlcat(tmpfile, ".XXXXXXXX", sizeof(tmpfile)) >= sizeof(tmpfile))
		return (ENAMETOOLONG);
	fd = mkstemp(tmpfile);
	if (fd == -1)
		return (errno);
	/*
	 * File name is random, so we don't really need file lock now, but it
	 * will be needed after rename(2).
	 */
	error = flock(fd, LOCK_EX);
	assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
	newfd = fdopen(fd, "r+");
	assert(newfd != NULL);
	/* Open old exports file. */
	oldfd = fopen(file, "r");
	if (oldfd == NULL) {
		if (share) {
			if (errno != ENOENT) {
				error = errno;
				goto out;
			}
		} else {
			/* If there is no exports file, ignore the error. */
			if (errno == ENOENT)
				errno = 0;
			error = errno;
			goto out;
		}
	} else {
		error = flock(fileno(oldfd), LOCK_EX);
		assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
		error = 0;
	}

	/* Place big, fat warning at the begining of the file. */
	fprintf(newfd, "%s", FILE_HEADER);
	while (oldfd != NULL && (line = zgetline(oldfd, mountpoint)) != NULL)
		fprintf(newfd, "%s\n", line);
	if (oldfd != NULL && ferror(oldfd) != 0) {
		error = ferror(oldfd);
		goto out;
	}
	if (ferror(newfd) != 0) {
		error = ferror(newfd);
		goto out;
	}
	if (share) {
		fprintf(newfd, "%s\t%s\n", mountpoint,
		    translate_opts(shareopts));
	}

out:
	if (error != 0)
		unlink(tmpfile);
	else {
		if (rename(tmpfile, file) == -1) {
			error = errno;
			unlink(tmpfile);
		} else {
			/*
			 * Send SIGHUP to mountd, but unlock exports file later.
			 */
			restart_mountd();
		}
	}
	if (oldfd != NULL) {
		flock(fileno(oldfd), LOCK_UN);
		fclose(oldfd);
	}
	if (newfd != NULL) {
		flock(fileno(newfd), LOCK_UN);
		fclose(newfd);
	}
	return (error);
}
コード例 #3
0
int main()
{
    char temp[MAXLINE];
    char *tokens[MAXNUM];
    int token_num = 0;
    int type, i;
    double op2;

    printf("Reverse Polish Calculator(type 'Quit' to quit)\n");
    while (zgetline(temp, MAXLINE) > 0) {
        if (!strcmp(temp, "Quit \n"))
            break;

        token_num = 0;
        if ((tokens[token_num] = strtok(temp, "\t ")) == NULL) {
            continue;
        }
        else {
            token_num++;
        }
        while ((tokens[token_num] = strtok(NULL, "\t ")) != NULL) {
            token_num++;
        }
        for (i = 0; i < token_num; i++) {
            type = gettokentype(tokens[i]);
            if (type == QUIT) {
                printf("\t%.8g\n", pop());
                clear_stack();
                break;                
            }
            switch (type) {
            case NUMBER:
                push(atof(tokens[i]));
                break;
            case PLUS:
                push(pop()+pop());
                break;
            case MULTIPLE:
                push(pop()*pop());
                break;
            case MINUS:
                op2 = pop();
                push(pop()-op2);
                break;
            case DIVIDE:
                op2 = pop();
                if (op2 != 0.0)
                    push(pop()/op2);
                else
                    printf("error: zero divisor\n");
                break;
            case MOD:
                op2 = pop();
                push((int)pop()%(int)op2);
                break;
            case SIN:
                push(sin(pop()));
                break;
            case COS:
                push(sin(pop()));
                break;
            case EXP:
                push(exp(pop()));
                break;
            case POW:
                op2 = pop();
                push(pow(pop(), op2));
                break;
            default:
                printf("error: unknown command %s\n", tokens[i]);
                break;
            }
        }
        printf("Reverse Polish Calculator(type 'Quit' to quit)\n");
    }

    return 0;
}