int pidof_main(const struct cmd_pidof_info* info) { const char* name = info->name; DIR* procdir = xopendir("/proc"); struct dirent* ent; unsigned long found_pid = 0; bool found = false; while ((ent = readdir(procdir)) != NULL) { SCOPED_RESLIST(rl); char* endptr = NULL; errno = 0; unsigned long pid = strtoul(ent->d_name, &endptr, 10); if (pid == 0 || errno != 0 || *endptr != '\0') continue; int cmdline_fd = try_xopen(xaprintf("/proc/%lu/cmdline", pid), O_RDONLY, 0); if (cmdline_fd == -1) continue; char* name = slurp_fd(cmdline_fd, NULL); if (strcmp(name, info->name) == 0) { if (found) die(EINVAL, "more than one process has name `%s'", name); found = true; found_pid = pid; } } if (!found) die(ENOENT, "no process has name `%s'", name); xprintf(xstdout, "%lu%s", found_pid, info->pidof.zero ? "" : "\n"); xflush(xstdout); return 0; }
void echo_main(void) { int i = 0, out; char *arg, *c; for (;;) { arg = toys.optargs[i]; if (!arg) break; if (i++) putchar(' '); // Should we output arg verbatim? if (!(toys.optflags & FLAG_e)) { xprintf("%s", arg); continue; } // Handle -e for (c = arg;;) { if (!(out = *(c++))) break; // handle \escapes if (out == '\\' && *c) { int slash = *(c++), n = unescape(slash); if (n) out = n; else if (slash=='c') goto done; else if (slash=='0') { out = 0; while (*c>='0' && *c<='7' && n++<3) out = (out*8)+*(c++)-'0'; } else if (slash=='x') { out = 0; while (n++<2) { if (*c>='0' && *c<='9') out = (out*16)+*(c++)-'0'; else { int temp = tolower(*c); if (temp>='a' && temp<='f') { out = (out*16)+temp-'a'+10; c++; } else break; } } // Slash in front of unknown character, print literal. } else c--; } putchar(out); } } // Output "\n" if no -n if (!(toys.optflags&FLAG_n)) putchar('\n'); done: xflush(); }
int getprop_main(const struct cmd_getprop_info* info) { const char* format = info->getprop.format; const char* format_not_found = info->getprop.format_not_found; bool null = info->getprop.null; if (null && format == NULL && format_not_found == NULL) usage_error("must supply --format or " "--format-not-found or both if using -0"); find_symbol_in_libc("__system_property_foreach", &property_foreach); if (property_foreach == NULL) property_foreach = compat_property_foreach; dbg("using %s for property enumeration", property_foreach == compat_property_foreach ? "compat_property_foreach" : "__system_property_foreach"); int exit_status = 0; struct json_writer* writer = NULL; if (format == NULL && format_not_found == NULL) { writer = json_writer_create(xstdout); json_begin_object(writer); } const char** properties = ARGV_CONCAT(info->properties); bool first = true; char sep = null ? '\0' : '\n'; if (*properties == NULL) { struct property_vector* pv = find_all_properties(); char prev_name[PROP_NAME_MAX]; for (size_t i = 0; i < pv->size; ++i) { char name[PROP_NAME_MAX]; char value[PROP_VALUE_MAX]; (void) __system_property_read(pv->props[i], name, value); if (i > 0 && strcmp(name, prev_name) == 0) continue; if (writer != NULL) { json_begin_field(writer, name); json_emit_string(writer, value); } else { output_property(&first, sep, format, name, value); } strcpy(prev_name, name); } } else { size_t nproperties = argv_count(properties); qsort(properties, nproperties, sizeof (properties[0]), property_argv_compare); const char* property; const char* prev_property = NULL; while ((property = *properties++)) { if (prev_property != NULL && !strcmp(prev_property, property)) continue; if (writer != NULL) json_begin_field(writer, property); const prop_info* pi = __system_property_find(property); if (pi) { char value[PROP_VALUE_MAX]; __system_property_read(pi, NULL, value); if (writer != NULL) json_emit_string(writer, value); else if (format != NULL) output_property(&first, sep, format, property, value); } else { if (writer != NULL) json_emit_null(writer); else if (format_not_found != NULL) output_property(&first, sep, format_not_found, property, NULL); exit_status = 4; } prev_property = property; } } if (writer == NULL && !first && !null) xputc(sep, xstdout); if (writer != NULL) json_end_object(writer); xflush(xstdout); return exit_status; }