From owner-svn-src-stable-10@FreeBSD.ORG Tue Apr 14 18:57:53 2015 Return-Path: Delivered-To: svn-src-stable-10@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 3F6034F5; Tue, 14 Apr 2015 18:57:53 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 20782E92; Tue, 14 Apr 2015 18:57:53 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id t3EIvqiH039363; Tue, 14 Apr 2015 18:57:52 GMT (envelope-from pfg@FreeBSD.org) Received: (from pfg@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id t3EIvp4Z039359; Tue, 14 Apr 2015 18:57:51 GMT (envelope-from pfg@FreeBSD.org) Message-Id: <201504141857.t3EIvp4Z039359@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: pfg set sender to pfg@FreeBSD.org using -f From: "Pedro F. Giffuni" Date: Tue, 14 Apr 2015 18:57:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r281535 - stable/10/usr.bin/sort X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-10@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for only the 10-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Apr 2015 18:57:53 -0000 Author: pfg Date: Tue Apr 14 18:57:50 2015 New Revision: 281535 URL: https://svnweb.freebsd.org/changeset/base/281535 Log: MFC r281181, r281182; sort(1): Cleanups and a small memory leak. Remove custom getdelim(3) and fix a small memory leak. Obtained from: OpenBSD Modified: stable/10/usr.bin/sort/bwstring.c stable/10/usr.bin/sort/file.c stable/10/usr.bin/sort/file.h stable/10/usr.bin/sort/sort.c Directory Properties: stable/10/ (props changed) Modified: stable/10/usr.bin/sort/bwstring.c ============================================================================== --- stable/10/usr.bin/sort/bwstring.c Tue Apr 14 18:46:42 2015 (r281534) +++ stable/10/usr.bin/sort/bwstring.c Tue Apr 14 18:57:50 2015 (r281535) @@ -65,18 +65,12 @@ initialise_months(void) for (int i = 0; i < 12; i++) { cmonths[i] = NULL; tmp = (unsigned char *) nl_langinfo(item[i]); - if (tmp == NULL) - continue; if (debug_sort) printf("month[%d]=%s\n", i, tmp); - len = strlen((char*)tmp); - if (len < 1) + if (*tmp == '\0') continue; - while (isblank(*tmp)) - ++tmp; - m = sort_malloc(len + 1); - memcpy(m, tmp, len + 1); - m[len] = '\0'; + m = sort_strdup(tmp); + len = strlen(tmp); for (unsigned int j = 0; j < len; j++) m[j] = toupper(m[j]); cmonths[i] = m; @@ -91,18 +85,17 @@ initialise_months(void) for (int i = 0; i < 12; i++) { wmonths[i] = NULL; tmp = (unsigned char *) nl_langinfo(item[i]); - if (tmp == NULL) - continue; if (debug_sort) printf("month[%d]=%s\n", i, tmp); - len = strlen((char*)tmp); - if (len < 1) + if (*tmp == '\0') continue; - while (isblank(*tmp)) - ++tmp; + len = strlen(tmp); m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1)); - if (mbstowcs(m, (char*)tmp, len) == ((size_t) -1)) + if (mbstowcs(m, (char*)tmp, len) == + ((size_t) - 1)) { + sort_free(m); continue; + } m[len] = L'\0'; for (unsigned int j = 0; j < len; j++) m[j] = towupper(m[j]); Modified: stable/10/usr.bin/sort/file.c ============================================================================== --- stable/10/usr.bin/sort/file.c Tue Apr 14 18:46:42 2015 (r281534) +++ stable/10/usr.bin/sort/file.c Tue Apr 14 18:57:50 2015 (r281535) @@ -188,42 +188,6 @@ file_is_tmp(const char* fn) } /* - * Read zero-terminated line from a file - */ -char * -read_file0_line(struct file0_reader *f0r) -{ - size_t pos = 0; - int c; - - if ((f0r->f == NULL) || feof(f0r->f)) - return (NULL); - - if (f0r->current_line && f0r->current_sz > 0) - f0r->current_line[0] = 0; - - while (!feof(f0r->f)) { - c = fgetc(f0r->f); - if (feof(f0r->f) || (c == -1)) - break; - if ((pos + 1) >= f0r->current_sz) { - size_t newsz = (f0r->current_sz + 2) * 2; - f0r->current_line = sort_realloc(f0r->current_line, - newsz); - f0r->current_sz = newsz; - } - f0r->current_line[pos] = (char)c; - if (c == 0) - break; - else - f0r->current_line[pos + 1] = 0; - ++pos; - } - - return f0r->current_line; -} - -/* * Generate new temporary file name */ char * Modified: stable/10/usr.bin/sort/file.h ============================================================================== --- stable/10/usr.bin/sort/file.h Tue Apr 14 18:46:42 2015 (r281534) +++ stable/10/usr.bin/sort/file.h Tue Apr 14 18:57:50 2015 (r281535) @@ -70,16 +70,6 @@ struct file_list bool tmp; }; -/* - * Structure for zero-separated file reading (for input files list) - */ -struct file0_reader -{ - char *current_line; - FILE *f; - size_t current_sz; -}; - /* memory */ /**/ @@ -110,8 +100,6 @@ struct file_reader *file_reader_init(con struct bwstring *file_reader_readline(struct file_reader *fr); void file_reader_free(struct file_reader *fr); -char *read_file0_line(struct file0_reader *f0r); - void init_tmp_files(void); void clear_tmp_files(void); char *new_tmp_file_name(void); Modified: stable/10/usr.bin/sort/sort.c ============================================================================== --- stable/10/usr.bin/sort/sort.c Tue Apr 14 18:46:42 2015 (r281534) +++ stable/10/usr.bin/sort/sort.c Tue Apr 14 18:57:50 2015 (r281535) @@ -229,34 +229,38 @@ usage(bool opt_err) static void read_fns_from_file0(const char *fn) { - if (fn) { - struct file0_reader f0r; - FILE *f; + FILE *f; + char *line = NULL; + size_t linesize = 0; + ssize_t linelen; - f = fopen(fn, "r"); - if (f == NULL) - err(2, NULL); - - memset(&f0r, 0, sizeof(f0r)); - f0r.f = f; - - while (!feof(f)) { - char *line = read_file0_line(&f0r); + if (fn == NULL) + return; - if (line && *line) { - if (argc_from_file0 == (size_t)-1) - argc_from_file0 = 0; - ++argc_from_file0; - argv_from_file0 = sort_realloc(argv_from_file0, - argc_from_file0 * sizeof(char *)); - if (argv_from_file0 == NULL) - err(2, NULL); - argv_from_file0[argc_from_file0 - 1] = - sort_strdup(line); - } + f = fopen(fn, "r"); + if (f == NULL) + err(2, "%s", fn); + + while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) { + if (*line != '\0') { + if (argc_from_file0 == (size_t) - 1) + argc_from_file0 = 0; + ++argc_from_file0; + argv_from_file0 = sort_realloc(argv_from_file0, + argc_from_file0 * sizeof(char *)); + if (argv_from_file0 == NULL) + err(2, NULL); + argv_from_file0[argc_from_file0 - 1] = line; + } else { + free(line); } - closefile(f, fn); + line = NULL; + linesize = 0; } + if (ferror(f)) + err(2, "%s: getdelim", fn); + + closefile(f, fn); } /*