거의 막혔어요. 어떤 버그부터 봐야 할까요?
while (getline(&line, &linecap, fp) != -1) {
char *name, *role, *port;
lineno++;
line[strcspn(line, "\r\n")] = '\0';
if (line[0] == '#' || line[0] == '\0')
continue;
name = strtok(line, " \t");
role = strtok(NULL, " \t");
port = strtok(NULL, " \t");
if (!name || !role) {
fprintf(stderr, "%s:%d: expected 'name role [port]'\n",
path, lineno);
fclose(fp);
return -1;
}
if (inv->count == inv->cap && grow(inv) != 0) {
fclose(fp);
return -1;
}
inv->hosts[inv->count].name = name;
inv->hosts[inv->count].role = role;
inv->hosts[inv->count].port = port ? atoi(port) : 22;
inv->count++;
}
free(line);
fclose(fp);
return 0;
hosts.txt:
# prod inventory
web-01 web 8080
web-02 web 8080
db-01 db 5432
cache-01 cache
hosts_bad.txt:
web-01 web 8080
db-01
cache-01 cache 6379
