home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / mod.std.unix.v3 / text0032.txt < prev    next >
Encoding:
Internet Message Format  |  1987-06-30  |  7.5 KB

  1. Date: Fri, 22 Nov 85 04:15:24 est
  2. From: seismo!hadron!jsdy (Joseph S. D. Yao)
  3.  
  4. One person suggested that having separate <limits.h> and
  5. /etc/limits files might lead to inconsistency.  True.  But
  6. it was a deliberate (and, i apologise, unspoken) design
  7. decision that reading limits.h implied knowing too much
  8. about the machine -- such as how long are longs, what byte
  9. order are ints vs longs vs ... (not to mention floats,
  10. which I completely ignore below, and which I shouldn't!),
  11. and so forth.  Reading a separate file gives me the freedom
  12. to read an n-ary list of ints, formatted God Alone knows
  13. how, which I can convert at my leisure to whatever I want.
  14.  
  15. Nevertheless, we aim to please, and I made the minor mods
  16. to limits.c to make limits2.c, designed to read a single
  17. int or long int from a define line.  As I said, I completely
  18. ignored floats, which in a working program is unforgivable.
  19. But the person who wants to read in floats may send me
  20. (readable!) code to do so PORTABLY.
  21.  
  22. The following is a compressed context diff.
  23.  
  24. *** limits.c    Sat Nov 16 15:08:15 1985
  25. --- limits2.c    Fri Nov 22 00:00:00 1985
  26. ***************
  27. *** 66,73
  28.   **    void endlimits()
  29. ! **    static int getnum(str)
  30.   **
  31.   ** Declared:
  32. ! **    static char limfile[] = "/etc/limits"
  33. ! **    static char *curlimfile = limfile
  34. ! **    static FILE *limf = (FILE *) NULL
  35.   **
  36. --- 66,75 -----
  37.   **    void endlimits()
  38. ! **    static int getnum(str, buf, n)
  39. ! **    static int getnbits()
  40.   **
  41.   ** Declared:
  42. ! **    static char defn[]    = "#define"
  43. ! **    static char limfile[]    = "/etc/limits"
  44. ! **    static char *curlimfile    = limfile
  45. ! **    static FILE *limf    = (FILE *) NULL
  46.   **
  47. ***************
  48. *** 136,138
  49.   #define NL    '\n'
  50. ! #define COMMENT    '#'
  51.   #define COMMA    ','
  52. --- 138,141 -----
  53.   #define NL    '\n'
  54. ! #define COMM1    '/'
  55. ! #define COMM2    '*'
  56.   #define COMMA    ','
  57. ***************
  58. *** 141,145
  59.   
  60. ! static char limfile[] = "/etc/limits";
  61. ! static char *curlimfile = limfile;
  62.   
  63. ! static FILE *limf = (FILE *) NULL;
  64.   
  65. --- 144,158 -----
  66.   
  67. + #ifdef    EBUG
  68. + #define WDPLG    2
  69. + #  else
  70. + #define WDPLG    (sizeof(long int)/sizeof(int))
  71. + #endif    EBUG
  72. + typedef char byte;    /* Very small signed value */
  73. + static char defn[]    = "#define";
  74. ! static char limfile[]    = "/etc/limits";
  75. ! static char *curlimfile    = limfile;
  76.   
  77. ! static FILE *limf    = (FILE *) NULL;
  78.   
  79. ***************
  80. *** 200,201
  81.       register int n;
  82.       bool been_here;
  83. --- 212,214 -----
  84.       register int n;
  85. +     register int defnlen = strlen(defn);
  86.       bool been_here;
  87. ***************
  88. *** 204,205
  89.       off_t current;
  90.       extern char *index();
  91. --- 213,219 -----
  92.       off_t current;
  93. +     int getnum();
  94.       extern char *index();
  95. ***************
  96. *** 225,240
  97.               if (new_line &&
  98. !                 /* and not a comment line */
  99. !                 *cp != COMMENT &&
  100. !                 /* and not a continuation line */
  101. !                 !isspace(*cp) &&
  102. !                 /* and the limit name is first */
  103. !                 strneq(cp, name, n) &&
  104. !                 /* followed by white space */
  105. !                 isspace(cp[n]))
  106. !                 break;    /* go handle it. */
  107.   
  108.               /* Check whether a NL terminated the read. */
  109. --- 243,259 -----
  110.               if (new_line &&
  111. !                 /* and starts with "#define" */
  112. !                 strneq(cp, defn, defnlen)) {
  113.   
  114. +                 /* Skip over defn */
  115. +                 cp += defnlen;
  116. +                 while (isspace(*cp))
  117. +                     ++cp;
  118. +                 /* If the limit name is first */
  119. +                 if (strneq(cp, name, n) &&
  120. +                     /* followed by white space */
  121. +                     isspace(cp[n]))
  122. +                     break;    /* go handle it. */
  123. +             }
  124.               /* Check whether a NL terminated the read. */
  125. ***************
  126. *** 269,334
  127.       cp += n + 1;
  128. -     n = 0;
  129. -     /* Check whether a NL terminated the read. */
  130. -     new_line = (index(cp, NL) != (char *) NULL);
  131.   
  132.       /*
  133. !     ** Our value may be preceded by whitespace, and is ended
  134. !     ** by a comma or whitespace or comment or EOL.
  135.       */
  136.       for ever {
  137. !         /* Skip any initial white space. */
  138. !         while (isspace(*cp))
  139.               ++cp;
  140. !         /* Check whether line is done. */
  141. !         if (*cp == COMMENT || *cp == NUL) {
  142. !             /* Save current position */
  143. !             current = ftell(limf);
  144. !             /* Get new line. */
  145. !             cp = fgets(inbuf, sizeof(inbuf), limf);
  146. !             /* If EOF, do it over again at loc 0. */
  147. !             if (cp == (char *) NULL) {
  148. !                 (void) fseek(limf, (off_t) 0L, 0);
  149. !                 current = (off_t) 0L;
  150. !                 cp = fgets(inbuf, sizeof(inbuf), limf);
  151. !                 /* There has to be data here. */
  152. !                 if (cp == (char *) NULL) {
  153. !                     /* "never happen" */
  154. !                     break;
  155. !                 }
  156. !             }
  157. !             /*
  158. !             ** If new line is start of line but no white
  159. !             ** space, go back to start of line and return.
  160. !             */
  161. !             if (!new_line || !isspace(*cp)) {
  162. !                 (void) fseek(limf, current, 0);
  163. !                 break;
  164. !             }
  165. !             /* Check whether a NL terminated the read. */
  166. !             new_line = (index(cp, NL) != (char *) NULL);
  167. !             continue;
  168. !         }
  169. !         /* If it can fit, store the present value. */
  170. !         if (n < length)
  171. !             buf[n] = getnum(cp);
  172. !         /* Count another value. */
  173. !         ++n;
  174. !         /*
  175. !         ** We want to break but not skip on space, COMMENT, and
  176. !         ** NUL.  We want to skip and break on COMMA.  We want
  177. !         ** just to skip over everything else.
  178. !         */
  179. !         while (!isspace(*cp) && *cp != COMMENT && *cp != NUL &&
  180. !                *cp++ != COMMA);
  181.       }
  182.   
  183. !     /* Return the number of values found. */
  184. !     return(n);
  185.   }
  186. --- 288,312 -----
  187.       cp += n + 1;
  188.   
  189.       /*
  190. !     ** Our value may be preceded by whitespace, or a comment.
  191.       */
  192.       for ever {
  193. !         if (isspace(*cp))
  194.               ++cp;
  195. !           else if (*cp == COMM1 && cp[1] == COMM2) {
  196. !             cp += 2;
  197. !             while (*cp != NUL) {
  198. !                 if (*cp++ != COMM2)
  199. !                     continue;
  200. !                 if (*cp == COMM1) {
  201. !                     ++cp;
  202. !                     break;
  203. !                 }
  204. !             }
  205. !         } else
  206. !             break;
  207.       }
  208.   
  209. !     /* Return the size (number of ints) of values found. */
  210. !     return(getnum(cp, buf, length));
  211.   }
  212. ***************
  213. *** 351,361
  214.   ** used because it doesn't allow different bases, and for the
  215. ! ** sake of consistency.
  216.   */
  217. ! static int getnum(str)
  218. !   register char *str;
  219.   {
  220.       register int base = 10;
  221. !     register int i = 0, digit;
  222. !     register int sign = 1;
  223.   
  224.       while (isspace(*str) || *str == PLUS || *str == MINUS) {
  225. --- 329,351 -----
  226.   ** used because it doesn't allow different bases, and for the
  227. ! ** sake of consistency.  Postfix 'L' or 'l' indicates long.
  228. ! **
  229. ! ** As long as we're reading in only one (variably sized) value,
  230. ! ** we'll also take on the task of storing it.
  231.   */
  232. ! static int getnum(str, buf, n)
  233. !   register char *str;        /* String containing number */
  234. !   int *buf;            /* Address of int storage array */
  235. !   int n;            /* Length of storage array */
  236.   {
  237.       register int base = 10;
  238. !     register int j, digit;
  239. !     register long int i = 0;
  240. !     byte sign = 1;
  241. !     bool is_long = FALSE; 
  242. !     static int nbits = 0;
  243.   
  244. +     /* If no room to store, don't bother. */
  245. +     if (n <= 0)
  246. +         return(0);
  247.       while (isspace(*str) || *str == PLUS || *str == MINUS) {
  248. ***************
  249. *** 430,432
  250.               break;
  251.   
  252.             default:    break;
  253. --- 420,427 -----
  254.               break;
  255.   
  256. +           case 'L':    /* Not a digit -- "long" indicator. */
  257. +           case 'l':
  258. +             is_long = TRUE;
  259. +             break;
  260.             default:    break;
  261. ***************
  262. *** 442,445
  263.       if (sign < 0)
  264. !         return(-i);
  265. !     return(i);
  266.   }
  267. --- 433,470 -----
  268.       if (sign < 0)
  269. !         i = -i;
  270. !     buf[0] = i;
  271. !     if (!is_long)        /* Value wasn't really long. */
  272. !         return(1);
  273. !     if (nbits == 0)        /* Figure out #bits/int */
  274. !         nbits = getnbits();
  275. !     if (n > WDPLG)        /* Return only long, at most. */
  276. !         n = WDPLG;
  277. !     for (j = 1; j < n; j++) {
  278. !         i >>= nbits;
  279. !         *++buf = i;
  280. !     }
  281. !     return(n);
  282. ! }
  283. ! static int getnbits()
  284. ! {
  285. !     register int i, n;
  286. ! #ifdef    EBUG
  287. !     return(16);
  288. ! #endif    EBUG
  289. !     n = 0;
  290. !     for (i = 1; i != 0; i <<= 1)
  291. !         ++n;
  292. !     return(n);
  293.   }
  294.  
  295.     Joe Yao        hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}
  296.  
  297. Volume-Number: Volume 3, Number 34
  298.  
  299.