github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/libsnap-confine-private/cleanup-funcs.c (about)

     1  /*
     2   * Copyright (C) 2015 Canonical Ltd
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License version 3 as
     6   * published by the Free Software Foundation.
     7   *
     8   * This program is distributed in the hope that it will be useful,
     9   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11   * GNU General Public License for more details.
    12   *
    13   * You should have received a copy of the GNU General Public License
    14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15   *
    16   */
    17  
    18  #include "cleanup-funcs.h"
    19  
    20  #include <mntent.h>
    21  #include <unistd.h>
    22  
    23  void sc_cleanup_string(char **ptr)
    24  {
    25  	if (ptr != NULL && *ptr != NULL) {
    26  		free(*ptr);
    27  		*ptr = NULL;
    28  	}
    29  }
    30  
    31  void sc_cleanup_file(FILE ** ptr)
    32  {
    33  	if (ptr != NULL && *ptr != NULL) {
    34  		fclose(*ptr);
    35  		*ptr = NULL;
    36  	}
    37  }
    38  
    39  void sc_cleanup_endmntent(FILE ** ptr)
    40  {
    41  	if (ptr != NULL && *ptr != NULL) {
    42  		endmntent(*ptr);
    43  		*ptr = NULL;
    44  	}
    45  }
    46  
    47  void sc_cleanup_closedir(DIR ** ptr)
    48  {
    49  	if (ptr != NULL && *ptr != NULL) {
    50  		closedir(*ptr);
    51  		*ptr = NULL;
    52  	}
    53  }
    54  
    55  void sc_cleanup_close(int *ptr)
    56  {
    57  	if (ptr != NULL && *ptr != -1) {
    58  		close(*ptr);
    59  		*ptr = -1;
    60  	}
    61  }