int main(int argc, char *argv[]) { char buffer[BUFSIZ]; char *sanitized, *result; while (fgets(buffer, BUFSIZ, stdin)) { CHOMP(buffer); sanitized = sanitize_string(buffer); result = is_palindrome(sanitized) ? "" : "not "; free(sanitized); printf("%s is %sa palindrome!\n", buffer, result); } return EXIT_SUCCESS; }
/** * Return contents of F</sys/class/net/I<if_name>/address> (if found). */ char * get_if_addr (const char *if_name) { CLEANUP_FCLOSE FILE *fp = NULL; CLEANUP_FREE char *path = NULL; char *content = NULL; size_t len = 0; ssize_t n; if (asprintf (&path, "/sys/class/net/%s/address", if_name) == -1) error (EXIT_FAILURE, errno, "asprintf"); fp = fopen (path, "r"); if (fp == NULL) return NULL; if ((n = getline (&content, &len, fp)) == -1) { perror (path); free (content); return NULL; } CHOMP (content, n); return content; }
struct rfc822_header* rfc822_parse_stanza(FILE *file) { struct rfc822_header *head, **tail, *cur; static size_t buflen = 8192; static char *buf = NULL; if (!buf) { buf = malloc(buflen * sizeof *buf); if (!buf) DIE("Out of memory"); } head = NULL; tail = &head; cur = NULL; /* fprintf(stderr,"rfc822_parse_stanza(file)\n");*/ while (fgets(buf, buflen, file)) { char *tmp; size_t tmplen = strlen(buf); if (*buf == '\n') break; while (buf[tmplen - 1] != '\n') { buflen += 8192; buf = realloc(buf, buflen * sizeof *buf); if (!buf) DIE("Out of memory"); if (!fgets(buf + tmplen, buflen - tmplen, file)) break; tmplen += strlen(buf + tmplen); } CHOMP(buf); tmp = buf; if (isspace(*tmp)) { /* continuation line, just append it */ int len; if (cur == NULL) break; /* should report an error here */ len = strlen(cur->value) + strlen(tmp) + 2; cur->value = realloc(cur->value, len); strvacat(cur->value, len, "\n", tmp, NULL); } else { while (*tmp != 0 && *tmp != ':') tmp++; *tmp++ = '\0'; cur = NEW(struct rfc822_header); if (cur == NULL) return NULL; memset(cur, '\0',sizeof(struct rfc822_header)); cur->header = strdup(buf); while (isspace(*tmp)) tmp++; cur->value = strdup(unescapestr(tmp)); *tail = cur; tail = &cur->next; } } return head; }
/** * Return contents of F</sys/class/net/I<if_name>/device/vendor> (if * found), mapped to the PCI vendor. See: * L<http://pjwelsh.blogspot.co.uk/2011/11/howto-get-network-card-vendor-device-or.html> */ char * get_if_vendor (const char *if_name, int truncate) { CLEANUP_FCLOSE FILE *fp = NULL; CLEANUP_FREE char *path = NULL; char *line = NULL; size_t len = 0; ssize_t n; char vendor[5]; if (asprintf (&path, "/sys/class/net/%s/device/vendor", if_name) == -1) error (EXIT_FAILURE, errno, "asprintf"); fp = fopen (path, "r"); if (fp == NULL) { perror (path); return NULL; } if ((n = getline (&line, &len, fp)) == -1) { perror (path); free (line); return NULL; } /* Vendor is (always?) a 16 bit quantity (as defined by PCI), * something like "0x8086" (for Intel Corp). */ CHOMP (line, n); if (line[0] != '0' || line[1] != 'x' || strlen (&line[2]) != 4) { free (line); return NULL; } strcpy (vendor, &line[2]); fclose (fp); fp = fopen ("/usr/share/hwdata/pci.ids", "r"); if (fp == NULL) { perror ("/usr/share/hwdata/pci.ids"); free (line); return NULL; } while ((n = getline (&line, &len, fp)) != -1) { CHOMP (line, n); if (STRPREFIX (line, vendor)) { /* Find the start of the name after the vendor ID and whitespace. */ size_t i = 4; n -= 4; while (n > 0 && isspace (line[i])) { i++; n--; } memmove (&line[0], &line[i], n+1 /* copy trailing \0 */); /* Truncate? */ if (truncate > 0 && n > truncate) line[n] = '\0'; return line; } } free (line); return NULL; }
static struct package *extract(const char *file) { int pipefd[2]; int pid; if (pipe(pipefd) < 0) { perror("pipe"); return NULL; } pid = fork(); if (pid < 0) { perror("fork"); close(pipefd[0]); close(pipefd[1]); return NULL; } else if (pid == 0) /* child */ { close(pipefd[0]); dup2(pipefd[1], 1); close(pipefd[1]); execlp(APT_EXTRACTTEMPLATES, APT_EXTRACTTEMPLATES, file, NULL); /* if we reach here, then execlp failed */ perror("execlp"); _exit (127); } else /* parent */ { FILE *query; char buffer[1024]; struct package *retval = NULL; close(pipefd[1]); query = fdopen(pipefd[0], "r"); while (!feof(query)) { if (fgets(buffer, 1024, query)) { CHOMP(buffer); struct package *p = NEW(struct package); memset(p, 0, sizeof(*p)); p->buffer = strdup(buffer); p->name = strtok(p->buffer, " \t\n"); p->version = strtok(NULL, " \t\n"); p->templatefile = strtok(NULL, " \t\n"); p->configfile = strtok(NULL, " \t\n"); if (p->configfile == NULL) { free(p->buffer); free(p); retval = NULL; } else { p->next = retval; retval = p; } } } fclose(query); return retval; }
int strwrap(const char *str, const int width, char *lines[], int maxlines) { #ifdef HAVE_LIBTEXTWRAP textwrap_t p; int j; char *s; char *s0; char *t; textwrap_init(&p); textwrap_columns(&p, width); s0 = s = textwrap(&p, str); for (j=0; j<maxlines; j++) { t = strchr(s, '\n'); if (t == NULL) { lines[j] = (char *)malloc(strlen(s) + 1); strcpy(lines[j], s); free(s0); return j + 1; } else { lines[j] = (char *)malloc(t - s + 1); strncpy(lines[j], s, t-s); lines[j][t-s] = 0; s = t + 1; } } return j; #else /* "Simple" greedy line-wrapper */ int len = STRLEN(str); int l = 0; const char *s, *e, *end, *lb; if (str == 0) return 0; /* * s = first character of current line, * e = last character of current line * end = last character of the input string (the trailing \0) */ s = e = str; end = str + len; while (len > 0) { /* try to fit the most characters */ e = s + width - 1; /* simple case: we got everything */ if (e >= end) { e = end; } else { /* find a breaking point */ while (e > s && !isspace(*e) && *e != '.' && *e != ',') e--; } /* no word-break point found, so just unconditionally break * the line */ if (e == s) e = s + width; /* if there's an explicit linebreak, honor it */ lb = strchr(s, '\n'); if (lb != NULL && lb < e) e = lb; lines[l] = (char *)malloc(e-s+2); strncpy(lines[l], s, e-s+1); lines[l][e-s+1] = 0; CHOMP(lines[l]); len -= (e-s+1); s = e + 1; if (++l >= maxlines) break; } return l; #endif /* HAVE_LIBTEXTWRAP */ }
/* Preload images for entities that could be found on this map and gamemode. This was basically cut & paste from bits of ../server/sv_map.c */ void preload_entities(char *filename, int gamemode) { char buf[MAXLINE], name[32], *line; ent_type_t *et; FILE *file; int entity_section, i, skipline; /* Open the map file */ if( !(file = open_data_file("maps", filename) ) ) { printf("%s: Couldn't open %s\n", __FILE__, filename); return; } if( !ent_img_loaded[client.entity_type] ) { /* Load clients entity image */ if( (et = entity_type(client.entity_type)) == NULL ) return; image(entity_type_animation(et, ALIVE)->pixmap, MASKED); ent_img_loaded[client.entity_type] = 1; } /* Scan map for entities that will be loaded */ entity_section = 0; /* Found section yet? */ while( !feof(file) ) { if( !fgets(buf, MAXLINE, file) ) break; CHOMP(buf); if( buf[0] == '#' || buf[0] == '\0' ) continue; /* skip line */ if( entity_section ) { line = buf; skipline = 0; /* check for gamemode specific line prefix characters */ for(i=0 ; i<NUM_GAME_MODES ; i++ ) { if( buf[0] == gamemodechar[i] ) { if( gamemode == i ) line = buf + 1; else skipline = 1; } } if( skipline ) continue; /* Not in our mode */ if( sscanf(line, "%s", name) && strcasecmp(name, "SPAWN") != 0 ) { for( i=0 ; i < num_entity_types ; i++ ) { if( (et = entity_type(i)) == NULL ) continue; if( !ent_img_loaded[i] && !strcasecmp( et->name, name ) ) { ent_img_loaded[i] = 1; image(entity_type_animation(et,ALIVE)->pixmap, MASKED); } } } } else if( !strcasecmp("ENTITY", buf) ) entity_section = 1; } fclose(file); }