home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 01e / msjall.zip / MSJV2-4.ZIP / BY_TIME.ALL next >
Text File  |  1987-10-30  |  8KB  |  228 lines

  1. Microsoft Systems Journal
  2. Volume 2; Issue 4; September, 1987
  3.  
  4. Code Listings For:
  5.  
  6.     BY_TIME
  7.     pp. 35-45
  8.  
  9. Author(s): Steve Schustack
  10. Title:     Dynamic Allocation Techniques for Memory Management in C Programs
  11.  
  12.  
  13.  
  14.  
  15.  
  16. Figure 14
  17. =========
  18.  
  19.  
  20. /****************************************************************
  21.  * BY_TIME.H:  Header file for BY_TIME program, define linked  
  22.  *   list structure as next node pointer and line of DIR text  
  23.  */
  24. #define DIR_LINE_LEN 39         /* DIR output line length      */
  25. struct s_dir_node               /* Linked list node structure  */
  26.         {
  27.         struct s_dir_node *next;         /* Next node pointer  */
  28.                                          /*   or NULL (0)      */
  29.         char dir_line[DIR_LINE_LEN + 1]; /* DIR output line    */
  30.         };
  31.  
  32.  
  33.  
  34.  
  35.  
  36. Figure 15
  37. =========
  38.  
  39.  
  40. /****************************************************************
  41.  * BY_TIME.C: Filter sorts DIR output in linked list, most recent 
  42.  * first. This is the main function of the program.
  43.  */
  44.  
  45. #include <stdio.h>                      /* For BUFSIZ symbol   */
  46. #include "by_time.h"                    /* Linked list struct  */
  47. main()
  48.         {
  49.         struct s_dir_node head;         /* First node in list  */
  50.         char in_buf[BUFSIZ];            /* Input buffer for    */
  51.                                         /*   DIR output        */
  52.         bgn_list(&head);                /* Initialize list     */
  53.         while (get_dir(in_buf))         /* Input DIR info for  */
  54.                                         /*   next file         */
  55.                 sav_dir(in_buf, &head); /* Save file info in   */
  56.                                         /*  sorted linked list */
  57.         sho_list(&head);                /* Show sorted list    */
  58.         }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. Figure 16
  65. =========
  66.  
  67.  
  68. /****************************************************************
  69.  * BGN_LIST.C: Initialize head of list to dummy highest value  
  70.  */
  71. #include <stdio.h>              /* For NULL (zero) pointer     */
  72. #include "by_time.h"            /* For linked list struct      */
  73.  
  74. bgn_list(p_head)
  75. struct s_dir_node *p_head;      /* Pointer to head of list     */
  76.         {
  77.         /* Date in head is greater than DIR date for any file  */
  78.         strcpy(p_head->dir_line, 
  79.                 "ZZZZZZZZ ZZZ    99999  99-99-99  99:99p");
  80.         p_head->next = NULL;    /* No next node - empty list   */
  81.         }
  82.  
  83.  
  84.  
  85.  
  86. Figure 17
  87. =========
  88.  
  89.  
  90. /****************************************************************
  91.  * GET_DIR.C:  Input DIR info for next fil
  92. e from stdin
  93.  */
  94. #include <stdio.h>              /* For NULL (zero) pointer     */
  95.  
  96. int get_dir(buf)        
  97. char *buf;              /* Buffer for read and pass line back  */
  98.         {
  99.         char *rtn;                      /* save gets() return  */
  100.  
  101.         /* Loop:  Input lines until no more input or got line  */
  102.         /*   starting with an uppercase letter (has file data) */
  103.         while ((rtn = gets(buf)) &&     /* Input a DIR line    */
  104.                 (buf[0] < 'A' || buf[0] > 'Z'))   /* For file? */
  105.                 ;
  106.         return (rtn != NULL);  /* Return 1 if got data, else 0 */
  107.         }        
  108.  
  109.  
  110.  
  111. Figure 18
  112. =========
  113.  
  114.  
  115. /****************************************************************
  116.  * SAV_DIR.C: Allocate new node in list, save DIR info in it
  117.  */
  118. #include <stdio.h>              /* For NULL (zero) pointer     */
  119. #include "by_time.h"            /* For linked list struct      */
  120.  
  121. sav_dir(buf, p_head)
  122. char *buf;                      /* Line of DIR output to save  */
  123. struct s_dir_node *p_head;      /* Pointer to head of list     */
  124.         {
  125.         struct s_dir_node *p_next;      /* Pointer to next     */
  126.                                         /*   node in list      */
  127.         struct s_dir_node *old_p_next;  /* Pointer to previous */
  128.                 /*   next node, parent of current next node    */
  129.         struct s_dir_node *p_new;       /* Pointer to new node */
  130.  
  131.         /* Loop: for each node in list until end of list or    */
  132.         /*   insert point that will keep list sorted is found  */
  133.         for (p_next = p_head->next, old_p_next = p_head;
  134.                 p_next && time_b4(buf, p_next);         
  135.                 old_p_next = p_next, p_next = p_next->next)
  136.                 ;
  137.         /* Dynamically allocate memory for new node - DIR output 
  138.          *   line. Note use of the cast (struct s_dir_node *) 
  139.          *   operator in the assignment to avoid this message: 
  140.          *         warning 47: '=' : different levels of 
  141.  
  142.          *                 indirection
  143.          */
  144.         p_new = (struct s_dir_node *) 
  145.                 malloc(sizeof (struct s_dir_node));
  146.         if (p_new == NULL)      /* malloc() failed, out of RAM */
  147.                 {
  148.                 puts("Out of memory!!!");
  149.                 return;
  150.                 }
  151.         strcpy(p_new->dir_line, buf);   /* Save DIR line in    */
  152.                                         /*  newly alloc'd node */
  153.         p_new->next = old_p_next->next; /* New node points to  */
  154.                                         /*   rest of list      */
  155.         old_p_next->next = p_new;       /* Insert new node in  */
  156.                                         /*   list              */
  157.         }
  158.  
  159.  
  160.  
  161.  
  162.  
  163. Figure 19
  164. =========
  165.  
  166.  
  167.  
  168. /****************************************************************
  169.  * TIME_B4.C: Return 1 if date and time in buf is before date 
  170.  *   and time in node p_next points to, otherwise return 0.
  171.  */
  172. #include "by_time.h"            /* For linked list struct      */
  173. int time_b4(buf, p_next)
  174. char *buf;                      /* Line of DIR output to find  */ 
  175.                                 /*   insert point in list for  */
  176. struct s_dir_node *p_next;      /* Pointer to node in list to  */
  177.         {                       /*   compare time in buf with  */
  178.         int rtn;                /* Return value from strncmp() */
  179.  
  180.         /* compare year, month, day, am/pm, hour, and minute   */
  181.  
  182.         if (rtn = strncmp(&buf[29], &(p_next->dir_line)[29], 2))
  183.                  return (rtn < 0);      /* Years differ        */
  184.         if (rtn = strncmp(&buf[23], &(p_next->dir_line)[23], 2)) 
  185.                  return (rtn < 0);      /* Months differ       */
  186.         if (rtn = strncmp(&buf[26], &(p_next->dir_line)[26], 2)) 
  187.                  return (rtn < 0);      /* Days differ         */
  188.         if (buf[38] != (p_next->dir_line)[38])  /* am/pm's     */ 
  189.                 return (buf[38] == 'a');        /* differ      */
  190.         if (rtn = strncmp(&buf[33], &(p_next->dir_line)[33], 2)) 
  191.                  return (rtn < 0);      /* Hours differ        */
  192.         if (rtn = strncmp(&buf[36], &(p_next->dir_line)[36], 2)) 
  193.                  return (rtn < 0);      /*  Minutes differ     */
  194.         return (0);             /* Dates and times are equal   */
  195.         }
  196.  
  197.  
  198.  
  199.  
  200.  
  201. Figure 20
  202. =========
  203.  
  204.  
  205.  
  206. /****************************************************************
  207.  * SHO_LIST.C: Show sorted linked list - output it to stdout   
  208.  */
  209. #include <stdio.h>              /* For NULL (zero) pointer     */
  210. #include "by_time.h"            /* Linked list struct          */
  211.  
  212. sho_list(p_head)
  213. struct s_dir_node *p_head;      /* Pointer to head of list     */
  214.         {
  215.         struct s_dir_node *p_next;      /* Pointer to next     */
  216.                                         /*   node in list      */
  217.  
  218.         for (p_next = p_head->next;     /* Start at first node */
  219.                 p_next != NULL;         /* Still more list?    */
  220.                 p_next = p_next->next)  /* Move down a node    */
  221.  
  222.                 puts(p_next->dir_line); /* Output a DIR line   */
  223.         }
  224.  
  225.  
  226.  
  227.  
  228.