int parse_at_list(char *list, int use_count, int abs_path) { char *b, *c, *s, *l; int comma = 0; char *user, *host; struct hostlist *ph, *nh, *hostlist = (struct hostlist *)0; if (strlen(list) == 0) return (1); if ((l = (char *)calloc(1, strlen(list) + 1)) == (char *)0) { fprintf(stderr, "Out of memory.\n"); exit(1); } strcpy(l, list); c = l; while (*c != '\0') { /* Drop leading white space */ while (isspace((int)*c)) c++; /* Find the next comma */ s = c; while (*c != ',' && *c) c++; /* If requested, is this an absolute path */ if (abs_path && *s != '/') { free(l); return 1; } /* Drop any trailing blanks */ comma = (*c == ','); *c = '\0'; b = c - 1; while (isspace((int)*b)) *b-- = '\0'; /* Parse the individual list item */ if (parse_at_item(s, &user, &host)) { free(l); return 1; } /* The user part must be given */ if (strcmp(user, "") == 0) { free(l); return 1; } /* If requested, make sure the host name is not repeated */ if (use_count) { ph = hostlist; while (ph) { if (strcmp(ph->host, host) == 0) { free(l); return 1; } ph = ph->next; } nh = (struct hostlist *) calloc(1, sizeof(struct hostlist)); if (!nh) { free(l); fprintf(stderr, "Out of memory\n"); exit(1); } nh->next = hostlist; snprintf(nh->host, PBS_MAXHOSTNAME, "%s", host); hostlist = nh; } if (comma) { c++; } } /* Release memory for hostlist and argument list */ ph = hostlist; while (ph) { nh = ph->next; free(ph); ph = nh; } free(l); /* Make sure the list does not end with a comma */ if (comma) return 1; return 0; }
/** @fn int parse_at_list(char *list, int use_count, int abs_path) * @brief parse a comma-separated list of name[@host] items * * @param[in] list * @param[out] use_count if true, make sure no host is repeated * in the list, and host is defaulted only * once * @param[out] abs_path if true, make sure the item appears to * begin with an absolute path name * * @return int * @retval 1 parsing failure * @retval 0 success * @par MT-Safe: no * @par Side Effects: * exits with return code 1 on memory allocation failure */ int parse_at_list(char *list, int use_count, int abs_path) { char *b, *c, *s, *list_dup; int rc = 0; char user[MAXPATHLEN+1]; char host[PBS_MAXSERVERNAME]; struct hostlist *ph, *nh, *hostlist = NULL; if ((list == NULL) || (*list == '\0')) return 1; #ifdef WIN32 back2forward_slash(list); /* "\" translate to "/" for path */ #endif if ((list_dup = strdup(list)) == NULL) { fprintf(stderr, "Out of memory.\n"); return 1; } for (c = list_dup; *c != '\0'; rc = 0) { rc = 1; /* Drop leading white space */ while (isspace(*c)) c++; /* If requested, is this an absolute path */ if (abs_path && !is_full_path(c)) break; /* Find the next comma */ for (s = c; *c && *c != ','; c++) ; /* Drop any trailing blanks */ for (b = c - 1; (b >= list_dup) && isspace(*b); b--) *b = '\0'; /* Make sure the list does not end with a comma */ if (*c == ',') { *c++ = '\0'; if (*c == '\0') break; } /* Parse the individual list item */ if (parse_at_item(s, user, host)) break; /* The user part must be given */ if (*user == '\0') break; /* If requested, make sure the host name is not repeated */ if (use_count) { ph = hostlist; while (ph) { if (strcmp(ph->host, host) == 0) goto duplicate; ph = ph->next; } nh = (struct hostlist *) malloc(sizeof(struct hostlist)); if (nh == NULL) { fprintf(stderr, "Out of memory\n"); return 1; } nh->next = hostlist; strcpy(nh->host, host); hostlist = nh; } } duplicate: /* Release memory for hostlist and argument list */ ph = hostlist; while (ph) { nh = ph->next; free(ph); ph = nh; } free(list_dup); return rc; }