+
+/* Utility for cb_host_to_target_stat to store values in the target's
+ stat struct. */
+
+static void
+store (p, size, val, big_p)
+ char *p;
+ int size;
+ long val; /* ??? must be as big as target word size */
+ int big_p;
+{
+ if (big_p)
+ {
+ p += size;
+ while (size-- > 0)
+ {
+ *--p = val;
+ val >>= 8;
+ }
+ }
+ else
+ {
+ while (size-- > 0)
+ {
+ *p++ = val;
+ val >>= 8;
+ }
+ }
+}
+
+/* Translate a host's stat struct into a target's.
+ If HS is NULL, just compute the length of the buffer required,
+ TS is ignored.
+
+ The result is the size of the target's stat struct,
+ or zero if an error occured during the translation. */
+
+int
+cb_host_to_target_stat (cb, hs, ts)
+ host_callback *cb;
+ const struct stat *hs;
+ PTR ts;
+{
+ const char *m = cb->stat_map;
+ char *p;
+ int big_p = 0;
+
+ if (hs == NULL)
+ ts = NULL;
+ p = ts;
+
+ while (m)
+ {
+ char *q = strchr (m, ',');
+ int size;
+
+ /* FIXME: Use sscanf? */
+ if (q == NULL)
+ {
+ /* FIXME: print error message */
+ return 0;
+ }
+ size = atoi (q + 1);
+ if (size == 0)
+ {
+ /* FIXME: print error message */
+ return 0;
+ }
+
+ if (hs != NULL)
+ {
+ if (strncmp (m, "st_dev", q - m) == 0)
+ store (p, size, hs->st_dev, big_p);
+ else if (strncmp (m, "st_ino", q - m) == 0)
+ store (p, size, hs->st_ino, big_p);
+ /* FIXME:wip */
+ else
+ store (p, size, 0, big_p); /* unsupported field, store 0 */
+ }
+
+ p += size;
+ m = strchr (q, ':');
+ if (m)
+ ++m;
+ }
+
+ return p - (char *) ts;
+}