📄 process.c
字号:
/* -------------------------------------------------------------------- *//* SMS Client, send messages to mobile phones and pagers *//* *//* process.c *//* *//* Copyright (C) 1997,1998 Angelo Masci *//* *//* This library is free software; you can redistribute it and/or *//* modify it under the terms of the GNU Library General Public *//* License as published by the Free Software Foundation; either *//* version 2 of the License, or (at your option) any later version. *//* *//* This library is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *//* Library General Public License for more details. *//* *//* You should have received a copy of the GNU Library General Public *//* License along with this library; if not, write to the Free *//* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* *//* You can contact the author at this e-mail address: *//* *//* angelo@styx.demon.co.uk *//* *//* -------------------------------------------------------------------- *//* $Id$ -------------------------------------------------------------------- */#include <stdio.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include <limits.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include "process.h"/* -------------------------------------------------------------------- */#define MAX_PATH 256/* -------------------------------------------------------------------- */#define TF_UNKNOWN 0#define TF_CONTROL 1#define TF_DATA 2/* -------------------------------------------------------------------- */struct file_list_struct{ char *name; int type; time_t mtime; struct file_list_struct *next;};typedef struct file_list_struct FILE_LIST;/* -------------------------------------------------------------------- */static FILE_LIST *add_file_item(FILE_LIST *list, char *name, time_t mtime, int type);static FILE_LIST *qsort_list(FILE_LIST *list);static int num_items_in_list(FILE_LIST *list);static int file_item_compare(const void *item1, const void *item2);static void dump_list(FILE_LIST *list);static void free_list(FILE_LIST *list);/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */static FILE_LIST *add_file_item(FILE_LIST *list, char *name, time_t mtime, int type){ FILE_LIST *item; item = (FILE_LIST *)malloc(sizeof(FILE_LIST)); if (item == NULL) { fprintf(stderr, "Error: malloc() failed\n"); exit(1); } item->name = (char *)malloc(sizeof(char) * (strlen(name) +1)); if (item->name == NULL) { fprintf(stderr, "Error: malloc() failed\n"); exit(1); } strcpy(item->name, name); item->mtime = mtime; item->type = type; item->next = list; return item;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */static int num_items_in_list(FILE_LIST *list){ int count; FILE_LIST *item; count = 0; item = list; while(item != NULL) { count++; item = item->next; } return count;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */static int file_item_compare(const void *item1, const void *item2){ time_t mtime_1, mtime_2; mtime_1 = (*(FILE_LIST **)item1)->mtime; mtime_2 = (*(FILE_LIST **)item2)->mtime; if (mtime_1 > mtime_2) { return 1; } if (mtime_1 < mtime_2) { return -1; } return strcmp((*(FILE_LIST **)item1)->name, (*(FILE_LIST **)item2)->name);}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */static FILE_LIST *qsort_list(FILE_LIST *list){ int count, i; FILE_LIST **file_list_array, *item; count = num_items_in_list(list); file_list_array = (FILE_LIST **)malloc(sizeof(FILE_LIST *) * count); if (file_list_array == NULL) { fprintf(stderr, "Error: malloc() failed\n"); exit(1); } item = list; for (i=0; item != NULL; i++) { file_list_array[i] = item; item = item->next; } qsort(file_list_array, count, sizeof(FILE_LIST *), file_item_compare); list = file_list_array[0]; for (i=0; i<count-1; i++) { file_list_array[i]->next = file_list_array[i+1]; } file_list_array[i]->next = NULL; free(file_list_array); return list;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */static void dump_list(FILE_LIST *list){ FILE_LIST *item; char *stype; item = list; while(item != NULL) { if (item->type == TF_CONTROL) { stype = "CONTROL"; } else if (item->type == TF_DATA) { stype = "DATA"; } else { stype = "UNKNOWN"; } printf("%s %d %s\n", item->name, (int)item->mtime, stype); item = item->next; }}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */static void free_list(FILE_LIST *list){ FILE_LIST *prev_item, *item; item = list; while(item != NULL) { prev_item = item; item = item->next; free(prev_item->name); free(prev_item); }}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int process_queue(char *queue){ DIR *dir; struct dirent *entry; struct stat status; char cwd[MAX_PATH+1]; FILE_LIST *file_list = NULL; int type; dir = opendir(queue); if (dir == NULL) { return -1; } if (getcwd(cwd, MAX_PATH+1) == NULL) { return -1; } if (chdir(queue) != 0) { return -1; } while((entry = readdir(dir)) != NULL) { if ((strcmp(entry->d_name, ".") != 0) && (strcmp(entry->d_name, "..") != 0)) { if (stat(entry->d_name, &status) != 0) { return -1; } if (!S_ISDIR(status.st_mode)) { if (strncmp(entry->d_name, "cf", 2) == 0) { type = TF_CONTROL; } else if (strncmp(entry->d_name, "df", 2) == 0) { type = TF_DATA; } else { type = TF_UNKNOWN; } file_list = add_file_item(file_list, entry->d_name, status.st_mtime, type); } } } if (closedir(dir) != 0) { return -1; } if (chdir(cwd) != 0) { return -1; } file_list = qsort_list(file_list); dump_list(file_list); free_list(file_list); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -