Changeset f34dd65


Ignore:
Timestamp:
Feb 11, 2009, 12:20:22 PM (11 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
7980fb2
Parents:
823671c
git-author:
Nelson Elhage <nelhage@mit.edu> (02/08/09 16:31:14)
git-committer:
Nelson Elhage <nelhage@mit.edu> (02/11/09 12:20:22)
Message:
Kill a whole bunch of unused code.

I generated a list of dead functions by building with
-ffunction-sections and linking with -Wl,--gc-sections
-Wl,--print-gc-sections

I kept a number of functions that seemed to be logical parts of an
existing API, as well as stuff in varstubs.c, since that file is
autogenerated.
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • commands.c

    r8e5935d rf34dd65  
    17261726  return(NULL);
    17271727}
    1728 
    1729 
    1730 void owl_command_zlog_out(void)
    1731 {
    1732   owl_zephyr_zlog_out();
    1733 }
    1734 
    17351728
    17361729char *owl_command_subscribe(int argc, char **argv, char *buff)
  • filter.c

    r9711a6e rf34dd65  
    1818{
    1919  f->name=owl_strdup(name);
    20   f->polarity=0;
    2120  f->fgcolor=OWL_COLOR_DEFAULT;
    2221  f->bgcolor=OWL_COLOR_DEFAULT;
     
    157156}
    158157
    159 void owl_filter_set_polarity_match(owl_filter *f)
    160 {
    161   f->polarity=0;
    162 }
    163 
    164 void owl_filter_set_polarity_unmatch(owl_filter *f)
    165 {
    166   f->polarity=1;
    167 }
    168 
    169158void owl_filter_set_fgcolor(owl_filter *f, int color)
    170159{
     
    205194  if(!f->root) return 0;
    206195  ret = owl_filterelement_match(f->root, m);
    207   if(f->polarity) ret = !ret;
    208196  return ret;
    209197}
  • functions.c

    r99b50a5 rf34dd65  
    1414
    1515static const char fileIdent[] = "$Id$";
    16 
    17 void owl_function_noop(void)
    18 {
    19   return;
    20 }
    2116
    2217char *owl_function_command(char *cmdbuff)
     
    717712}
    718713
    719 void owl_function_nextmsg_personal()
    720 {
    721   owl_function_nextmsg_full("personal", 0, 0);
    722 }
    723 
    724 void owl_function_prevmsg_personal()
    725 {
    726   owl_function_prevmsg_full("personal", 0, 0);
    727 }
    728 
    729 
    730714/* if move_after is 1, moves after the delete */
    731715void owl_function_deletecur(int move_after)
     
    12761260
    12771261  va_end(ap);
    1278 }
    1279 
    1280 void owl_function_refresh()
    1281 {
    1282   owl_function_resize();
    12831262}
    12841263
     
    16471626  owl_global_set_typwin_lines(&g, newsize);
    16481627  owl_function_resize();
    1649 }
    1650 
    1651 void owl_function_typwin_grow()
    1652 {
    1653   int i;
    1654 
    1655   i=owl_global_get_typwin_lines(&g);
    1656   owl_function_resize_typwin(i+1);
    1657 }
    1658 
    1659 void owl_function_typwin_shrink()
    1660 {
    1661   int i;
    1662 
    1663   i=owl_global_get_typwin_lines(&g);
    1664   if (i>2) {
    1665     owl_function_resize_typwin(i-1);
    1666   }
    16671628}
    16681629
  • global.c

    r58d1f8a rf34dd65  
    5151  owl_keys_setup_keymaps(&g->kh);
    5252
    53   owl_list_create(&(g->muxevents));
    5453  owl_list_create(&(g->filterlist));
    5554  owl_list_create(&(g->puntlist));
     
    588587}
    589588
    590 /* muxevents */
    591 
    592 owl_muxevents *owl_global_get_muxevents(owl_global *g) {
    593   return(&(g->muxevents));
    594 }
    595 
    596589/* filterlist */
    597590
     
    717710int owl_global_get_newmsgproc_pid(owl_global *g) {
    718711  return(g->newmsgproc_pid);
    719 }
    720 
    721 void owl_global_add_to_malloced(owl_global *g, int i) {
    722   g->malloced+=i;
    723 }
    724 
    725 void owl_global_add_to_freed(owl_global *g, int i) {
    726   g->freed+=1;
    727 }
    728 
    729 int owl_global_get_malloced(owl_global *g) {
    730   return(g->malloced);
    731 }
    732 
    733 int owl_global_get_freed(owl_global *g) {
    734   return(g->freed);
    735 }
    736 
    737 int owl_global_get_meminuse(owl_global *g) {
    738   return(g->malloced-g->freed);
    739712}
    740713
  • list.c

    r50622a5 rf34dd65  
    7878}
    7979
    80 /* todo: might leak memory */
    81 int owl_list_replace_element(owl_list *l, int n, void *element)
    82 {
    83   if (n>l->size-1) return(-1);
    84 
    85   l->list[n]=element;
    86   return(0);
    87 }
    88 
    8980void owl_list_free_all(owl_list *l, void (*elefree)(void *))
    9081{
  • message.c

    r7d471c3 rf34dd65  
    338338}
    339339
    340 /* caller must free the return */
    341 char *owl_message_get_shorttimestr(owl_message *m)
    342 {
    343   struct tm *tmstruct;
    344   char *out;
    345 
    346   tmstruct=localtime(&(m->time));
    347   out=owl_sprintf("%2.2i:%2.2i", tmstruct->tm_hour, tmstruct->tm_min);
    348   if (out) return(out);
    349   return("??:??");
    350 }
    351 
    352340void owl_message_set_type_admin(owl_message *m)
    353341{
     
    384372{
    385373  return owl_message_is_type(m, "admin");
    386 }
    387 
    388 int owl_message_is_type_generic(owl_message *m)
    389 {
    390   char * t = owl_message_get_attribute_value(m, "type");
    391   return (t == NULL);
    392374}
    393375
     
    573555}
    574556
    575 int owl_message_is_from_me(owl_message *m)
    576 {
    577   if (owl_message_is_type_zephyr(m)) {
    578     if (!strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) {
    579       return(1);
    580     } else {
    581       return(0);
    582     }
    583   } else if (owl_message_is_type_aim(m)) {
    584     if (!strcasecmp(owl_message_get_sender(m), owl_global_get_aim_screenname(&g))) {
    585       return(1);
    586     } else {
    587       return(0);
    588     }
    589   } else if (owl_message_is_type_admin(m)) {
    590     return(0);
    591   }
    592   return(0);
    593 }
    594 
    595557int owl_message_is_mail(owl_message *m)
    596558{
    597559  if (owl_message_is_type_zephyr(m)) {
    598560    if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) {
    599       return(1);
    600     } else {
    601       return(0);
    602     }
    603   }
    604   return(0);
    605 }
    606 
    607 int owl_message_is_ping(owl_message *m)
    608 {
    609   if (owl_message_is_type_zephyr(m)) {
    610     if (!strcasecmp(owl_message_get_opcode(m), "ping")) {
    611561      return(1);
    612562    } else {
     
    992942}
    993943
    994 void owl_message_pretty_zsig(owl_message *m, char *buff)
    995 {
    996   /* stick a one line version of the zsig in buff */
    997   char *ptr;
    998 
    999   strcpy(buff, owl_message_get_zsig(m));
    1000   ptr=strchr(buff, '\n');
    1001   if (ptr) ptr[0]='\0';
    1002 }
    1003 
    1004944void owl_message_free(owl_message *m)
    1005945{
  • owl.h

    r40c6657 rf34dd65  
    377377  int active;
    378378  int needsfirstrefresh;
    379   void (*handler) (int ch);
    380379} owl_popwin;
    381380
     
    404403typedef struct _owl_filter {
    405404  char *name;
    406   int polarity;
    407405  owl_filterelement * root;
    408406  int fgcolor;
     
    445443} owl_editwin;
    446444
    447 typedef struct _owl_mux {
    448   int handle;                   /* for referencing this */
    449   int active;                   /* has this been deleted? */
    450   int fd;                     
    451   int eventmask;                /* bitmask of OWL_MUX_* */
    452   void (*handler_fn)(int handle, int fd, int eventmask, void *data);
    453   void *data;                   /* data reference to pass to callback */
    454 } owl_mux;
    455 typedef owl_list owl_muxevents;
    456 
    457445typedef struct _owl_keybinding {
    458446  int  *j;                      /* keypress stack (0-terminated) */ 
     
    541529  owl_list filterlist;
    542530  owl_list puntlist;
    543   owl_muxevents muxevents;      /* fds to dispatch on */
    544531  owl_vardict vars;
    545532  owl_cmddict cmds;
  • popwin.c

    r818a073 rf34dd65  
    5757}
    5858
    59 int owl_popwin_display_text(owl_popwin *pw, char *text)
    60 {
    61   waddstr(pw->popwin, text);
    62   wnoutrefresh(pw->popwin);
    63   touchwin(pw->borderwin);
    64   wnoutrefresh(pw->borderwin);
    65   owl_global_set_needrefresh(&g);
    66   return(0);
    67 }             
    68 
    6959int owl_popwin_close(owl_popwin *pw)
    7060{
     
    9686}
    9787
    98 void owl_popwin_set_handler(owl_popwin *pw, void (*func)(int ch))
    99 {
    100   pw->handler=func;
    101 }
    102 
    103 void owl_popwin_unset_handler(owl_popwin *pw)
    104 {
    105   pw->handler=NULL;
    106 }
    107 
    10888WINDOW *owl_popwin_get_curswin(owl_popwin *pw)
    10989{
  • text.c

    rf8d9df1 rf34dd65  
    66
    77static const char fileIdent[] = "$Id$";
    8 
    9 /* start with line aline (where the first line is 1) and print 'lines'
    10  *  lines
    11  */
    12 int owl_text_truncate_lines(char *out, char *in, int aline, int lines)
    13 {
    14   char *ptr1, *ptr2;
    15   int i;
    16 
    17   strcpy(out, "");
    18  
    19   if (aline==0) aline=1; /* really illegal use */
    20 
    21   /* find the starting line */
    22   ptr1=in;
    23   if (aline!=1) {
    24      for (i=0; i<aline-1; i++) {
    25       ptr1=strchr(ptr1, '\n');
    26       if (!ptr1) return(-1);
    27       ptr1++;
    28     }
    29   }
    30   /* ptr1 now holds the starting point */
    31 
    32   /* copy in the next 'lines' lines */
    33   if (lines<1) return(-1);
    34  
    35   for (i=0; i<lines; i++) {
    36     ptr2=strchr(ptr1, '\n');
    37     if (!ptr2) {
    38       strcat(out, ptr1);
    39       return(-1);
    40     }
    41     strncat(out, ptr1, ptr2-ptr1+1);
    42     ptr1=ptr2+1;
    43   }
    44   return(0);
    45 }
    46 
    47  
    48 /* the first column is column 0.  The message is expected to end in a
    49  * new line for now */
    50 void owl_text_truncate_cols(char *out, char *in, int acol, int bcol)
    51 {
    52   char *ptr_s, *ptr_e, *ptr_c, *tmpbuff, *last;
    53   int col, cnt, padding;
    54  
    55   tmpbuff=owl_malloc(strlen(in)+20);
    56 
    57   strcpy(tmpbuff, "");
    58   last=in+strlen(in)-1;
    59   ptr_s=in;
    60   while (ptr_s<last) {
    61     ptr_e=strchr(ptr_s, '\n');
    62     if (!ptr_e) {
    63       /* but this shouldn't happen if we end in a \n */
    64       break;
    65     }
    66    
    67     if (ptr_e==ptr_s) {
    68       strcat(tmpbuff, "\n");
    69       ptr_s++;
    70       continue;
    71     }
    72 
    73     col = 0;
    74     cnt = 0;
    75     padding = 0;
    76     ptr_c = ptr_s;
    77     while(col < bcol && ptr_c < ptr_e) {
    78       gunichar c = g_utf8_get_char(ptr_c);
    79       if (col + mk_wcwidth(c) > bcol) break;
    80       col += mk_wcwidth(c);
    81       ptr_c = g_utf8_next_char(ptr_c);
    82       if (col >= acol) {
    83         if (cnt == 0) {
    84           ptr_s = ptr_c;
    85           padding = col - acol;
    86         }
    87         ++cnt;
    88       }
    89     }
    90     if (cnt) {
    91       while(padding-- > 0) {
    92         strcat(tmpbuff, " ");
    93       }
    94       strncat(tmpbuff, ptr_s, ptr_c - ptr_s - 1);
    95     }
    96     strcat(tmpbuff, "\n");
    97     ptr_s = ptr_e + 1;
    98 #if 0
    99     /* we need to check that we won't run over here */
    100     if ( (ptr_e-ptr_s) < (bcol-acol) ) {
    101       len=ptr_e-(ptr_s+acol);
    102     } else {
    103       len=bcol-acol;
    104     }
    105     if ((ptr_s+len)>=last) {
    106       len-=last-(ptr_s+len);
    107     }
    108 
    109     strncat(tmpbuff, ptr_s+acol, len);
    110     strcat(tmpbuff, "\n");
    111 
    112     ptr_s=ptr_e+1;
    113 #endif
    114   }
    115   strcpy(out, tmpbuff);
    116   owl_free(tmpbuff);
    117 }
    118 
    1198
    1209void owl_text_indent(char *out, char *in, int n)
  • util.c

    re97c4a30 rf34dd65  
    125125  wattroff(sepwin, A_REVERSE);
    126126  wnoutrefresh(sepwin);
    127 }
    128 
    129 
    130 void pophandler_quit(int ch)
    131 {
    132   if (ch=='q') {
    133     owl_popwin_close(owl_global_get_popwin(&g));
    134   }
    135127}
    136128
     
    380372}
    381373
    382 /* return the index of the last char before a change from the first one */
    383 int owl_util_find_trans(char *in, int len)
    384 {
    385   int i;
    386   for (i=1; i<len; i++) {
    387     if (in[i] != in[0]) return(i-1);
    388   }
    389   return(i);
    390 }
    391 
    392 int owl_util_find_trans_short(short *in, int len)
    393 {
    394   int i;
    395   for (i=1; i<len; i++) {
    396     if (in[i] != in[0]) return(i-1);
    397   }
    398   return(i);
    399 }
    400 
    401 /* Caller must free response.
    402  * Takes in strings which are space-separated lists of tokens
    403  * and returns a single string containing no token more than once.
    404  * If prohibit is non-null, no token may start with a character
    405  * in prohibit.
    406  */
    407 char *owl_util_uniq(char *A, char *B, char *prohibit)
    408 {
    409  
    410   char *cat, **tok;
    411   int toklen, i, j, first=1;
    412   cat = owl_malloc(strlen(A)+strlen(B)+3);
    413   strcpy(cat, A);
    414   strcat(cat, " ");
    415   strcat(cat, B);
    416   tok = atokenize(cat, " ", &toklen);
    417   strcpy(cat, "");
    418   for (i=0; i<toklen; i++) {
    419     int dup=0;
    420     for (j=0; j<i; j++) {
    421       if (!strcmp(tok[i], tok[j])) dup=1;
    422     }
    423     if (!dup && (!prohibit || !strchr(prohibit, tok[i][0]))) {
    424       if (!first) {
    425         strcat(cat, " ");
    426       }
    427       first=0;
    428       strcat(cat, tok[i]);
    429     }
    430   }
    431   atokenize_free(tok, toklen);
    432   return(cat);
    433 }
    434 
    435374/* hooks for doing memory allocation et. al. in owl */
    436375
     
    692631}
    693632
    694 /* add the string 'str' to the list 'list' of strings, only if it
    695  * is not already present
    696  */
    697 void owl_util_list_add_unique_string(owl_list *list, char *str)
    698 {
    699   int i, j;
    700 
    701   j=owl_list_get_size(list);
    702   for (i=0; i<j; i++) {
    703     if (!strcmp(str, owl_list_get_element(list, i))) return;
    704   }
    705   owl_list_append_element(list, owl_strdup(str));
    706 }
    707 
    708 int owl_util_common_strings_in_lists(owl_list *a, owl_list *b)
    709 {
    710   int i, j, x, y;
    711 
    712   j=owl_list_get_size(a);
    713   for (i=0; i<j; i++) {
    714     y=owl_list_get_size(b);
    715     for (x=0; x<y; x++) {
    716       if (!strcmp(owl_list_get_element(a, i), owl_list_get_element(b, x))) return(1);
    717     }
    718   }
    719   return(0);
    720 }
    721 
    722633int owl_util_max(int a, int b)
    723634{
     
    883794              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
    884795
    885   FAIL_UNLESS("owl_util_uniq 1",
    886               !strcmp("foo bar x", owl_util_uniq("foo", "bar x", "-")));
    887   FAIL_UNLESS("owl_util_uniq 2",
    888               !strcmp("foo bar x", owl_util_uniq("foo", "bar -y x", "-")));
    889   FAIL_UNLESS("owl_util_uniq 3",
    890               !strcmp("meep foo bar", owl_util_uniq("meep foo", "bar foo meep", "-")));
    891 
    892796  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
    893797  printf("# END testing owl_util (%d failures)\n", numfailed);
  • zcrypt.c

    r8412869 rf34dd65  
    6666#define M_RANDOMIZE       4
    6767#define M_SETKEY          5
    68 
    69 int zcrypt(int argc, char *argv[]) {
    70   char *fname = NULL;
    71   int error = FALSE;
    72   int zephyr = FALSE;
    73   char *class = NULL, *instance = NULL;
    74   int mode = M_NONE;
    75 
    76   extern int optind, opterr;
    77   extern char *optarg;
    78   char c;
    79 
    80   int messageflag = FALSE;
    81   ZWRITEOPTIONS zoptions;
    82   zoptions.flags = 0;
    83 
    84   while ((c = getopt(argc, argv, "ZDERSF:c:i:advqtluons:f:m")) != (char)EOF) {
    85     switch(c) {
    86       case 'Z':
    87         /* Zephyr encrypt */
    88         mode = M_ZEPHYR_ENCRYPT;
    89         break;
    90       case 'D':
    91         /* Decrypt */
    92         mode = M_DECRYPT;
    93         break;
    94       case 'E':
    95         /* Encrypt */
    96         mode = M_ENCRYPT;
    97         break;
    98       case 'R':
    99         /* Randomize the keyfile */
    100         mode = M_RANDOMIZE;
    101         break;
    102       case 'S':
    103         /* Set a new key value from stdin */
    104         mode = M_SETKEY;
    105         break;
    106       case 'F':
    107         /* Specify the keyfile explicitly */
    108         if (fname != NULL) error = TRUE;
    109         fname = optarg;
    110         break;
    111       case 'c':
    112         /* Zwrite/zcrypt: class name */
    113         if (class != NULL) error = TRUE;
    114         class = optarg;
    115         break;
    116       case 'i':
    117         /* Zwrite/zcrypt: instance name */
    118         if (instance != NULL) error = TRUE;
    119         instance = optarg;
    120         break;
    121       case 'a':
    122         /* Zwrite: authenticate (default) */
    123         zoptions.flags &= ~ZWRITE_OPT_NOAUTH;
    124         break;
    125       case 'd':
    126         /* Zwrite: do not authenticate */
    127         zoptions.flags |= ZWRITE_OPT_NOAUTH;
    128         break;
    129       case 'v':
    130         /* Zwrite: verbose */
    131         zoptions.flags |= ZWRITE_OPT_VERBOSE;
    132         break;
    133       case 'q':
    134         /* Zwrite: quiet */
    135         zoptions.flags |= ZWRITE_OPT_QUIET;
    136         break;
    137       case 't':
    138         /* Zwrite: no expand tabs (ignored) */
    139         break;
    140       case 'l':
    141         /* Zwrite: ignore '.' on a line by itself (ignored) */
    142         zoptions.flags |= ZCRYPT_OPT_IGNOREDOT;
    143         break;
    144       case 'u':
    145         /* Zwrite: urgent message */
    146         instance = "URGENT";
    147         break;
    148       case 'o':
    149         /* Zwrite: ignore zephyr variables zwrite-class, zwrite-inst, */
    150         /*         zwrite-opcode */
    151         zoptions.flags |= ZWRITE_OPT_IGNOREVARS;
    152         break;
    153       case 'n':
    154         /* Zwrite: prevent PING message (always used) */
    155         break;
    156       case 's':
    157         /* Zwrite: signature */
    158         zoptions.flags |= ZWRITE_OPT_SIGNATURE;
    159         zoptions.signature = optarg;
    160         break;
    161       case 'f':
    162         /* Zwrite: file system specification (ignored) */
    163         break;
    164       case 'm':
    165         /* Message on rest of line */
    166         messageflag = TRUE;
    167         break;
    168       case '?':
    169         error = TRUE;
    170         break;
    171     }
    172     if (error || messageflag) break;
    173   }
    174 
    175   if (class != NULL || instance != NULL) {
    176     zephyr = TRUE;
    177   }
    178 
    179   if (messageflag) {
    180     zoptions.flags |= ZCRYPT_OPT_MESSAGE;
    181     zoptions.message = BuildArgString(argv, optind, argc);
    182     if (!zoptions.message)
    183     {
    184       printf("Memory allocation error.\n");
    185       error = TRUE;
    186     }
    187   } else if (optind < argc) {
    188     error = TRUE;
    189   }
    190 
    191   if (mode == M_NONE) mode = (zephyr?M_ZEPHYR_ENCRYPT:M_ENCRYPT);
    192 
    193   if (mode == M_ZEPHYR_ENCRYPT && !zephyr) error = TRUE;
    194 
    195   if (!error && fname == NULL && (class != NULL || instance != NULL)) {
    196     fname = GetZephyrVarKeyFile(argv[0], class, instance);
    197   }
    198 
    199  
    200   if (error || fname == NULL) {
    201     printf("Usage: %s [-Z|-D|-E|-R|-S] [-F Keyfile] [-c class] [-i instance]\n", argv[0]);
    202     printf("       [-advqtluon] [-s signature] [-f arg] [-m message]\n");
    203     printf("  One or more of class, instance, and keyfile must be specified.\n");
    204   } else {
    205     if (mode == M_RANDOMIZE) {
    206       /* Choose a new, random key */
    207 /*
    208       FILE *fkey = fopen(fname, "w");
    209       if (!fkey)
    210         printf("Could not open key file for writing: %s\n", fname);
    211       else
    212       {
    213         char string[100];
    214         fputs(fkey, string);
    215         fclose(fkey);
    216         }
    217  */
    218       printf("Feature not yet implemented.\n");
    219     } else if (mode == M_SETKEY) {
    220       /* Set a new, user-entered key */
    221       char newkey[MAX_KEY];
    222       FILE *fkey;
    223      
    224       if (isatty(0)) {
    225         printf("Enter new key: ");
    226         /* Really should read without echo!!! */
    227         fgets(newkey, MAX_KEY - 1, stdin);
    228       } else {
    229         fgets(newkey, MAX_KEY - 1, stdin);
    230       }
    231 
    232       fkey = fopen(fname, "w");
    233       if (!fkey) {
    234         printf("Could not open key file for writing: %s\n", fname);
    235       } else {
    236         if (fputs(newkey, fkey) != strlen(newkey) || putc('\n', fkey) != '\n') {
    237           printf("Error writing to key file.\n");
    238           fclose(fkey);
    239         } else {
    240           fclose(fkey);
    241           printf("Key update complete.\n");
    242         }
    243       }
    244     } else {
    245       /* Encrypt/decrypt */
    246       FILE *fkey = fopen(fname, "r");
    247       if (!fkey) {
    248         printf("Could not open key file: %s\n", fname);
    249       } else {
    250         char keystring[MAX_KEY];
    251         fgets(keystring, MAX_KEY-1, fkey);
    252         if (mode == M_ZEPHYR_ENCRYPT || mode == M_ENCRYPT) {
    253           do_encrypt(keystring, (mode == M_ZEPHYR_ENCRYPT), class, instance,
    254                      &zoptions, fname);
    255         } else {
    256           do_decrypt(keystring);
    257         }
    258         fclose(fkey);
    259       }
    260     }
    261   }
    262 
    263   /* Always print the **END** message if -D is specified. */
    264   if (mode == M_DECRYPT) printf("**END**\n");
    265 
    266   exit(0);
    267 }
    26868
    26969/* The 'owl_zcrypt_decrypt' function was written by kretch for Owl.
     
    384184}
    385185
    386 /* Build a space-separated string from argv from elements between start  *
    387  * and end - 1.  owl_malloc()'s the returned string. */
    388 char *BuildArgString(char **argv, int start, int end) {
    389   int len = 1;
    390   int i;
    391   char *result;
    392 
    393   /* Compute the length of the string.  (Plus 1 or 2) */
    394   for (i = start; i < end; i++) {
    395     len += strlen(argv[i]) + 1;
    396   }
    397 
    398   /* Allocate memory */
    399   result = (char *)owl_malloc(len);
    400   if (result) {
    401     /* Build the string */
    402     char *ptr = result;
    403     /* Start with an empty string, in case nothing is copied. */
    404     *ptr = '\0';
    405     /* Copy the arguments */
    406     for (i = start; i < end; i++) {
    407       char *temp = argv[i];
    408       /* Add a space, if not the first argument */
    409       if (i != start) *ptr++ = ' ';
    410       /* Copy argv[i], leaving ptr pointing to the '\0' copied from temp */
    411       while ((*ptr = *temp++)!=0) {
    412         ptr++;
    413       }
    414     }
    415   }
    416 
    417   return(result);
    418 }
    419186
    420187#define MAX_BUFF 258
     
    508275static pid_t zephyrpipe_pid = 0;
    509276
    510 /* Open a pipe to zwrite */
    511 static FILE *GetZephyrPipe(char *class, char *instance, ZWRITEOPTIONS *zoptions) {
    512   int fildes[2];
    513   pid_t pid;
    514   FILE *result;
    515   char *argv[20];
    516   int argc = 0;
    517 
    518   if (pipe(fildes) < 0) return(NULL);
    519   pid = fork();
    520 
    521   if (pid < 0) {
    522     /* Error: clean up */
    523     close(fildes[0]);
    524     close(fildes[1]);
    525     result = NULL;
    526   } else if (pid == 0) {
    527     /* Setup child process */
    528     argv[argc++] = "zwrite";
    529     argv[argc++] = "-n";     /* Always send without ping */
    530     if (class) {
    531       argv[argc++] = "-c";
    532       argv[argc++] = class;
    533     }
    534     if (instance) {
    535       argv[argc++] = "-i";
    536       argv[argc++] = instance;
    537     }
    538     if (zoptions->flags & ZWRITE_OPT_NOAUTH) argv[argc++] = "-d";
    539     if (zoptions->flags & ZWRITE_OPT_QUIET) argv[argc++] = "-q";
    540     if (zoptions->flags & ZWRITE_OPT_VERBOSE) argv[argc++] = "-v";
    541     if (zoptions->flags & ZWRITE_OPT_SIGNATURE) {
    542       argv[argc++] = "-s";
    543       argv[argc++] = zoptions->signature;
    544     }
    545     argv[argc++] = "-O";
    546     argv[argc++] = "crypt";
    547     argv[argc] = NULL;
    548     close(fildes[1]);
    549     if (fildes[0] != STDIN_FILENO) {
    550       if (dup2(fildes[0], STDIN_FILENO) != STDIN_FILENO) exit(0);
    551       close(fildes[0]);
    552     }
    553     close(fildes[0]);
    554     execvp(argv[0], argv);
    555     printf("Exec error: could not run zwrite\n");
    556     exit(0);
    557   } else {
    558     close(fildes[0]);
    559     /* Create a FILE * for the zwrite pipe */
    560     result = (FILE *)fdopen(fildes[1], "w");
    561     zephyrpipe_pid = pid;
    562   }
    563 
    564   return(result);
    565 }
    566 
    567 /* Close the pipe to zwrite */
    568 void CloseZephyrPipe(FILE *pipe) {
    569   fclose(pipe);
    570   waitpid(zephyrpipe_pid, NULL, 0);
    571   zephyrpipe_pid = 0;
    572 }
    573 
    574 #define MAX_RESULT 2048
    575 
    576 
    577 void block_to_ascii(char *output, FILE *outfile) {
    578   int i;
    579   for (i = 0; i < 8; i++) {
    580     putc(((output[i] & 0xf0) >> 4) + BASE_CODE, outfile);
    581     putc( (output[i] & 0x0f)       + BASE_CODE, outfile);
    582   }
    583 }
    584 
    585 #define MAX_LINE 128
    586 
    587 /* Encrypt stdin, with prompt if isatty, and send to stdout, or to zwrite
    588    if zephyr is set. */
    589 static int do_encrypt(char *keystring, int zephyr, char *class, char *instance, ZWRITEOPTIONS *zoptions, char* keyfile) {
    590   des_cblock key;
    591   des_key_schedule schedule;
    592   char input[8], output[8];
    593   int size;
    594   FILE *outfile = stdout;
    595   int error = FALSE;
    596   char *inbuff = NULL, *inptr;
    597   int freein = FALSE;
    598   int use_buffer = FALSE;
    599   int num_blocks=0, last_block_size=0;
    600 
    601   des_string_to_key(keystring, key);
    602   des_key_sched(key, schedule);
    603 
    604   if (zephyr) {
    605     if (zoptions->flags & ZCRYPT_OPT_MESSAGE) {
    606       /* Use the -m message */
    607       int length;
    608       inbuff = zoptions->message;     
    609       length = strlen(inbuff);
    610       num_blocks = (length + 7) / 8;
    611       last_block_size = ((length + 7) % 8) + 1;
    612       use_buffer = TRUE;
    613     } else if (isatty(0)) {
    614       /* tty input, so show the "Type your message now..." message */
    615       if (zoptions->flags & ZCRYPT_OPT_IGNOREDOT) {
    616         printf("Type your message now.  End with the end-of-file character.\n");
    617       } else {
    618         printf("Type your message now.  End with control-D or a dot on a line by itself.\n");
    619       }
    620       use_buffer = TRUE;
    621       if ((inptr = inbuff = (char *)owl_malloc(MAX_RESULT)) == NULL) {
    622         printf("Memory allocation error\n");
    623         return FALSE;
    624       }
    625       while (inptr - inbuff < MAX_RESULT - MAX_LINE - 20) {
    626         fgets(inptr, MAX_LINE, stdin);
    627         if (inptr[0]) {
    628           if (inptr[0] == '.' && inptr[1] == '\n' &&
    629               !(zoptions->flags & ZCRYPT_OPT_IGNOREDOT)) {
    630             inptr[0] = '\0';
    631             break;
    632           } else {
    633             inptr += strlen(inptr);
    634           }
    635         } else {
    636           break;
    637         }
    638       }
    639       num_blocks = (inptr - inbuff + 7) / 8;
    640       last_block_size = ((inptr - inbuff + 7) % 8) + 1;
    641       freein = TRUE;
    642     }
    643 
    644     /* if (zephyr) */
    645     outfile = GetZephyrPipe(class, instance, zoptions);
    646     if (!outfile) {
    647       printf("Could not run zwrite\n");
    648       if (freein && inbuff) {
    649         owl_free(inbuff);
    650       }
    651       return(FALSE);
    652     }
    653   }
    654 
    655   inptr = inbuff;
    656 
    657   /* Encrypt the input (inbuff or stdin) and send it to outfile */
    658   while (TRUE) {
    659     if (use_buffer) {
    660       /* Get 8 bytes from buffer */
    661       if (num_blocks > 1) {
    662         size = 8;
    663         memcpy(input, inptr, size);
    664         inptr += 8;
    665         num_blocks--;
    666       } else if (num_blocks == 1) {
    667         size = last_block_size;
    668         memcpy(input, inptr, size);
    669         num_blocks--;
    670       } else {
    671         size = 0;
    672       }
    673     } else {
    674       /* Get 8 bytes from stdin */
    675       size = fread(input, 1, 8, stdin);
    676     }
    677 
    678     /* Check for EOF and pad the string to 8 chars, if needed */
    679     if (size == 0) break;     /* END OF INPUT: BREAK FROM while LOOP! */
    680      
    681     if (size < 8) memset(input + size, 0, 8 - size);
    682 
    683     /* Encrypt and output the block */
    684     des_ecb_encrypt(input, output, schedule, TRUE);
    685     block_to_ascii(output, outfile);
    686 
    687     if (size < 8) break;
    688   }
    689 
    690   /* Close out the output */
    691   if (!error) putc('\n', outfile);
    692   if (zephyr) CloseZephyrPipe(outfile);
    693 
    694   /* Free the input buffer, if necessary */
    695   if (freein && inbuff) owl_free(inbuff);
    696 
    697   return(!error);
    698 }
    699 
    700 /* Read a half-byte from stdin, skipping invalid characters.  Returns -1
    701    if at EOF or file error */
    702 int read_ascii_nybble() {
    703   char c;
    704 
    705   while (TRUE) {
    706     if (fread(&c, 1, 1, stdin) == 0) {
    707       return(-1);
    708     } else if (c >= BASE_CODE && c <= LAST_CODE) {
    709       return(c - BASE_CODE);
    710     }
    711   }
    712 }
    713 
    714 /* Read both halves of the byte and return the single byte.  Returns -1
    715    if at EOF or file error. */
    716 int read_ascii_byte() {
    717   int c1, c2;
    718   c1 = read_ascii_nybble();
    719   if (c1 >= 0) {
    720     c2 = read_ascii_nybble();
    721     if (c2 >= 0) {
    722       return c1 * 0x10 + c2;
    723     }
    724   }
    725   return(-1);
    726 }
    727 
    728 /* Read an 8-byte DES block from stdin */
    729 int read_ascii_block(char *input) {
    730   int c, i;
    731 
    732   for (i = 0; i < 8; i++) {
    733     c = read_ascii_byte();
    734     if (c < 0) return(FALSE);
    735     input[i] = c;
    736   }
    737 
    738   return(TRUE);
    739 }
    740 
    741 /* Decrypt stdin */
    742 int do_decrypt(char *keystring) {
    743   des_cblock key;
    744   des_key_schedule schedule;
    745   char input[8], output[9];
    746 
    747   output[0] = '\0';    /* In case no message at all                 */
    748   output[8] = '\0';    /* NULL at end will limit string length to 8 */
    749 
    750   des_string_to_key(keystring, key);
    751   des_key_sched(key, schedule);
    752 
    753   while (read_ascii_block(input)) {
    754     des_ecb_encrypt(input, output, schedule, FALSE);
    755     printf("%s", output);
    756   }
    757 
    758   if (output[0]) {
    759     if (output[strlen(output)-1] != '\n') {
    760       printf("\n");
    761     }
    762   } else {
    763     printf("\n");
    764   }
    765   return(0);
    766 }
    767 
    768 #endif
     277#endif
Note: See TracChangeset for help on using the changeset viewer.