Permalink
Browse files

Rename xmsprintf() to just xmprintf().

Partly because there's no supplied target string ala sprintf, and partly
because I can never remember what order the m and s go in.
  • Loading branch information...
landley committed Jan 16, 2014
1 parent 2ded833 commit 59d85e2bb065a3bdc27868acb7a65f89d872c7fa
@@ -83,7 +83,7 @@ void *xzalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrndup(char *s, size_t n);
char *xstrdup(char *s);
char *xmsprintf(char *format, ...);
char *xmprintf(char *format, ...);
void xprintf(char *format, ...);
void xputs(char *s);
void xputc(char c);
@@ -129,7 +129,7 @@ int update_password(char *filename, char* username, char* entry)
struct flock lock;

shadow = strstr(filename, "shadow");
filenamesfx = xmsprintf("%s+", filename);
filenamesfx = xmprintf("%s+", filename);
sfx = strchr(filenamesfx, '+');

exfp = fopen(filename, "r+");
@@ -163,7 +163,7 @@ int update_password(char *filename, char* username, char* entry)
}

ret = 0;
namesfx = xmsprintf("%s:",username);
namesfx = xmprintf("%s:",username);
while ((line = get_line(fileno(exfp))) != NULL)
{
if (strncmp(line, namesfx, strlen(namesfx)))
@@ -66,7 +66,7 @@ char *xstrdup(char *s)
}

// Die unless we can allocate enough space to sprintf() into.
char *xmsprintf(char *format, ...)
char *xmprintf(char *format, ...)
{
va_list va, va2;
int len;
@@ -38,7 +38,7 @@ void mktemp_main(void)
if (!TT.tmpdir) TT.tmpdir = "/tmp";
tmp = "tmp.xxxxxx";
}
if (TT.tmpdir) tmp = xmsprintf("%s/%s", TT.tmpdir ? TT.tmpdir : "/tmp",
if (TT.tmpdir) tmp = xmprintf("%s/%s", TT.tmpdir ? TT.tmpdir : "/tmp",
*toys.optargs ? *toys.optargs : "tmp.XXXXXX");

if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1)
@@ -160,7 +160,7 @@ void passwd_main(void)
} else if (toys.optflags & FLAG_l) {
if (pass[0] == '!') error_exit("password is already locked for %s",name);
printf("Locking password for %s\n",name);
encrypted = xmsprintf("!%s",pass);
encrypted = xmprintf("!%s",pass);
} else if (toys.optflags & FLAG_u) {
if (pass[0] != '!') error_exit("password is already unlocked for %s",name);

@@ -33,7 +33,7 @@ static char *snapshot_env(char *name)
{
char *s = getenv(name);

if (s) return xmsprintf("%s=%s", name, s);
if (s) return xmprintf("%s=%s", name, s);

return 0;
}
@@ -57,7 +57,7 @@ void comma_collate(char **old, char *new)
char *comma = ",";

if (atold[strlen(atold)-1] == ',') comma = "";
temp = xmsprintf("%s%s%s", atold, comma, new);
temp = xmprintf("%s%s%s", atold, comma, new);
} else temp = xstrdup(new);
free (atold);
*old = temp;
@@ -23,7 +23,7 @@ void pwdx_main(void)
char *path;
int num_bytes;

path = xmsprintf("/proc/%s/cwd", *optargs);
path = xmprintf("/proc/%s/cwd", *optargs);
num_bytes = readlink(path, toybuf, sizeof(toybuf)-1);
free(path);

@@ -1005,7 +1005,7 @@ static void add_new_partition(void)
return;
}

msg = xmsprintf(" %s\n p primary partition(1-4)\n",
msg = xmprintf(" %s\n p primary partition(1-4)\n",
extended_offset? "l logical (5 or over)" : "e extended");

choice = 0x20 | read_input(msg, NULL);
@@ -198,9 +198,9 @@ static void do_fsck(struct f_sys_info *finfo)
else type = "auto";

args = xzalloc((toys.optc + 2 + 1 + 1) * sizeof(char*)); //+1, for NULL, +1 if -C
args[0] = xmsprintf("fsck.%s", type);
args[0] = xmprintf("fsck.%s", type);

if(toys.optflags & FLAG_C) args[i++] = xmsprintf("%s %d","-C", TT.fd_num);
if(toys.optflags & FLAG_C) args[i++] = xmprintf("%s %d","-C", TT.fd_num);
while(toys.optargs[j]) {
if(*toys.optargs[j]) args[i++] = xstrdup(toys.optargs[j]);
j++;
@@ -59,10 +59,10 @@ static void new_group()
}
}

entry = xmsprintf("%s:%s:%d:", *toys.optargs, "x", TT.gid);
entry = xmprintf("%s:%s:%d:", *toys.optargs, "x", TT.gid);
update_password(GROUP_PATH, *toys.optargs, entry);
free(entry);
entry = xmsprintf("%s:%s::", *toys.optargs, "!");
entry = xmprintf("%s:%s::", *toys.optargs, "!");
update_password(SECURE_GROUP_PATH, *toys.optargs, entry);
free(entry);
}
@@ -82,7 +82,7 @@ void groupadd_main(void)
xgetpwnam(*toys.optargs);
if (!(grp = getgrnam(toys.optargs[1])))
error_exit("group '%s' does not exist", toys.optargs[1]);
if (!grp->gr_mem) entry = xmsprintf("%s", *toys.optargs);
if (!grp->gr_mem) entry = xmprintf("%s", *toys.optargs);
else {
int i;

@@ -155,8 +155,8 @@ static void inittab_parsing(void)
case 1:
if (*extracted_token) {
if (!strncmp(extracted_token, "/dev/", 5))
tty_name = xmsprintf("%s",extracted_token);
else tty_name = xmsprintf("/dev/%s",extracted_token);
tty_name = xmprintf("%s",extracted_token);
else tty_name = xmprintf("/dev/%s",extracted_token);
} else tty_name = xstrdup("");
break;
case 2:
@@ -115,7 +115,7 @@ static char *append_pathandfile(char *path, char *fname)
if (!path) path = "";
c = find_last_char(path, '/');
while (*fname == '/') fname++;
return xmsprintf("%s%s%s", path, (c)? "" : "/", fname);
return xmprintf("%s%s%s", path, (c)? "" : "/", fname);
}
/*
* Concat sub-path and the file name.
@@ -211,10 +211,10 @@ static void show_data(unsigned rport, char *label, unsigned rxq, unsigned txq, c
static char *get_servname(int port, char *label)
{
int lport = htons(port);
if (!lport) return xmsprintf("%s", "*");
if (!lport) return xmprintf("%s", "*");
struct servent *ser = getservbyport(lport, label);
if (ser) return xmsprintf("%s", ser->s_name);
return xmsprintf("%u", (unsigned)ntohs(lport));
if (ser) return xmprintf("%s", ser->s_name);
return xmprintf("%u", (unsigned)ntohs(lport));
}
/*
* used to convert address into text format.
@@ -104,8 +104,8 @@ static void print_header(void)
temp++;
while (def_header[i].name) {
if (!(strcmp(def_header[i].name, ptr))) { // search from default header
if (str) ptr = xmsprintf("%s,%s", temp, str); //handle condition like ppid = M,OM
else ptr = xmsprintf("%s", temp);
if (str) ptr = xmprintf("%s,%s", temp, str); //handle condition like ppid = M,OM
else ptr = xmprintf("%s", temp);
list_add(&o_list, &def_header[i], ptr);
break;
}
@@ -163,7 +163,7 @@ void get_etime(unsigned long s_time)
min = info.uptime - min;
sec = min % 60;
min = min / 60;
temp = xmsprintf("%3lu:%02u", min,sec);
temp = xmprintf("%3lu:%02u", min,sec);
xprintf("%*.*s",7,7,temp);
free(temp);
}
@@ -177,7 +177,7 @@ void get_time(unsigned long s_time, unsigned long u_time)
min = (s_time + u_time)/sysconf(_SC_CLK_TCK);
sec = min % 60;
min = min / 60;
temp = xmsprintf("%3lu:%02u", min,sec);
temp = xmprintf("%3lu:%02u", min,sec);
xprintf("%*.*s",6,6,temp);
free(temp);
}
@@ -237,7 +237,7 @@ static void do_ps_line(int pid, int tid)
name = ptr;
ptr = strrchr(stat_buff, ')');
*ptr = '\0'; //unecessary if?
name = xmsprintf("[%s]", name);
name = xmprintf("[%s]", name);
ptr += 2; // goto STATE
n = sscanf(ptr, "%c %u %u %*u %d %*s %*s %*s %*s %*s %*s "
"%lu %lu %*s %*s %*s %d %*s %*s %lu %lu %ld",
@@ -272,15 +272,15 @@ static void do_ps_line(int pid, int tid)
switch (p->position) {
case 0:
pw = getpwuid(stats.st_uid);
if (!pw) user = xmsprintf("%d",(int)stats.st_uid);
else user = xmsprintf("%s", pw->pw_name);
if (!pw) user = xmprintf("%d",(int)stats.st_uid);
else user = xmprintf("%s", pw->pw_name);
printf("%-*.*s", width, width, user);
free(user);
break;
case 1:
gr = getgrgid(stats.st_gid);
if (!gr) group = xmsprintf("%d",(int)stats.st_gid);
else group = xmsprintf("%s", gr->gr_name);
if (!gr) group = xmprintf("%d",(int)stats.st_gid);
else group = xmprintf("%s", gr->gr_name);
printf("%-*.*s", width, width, group);
free(group);
break;
@@ -322,16 +322,16 @@ static void do_ps_line(int pid, int tid)
case 9:
get_uid_gid(stat_buff, "Gid:", &rgid);
gr = getgrgid(rgid);
if (!gr) rgroup = xmsprintf("%d",(int)stats.st_gid);
else rgroup = xmsprintf("%s", gr->gr_name);
if (!gr) rgroup = xmprintf("%d",(int)stats.st_gid);
else rgroup = xmprintf("%s", gr->gr_name);
printf("%-*.*s", width,width, rgroup);
free(rgroup);
break;
case 10:
get_uid_gid(stat_buff, "Uid:", &ruid);
pw = getpwuid(ruid);
if (!pw) ruser = xmsprintf("%d",(int)stats.st_uid);
else ruser = xmsprintf("%s", pw->pw_name);
if (!pw) ruser = xmprintf("%d",(int)stats.st_uid);
else ruser = xmprintf("%s", pw->pw_name);
printf("%-*.*s", width, width, ruser);
free(ruser);
break;
@@ -340,7 +340,7 @@ static void do_ps_line(int pid, int tid)
break;
case 12:
if (tty_major) {
char *temp = xmsprintf("%d,%d", tty_major,tty_minor);
char *temp = xmprintf("%d,%d", tty_major,tty_minor);
printf("%-*s", width, temp);
free(temp);
} else printf("%-*s", width, "?");
@@ -370,7 +370,7 @@ void do_ps_threads(int pid)
DIR *d;
int tid;
struct dirent *de;
char *tmp = xmsprintf("/proc/%d/task",pid);
char *tmp = xmprintf("/proc/%d/task",pid);

if (!(d = opendir(tmp))) {
free(tmp);
@@ -176,7 +176,7 @@ static int parse_config_file(void)
*/
if (toys.optflags & FLAG_R) {
file = xzalloc(sizeof(struct logfile));
file->filename = xmsprintf("@%s",TT.remote_log);
file->filename = xmprintf("@%s",TT.remote_log);
TT.lfiles = file;
if (!(toys.optflags & FLAG_L)) return 0;
}
@@ -79,8 +79,8 @@ static char *sock_to_address(struct sockaddr *sock, int flags)

if (!(status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), flags))) {
if (flags & NI_NUMERICSERV) return xmsprintf("%s:%s",hbuf, sbuf);
return xmsprintf("%s",hbuf);
if (flags & NI_NUMERICSERV) return xmprintf("%s:%s",hbuf, sbuf);
return xmprintf("%s",hbuf);
}
error_exit("getnameinfo: %s", gai_strerror(status));
}
@@ -90,7 +90,7 @@ static void create_copy_skel(char *skel, char *hdir)

args[0] = "chown";
args[1] = "-R";
args[2] = xmsprintf("%u:%u", TT.uid, TT.gid);
args[2] = xmprintf("%u:%u", TT.uid, TT.gid);
args[3] = hdir;
//Change ownership to that of UID and GID of new user
toys.exitval = exec_wait(args);
@@ -118,7 +118,7 @@ static void new_user()
if (toys.optflags & FLAG_g) pwd.pw_gecos = TT.gecos;
else pwd.pw_gecos = "Linux User,";
if (toys.optflags & FLAG_h) pwd.pw_dir = TT.dir;
else pwd.pw_dir = xmsprintf("/home/%s", *toys.optargs);
else pwd.pw_dir = xmprintf("/home/%s", *toys.optargs);
if (toys.optflags & FLAG_s) pwd.pw_shell = TT.shell;
else pwd.pw_shell = get_shell();

@@ -168,7 +168,7 @@ static void new_user()
//add group, invoke addgroup command
args[0] = "groupadd";
args[1] = toys.optargs[0];
args[2] = xmsprintf("-g%ld", pwd.pw_gid);
args[2] = xmprintf("-g%ld", pwd.pw_gid);
args[3] = NULL;
if (exec_wait(args)) error_msg("addgroup fail");
}
@@ -180,15 +180,15 @@ static void new_user()
*/

// 1. add an entry to /etc/passwd and /etc/shadow file
entry = xmsprintf("%s:%s:%ld:%ld:%s:%s:%s", pwd.pw_name, pwd.pw_passwd,
entry = xmprintf("%s:%s:%ld:%ld:%s:%s:%s", pwd.pw_name, pwd.pw_passwd,
pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos, pwd.pw_dir, pwd.pw_shell);
if (update_password("/etc/passwd", pwd.pw_name, entry)) error_exit("updating passwd file failed");
free(entry);

if (toys.optflags & FLAG_S)
entry = xmsprintf("%s:!!:%u::::::", pwd.pw_name,
entry = xmprintf("%s:!!:%u::::::", pwd.pw_name,
(unsigned)(time(NULL))/(24*60*60)); //passwd is not set initially
else entry = xmsprintf("%s:!!:%u:%ld:%ld:%ld:::", pwd.pw_name,
else entry = xmprintf("%s:!!:%u:%ld:%ld:%ld:::", pwd.pw_name,
(unsigned)(time(NULL))/(24*60*60), 0, 99999, 7); //passwd is not set initially
update_password("/etc/shadow", pwd.pw_name, entry);
free(entry);
@@ -35,10 +35,10 @@ void watch_main(void)
while(toys.optargs[++i])
{
char * oldcmd = cmd;
cmd = xmsprintf("%s %s", oldcmd, toys.optargs[i]);
cmd = xmprintf("%s %s", oldcmd, toys.optargs[i]);
if (CFG_TOYBOX_FREE) free(oldcmd);
}
header = xmsprintf("Every %us: %s", TT.interval, cmd);
header = xmprintf("Every %us: %s", TT.interval, cmd);
hlen = strlen(header);

while(1) {
@@ -176,7 +176,7 @@ int cp_node(struct dirtree *try)

if (*or->name == '/') dotdots = 0;
if (dotdots) {
char *s2 = xmsprintf("% *c%s", 3*dotdots, ' ', s);
char *s2 = xmprintf("% *c%s", 3*dotdots, ' ', s);
free(s);
s = s2;
while(dotdots--) {
@@ -275,7 +275,7 @@ void cp_main(void)
char *src = toys.optargs[i];
int rc = 1;

if (destdir) TT.destname = xmsprintf("%s/%s", destname, basename(src));
if (destdir) TT.destname = xmprintf("%s/%s", destname, basename(src));
else TT.destname = destname;

errno = EXDEV;
@@ -48,7 +48,7 @@ void ln_main(void)
int rc;
char *try = toys.optargs[i];

if (S_ISDIR(buf.st_mode)) new = xmsprintf("%s/%s", dest, basename(try));
if (S_ISDIR(buf.st_mode)) new = xmprintf("%s/%s", dest, basename(try));
else new = dest;
// Silently unlink the existing target (if any)
if (toys.optflags & FLAG_f) unlink(new);
@@ -28,7 +28,7 @@ void nohup_main(void)
S_IRUSR|S_IWUSR ))
{
char *temp = getenv("HOME");
temp = xmsprintf("%s/%s", temp ? temp : "", "nohup.out");
temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out");
xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, S_IRUSR|S_IWUSR);
}
}
@@ -100,7 +100,7 @@ void split_main(void)
if (!TT.bytes && !TT.lines) TT.lines = 1000;

// Allocate template for output filenames
TT.outfile = xmsprintf("%s% *c", (toys.optc == 2) ? toys.optargs[1] : "x",
TT.outfile = xmprintf("%s% *c", (toys.optc == 2) ? toys.optargs[1] : "x",
(int)TT.suflen, ' ');

// We only ever use one input, but this handles '-' or no input for us.

0 comments on commit 59d85e2

Please sign in to comment.