char * join(char **list,char *delim) { int i; int len = strllen(list); char *result; int total_len = 0; for (i=0; i < len; i++) total_len += strlen(list[i]); total_len += strlen(delim) * len - 1; result = malloc(sizeof(char) * total_len + 1); for (i = 0;i < len;i++) { strcat(result,list[i]); if (i < (len - 1)) strcat(result,delim); } return result; }
char /* PURPOSE : process the command line arguments -------------------*/ PROG_args (int argc, char *argv[]) { /*---(locals)-------------------------*/ char *a = NULL; /* current argument */ int i = 0; /* loop iterator -- arguments */ int len = 0; /* argument length */ char two_arg = 0; /*---(begin)--------------------------*/ DEBUG_TOPS yLOG_enter (__FUNCTION__); DEBUG_TOPS yLOG_value ("argc" , argc); if (argc == 1) { DEBUG_TOPS yLOG_exit (__FUNCTION__); return 0; } /*---(program name)--------------------------*/ strlcpy(my.prog, argv[0], LEN_USER); DEBUG_TOPS yLOG_value ("prog name" , my.prog); /*---(process)------------------------*/ for (i = 1; i < argc; ++i) { a = argv[i]; if (i < argc - 1) two_arg = 1; else two_arg = 0; len = strllen (a, LEN_LINE); /*---(skip debugging)--------------*/ if (a[0] == '@') continue; my.user_mode = MODE_USER; /*---(version)---------------------*/ if (strcmp (a, "--version" ) == 0 || strcmp (a, "-V") == 0) { PROG_version (); printf ("%s\n", verstring); return -1; } /*---(usage/help)------------------*/ else if (strcmp(a, "-h" ) == 0) PROG_usage(); else if (strcmp(a, "--help" ) == 0) PROG_usage(); /*---(lists)-----------------------*/ else if (strcmp(a, "--list" ) == 0) tabs_global (my.who, 'l'); else if (strcmp(a, "--all" ) == 0) tabs_global ("ALL" , 'l'); else if (strcmp(a, "--here" ) == 0) tabs_local (my.who, 'l'); /*---(installing)------------------*/ else if (a[0] != '-' ) tabs_install (a); /*> else if (strcmp(a, "--test" ) == 0) { TWOARG crontab_test (argv[++i]); } <*/ else if (strcmp(a, "--reload" ) == 0) tabs_local (my.who, 'i'); /*---(removing)--------------------*/ else if (strcmp(a, "-d" ) == 0) { TWOARG tabs_delete (argv[++i]); } else if (strcmp(a, "-r" ) == 0) { TWOARG tabs_delete (argv[++i]); } else if (strcmp(a, "--purge" ) == 0) tabs_global (my.who, 'p'); else if (strcmp(a, "--cleanse" ) == 0) tabs_global ("ALL" , 'p'); /*---(user switches)---------------*/ else if (strcmp(a, "-u" ) == 0) { TWOARG tabs_user (argv[++i]); } else if (strcmp(a, "--user" ) == 0) { TWOARG tabs_user (argv[++i]); } /*---(warnings)--------------------*/ else if (strcmp(a, "-l" ) == 0) tabs_cat_stub (); else if (strcmp(a, "-c" ) == 0) tabs_dir_stub (); else if (strcmp(a, "-e" ) == 0) tabs_edit_stub (); else if (strcmp(a, "-" ) == 0) tabs_stdin_stub (); /*---(unknown)---------------------*/ else printf("requested action not understood or incomplete\n"); } /*---(complete)-----------------------*/ DEBUG_TOPS yLOG_exit (__FUNCTION__); return 0; }
char /* PURPOSE : validate the crontab name */ file_parse_name (cchar *a_file, cchar a_loc) { /*---(locals)-----------+-----+-----+-*/ char rce = -10; char rc = 0; char *p = NULL; char *q = NULL; int x_len = 0; char x_name [LEN_NAME]; char x_generic = 0; /*---(header)-------------------------*/ DEBUG_INPT yLOG_enter (__FUNCTION__); /*---(prepare)------------------------*/ my.f_ready = '-'; strlcpy (my.f_name, "", LEN_NAME); strlcpy (my.f_user, "", LEN_NAME); my.f_uid = -1; strlcpy (my.f_desc, "", LEN_NAME); strlcpy (my.f_ext , "", LEN_NAME); /*---(defense)------------------------*/ DEBUG_INPT yLOG_point ("a_file" , a_file); --rce; if (a_file == NULL) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } DEBUG_INPT yLOG_info ("a_file" , a_file); DEBUG_INPT yLOG_char ("first char", a_file [0]); --rce; if (a_file [0] == '.') { DEBUG_INPT yLOG_note ("will not process hidden crontabs"); DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } x_len = strllen (a_file, LEN_NAME); DEBUG_INPT yLOG_value ("x_len" , x_len); DEBUG_INPT yLOG_value ("min" , 7); --rce; if (x_len < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } DEBUG_INPT yLOG_value ("max" , LEN_NAME); --rce; if (x_len >= LEN_NAME) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } /*---(parse)--------------------------*/ strlcpy (x_name, a_file, LEN_NAME); p = strtok (x_name, "."); DEBUG_INPT yLOG_point ("p" , p); /*---(check user)---------------------*/ rc = file_check_user (p, a_loc); DEBUG_INPT yLOG_value ("check user", rc); --rce; if (rc < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } /*---(parse again)--------------------*/ p = strtok (NULL , "."); DEBUG_INPT yLOG_point ("p" , p); /*---(check desc)---------------------*/ rc = file_check_desc (p); DEBUG_INPT yLOG_value ("check desc", rc); --rce; if (rc < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } /*---(parse again)--------------------*/ p = strtok (NULL , "."); DEBUG_INPT yLOG_point ("p" , p); /*---(check ext)----------------------*/ rc = file_check_ext (p, a_loc); DEBUG_INPT yLOG_value ("check ext" , rc); --rce; if (rc < 0) { DEBUG_INPT yLOG_exitr (__FUNCTION__, rce); return rce; } /*---(save)---------------------------*/ strlcpy (my.f_name, a_file , LEN_DESC); if (a_loc == LOC_CENTRAL) my.f_ready = 'y'; /*---(set full name)------------------*/ if (a_loc == LOC_CENTRAL) { snprintf (my.f_full, LEN_RECD, "%s%s.%s", my.dir_central, my.f_user, my.f_desc); } /*---(header)-------------------------*/ DEBUG_INPT yLOG_exit (__FUNCTION__); return 0; }
char /* PURPOSE : figure out who launched us and how -------------------*/ PROG_whoami (void) { /*---(locals)-------------------------*/ char rce = -10; int rc = 0; /* generic return code */ tPASSWD *x_pass = NULL; /* passwd data structure */ int x_len = 0; /* user name length */ /*---(header)-------------------------*/ DEBUG_TOPS yLOG_enter (__FUNCTION__); /*---(get real uid)-------------------*/ my.uid = getuid(); DEBUG_TOPS yLOG_value ("uid" , my.uid); /*---(pull user name)-----------------*/ x_pass = getpwuid (my.uid); DEBUG_TOPS yLOG_point ("x_pass" , x_pass); --rce; if (x_pass == NULL) { printf("can not retreive your user information from the system\n"); DEBUG_TOPS yLOG_exitr (__FUNCTION__, rce); return rce; } DEBUG_TOPS yLOG_info ("->name" , x_pass->pw_name); /*---(check user name)----------------*/ x_len = strllen (x_pass->pw_name, 20); DEBUG_TOPS yLOG_value ("x_len" , x_len); --rce; if (x_len < 1) { printf("your user name can not be empty\n"); DEBUG_TOPS yLOG_exitr (__FUNCTION__, rce); return rce; } --rce; if (x_len > 20) { printf("user name is too long\n"); DEBUG_TOPS yLOG_exitr (__FUNCTION__, rce); return rce; } strlcpy(my.who, x_pass->pw_name, 20); /*---(check for root user)--------------*/ my.am_root = 'n'; if (my.uid == 0) my.am_root = 'y'; DEBUG_TOPS yLOG_char ("am_root" , my.am_root); /*---(change uid/permissions)-----------*/ --rce; if (my.am_root != 'y') { DEBUG_TOPS yLOG_note ("attempt to gain root (legally;)"); rc = setuid(0); DEBUG_TOPS yLOG_value ("setuid" , rc); if (rc != 0) { printf("could not gain root authority\n"); DEBUG_TOPS yLOG_exitr (__FUNCTION__, rce); return rce; } DEBUG_TOPS yLOG_note ("successfully gained root"); } /*---(log pid info)---------------------*/ my.pid = getpid(); DEBUG_TOPS yLOG_value ("pid" , my.pid); my.ppid = getppid(); DEBUG_TOPS yLOG_value ("ppid" , my.ppid); /*---(complete)-----------------------*/ DEBUG_TOPS yLOG_exit (__FUNCTION__); return 0; }
char * concatenate_results (RESULT_NODE *r, int shuffle, Bool convert_indent, char **error_text) { RESULT_NODE *op; int indent = 0, shuffle_cnt, oplines, totlines, linewidth, opwidth, totwidth, totlinelength, runlength, line; char **blk, *txt, *line_end, *last_line, *rc; if (error_text) *error_text = NULL; /* No errors yet */ totlines = 0; totwidth = 0; totlinelength = 0; shuffle_cnt = 0; op = first_operand (r, & indent); if (convert_indent) r-> indent = 0; /* Coz indent has been migrated. */ else indent = 0; /* Forget about initial indentation */ while (op) { op-> indent = indent; /* Copy indent value to operand. */ /* Perform shuffle */ if (shuffle > 0 && op-> indent >= shuffle) { if (op-> indent - shuffle_cnt >= shuffle) op-> indent -= shuffle_cnt; else op-> indent = shuffle; } /* Calculate length & width of operand for shuffle. */ if (op-> value. type == TYPE_BLOCK) { /* Catch undefined operand. */ if (! op-> value. b) return NULL; oplines = 0; opwidth = 0; blk = op-> value. b; while (*blk) { oplines++; linewidth = strlen (*blk); if (linewidth > opwidth) opwidth = linewidth; blk++; } opwidth += op-> indent; totwidth += opwidth; totlinelength += opwidth; } else { if (op-> value. type != TYPE_UNDEFINED) string_value (& op-> value); /* Catch undefined operand. */ if (op-> value. type == TYPE_UNDEFINED || (! op-> value. s)) { /* Pass node result coz that's where culprit is stored */ result_node_error ("Undefined expression", r, error_text); return NULL; } string_value (& op-> value); oplines = 1; opwidth = op-> indent + strllen (op-> value. s); if (strchr (op-> value. s, '\n')) totwidth = opwidth; else totwidth += opwidth; totlinelength += op-> indent + strlen (op-> value. s); } if (oplines > totlines) totlines = oplines; if (op-> value. s && strchr (op-> value. s, '\n')) shuffle_cnt = 0; else if (op-> script_node-> extend) shuffle_cnt = totwidth; else shuffle_cnt += opwidth - (op-> script_node-> width + indent); op = next_operand (op, & indent); } /* Now build the result */ rc = mem_alloc (totlines * totlinelength + 1); memset (rc, ' ', totlines * totlinelength + 1); op = first_operand (r, NULL); runlength = 0; while (op) { line = 0; if (op-> value. type == TYPE_BLOCK) { opwidth = 0; blk = op-> value. b; while (*blk) { linewidth = strlen (*blk); if (linewidth > opwidth) opwidth = linewidth; txt = rc + line * totlinelength + runlength + op-> indent; memcpy (txt, *blk, strlen (*blk)); blk++; line++; } opwidth += op-> indent; runlength += opwidth; } else { for (line = 0; line < totlines; line++) { txt = rc + line * totlinelength + runlength + op-> indent; memcpy (txt, op-> value. s, strlen (op-> value. s)); } runlength += op-> indent + strlen (op-> value. s); } op = next_operand (op, NULL); } rc [totlines * totlinelength] = 0; /* Trim whitespace from all lines */ txt = rc; while (txt) { line_end = strchr(txt, '\n'); if (!line_end) break; if (*(line_end - 1) == '\r') line_end = line_end - 1; last_line = line_end; while (last_line > txt) { if (!isspace (*(last_line - 1))) break; last_line--; } memmove(last_line, line_end, strlen(line_end) + 1); txt = last_line + (*last_line == '\r' ? 2 : 1); } /* JS Blunder check */ ASSERT (runlength == totlinelength); return rc; }