[BACK]Return to init.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sort

Diff for /src/usr.bin/sort/Attic/init.c between version 1.3 and 1.4

version 1.3, 1999/05/24 17:57:18 version 1.4, 2003/04/06 18:39:11
Line 214 
Line 214 
         }          }
 }  }
   
   /*
    * Convert obsolescent "+pos1 [-pos2]" format to POSIX -k form.
    * Note that the conversion is tricky, see the manual for details.
    */
 void  void
 fixit(argc, argv)  fixit(argc, argv)
         int *argc;          int *argc;
         char **argv;          char **argv;
 {  {
         int i, j, v, w, x;          int i, j;
         static char *vbuf, *vpos, *tpos;          long v, w, x;
           char *p, *ep;
           char buf[128], *bufp, *bufend;
           size_t n;
   
         if ((vpos = vbuf = calloc(ND*20, sizeof(char))) == NULL)          bufend = buf + sizeof(buf);
                 errx(2, "cannot allocate memory");  
   
         for (i = 1; i < *argc; i++) {          for (i = 1; i < *argc; i++) {
                 if (argv[i][0] == '+') {                  if (argv[i][0] == '+') {
                         tpos = argv[i]+1;                          bufp = buf;
                         argv[i] = vpos;                          p = argv[i] + 1;
                         vpos += sprintf(vpos, "-k");                          v = strtol(p, &ep, 10);
                         tpos += sscanf(tpos, "%d", &v);                          if (ep == p || v < 0 ||
                         while (isdigit(*tpos))                              (v == LONG_MAX && errno == ERANGE))
                                 tpos++;                                  errx(2, "invalid field number");
                         vpos += sprintf(vpos, "%d", v+1);                          p = ep;
                         if (*tpos == '.') {                          if (*p == '.') {
                                 tpos += sscanf(++tpos, "%d", &x);                                  x = strtol(++p, &ep, 10);
                                 vpos += sprintf(vpos, ".%d", x+1);                                  if (ep == p || x < 0 ||
                                       (x == LONG_MAX && errno == ERANGE))
                                           errx(2, "invalid field number");
                                   p = ep;
                                   n = snprintf(bufp, bufend - bufp, "-k%ld.%ld%s",
                                       v+1, x+1, p);
                           } else {
                                   n = snprintf(bufp, bufend - bufp, "-k%ld%s",
                                       v+1, p);
                         }                          }
                         while (*tpos)                          if (n >= bufend - bufp)
                                 *vpos++ = *tpos++;                                  errx(2, "bad field specification");
                         vpos += sprintf(vpos, ",");                          bufp += n;
   
                         if (argv[i+1] &&                          if (argv[i+1] &&
                             argv[i+1][0] == '-' && isdigit(argv[i+1][1])) {                              argv[i+1][0] == '-' && isdigit(argv[i+1][1])) {
                                 tpos = argv[i+1] + 1;                                  p = argv[i+1] + 1;
                                 tpos += sscanf(tpos, "%d", &w);                                  w = strtol(p, &ep, 10);
                                 while (isdigit(*tpos))                                  if (ep == p || w < 0 ||
                                         tpos++;                                      (w == LONG_MAX && errno == ERANGE))
                                           errx(2, "invalid field number");
                                   p = ep;
                                 x = 0;                                  x = 0;
                                 if (*tpos == '.') {                                  if (*p == '.') {
                                         tpos += sscanf(++tpos, "%d", &x);                                          x = strtol(++p, &ep, 10);
                                         while (isdigit(*tpos))                                          if (ep == p || x < 0 ||
                                                 tpos++;                                              (x == LONG_MAX && errno == ERANGE))
                                                   errx(2, "invalid field number");
                                           p = ep;
                                 }                                  }
                                 if (x) {                                  if (x == 0) {
                                         vpos += sprintf(vpos, "%d", w+1);                                          n = snprintf(bufp, bufend - bufp,
                                         vpos += sprintf(vpos, ".%d", x);                                              ",%ld%s", w, p);
                                 } else                                  } else {
                                         vpos += sprintf(vpos, "%d", w);                                          n = snprintf(bufp, bufend - bufp,
                                 while (*tpos)                                              ",%ld.%ld%s", w+1, x, p);
                                         *vpos++ = *tpos++;                                  }
                                 for (j= i+1; j < *argc; j++)                                  if (n >= bufend - bufp)
                                           errx(2, "bad field specification");
   
                                   /* shift over argv */
                                   for (j = i+1; j < *argc; j++)
                                         argv[j] = argv[j+1];                                          argv[j] = argv[j+1];
                                 *argc -= 1;                                  *argc -= 1;
                         }                          }
                           if ((argv[i] = strdup(buf)) == NULL)
                                   err(2, NULL);
                 }                  }
         }          }
 }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4