console.c 1009 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "sysdeps.h"
  2. #include "adb.h"
  3. #include "adb_client.h"
  4. #include <stdio.h>
  5. static int connect_to_console(void)
  6. {
  7. int fd, port;
  8. port = adb_get_emulator_console_port();
  9. if (port < 0) {
  10. if (port == -2)
  11. fprintf(stderr, "error: more than one emulator detected. use -s option\n");
  12. else
  13. fprintf(stderr, "error: no emulator detected\n");
  14. return -1;
  15. }
  16. fd = socket_loopback_client( port, SOCK_STREAM );
  17. if (fd < 0) {
  18. fprintf(stderr, "error: could not connect to TCP port %d\n", port);
  19. return -1;
  20. }
  21. return fd;
  22. }
  23. int adb_send_emulator_command(int argc, char** argv)
  24. {
  25. int fd, nn;
  26. fd = connect_to_console();
  27. if (fd < 0)
  28. return 1;
  29. #define QUIT "quit\n"
  30. for (nn = 1; nn < argc; nn++) {
  31. adb_write( fd, argv[nn], strlen(argv[nn]) );
  32. adb_write( fd, (nn == argc-1) ? "\n" : " ", 1 );
  33. }
  34. adb_write( fd, QUIT, sizeof(QUIT)-1 );
  35. adb_close(fd);
  36. return 0;
  37. }