1: #include "../h/rt.h"
   2: 
   3: /*
   4:  * left(s1,n,s2) - pad s1 on right with s2 to length n.
   5:  */
   6: 
   7: Xleft(nargs, arg3, arg2, arg1, arg0)
   8: int nargs;
   9: struct descrip arg3, arg2, arg1, arg0;
  10:    {
  11:    register char *s, *st;
  12:    int cnt, slen;
  13:    char *sbuf, *s3, sbuf1[MAXSTRING], sbuf2[MAXSTRING];
  14:    extern char *alcstr();
  15: 
  16:    /*
  17:     * s1 must be a string.  n must be a non-negative integer and defaults
  18:     *  to 1.  s2 must be a string and defaults to a blank.
  19:     */
  20:    if (cvstr(&arg1, sbuf1) == NULL)
  21:       runerr(103, &arg1);
  22:    defshort(&arg2, 1);
  23:    if ((cnt = INTVAL(arg2)) < 0)
  24:       runerr(205, &arg2);
  25:    defstr(&arg3, sbuf2, &blank);
  26: 
  27:    sneed(cnt);
  28:    if (STRLEN(arg3) == 0) {
  29:       /*
  30:        * The padding string is null, make it a blank.
  31:        */
  32:       slen = 1;
  33:       s3 = " ";
  34:       }
  35:    else {
  36:       slen = STRLEN(arg3);
  37:       s3 = STRLOC(arg3);
  38:       }
  39: 
  40:    /*
  41:     * Get n bytes of string space.  Start at the right end of the new
  42:     *  string and copy s2 into the new string as many times as it fits.
  43:     *  Note that s2 is copied from right to left.
  44:     */
  45:    sbuf = alcstr(NULL, cnt);
  46:    s = sbuf + cnt;
  47:    while (s > sbuf) {
  48:       st = s3 + slen;
  49:       while (st > s3 && s > sbuf)
  50:          *--s = *--st;
  51:       }
  52: 
  53:    /*
  54:     * Copy s1 into the new string, starting at the left end.  If *s1 > n,
  55:     *  only copy n bytes.
  56:     */
  57:    s = sbuf;
  58:    slen = STRLEN(arg1);
  59:    st = STRLOC(arg1);
  60:    if (slen > cnt)
  61:       slen = cnt;
  62:    while (slen-- > 0)
  63:       *s++ = *st++;
  64: 
  65:    /*
  66:     * Return the new string.
  67:     */
  68:    STRLEN(arg0) = cnt;
  69:    STRLOC(arg0) = sbuf;
  70:    }
  71: 
  72: Procblock(left,3)

Defined functions

Xleft defined in line 7; never used
Last modified: 1984-11-18
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 837
Valid CSS Valid XHTML 1.0 Strict