コード例 #1
0
ファイル: calculator.c プロジェクト: rdentato/clibutl
int close(const char *from, const char *to, void *aux)
{
  stack_t *stk = aux;
  int val;
  val = stack_top(stk).val;
  stack_drop(stk);
  calc_op(stk,val);
  printf(")");
  return 0;
}
コード例 #2
0
ファイル: daemon.c プロジェクト: qnaal/fls
static bool daemon_serve(int s, char *cmd) {
  /* Do <cmd> for client connected on <s>. */
  static Node *null=NULL, **stack=&null;
  char buf[FILEPATH_MAX];
  bool keep_running=true;

  if( strcmp(cmd, CMD_PUSH) == 0 ) {
    char *status;
    if( stack_len(stack) >= STACK_MAX ) {
      printf("daemon: push request failed (stack full)\n");
      status = MSG_ERROR;
      strcpy(buf, MSG_ERR_STACK_FULL);
    } else {
      soc_w(s, MSG_SUCCESS);
      if( soc_r(s, buf, FILEPATH_MAX) <= 0 ) {
	printf("daemon: push request failed (read error)\n");
	status = MSG_ERROR;
	strcpy(buf, MSG_ERR_LENGTH);
      } else {
	status = MSG_SUCCESS;
	stack_push(buf, stack);
	printf("daemon: PUSH `%s'\n", buf);
      }
    }
    soc_w(s, status);
    soc_w(s, buf);

  } else if( strcmp(cmd, CMD_POP) == 0 ) {
    char *status;
    if( stack_len(stack) > 0 ) {
      status = MSG_SUCCESS;
      sprintf(buf, "%s", stack_peek(stack));
      stack_drop(stack);
      printf("daemon: POP `%s'\n", buf);
    } else {
      printf("daemon: tried to pop from empty stack\n");
      status = MSG_ERROR;
      sprintf(buf, MSG_ERR_STACK_EMPTY);
    }
    soc_w(s, status);
    soc_w(s, buf);

  } else if( strcmp(cmd, CMD_PEEK) == 0 ) {
    char *status;
    if( stack_len(stack) > 0 ) {
      status = MSG_SUCCESS;
      sprintf(buf, "%s", stack_peek(stack));
    } else {
      status = MSG_ERROR;
      sprintf(buf, MSG_ERR_STACK_EMPTY);
    }
    soc_w(s, status);
    soc_w(s, buf);

  } else if( strcmp(cmd, CMD_PICK) == 0 ) {
    char *picked;
    soc_w(s, MSG_SUCCESS);
    soc_r(s, buf, MSG_MAX);
    picked = stack_nth(atoi(buf), stack);
    if( picked == NULL ) {
      soc_w(s, MSG_ERROR);
      soc_w(s, "stack is not quite that deep");
    } else {
      soc_w(s, MSG_SUCCESS);
      soc_w(s, picked);
    }

  } else if( strcmp(cmd, CMD_SIZE) == 0 ) {
    sprintf(buf, "%d", stack_len(stack));
    soc_w(s, buf);

  } else if( strcmp(cmd, CMD_STOP) == 0 ) {
    printf("daemon: Shutting down...\n");
    soc_w(s, MSG_SUCCESS);
    keep_running = false;

  } else {
    char msg[MSG_MAX + FILEPATH_MAX];
    sprintf(msg, "unknown command `%s'", cmd);
    soc_w(s, msg);
  }

  return keep_running;
}